var nButtonColorOver = "#E00000";
var nButtonColorSelected = "red";

var g_objWindowChange;
var g_strFloatSeparator = ".";
var g_strSiteAddress = "admin/";
var g_nDecimals = 2;
var g_arrControlEnabled = new Array();
var g_arrTablesToFix = new Array();
var g_bDebug = false;

var g_bUseControlPanelDate = false;
var g_strDateSeparator = "/";
var g_strDateFormat = "d/m/Y";

var g_bIE = document.all ? true : false;
var g_v45 = /msie 5\.[01234]/i.test(navigator.userAgent) || /msie 4/i.test(navigator.userAgent);

function OpenPopup(strURL, strName, nWidth, nHeight)
{
	var bScroll = false;
	var bOverflowX = (nWidth > window.screen.width);
	var bOverflowY = (nHeight > window.screen.height);
	if (bOverflowX) { nWidth = window.screen.width; bScroll = true; }
	if (bOverflowY) { nHeight = window.screen.height; bScroll = true; }
	var nLeft = (window.screen.width - nWidth) / 2;
	var nTop = (window.screen.height - nHeight) / 2;
	if (bOverflowX) { nWidth -= 20; }
	if (bOverflowY) { nHeight -= 50; }
	if (bOverflowX && !bOverflowY)
		nHeight += 16;
	else if (bOverflowY && !bOverflowX)
		nWidth += 16;
	var strFeatures = "left=" + nLeft + ",top=" + nTop + ",width=" + nWidth + ",height=" + nHeight + ",resizable=no," + (bScroll ? "scrollbars=yes," : "scrollbars=no,") + "modal=yes";
	if (strURL.indexOf("/") == -1)
		strURL = g_strRootPath + (g_strRootPath.substr(g_strRootPath.length - 1) != "/" ? "/" : "") + strURL;
	var objWin = window.open(strURL, strName, strFeatures);
	return objWin;
}

function OpenImagePopup(strImage, nWidth, nHeight)
{
	OpenPopup("admin/showimage.php?img=" + escape(strImage) + "&width=" + nWidth + "&height=" + nHeight, "image", nWidth, nHeight)
}

function ChangeImage(el, url)
{
	el.src = url;
}

function BlurLink(obj)
{
	if (obj.blur) obj.blur();
}

function ChangeWndLocation()
{
	if ((g_strURLChange.length > 3) && (g_strURLChange.substr(0, 3) == "../"))
		g_strURLChange = g_strURLChange.substr(3);
	g_objWindowChange.location.href = g_strURLChange;
}

function ChangeWindowLocation(objWindow, strURL)
{
	document.body.style.cursor = "wait";

	if (g_v45)
	{
		g_objWindowChange = objWindow;
		g_strURLChange = strURL;
		objWindow.setTimeout(ChangeWndLocation, 1);
	}
	else
		objWindow.location.href = strURL;
}

function SubmitFrm()
{
	g_objWindowChange.submit();
}

function SubmitForm(objForm)
{
	if (g_v45)
	{
		g_objWindowChange = objForm;
		objForm.document.parentWindow.setTimeout(SubmitFrm, 1);
	}
	else
	{
		objForm.submit();
	}
}

function GoToPage(nPage, nCount, nPages)
{
	var gpagination = nCount + "+" + (nPage - 1);
	var strURL = window.document.location.toString();
	var nPos = strURL.indexOf("gpagination=");
	if (nPos >= 0)
	{
		nPos += 12;
		var nStart = nPos;
		while ((nPos < strURL.length) && ((strURL.charAt(nPos) >= '0') && (strURL.charAt(nPos) <= '9') || (strURL.charAt(nPos) == '+')))
			nPos++;
		strURL = strURL.substr(0, nStart) + gpagination + strURL.substr(nPos);
	}
	else
	{
		nPos = strURL.indexOf("?");
		strURL += (nPos < 0) ? "?" : "&";
		strURL += "gpagination=" + gpagination;
	}
	ChangeWindowLocation(window, strURL);
}

function SetURLArg(strURL, strArg, strValue)
{
	var nPos = strURL.indexOf(strArg + "=");
	while ((nPos > 0) && (strURL.charAt(nPos - 1) != "?") && (strURL.charAt(nPos - 1) != "&"))
		nPos = strURL.indexOf(strArg + "=", nPos + 1);
	if (nPos >= 0)
	{
		nPos += strArg.length + 1;
		var nStart = nPos;
		while ((nPos < strURL.length) && (strURL.charAt(nPos) != '&'))
			nPos++;
		strURL = strURL.substr(0, nStart) + strValue + strURL.substr(nPos);
	}
	else
	{
		nPos = strURL.indexOf("?");
		strURL += (nPos < 0) ? "?" : "&";
		strURL += strArg + "=" + strValue;
	}
	return strURL;
}

