var blnClickCheckBox		= false;
var strLastMarkedObjectID	= "";
var xmlHttp;

function gCheckKey(event, oField) {
	var iKeyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

	if (oField != undefined) {
		var sFieldName	= oField.name;
		var sFieldValue	= oField.value;
	}
	else {
		var sFieldName	= "";
		var sFieldValue	= "";
	}

	// check key

	if (sFieldName == "Username" || sFieldName == "Password") {
		if (iKeyCode == 13) {
			return(gValidateLogin());
		}
	}
}

function gValidateLogin() {
	var oFrm = document.getElementsByTagName("form")[0];

	if (oFrm.Username.value == "") return(gFieldError("Du har inte angett ditt användarnamn.", oFrm.Username));
	if (oFrm.Password.value == "") return(gFieldError("Du har inte angett ditt lösenord.", oFrm.Password));

	oFrm.action = document.location.href;

	gDoAction("Login");

	return(false);
}

function GetRadioData(oElement) {
	oRadio		= document.all[oElement];
	bChecked	= false;

	for (j = 0; j < oRadio.length && !bChecked; j++) {
		if (oRadio[j].checked) bChecked = true;
	}

	if (bChecked) { return(oRadio[j - 1].value); }
}

function OpenPopup(sURL, sName, iWidth, iHeight) {
	if (document.all) {
		var w = (top.document.body.clientWidth / 2) + top.window.screenLeft;
		var h = (top.document.body.clientHeight / 2) + top.window.screenTop;
	}
	else if (document.layers) {
		var w = window.innerWidth;
		var h = window.innerHeight;
	}

	var iPosX = w - (iWidth / 2);
	var iPosY = h - (iHeight / 2);

	if (sName == "FIRSTADMIN") iPosY = iPosY - 57;

	var oWin = window.open(sURL, sName, "toolbar=no, location=no, directories=no, status=no, resizable=yes, scrollbars=no, width=" + iWidth + ", height=" + iHeight + ", top="+ iPosY + ",left=" + iPosX);
	if (parseInt(navigator.appVersion) >= 4) { oWin.window.focus(); }
}

function OpenPopupCenter(sURL, sName, iWidth, iHeight, sOptions) {
	if (document.all) {
		var w = (top.document.body.clientWidth / 2) + top.window.screenLeft;
		var h = (top.document.body.clientHeight / 2) + top.window.screenTop;
	}
	else if (document.layers) {
		var w = window.innerWidth;
		var h = window.innerHeight;
	}

	var iPosX = w - (iWidth / 2);
	var iPosY = h - (iHeight / 2);

	if (sOptions != "") sOptions = sOptions + ", ";

	var oWin = window.open(sURL, sName, sOptions + "toolbar=no, location=no, directories=no, status=no, resizable=yes , width=" + iWidth + ", height=" + iHeight + ", top="+ iPosY + ",left=" + iPosX);
	if (parseInt(navigator.appVersion) >= 4) { oWin.window.focus(); }
}

function OpenPopupHidden(sURL, sName, iWidth, iHeight) {
	var iPosX = screen.width;
	var iPosY = 0;

	var oWin = window.open(sURL, sName, "toolbar=no, location=no, directories=no, status=no, resizable=yes, scrollbars=no, width=" + iWidth + ", height=" + iHeight + ", top="+ iPosY + ",left=" + iPosX);
}

function OpenPopupSet(sURL, sName, iWidth, iHeight, iPosX, iPosY) {
	var oWin = window.open(sURL, sName, "toolbar=no, location=no, directories=no, status=no, resizable=yes, scrollbars=no, width=" + iWidth + ", height=" + iHeight + ", top="+ iPosY + ",left=" + iPosX);
}

function gDoAction(sAction, sConfirm){
	var oForm = document.getElementsByTagName("form")[0];

	if (sConfirm != "" && sConfirm != undefined) {
		if (confirm(sConfirm)) {
			oForm.Action.value = sAction;
			oForm.submit();
		}
	}
	else {
		oForm.Action.value = sAction;
		oForm.submit();
	}
}

function gGoBack(sBackURL) {
	if (sBackURL == "" || sBackURL == undefined) {
		history.back(-1);
	}
	else {
		location.href = sBackURL;
	}
}

function gDisplayMsg(sMsg) {
	if (sMsg != "") alert(sMsg);
}

