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)
{
	
	if (strName == "basket")
		var bScroll = true;
	else
		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=" + (g_bIE ? "auto" : "yes") + ",") + "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)
{
	objWindow.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;
	}
	if (window.strGETFilters != null) strURL += window.strGETFilters;
	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 SetLanguage(strLang)
{
	window.top.location.href = SetURLArg(window.top.location.href.toString(), 'lang', strLang).replace("#", "");
}

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() == "password") 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.replace(/'/g, "\\'") + "';");
		}
		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,5}$/;
	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"]);
}

function AddCodeToFunction(strFunc, strCode)
{
	var strParams = "";
	if (strCode.length > 0)
	{
		if (window[strFunc] != null)
		{
			strFnCode = window[strFunc].toString();
			if (strFnCode.length > 0)
			{
				var nPos = strFnCode.indexOf("{");
				var nParamPos = strFnCode.indexOf("(");
				var nLastPos;
				if ((nParamPos >= 0) && (nParamPos < nPos))
				{
					nLastPos = nParamPos + 1;
					while ((strFnCode.charAt(nLastPos) != ")") && (nLastPos < strFnCode.length))
						nLastPos++;
					strParams = strFnCode.substr(nParamPos + 1, nLastPos - nParamPos - 1);
				}
	
				nLastPos = strFnCode.lastIndexOf("}");
				strFnCode = strFnCode.substr(nPos + 1, nLastPos - nPos - 1);
			}
			strFnCode =  strFnCode + ";" + strCode + ";";
		}
		else
			strFnCode = strCode;
		window[strFunc] = new Function(strParams, strFnCode);
	}
}

function AddCodeToJSFunction(obj, strFunc, strCode)
{
	if (obj == null) return;
	var strParams = "";
	var fn;
	eval ("fn = obj." + strFunc + ";");
	if (strCode.length > 0)
	{
		if (fn != null)
		{
			strFnCode = fn.toString();
			if (strFnCode.length > 0)
			{
				var nPos = strFnCode.indexOf("{");
				var nParamPos = strFnCode.indexOf("(");
				var nLastPos;
				if ((nParamPos >= 0) && (nParamPos < nPos))
				{
					nLastPos = nParamPos + 1;
					while ((strFnCode.charAt(nLastPos) != ")") && (nLastPos < strFnCode.length))
						nLastPos++;
					strParams = strFnCode.substr(nParamPos + 1, nLastPos - nParamPos - 1);
				}
	
				nLastPos = strFnCode.lastIndexOf("}");
				strFnCode = strFnCode.substr(nPos + 1, nLastPos - nPos - 1);
			}
			strFnCode =  strFnCode + ";" + strCode + ";";
		}
		else
			strFnCode = strCode;
		eval("obj." + strFunc + " = new Function(strParams, strFnCode);");
	}
}