function GetURLArg(strURL, strArg, strValue)
{
	var strVal = null;
	var nPos = strURL.indexOf(strArg + "=");
	if (nPos >= 0)
	{
		nPos += strArg.length + 1;
		var nStart = nPos;
		while ((nPos < strURL.length) && (strURL.charAt(nPos) != '&'))
			nPos++;
		strVal = strURL.substr(nStart, nPos - nStart);
	}
	return strVal;
}

function IsControlEmpty(strName, strValueType, strEmptyItem)
{
	var bRes = true;
	var obj = document.getElementById(strName);
	if (obj == null) { obj = document.all[strName]; if (obj.type == null) obj.type = "radio"; }
	if (strValueType == null) strValueType = "selection";

	if (obj != null)
	{
		if (obj.type.toLowerCase() == "select-one")
		{
			bRes = (obj.selectedIndex < 0);
			if (!bRes && (strEmptyItem != null))
				bRes = (obj.value == strEmptyItem);
		}
		else if (obj.type.toLowerCase() == "select-multiple")
		{
			if (strValueType == "selection")
			{
				for (var i = 0; i < obj.options.length; i++)
					if (obj.options[i].selected) bRes = false;
			}
			else if (strValueType == "content")
				bRes = (obj.options.length == 0);
		}
		else if (obj.type.toLowerCase() == "radio")
		{
			var objRadio = g_bIE ? document.all[obj.name] : obj;
			for (var i = 0; i < objRadio.length; i++)
				if (objRadio[i].checked) bRes = false;
					
		}
		else if (obj.type.toLowerCase() == "text") bRes = (obj.value.length == 0);
		else if (obj.type.toLowerCase() == "textarea") bRes = (obj.value.length == 0);
	}
	return bRes;
}

//Set focus on control
function ControlSetFocus(strObject)
{
	if (strObject == null) return;

	if ( (strObject.disabled != true) && ((strObject.style == null) || (strObject.style.display != "none") && (strObject.style.visibility != "hidden")) )
	{
		try
		{
			strObject.focus();
		}
		catch(err)
		{
		}

		if (strObject.tagName == "SELECT")
		{
			if (strObject.click != null)
				strObject.click();
			if ((strObject.onchange != null) && (strObject.onchange != "undefined"))
				strObject.onchange();
		}
	}

}

function decode_html_entities(strValue)
{
	var strRes = "";
	var i, j, k, cnt;
	var len = strValue.length;
	var strPrefix;
	var strVal, nVal;
	var nBase;

	for (i = 0; i < len; i++)
	{
		if (strValue.substr(i, 2) == "&&")
		{
			i++;
			strRes += strValue.charAt(i);
		}
		else if (strValue.charAt(i) == "&")
		{
			strPrefix = "";
			i++;
			j = i;
			while ((i < len) && (strValue.charAt(i) != ";"))
				i++;
			if (strValue.charAt(j) == "#")
			{
				cnt = 2;
				if (i - j > 3)
				{
					strPrefix = "\\u";
					cnt += 2;
				}
				else
					strPrefix = "\\x";
		
				j++;
			}
			if (strValue.substr(j, 1) == "x")
			{
				nBase = 16;
				j++;
			}
			else
				nBase = 10;

			strVal = strValue.substr(j, i - j);
			nVal = parseInt(strVal, nBase);

			if (isNaN(nVal))
			{
				if (document.getElementById("spanHiddenEntitiesTranslate") == null)
				{
					var objSpan = document.createElement("SPAN");
					objSpan.id = "spanHiddenEntitiesTranslate";
					objSpan.style.display = "none";
					document.body.appendChild(objSpan);
				}
				document.getElementById("spanHiddenEntitiesTranslate").innerHTML = "&" + strVal + ";";
				if (document.getElementById("spanHiddenEntitiesTranslate").innerText == null)
					strVal = document.getElementById("spanHiddenEntitiesTranslate").childNodes[0].nodeValue;
				else
					strVal = document.getElementById("spanHiddenEntitiesTranslate").innerText;
				strPrefix = "";
			}
			else
			{
				strVal = nVal.toString(16);

				for (k = strVal.length; k < 4; k++)
					strPrefix += "0";
			}
			eval("strRes += '" + strPrefix + strVal + "';");
		}
		else
			strRes += strValue.charAt(i);
	}

	return strRes;
}