function insertText(oTextArea, sValue) {
	oTextArea.focus();

	var sRange	= document.selection.createRange();
	sRange.text = sValue;

	UpdateCharsLeft(frm.txtMessage, 160, CharsLeft);
}

function M(oObject, sColor) {
	if (sColor == undefined || sColor == "") {
		sColor = "#e0e0e0";
	}

	oObject.style.backgroundColor = sColor;
}

function U(oObject) {
	oObject.style.backgroundColor = "";
}

function gCountChecked_old(sElementName) {
	var c = 0; // #/checkboxs checked
	var oElm = document.getElementsByName(sElementName)[0];

	for (var i = 0; i < oElm.length; i++) {
		var e = document.forms(0).elements[i];
		if ((e.type == "checkbox") && (!e.disabled) && (e.name == sElementName)) {
			if (e.checked) c++;
		}
	}

	return c;
}

function gCountChecked(sName) {
	var n = 0;
	var c = 0;
	var oElm;

	if (isObject(sName)) {
		oElm = document.getElementsByName(sName);
		n = oElm.length;

		if (n == undefined) {
			if (oElm.checked) {
				c = 1;
			}
		}
		else {
			for (var i = 0; i < n; i++) {
				var e = oElm[i];
				if (e.type == "checkbox" && e.checked) {
					c++;
				}
			}
		}
	}

	return(c);
}

function gSwitchAll(sElementName, sItemsName) {
	var n = 0; // #/checkboxs
	var c = 0; // #/checkboxs checked

	for (var i = 0; i < document.forms(0).elements.length; i++) {
		var e = document.forms(0).elements[i];
		if ((e.type == "checkbox") && (!e.disabled) && (e.name == sElementName)) {
			n++;
			if (e.checked) c++;
		}
	}

	if (c < n) {
		for (var i = 0; i < document.forms(0).elements.length; i++) {
			var e = document.forms(0).elements[i];
			if ((e.type == "checkbox") && (!e.disabled) && (e.name == sElementName)) {
				e.checked = true;
			}
		}
	}
	else {
		for (var i = 0; i < document.forms(0).elements.length; i++) {
			var e = document.forms(0).elements[i];
			if ((e.type == "checkbox") && (!e.disabled) && (e.name == sElementName)) {
				e.checked = false;
			}
		}
	}
}

function CheckAll(sElementName, sItemsName) {
	var n = 0;			// #/checkboxs
	var c = 0;			// #/checkboxs checked

	for (var i = 0; i < document.forms(0).elements.length; i++) {
		var e = document.forms(0).elements[i];
		if ((e.type == "checkbox") && (!e.disabled) && (e.name == sElementName)) {
			n++;
			if (e.checked) c++;
		}
	}

	if ((c == n) && (c > 0)) CheckSwitchBox(sItemsName);
	else UncheckSwitchBox(sItemsName);
}

function isObject(sName) {
	var oTest = document.getElementsByName(sName);
	if (oTest.length > 0) return(true);
	else return(false);
}

function UpdateHitInfo(iHits, iPage, iPageCount, sName1, sName2) {
	if (iHits == 0) {
		parent.SetButtonsDisabled(true);
		if (parent.isObject("HitInfo"))		parent.HitInfo.innerHTML		= "";
		if (parent.isObject("PageInfo"))	parent.PageInfo.innerHTML		= "";
		if (parent.isObject("btnPrevPage"))	parent.frm.btnPrevPage.disabled	= true;
		if (parent.isObject("btnNextPage"))	parent.frm.btnNextPage.disabled	= true;
	}
	else {
		if (isObject("S")) CheckAll("S", sName2);
		if (iPageCount >= 0) {
			parent.SetButtonsDisabled(false);
			if (parent.isObject("PageInfo")) parent.PageInfo.innerHTML = "Sida " + iPage + " av " + iPageCount;

			if (iHits == 1) {
				if (parent.isObject("HitInfo")) parent.HitInfo.innerText = iHits + " " + sName1;
			}
			else {
				if (parent.isObject("HitInfo")) parent.HitInfo.innerHTML = iHits + " " + sName2;
			}

			if (iPage < iPageCount) {
				if (parent.isObject("btnNextPage")) parent.frm.btnNextPage.disabled = false;
			}
			else {
				if (parent.isObject("btnNextPage")) parent.frm.btnNextPage.disabled = true;
			}

			if (iPage > 1) {
				if (parent.isObject("btnPrevPage")) parent.frm.btnPrevPage.disabled = false;
			}
			else {
				if (parent.isObject("btnPrevPage")) parent.frm.btnPrevPage.disabled = true;
			}
		}
	}
}