function FilterChildCombo(strCombo, strParent, strFilter, strParentValue, bEmpty)
{
	var i, j, bFound;
	var arrFilter, strHTML;
	var arrValue = new Array();
	var obj = document.getElementById(strCombo);
	var objAvail = document.getElementById(strCombo + "_available");
	var objParent = document.getElementById(strParent);
	eval("arrFilter = window." + strFilter + ";");
	var strValue;

	if (strParentValue == null) strParentValue  = "selection";

	if (obj.type.toLowerCase() == "select-multiple")
	{
		strValue = new Array();
		for (i = 0; i < obj.options.length; i++)
			if (obj.options[i].selected) strValue[strValue.length] = obj.options[i].value;
	}
	else
		strValue = obj.value;

	if (strParentValue == "selection")
	{
		if (objParent.type.toLowerCase() == "select-multiple")
		{
			for (i = 0; i < objParent.options.length; i++)
				if (objParent.options[i].selected) arrValue[arrValue.length] = objParent.options[i].value;
		}
		else
			arrValue[0] = objParent.value;
	}
	if (arrValue[0] == "") strParentValue = "content";
	if (strParentValue == "content")
	{
		if ((objParent.type.toLowerCase() == "select-one") || (objParent.type.toLowerCase() == "select-multiple"))
		{
			for (i = 0; i < objParent.options.length; i++)
				arrValue[arrValue.length] = objParent.options[i].value;
		}
		else
			arrValue[0] = objParent.value;
	}
	strHTML = "";
	var arrSetValues = new Array();
	for (i in arrFilter)
	{
		bFound = false;
		for (j = 0; j < arrValue.length; j++)
			if (arrFilter[i][0] == arrValue[j])
			{
				bFound = true;
				break;
			}
		if (bFound)
		{
			var strVal = arrFilter[i][2] != null ? arrFilter[i][2] : i;
			if (arrSetValues[strVal] == null)
			{
				if (strHTML.length > 0) strHTML += "\n";
				arrSetValues[strVal] = 1;
				strHTML += strVal + "\t" + arrFilter[i][1];
			}
		}
	}
	if (bEmpty == null) bEmpty = false;
	if ((objAvail != null) && (objParent != objAvail))
	{
		var arrAvail = strHTML.split("\n");
		strHTML = "";
		strHTMLAvail = "";
		for (i = 0; i < arrAvail.length; i++)
		{
			if (arrAvail[i] != "")
			{
				var bFound = false;
				var arrTmp = arrAvail[i].split("\t");
				var strVal = arrTmp[0];
				for (j = 0; !bFound && (j < obj.options.length); j++)
					if (obj.options[j].value == strVal)
						bFound = true;
				if (bFound)
				{
					if (strHTML.length > 0) strHTML += "\n";
					strHTML += arrTmp[0] + "\t" + arrTmp[1];
				}
				else
				{
					if (strHTMLAvail.length > 0) strHTMLAvail += "\n";
					strHTMLAvail += arrTmp[0] + "\t" + arrTmp[1];
				}
			}
		}
		RenderCombo(strCombo, strHTML, "", bEmpty, !obj.disabled)
		RenderCombo(strCombo + "_available", strHTMLAvail, "", bEmpty, !obj.disabled)
	}
	else
		RenderCombo(strCombo, strHTML, strValue, bEmpty, !obj.disabled)
	if (document.getElementById(strCombo).onchange != null) document.getElementById(strCombo).onchange();
}

function AfterFilterChildComboRemote(strResult)
{
	var arrTmp = strResult.split(" ");
	var strCombo = arrTmp[0];
	var strParent = arrTmp[1];
	var obj = document.getElementById(strCombo);
	var objAvail = document.getElementById(strCombo + "_available");
	var objParent = document.getElementById(strParent);
	var bEmpty = (arrTmp[2] == "1");
	var strHTML = urlunescape(arrTmp[3]);

	if (obj.type.toLowerCase() == "select-multiple")
	{
		strValue = new Array();
		for (i = 0; i < obj.options.length; i++)
			if (obj.options[i].selected) strValue[strValue.length] = obj.options[i].value;
	}
	else
		strValue = obj.value;

	if ((objAvail != null) && (objParent != objAvail))
	{
		var arrAvail = strHTML.split("\n");
		strHTML = "";
		strHTMLAvail = "";
		for (i = 0; i < arrAvail.length; i++)
		{
			if (arrAvail[i] != "")
			{
				var bFound = false;
				var arrTmp = arrAvail[i].split("\t");
				var strVal = arrTmp[0];
				for (j = 0; !bFound && (j < obj.options.length); j++)
					if (obj.options[j].value == strVal)
						bFound = true;
				if (bFound)
				{
					if (strHTML.length > 0) strHTML += "\n";
					strHTML += arrTmp[0] + "\t" + arrTmp[1];
				}
				else
				{
					if (strHTMLAvail.length > 0) strHTMLAvail += "\n";
					strHTMLAvail += arrTmp[0] + "\t" + arrTmp[1];
				}
			}
		}
		RenderCombo(strCombo, strHTML, "", bEmpty, !obj.disabled)
		RenderCombo(strCombo + "_available", strHTMLAvail, "", bEmpty, !obj.disabled)
	}
	else
		RenderCombo(strCombo, strHTML, strValue, bEmpty, !obj.disabled)
	if (document.getElementById(strCombo).onchange != null) document.getElementById(strCombo).onchange();
}