function parseIntLocale(nValue, nBase)
{
	if (nBase == null)
		nBase = 10;
	return parseInt(nValue, nBase);
}

function parseFloatLocale(strVar)
{
	var strTmp;

	if ((strVar == "undefined") || (strVar == null) || (strVar.length == 0))
		strVar = 0;
	strVar = strVar.toString().replace(/,/g, ".");
	if (g_strFloatSeparator != ".")
	{
		strCode = "strTmp = strVar.replace(/" + g_strFloatSeparator + "/g, \".\");";
		eval(strCode);
	}
	else
		strTmp = strVar;
	if (strTmp.charAt(0) == '.')
		strTmp = '0' + strTmp;

	return parseFloat(strTmp);
}

//Validation functions
function ValidDigit(strDigit)
{
	return ((strDigit >= "0") && (strDigit <= "9"))
}

function ValidateNumeric(objText)
{
	if (!ValidateNotNull(objText)) return false;

	for (i = 0; i < objText.value.length; i++)
	{
		if (!isDigit(objText.value.charAt(i))) return false;
	}

 	return true;
}

function ValidateFloat(objText)
{
	fTmp = parseFloat(1) / 10;
	strFloatSeparator = fTmp.toLocaleString().substr(1, 1);

	var bSeparator = false;

	if (!ValidateNotNull(objText)) return false;

	for (i = 0; i < objText.value.length; i++)
	{
		if ((objText.value.charAt(i) == strFloatSeparator) || (objText.value.charAt(i) == "."))
		{
			if (bSeparator) return false;
			else bSeparator = true;
		}
		else if (!isDigit(objText.value.charAt(i))) return false;
	}

	return true;
}

function MakeNumeric(objText, bNonZero)
{
	var i, fVal;
	var strValue = "";

	for (i = 0; i < objText.value.length; i++)
	{
		if (isDigit(objText.value.charAt(i)))
			strValue += objText.value.charAt(i).toString();
	}

	if (strValue.length == 0)
		strValue = (bNonZero == true) ? "1" : "0";
	if (bNonZero && (strValue == "0"))
		strValue = "1";
	objText.value = strValue;
}

function MakeFloat(objText, bNonZero)
{
	var i, fVal;
	var strValue = "";
	var bSeparator = false;

	for (i = 0; i < objText.value.length; i++)
	{
		if ((objText.value.charAt(i) == g_strFloatSeparator) || (objText.value.charAt(i) == "."))
		{
			if (!bSeparator)
				strValue += objText.value.charAt(i).toString();
		}
		else if (isDigit(objText.value.charAt(i)))
			strValue += objText.value.charAt(i).toString();
	}
	if (strValue.length == 0)
		strValue = (bNonZero == true) ? "1" : "0";
	if (bNonZero && (strValue == "0"))
		strValue = "1";
	objText.value = FormatFloat(parseFloatLocale(strValue));
}

function ValidateFloatAndNull(objText)
{
	fTmp = parseFloat(1) / 10;
	strFloatSeparator = fTmp.toLocaleString().substr(1, 1);

	var bSeparator = false;

	for (i = 0; i < objText.value.length; i++)
	{
		if (objText.value.charAt(i) == strFloatSeparator)
		{
			if (bSeparator) return false;
			else bSeparator = true;
		}
		else if (!isDigit(objText.value.charAt(i))) return false;
	}

	return true;
}

function ValidateDate(objText)
{
}

function ValidateBool(objText)
{
}


function ValidateNotNull(objText)
{
	if (objText.value.length == 0)
		return false;
	else
		return true;
}