function ValidateTime(sTime) {
	sTime = sTime.replace(/\./g, ":");

	var iHours;
	var iMinutes	= 0;
	var iPos		= sTime.indexOf(":");
	var bValidate	= false;

	if (iPos == -1) {
		if (!isNaN(sTime)) {
			if (sTime.length == 4) {
				iHours		= Number(sTime.substring(0, 1));
				iMiuntes	= Number(sTime.substring(2, 3));
			}
			else {
				iHours = Number(sTime);
			}

			if (iHours >= 0 && iHours <= 24 && iMinutes >= 0 && iMinutes <= 59)	bValidate = true;
		}
	}
	else if (iPos == 1 || iPos == 2) {
		iHours		= sTime.substring(0, iPos);
		iMinutes	= sTime.substring(iPos + 1, iPos + 4);

		if (iMinutes.length == 2 && !isNaN(iHours) && !isNaN(iMinutes)) {
			iHours		= Number(iHours);
			iMinutes	= Number(iMinutes);

			if (iHours >= 0 && iHours <= 24 && iMinutes >= 0 && iMinutes <= 59)	bValidate = true;
		}
	}

	return(bValidate);
}

function gListboxSelect(oListbox, sValue) {
	for (var i = 0; i < oListbox.length; i++) {
		if (oListbox.options[i].value == sValue) {
			oListbox.selectedIndex = i;
		}
	}
}

function gFieldError(sErrorMsg, oField) {
	alert(sErrorMsg);

	if (oField != undefined) {
		oField.focus();
		if ((oField.type == "text" || oField.type == "textarea") && oField.value != "") oField.select();
	}

	return(false);
}

function gFieldFocus(sFieldName, bSelect) {
	var oField = document.getElementsByName(sFieldName);
	var oField2;

	if (oField.length > 0) {
		oField2 = oField[0];

		oField2.focus();

		if (bSelect) {
			if ((oField2.type == "text" || oField2.type == "textarea") && oField2.value != "") oField2.select();
		}
	}

	return(false);
}

//*************************************************************************************************
//** FUNCTION: GetChecked() // Author: jon.andersson@firstinsys.se
//*************************************************************************************************
function GetChecked(sElementName, iIndex) {
	var sList = "";
	var j = 0;
	var n = 0; // #/checkboxs on page

	if (iIndex == undefined) {
		iIndex = 0;
	}

	if (isObject(sElementName)) {
		n = document.forms(0)[sElementName].length;

		if (n == undefined) {
			if (document.forms(0)[sElementName].checked) {
				sList = "," + document.forms(0)[sElementName].value;
			}
		}
		else {
			for (var i = 0; i < n; i++) {
				var e = document.forms(0)[sElementName][i];
				if (e.type == "checkbox" && e.checked) {
					if (iIndex == 0) {
						sList = sList + "," + e.value;
					}
					else {
						if (j < iIndex) { j++; sList = e.value; }
					}
				}
			}
		}

		if (iIndex == 0) {
			sList = sList.substring(1, 1000);
		}
	}

	return(sList);
}

function GetChecked_old(sElementName, iIndex) {
	var sList = "";
	var j = 0;

	for (var i = 0; i < document.forms(0).elements.length; i++) {
		var e = document.forms(0).elements[i];
		if (e.type == "checkbox" && e.checked && e.name == sElementName) {
			if (iIndex == 0) {
				sList = sList + "," + e.value;
			}
			else {
				if (j < iIndex) { j++; sList = e.value; }
			}
		}
	}

	if (iIndex == 0) {
		return(sList.substring(1, 1000));
	}
	else {
		return(sList);
	}
}

function StrReplace(lcStr, lcFrom, lcTo) {
	var lcLeft	= "";
	var lcRight	= lcStr;
	var lnPos	= lcRight.indexOf(lcFrom);

	if (lcFrom.length > 0) {
		while (lnPos > -1)	{
			lcLeft	= lcLeft + lcRight.substring(0,lnPos) + lcTo;
			lcRight	= lcRight.substring(lnPos + lcFrom.length, lcRight.length)
			lnPos	= lcRight.indexOf(lcFrom); 

			if(lnPos == -1) {
				lcRight = lcLeft + lcRight;
			}
		}
	} 
	return(lcRight);
}