function FilterChildComboRemote(strCombo, strParent, strFilter, strParentValue, bEmpty, strRemoteArgs)
{
	var i, j, bFound;
	var arrFilter, strHTML;
	var arrValue = new Array();
	var obj = document.getElementById(strCombo);
	var objAvail = document.getElementById(strCombo + "_available");
	var objParent = document.getElementById(strParent);
	eval("arrFilter = window." + strFilter + ";");
	var strValue;

	if (strParentValue == null) strParentValue  = "selection";

	if (obj.type.toLowerCase() == "select-multiple")
	{
		strValue = new Array();
		for (i = 0; i < obj.options.length; i++)
			if (obj.options[i].selected) strValue[strValue.length] = obj.options[i].value;
	}
	else
		strValue = obj.value;

	if (strParentValue == "selection")
	{
		if (objParent.type.toLowerCase() == "select-multiple")
		{
			for (i = 0; i < objParent.options.length; i++)
				if (objParent.options[i].selected) arrValue[arrValue.length] = objParent.options[i].value;
		}
		else
			arrValue[0] = objParent.value;
	}
	if (arrValue[0] == "") strParentValue = "content";
	if (strParentValue == "content")
	{
		if ((objParent.type.toLowerCase() == "select-one") || (objParent.type.toLowerCase() == "select-multiple"))
		{
			for (i = 0; i < objParent.options.length; i++)
				arrValue[arrValue.length] = objParent.options[i].value;
		}
		else
			arrValue[0] = objParent.value;
	}
	var strValues = "";
	for (j = 0; j < arrValue.length; j++)
		strValues += (j > 0 ? " " : "") + escape(arrValue[j]);
	if (bEmpty == null) bEmpty = false;

	jsrsExecute(g_strRootPath + "rs/objaccess.php", AfterFilterChildComboRemote, "FilterChildComboRemote", Array(strCombo + " " + strParent + " " + (bEmpty ? "1 " : "0 ") + strRemoteArgs, strValues), g_bDebug);
}

function RenderCombo(strName, strHTML, strValue, bAllowNull, bEnabled)
{
	var i;

	if (strHTML.length > 0)
		arrResult = strHTML.split("\n");
	else
		arrResult = new Array();

	var objCombo = document.getElementById(strName);
	if (objCombo == null)
		return;

	if (true) //new rendering
	{
		var strEmptyLabel = "";
		if (bAllowNull && (objCombo.options.length > 0) && (objCombo.options[0].value == ""))
			strEmptyLabel = objCombo.options[0].text;
		var arrOpt, idx = 0;
		objCombo.options.length = 0;
		if (bAllowNull)
			objCombo.options[idx++] = new Option(strEmptyLabel, '');
		var len = objCombo.options.length;
		for (var i = 0; (i < arrResult.length); i++)
		{
			arrOpt = arrResult[i].split("\t");
			objCombo.options[idx++] = new Option(arrOpt[1].replace(/\n/g, "").replace(/\r/g, ""), arrOpt[0]);
		}
		objCombo.options.length = idx;
		objCombo.disabled = !bEnabled;
		if(typeof(strValue) == "string")
			objCombo.value = strValue;
		else
		{
			for (var i = 0; i < objCombo.options.length; i++)
			{
				var bFound = false;
				for (var j = 0; j < strValue.length; j++)
					if (strValue[j] == objCombo.options[i].value)
					{
						bFound = true;
						break;
					}
				if (bFound)
					objCombo.options[i].selected = true;
			}
		}
		return;
	}

	if (document.getElementById("div" + strName) == null)
		return;

	var fnOnChange = document.getElementById(strName).onchange;
	strSelect = document.getElementById("div" + strName).innerHTML;
	nStart = strSelect.indexOf(" style=");
	if (nStart >= 0)
	{
		nEnd = parseInt(nStart, 10) + 8;
		nDelim = strSelect.substr(nEnd - 1, 1);
		while ((strSelect.substr(nEnd, 1) != nDelim) && (nEnd < strSelect.length))
			nEnd++;
		strStyle = strSelect.substr(nStart, nEnd - nStart + 1);
	}
	else
		strStyle = "";

	if (!bEnabled)
		strDisabled = " disabled";
	else
		strDisabled = "";
	if (document.getElementById(strName).multiple) strStyle += " multiple";
	if ((document.getElementById(strName).size != null) && (document.getElementById(strName).size != 0)) strStyle += " size='" + document.getElementById(strName).size+ "'";
	strSelect = "<SELECT class='" + document.getElementById(strName).className + "' id=" + strName + strStyle + " name=" + strName + strDisabled + ">";
	if (bAllowNull)
		strSelect += "<OPTION value=0>";
	for (i = 0; i < arrResult.length; i++)
	{
		arrOpt = arrResult[i].split("\t");
		strSelect += "<OPTION value=" + arrOpt[0] + "";

		if ((arrOpt[1] == "undefined") || (arrOpt[1] == null))
			strSelect += ">";
		else
			strSelect += ">" + arrOpt[1];
	}
	strSelect += "</OPTION></SELECT>";

	var strOld = document.getElementById("div" + strName).innerHTML.replace(/ selected/g, "").replace(/\x80/g, "&#x20AC;");
	var strNew = strSelect.replace(/\x80/g, "&#x20AC;").replace(/\x0d/g, "");
	if (strOld != strNew)
	{
		document.getElementById("div" + strName).innerHTML = strSelect;
	}
	
	document.getElementById(strName).value = strValue;
	document.getElementById(strName).onchange = fnOnChange;
}

