function showthis (idname){
	document.getElementById(idname).style.display = 'block'
}

function hidethis(idname) {
	document.getElementById(idname).style.display = 'none'
}

function resetForm(formId) {
	document.forms[formId].reset();
}

function showhide(idname) {
	if (document.getElementById(idname).style.display == 'none') {
		document.getElementById(idname).style.display = 'block'
	}
	else {
		document.getElementById(idname).style.display = 'none'
	}
}

function GetPassForm(url, newWindow, showError) {
	var form = document.forms["specForm"];
	form.action = url;
	form.target = (newWindow == true) ? "_blank" : "";
	document.getElementById('specFormError').innerHTML = (showError == true) ? "<p id='error_pass'>Felaktigt l&ouml;senord!</p>" : "&nbsp;";
	showthis('specFormDiv');
}
function GetSelectedGroups() {
	var elements = document.getElementById('idCheckBox').elements;
	var len = elements.length;
	//var result = new Array();
	var result = '';
	for(var i = 0; len > i; i++) {
		if (elements[i].checked == true) {
			//result.push(elements[i].value);
			result += '&groups[]='+elements[i].value;
		}
	}
	return result;
}

function GoWithCheckPassword(type, id, password, invalid) {
	document.getElementById('specFormError').innerHTML = (invalid == true) ? "<p id='error_pass'>Felaktigt l&ouml;senord!</p>" : "&nbsp;";
	var request = mk_xmlhttpreq();
	var passString = (password == null) ? "" : "&password="+password;
	var page = (type == "categories") ? "blog" : type;

	var urltxt  = PROJECT_URL+'/points.php?event=CheckPassword&type='+type+'&id='+id+passString;

	request.open('GET', urltxt , true);
	request.onreadystatechange = function () {
		if (request.readyState != 4) { return; }
		var response = request.responseText;
		var userPath = '/';

		if (response == 'yes') {
			document.location.href = PROJECT_URL+userPath+page+'/'+id+'/';
		}
		else {
			var form = document.forms["specForm"];
			var submit = "GoWithCheckPassword('"+type+"', "+id+", document.forms['specForm'].passwordField.value, true);";
			form.target = "_self";
			form.action = 'javascript:'+submit;
			form.onsubmit = submit;
			showthis('specFormDiv');
		}
	}
	request.send(null);
}

function GetWithCheckPassword(type, id, password, invalid) {
	document.getElementById('specFormError').innerHTML = (invalid == true) ? "<p id='error_pass'>Felaktigt l&ouml;senord!</p>" : "&nbsp;";
	var request = mk_xmlhttpreq();
	var passString = (password == null) ? "" : "&password="+password;
	var page = (type == "categories") ? "blog" : type;
	var servType = "";
	var cb = null;
	if (type == "albums") {
		servType = "image";
		cb = document.getElementById("albums"+id);
	}
	else {
		servType = "label";
		cb = document.getElementById("categories"+id);
	}
	

	var urltxt  = PROJECT_URL+'/points.php?event=CheckPassword&type='+type+'&id='+id+passString;

	request.open('GET', urltxt , true);
	request.onreadystatechange = function () {
		if (request.readyState != 4) { return; }
		var response = request.responseText;

		if (response == 'yes') {
			ApplyRecommendedView(cb, type, id);
			AddParameter(cb, servType, id, null);
			AddMarkers();
		}
		else {
			var form = document.forms["specForm"];
			var submit = "GetWithCheckPassword('"+type+"', "+id+", document.forms['specForm'].passwordField.value, true);";
			form.target = "_self";
			form.action = 'javascript:'+submit;
			form.onsubmit = submit;
			form.onreset = function () {cb.checked = false; }
			showthis('specFormDiv');
		}
	}
	request.send(null);
}

function mk_xmlhttpreq () {
	/*var xh = GXmlHttp.create();
	if (xh != null) { return xh; }*/
	// Std IExploder:
	try {
		xh = new ActiveXObject("Msxml2.XMLHTTP");
		return xh;
	} catch (e) { }
	try {
		xh = new ActiveXObject("Microsoft.XMLHTTP");
		return xh;
	} catch (e) { }
	try {
		xh = new XMLHttpRequest();
		return xh;
	} catch (e) { }
	// Only other way I could find...
	try {
		xh = window.createRequest();
		return xh;
	} catch (e) { }
	return undefined;
}

function ajaxManager(url, el_id) {
	if (document.getElementById) {
		var x = mk_xmlhttpreq();
	}
	el = document.getElementById(el_id);
	if (x) {
		x.onreadystatechange = function() {
			if (x.readyState == 4) {
				if (x.status == 200) {
					el.innerHTML = x.responseText;
				} else if (x.status >= 400) {
					el.innerHTML = "<center><h1>An error #"+x.status+" has occured!</h1></center>";
				}
			}
		}
	}
	x.open("GET", url, true);
	x.send(null);
}

function openPopUp(varURL, varWindowName, varWidth, varHeight) {
	varWidth = varWidth||400;
	varHeight = varHeight||90;
	window.open(varURL, varWindowName, "height=" + varHeight + ",width=" + varWidth + ",resizable=0,status=no,toolbar=no,menubar=no,location=no,scrollbars=no")
}

function ChangeKapchaImage(url) {
	if (document.getElementById) {
		var x = mk_xmlhttpreq();
	}
	el = document.getElementById('idKeyString');
	if (x) {
		x.onreadystatechange = function() {
			if (x.readyState == 4) {
				if (x.status == 200) {
					var now = new Date();
					el.src = url+'?'+now.getTime()+session;
				} else if (x.status >= 400) {
					el.innerHTML = "<center><h1>An error #"+x.status+" has occured!</h1></center>";
				}
			}
		}
	}
	x.open("GET", url, true);
	x.send(null);
}
function addSidUrl(isCookie, sid, sid_name) {
	if (isCookie == '1') {
		return;
	}
	var aTag = $('a');
	for (var i = 0; aTag.length > i; i++) {
		var url = sid_name+'='+sid;
		if (aTag[i].href.indexOf('javascript') != 0) {
			if (aTag[i].href.indexOf('?') > 0) {
				aTag[i].href += '&'+url;
			} else {
				aTag[i].href += '?'+url;
			}
		}
	}
	var formsTag = $('form');
	for (var i = 0; formsTag.length > i; i++) {
		var url = sid_name+'='+sid;
		if (formsTag[i].action.indexOf('?') > 0) {
			formsTag[i].action += '&'+url;
		} else {
			formsTag[i].action += '?'+url;
		}
	}
}