function isLetter (c)
{
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

//checks if s is a valid email
function ValidEmail(s)
{
    if (s.length == 0) return true;

	var regex = /^[a-zA-Z][a-zA-Z0-9._-]*@([a-zA-Z0-9.-]+\.)+[a-zA-Z]{2,3}$/;
	return regex.test(s);
}

function FormatFloatToLocaleSettings(objText)
{
	strVar = objText.value;
	strVar = strVar.replace(".", g_strFloatSeparator);
	strVar = strVar.replace(",", g_strFloatSeparator);
	objText.value = strVar;
}
function FormatFloat(strTotal)
{
	return getPrecision(strTotal, g_nDecimals);
}

function getPrecision(nVar, nDec)
{
	var i, strVar, nSepPos, nDecNum;

	if ((nVar == "undefined") || (nVar == null) || (nVar.length == 0))
		nVar = "0";

	fTmp = parseFloat(1) / 10;
	strFloatSeparator = fTmp.toLocaleString().substr(1, 1);
	if (strFloatSeparator == "")
		strFloatSeparator = ".";

	strVar = ((Math.round(parseFloatLocale(nVar) * Math.pow(10, nDec))) / Math.pow(10, nDec)).toString();
	nSepPos = strVar.indexOf(".");
	nDecNum = strVar.substr(nSepPos + 1).length;

	if (nSepPos == -1)
	{
		strVar += strFloatSeparator;
		for (i = 0; i < nDec; i++)
		{
			strVar += 0;
		}
	}
	else
	{
		strVar = strVar.substr(0, nSepPos) + strFloatSeparator + strVar.substr(nSepPos + 1);
		if (nDecNum < nDec)
		{
			for (i = nDecNum; i < nDec; i++)
			{
				strVar += 0;
			}
		}
	}

	fTmp = 1000;
	strTmp = fTmp.toLocaleString();
	fTmp = 1;

	if (strTmp.length - 4 > fTmp.toLocaleString().length - 1)
	{
		g_strGroupSeparator = strTmp.substr(1, 1);

		i = strVar.indexOf(g_strGroupSeparator);
		nLast = -1;
		if (g_strGroupSeparator == strFloatSeparator)
			nLast = strVar.lastIndexOf(g_strGroupSeparator);

		while ((i >= 0) && (i != nLast))
		{
			strVar = strVar.substr(0, i) + strVar.substr(i + 1);
			i = strVar.indexOf(g_strGroupSeparator);
			nLast--;
		}
	}

	return strVar;
}

function ButtonBkgOver(objBtn, objText, strButton)
{
	if (g_arrControlEnabled[objText.id] == 0) return;

	objBtn.style.visibility = "inherit";
	objText.style.color = nButtonColorOver;
}

function ButtonBkgPush(objBtn, objText, strButton)
{
	if (g_arrControlEnabled[objText.id] == 0) return;

	clearSel();
	objBtn.style.visibility = "inherit";
	objBtn.style.background = "url('" + g_strSiteAddress + "image/" + strButton + "_sel.gif')";
	objBtn.style.backgroundRepeat = "no-repeat";

	objText.className = "btnpush";
}

function ButtonBkgUnpush(objBtn, objText, strButton)
{
	if (g_arrControlEnabled[objText.id] == 0) return;

	objText.className = "";

	objBtn.style.visibility = "inherit";
	objBtn.style.background = "url('" + g_strSiteAddress + "image/" + strButton + ".gif')";
	objBtn.style.backgroundRepeat = "no-repeat";
}

function clearSel()
{
	if (window.getSelection)
		window.getSelection().removeAllRanges();
}

function ButtonBkgOut(objBtn, objText, strButton)
{
	if (g_arrControlEnabled[objText.id] == 0) return;

	/*
	if (parseIntLocale(objText.style.top) >= 0)
	{
		objText.style.top = parseIntLocale(objText.style.top) - 1 + "px";
		objText.style.left = parseIntLocale(objText.style.left) - 1 + "px";
	}
	*/
	objText.className = "";

	if (objText.style.selected == "true")
		objText.style.color = nButtonColorSelected;
	else
		objText.style.color = "#333333";

	objBtn.style.background = "url('" + g_strSiteAddress + "image/" + strButton + ".gif')";
	objBtn.style.backgroundRepeat = "no-repeat";
}

function ButtonSetEnabled(objBtn, bEnabled)
{
	if (objBtn != null)
	{
		if (bEnabled)
		{
			eval("g_arrControlEnabled['span" + objBtn.id + "'] = 1;");
			eval("document.getElementById('span" + objBtn.id + "').style.color = 'black';");
		}
		else
		{
			eval("g_arrControlEnabled['span" + objBtn.id + "'] = 0;");
			eval("document.getElementById('span" + objBtn.id + "').style.color = 'darkgray';");
		}
	}
}

function SetButtonText(objBtn, strText)
{
	eval("document.getElementById('span" + objBtn.id + "').innerHTML = strText;");
}

function emulateEventHandlers(eventNames) {
   for (var i = 0; i < eventNames.length; i++) {	
      document.addEventListener(eventNames[i], function (e) {
         window.event = e;
         window.event.srcElement = e.target;
      }, true); // using capture
   }
}

if (!g_bIE) {
   emulateEventHandlers(["click", "mousemove", "keydown", "keyup", "mouseover", "mouseout"]);
}