function urlunescape(str)
{
	return unescape(str.replace(/\+/g, " "));
}

function ClearAllInputs()
{
	var arrElements = window.document.getElementsByTagName("input");
	for (var el in arrElements)
	{
		if (arrElements[el].length > 0)
			for (var i = 0; i < arrElements[el].length; i++)
			{
				if (((arrElements[el][i].disabled == null) || !arrElements[el][i].disabled) &&
				    ((arrElements[el][i].readonly == null) || !arrElements[el][i].readonly))
				{
					arrElements[el][i].value = "";
					arrElements[el][i].checked = false;
				}
			}
		else
			if (((arrElements[el].disabled == null) || !arrElements[el].disabled) &&
			    ((arrElements[el].readonly == null) || !arrElements[el].readonly))
			{
				arrElements[el].value = "";
				arrElements[el].checked = false;
			}
	}
	var arrElements = window.document.getElementsByTagName("select");
	for (var el in arrElements)
	{
		if ((arrElements[el].disabled != null) && !arrElements[el].disabled)
			arrElements[el].selectedIndex = 0;
	}
	arrElements = window.document.getElementsByTagName("textarea");
	for (var el in arrElements)
	{
		if ((arrElements[el].disabled != null) && !arrElements[el].disabled)
			arrElements[el].value = "";
	}
}

function SetTabIndex(arrItems, nInitial)
{
	if (nInitial == null) nInitial = 0;
	for (var i = 0; i < arrItems.length; i++)
	{
		document.getElementById(arrItems[i]).setAttribute("tabIndex", i + nInitial);
	}
}

function GetYMDDate(strCtrl)
{
	var dtPicker = getDTObject(strCtrl);
	var strRes, nTmp;

	if ((dtPicker.getYear() < 0) || (dtPicker.getValue() == null))
		strRes = "";
	else
	{
		strRes = dtPicker.getYear();
		strRes += "/";
		nTmp = dtPicker.getMonth();
		if (nTmp < 10)
			strRes += "0" + nTmp;
		else
			strRes += nTmp;
		strRes += "/";
		nTmp = dtPicker.getDay();
		if (nTmp < 10)
			strRes += "0" + nTmp;
		else
			strRes += dtPicker.getDay();
	}

	return strRes;
}

function AfterExpandTree(strResult)
{
	var arrTmp = strResult.split(" ");
	var strElement = arrTmp[0];
	var strList = arrTmp[1];
	var nID = arrTmp[2];
	var nLevel = arrTmp[3];
	var strResult = urlunescape(arrTmp[4]);
	var strName = "divTree" + strElement + "_" + strList + "_" + nID + "_" + nLevel;

	var strCodeEval = "";
	var nPos = strResult.indexOf("<script");
	while (nPos != -1)
	{
		var nPos2 = strResult.indexOf("</script", nPos + 1);
		nPos = strResult.indexOf(">", nPos + 1);
		var strCode = strResult.substr(nPos + 1, nPos2 - nPos - 1);
		strCodeEval += ";" + strCode;
		nPos = strResult.indexOf("<script", nPos + 1);
	}

	document.getElementById(strName).innerHTML = strResult;
	if (strCodeEval != "") eval(strCodeEval);
	document.getElementById(strName).loaded = true;
	document.getElementById(strName).style.display = "block";
}

function ExpandTree(obj, strElement, strList, nID, nLevel)
{
	var strName = "divTree" + strElement + "_" + strList + "_" + nID + "_" + nLevel;
	if (document.getElementById(strName).loaded == null)
		jsrsExecute(g_strRootPath + "rs/objaccess.php", AfterExpandTree, "ExpandTree", Array(strElement, strList, nID, nLevel), false);
	else if (document.getElementById(strName).style.display == "none")
	{
		document.getElementById(strName).style.display = "block";
	}
	else if (document.getElementById(strName).style.display == "block")
	{
		document.getElementById(strName).style.display = "none";
	}
}