//*************************************************************************************************
//* Static Layer script II (By maXimus, maximus@nsimail.com, http://maximus.ravecore.com/)
//* Modified slightly/ permission granted to Dynamic Drive to feature script in archive
//* For full source, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
//*************************************************************************************************
function gJS_StaticLayer() {
	//configure below variable for layer position on page
	var offsetleft=0
	var offsettop=0

	var ns4=document.layers?1:0
	var ie4=document.all?1:0
	var ns6=document.getElementById&&!document.all?1:0

	if (ns6||ie4||ns4)
		if (ie4) {object1.style.pixelTop=document.body.scrollTop+offsettop}
		else if (ns6) {document.getElementById("object1").style.top=window.pageYOffset+offsettop}
		else if (ns4) {eval(document.object1.top=eval(window.pageYOffset+offsettop));}
		setTimeout("gJS_StaticLayer()",0);
}

function gValidateLogout() {
	var oFrm = document.getElementsByTagName("form")[0];

	oFrm.action = document.location.href;

	gDoAction("Logout");
}

function gSearch() {
	var oFrm = document.getElementsByTagName("form")[0];

	gLiveSearch(oFrm.txtSearch.value);
}

function gLiveSearch(sSearch) {
    var sHTML;
	var sURL;
	var oImg;

    if (sSearch.length < 3) { 
        return(true);
    }

	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null) {
		sHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"
		sHTML = sHTML + "	<tr><td class=\"text_topic\" style=\"padding-bottom:5px;\">Ett problem uppstod</td></tr>"
		sHTML = sHTML + "	<tr><td class=\"text_body\">Din webbläsare stöder inte funktionen HTTP request.</td></tr>"
		sHTML = sHTML + "</table>"

		document.getElementById("content").innerHTML = sHTML;
		return(true);
	}
	else {
		sHTML = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"
		sHTML = sHTML + "	<tr><td class=\"text_topic\" style=\"padding-bottom:5px;\">Sökresultat</td></tr>"
		sHTML = sHTML + "	<tr><td class=\"text_body\">Sökning pågår...</td></tr>"
		sHTML = sHTML + "</table>"

		document.getElementById("imgSearch").style.visibility = "";
	}

	sURL = "search.asp?q=" + escape(sSearch) + "&rnd=" + Math.random();

	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", sURL, true);
	//xmlHttp.setRequestHeader("Content-Type", "text/html; charset=utf-8")
	xmlHttp.send(null);

	return(true);
}

function utf8_encode(string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);   

        if (c < 128) {   
            utftext += String.fromCharCode(c);   
        }   
        else if((c > 127) && (c < 2048)) {   
            utftext += String.fromCharCode((c >> 6) | 192);   
            utftext += String.fromCharCode((c & 63) | 128);   
        }   
        else {   
            utftext += String.fromCharCode((c >> 12) | 224);   
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);   
            utftext += String.fromCharCode((c & 63) | 128);   
        }   
    }   

    return utftext;   
}

function replaceAll(strSource, lookFor, replaceWith) {
	strLowerCase = strSource.toLowerCase();
	lookForLower = lookFor.toLowerCase();

	while(strLowerCase.indexOf(lookForLower) != -1) {
		posStart = strLowerCase.indexOf(lookForLower);
		posEnd = posStart + lookFor.length;
		strLowerCase = strLowerCase.substring(0, posStart) + replaceWith + strLowerCase.substring(posEnd, strLowerCase.length);
		strSource = strSource.substring(0, posStart) + replaceWith + strSource.substring(posEnd, strSource.length);
	}
	return strSource
}

function stateChanged() { 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		sHTML = xmlHttp.responseText;

//		sHTML = replaceAll(sHTML, "<html>", "")
//		sHTML = replaceAll(sHTML, "</html>", "")
//		sHTML = replaceAll(sHTML, "<body>", "")
//		sHTML = replaceAll(sHTML, "</body>", "")

		document.getElementById("content").innerHTML = sHTML;
		document.getElementById("imgSearch").style.visibility = "hidden";
	}
}

function GetXmlHttpObject() {
    var xmlHttp = null;

    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    return xmlHttp;
}