﻿/****** This JavaScript file is used to manipulate HTML content on the client side *****/
/****** Copyright by QTek                                                          *****/
/****** Creation Date: May 30th, 2007                                              *****/

var js_isMinNS4 = (navigator.appName.indexOf("Netscape")>=0 && parseFloat(navigator.appVersion)>=4) ? true : false;
var js_isMinIE4 = (document.all) ? true : false;
var js_isIE4 = (js_isMinIE4 && navigator.appVersion.indexOf("MSIE 4.") >= 0) ? true : false;
var js_isMinIE5 = (js_isMinIE55 || (js_isMinIE4 && navigator.appVersion.indexOf("MSIE 5.")>=0)) ? true : false;
var js_isMinIE55= (js_isMinIE6 || (js_isMinIE4 && navigator.appVersion.indexOf("MSIE 5.5")>=0)) ? true : false;
var js_isMinIE6 = (js_isMinIE4 && navigator.appVersion.indexOf("MSIE 6.")>=0) ? true : false;

var isOpera=(navigator.appName.toLowerCase().indexOf("opera")!=-1) ? true : false;
var isMinOpera5=(isOpera && parseFloat(navigator.appVersion)>= 5)? true : false;
var isMinOpera6=(isOpera && parseFloat(navigator.appVersion)>= 6)? true : false;
var isMinOpera9=(isOpera && parseFloat(navigator.appVersion)>= 9)? true : false;

var js_markX = 0;
var js_markY = 0;
var js_mouseX = 0;
var js_mouseY = 0;
var js_windowWidth = 0;
var js_windowHeight = 0;

/********************************** Width/Height **************************************/
if (typeof(window.innerWidth) == "number")
{
	js_windowWidth = window.innerWidth;
	js_windowHeight = window.innerHeight;
}
else 
{
	if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) //IE 6+ in 'standards compliant mode'
	{
		js_windowWidth = document.documentElement.clientWidth;
		js_windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) //IE 4 compatible
	{
		js_windowWidth = document.body.clientWidth;
		js_windowHeight = document.body.clientHeight;
    }
}

function js_getElementHeight(layer)
{
    var pvHeight = -1 ;
	if (typeof(layer) == "string") layer = js_getElementById(layer);	
	if (js_isMinNS4 && navigator.appVersion.indexOf('4.') >= 0) pvHeight = layer.clip.height;
	else 
	{
		if (isOpera && navigator.appVersion.indexOf('5.') >= 0) pvHeight = layer.style.pixelHeight;
		else 
		{
			if (layer.offsetHeight) pvHeight = layer.offsetHeight;
			else pvHeight = layer.style.height;
		}
	}
	return pvHeight;			 
}
function js_getElementWidth(layer) 
{
    var pvWidth = -1 ;
	if (typeof(layer) == "string") layer = js_getElementById(layer);
	if (js_isMinNS4 && navigator.appVersion.indexOf('4.') >= 0) pvHeight = layer.clip.width;
	else 
	{
		if (isOpera && navigator.appVersion.indexOf('5.') >= 0) pvWidth = layer.style.pixelWidth;
		else 
		{
			if (layer.offsetWidth) pvWidth = layer.offsetWidth;
			else pvWidth = layer.style.width;
		}
	}
	return pvWidth;		
}
function js_setLayerSize(layer, width, height)
{
    var measureUnit = "";
    if( js_isMinNS4 ) measureUnit = "px";
	if (typeof(layer) == "string") layer = js_getElementById(layer);	
	if (js_isMinNS4 && navigator.appVersion.indexOf('4.') >= 0) 
	{
		layer.clip.width = width + measureUnit;
		layer.clip.height = height + measureUnit;
	} 
	else 
	{
		if (isOpera && navigator.appVersion.indexOf('5.') >= 0) 
		{ 
			layer.style.pixelWidth = width + measureUnit;
			layer.style.pixelHeight = height + measureUnit;
		} 
		else 
		{
			layer.style.width = width + measureUnit;
			layer.style.height = height + measureUnit;
		}
	}
}

function js_setIframeHeight(iframeName, height)
{
    var iframeWin = window.frames[iframeName];
    var iframeEl = document.getElementById ? document.getElementById(iframeName) : document.all ? document.all[iframeName] : null;
    if (iframeEl && iframeWin) 
    {
        iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
        if (!height)
        {
            var docHt = js_getDocumentHeight(iframeWin.document);
            height = docHt + 30; // need to add to height to be sure it will all show
        }
        iframeEl.style.height = height + "px";
    }
}
function js_getDocumentHeight(doc) 
{
    var docHt = 0, sh, oh;
    if (doc.height) docHt = doc.height;
    else if (doc.body) 
    {
        if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
        if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
        if (sh && oh) docHt = Math.max(sh, oh);
    }
    return docHt;
}

/********************************** Key Events **************************************/
function js_getKeyCode(e)
{
    var keyEvent = (e) ? e : (window.event ? window.event : null);
    if (keyEvent) return (keyEvent.charCode ? keyEvent.charCode : (keyEvent.keyCode? keyEvent.keyCode : (keyEvent.which ? keyEvent.which : 0)));
    return 0;
}
function js_getKeyDown(e)
{
    return js_getKeyCode(e);
}

/********************************** Mouse Events **************************************/
if (js_isMinNS4) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = js_getMousePosition;
    
function js_getMousePosition(e)
{
	if(js_isMinNS4)
	{
		js_mouseX = e.pageX; 
		js_mouseY = e.pageY;
	}
	else if(js_isMinIE4)
	{
		js_mouseX = window.event.clientX + (document.body == null ? 0 : document.body.scrollLeft);
		js_mouseY = window.event.clientY + (document.body == null ? 0 : document.body.scrollTop);
	}
	return [js_mouseX, js_mouseY]
}

if (js_isMinNS4) document.captureEvents(Event.CLICK);    
document.onclick = js_getMouseClickPosition;

function js_getMouseClickPosition(e)
{
    if(js_isMinNS4)
    {
        js_markX = e.pageX; 
        js_markY = e.pageY;
    }
    if(js_isMinIE4)
    {
        js_markX = window.event.clientX + document.body.scrollLeft;
        js_markY = window.event.clientY + document.body.scrollTop;
    }
    return [js_markX, js_markY]
}

/********************************** Positioning **************************************/
function js_getScrollXY() 
{
    var scrOfX = 0, scrOfY = 0;
    if (typeof(window.pageYOffset) == "number") //Netscape compliant
    {
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } 
    else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) //DOM compliant
    {
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } 
    else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) //IE6 standards compliant mode
    {
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [scrOfX, scrOfY];
}
function js_getScreenCenterPosition()
{
	var js_centerX = 0;
	var js_centerY = 0;
	if (js_windowWidth > 0 && js_windowHeight > 0)
	{
		var scrollXY = js_getScrollXY();
		js_centerX = js_windowWidth / 2 + scrollXY[0];
		js_centerY = js_windowHeight / 2 + scrollXY[1];
	}
	return [js_centerX, js_centerY]
}

function js_getLayerPosition(layer)
{
    var positionLeft = positionTop = 0;
    if (typeof(layer) == "string") layer = js_getElementById(layer);
	if (layer.offsetParent) 
	{
		positionLeft = layer.offsetLeft;
		positionTop = layer.offsetTop;
		while (layer = layer.offsetParent) 
		{
			positionLeft += layer.offsetLeft;
			positionTop += layer.offsetTop;
		}
	}
	return [positionLeft, positionTop];
}
function js_getLayerLeft(layer)
{
	var pvPos = js_getLayerPosition(layer);
	if (pvPos) return pvPos[0];
	return -1 ;
}
function js_getLayerTop(layer)
{
	var pvPos = js_getLayerPosition(layer);
	if (pvPos)
		return pvPos[1];
	return -1 ;
}
function js_getLayerRight(layer)
{
	var pvRight = -1;
	var pvLeft = js_getLayerLeft(layer);
	var pvWidth = js_getElementWidth(layer);
	if (pvLeft > -1 && pvWidth > -1) pvRight = pvLeft + pvWidth;
	return pvRight;
}
function js_getLayerBottom(layer)
{
	var pvBottom = -1;
	var pvTop = js_getLayerTop(layer);
	var pvHeight = js_getElementHeight(layer);
	if (pvTop > -1 && pvHeight > -1) pvBottom = pvTop + pvHeight;
	return pvBottom;
}
function js_isBelongToArea(top, left, right, bottom, posX, posY)
{
    if(posX <= right && posX >= left && posY >= top && posY <= bottom)
        return true;
    return false;
}

/********************************** Moving **************************************/
function js_moveLayerTo(layer, x, y)
{
    var measureUnit = "";
	if( js_isMinNS4 ) measureUnit = "px";
	if (typeof(layer) == "string") layer = js_getElementById(layer);	
	if (document.layers) layer.moveTo(x, y);
	else
	{
		layer.style.left = x + measureUnit;
		layer.style.top = y + measureUnit;
	}
}
function js_moveLayerBy(layer,dx,dy)
{
	var pvPos = js_getLayerPosition(layer);
	if (pvPos)
	{
		pvPos[0] += dx;
		pvPos[1] += dy;
		js_moveLayerTo(layer,pvPos[0],pvPos[1]);
	}
}
function js_moveLayerToX(layer, x)
{
	var pvPos = js_getLayerPosition(layer);
	if (pvPos) js_moveLayerTo(layer, x, pvPos[1]);
}
function js_moveLayerToY(layer,y)
{
	var pvPos = js_getLayerPosition(layer);
	if (pvPos) js_moveLayerTo(layer, pvPos[0], y);
}

/******************************** Show/Hide/Visibility *************************************/
function js_showLayer(layer)
{
	if (typeof(layer) == "string") layer = js_getElementById(layer);
	layer.style.visibility = "visible";
	layer.style.display = "block";
	if (js_isMinNS4)
	{
		if (layer.visibility) layer.visibility = "show";
		else if (layer.style.visibility) layer.style.visibility = "visible";
	}
}
function js_hideLayer(layer)
{
    if (typeof(layer) == "string") layer = js_getElementById(layer);
	layer.style.visibility="hidden";
	layer.style.display="none";	
	if (js_isMinNS4) 
	{
		if(layer.visibility) layer.visibility = "hide";
		else if (layer.style.visibility) layer.style.visibility = "hidden";
	}
}
function js_isVisible(layer)
{
    if (typeof(layer) == "string") layer = js_getElementById(layer);
    if (layer.style.visibility == "visible" || layer.style.display == "block") return true;
    return false;
}

/***************************** Change Styles **********************************/
function js_changeBgColor(layer, toColor)
{
    if (typeof(layer) == "string") layer = js_getElementById(layer);
    layer.style.backgroundColor = toColor;
}
function js_changeBgImage(layer, toImageUrl)
{
    if (typeof(layer) == "string") layer = js_getElementById(layer);
    layer.style.backgroundImage = toImageUrl;
}
function js_changeCursor(layer, cursor)
{
    if (typeof(layer) == "string") layer = js_getElementById(layer);
    layer.style.cursor = cursor;
}

/***************************** Common Methods **********************************/
function js_getElementById(elemId)
{
	if (typeof(elemId) == "object" || elemId.style) return elemId;	
	if (document.getElementById) return document.getElementById(elemId);
	else if (document.all) return document.all[elemId];
	else if (document.layers) return document.layers[elemId];	
	return null;
}
function js_getArrayItemIndex(array, item)
{
    if (array != null && array.length > 0)
    {
        for(var i = 0; i < array.length; i++)
        {
            if (array[i].toString() == item.toString()) return i;
        }
    }
    return -1;
}

/***************************** Ajax with XMLHTTP ActiveX **********************************/
var ajaxRequest = null;
var ajaxRequestResponse = "";

function js_initXmlHttpObject()
{
    ajaxRequest = null; //reset the object
    try { ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
    catch(e)
    {
        try { ajaxRequest=new ActiveXObject("Microsoft.XMLHTTP"); }
        catch(oc) { }
    }
    if (!ajaxRequest && typeof(XMLHttpRequest) != "undefined") ajaxRequest = new XMLHttpRequest();
}
function js_loadXmlHttpResponse() //pass method and url or pass request object
{
    if (arguments.length == 2)
    {
        js_initXmlHttpObject();
        if (ajaxRequest != null)
        {
            ajaxRequestResponse = ""; //reset response text variable
            ajaxRequest.onreadystatechange = js_loadXmlHttpResponse;
            ajaxRequest.open(arguments[0], arguments[1], true);
            ajaxRequest.send(null);
        }
    }
    else if (arguments.length == 0 && ajaxRequest != null && ajaxRequest.readyState == 4 && ajaxRequest.status == 200)
    {
        ajaxRequestResponse = ajaxRequest.responseText;
    }
}
function js_getXmlHttpResponse()
{
    return ajaxRequestResponse;
}

/***************************** Window Objects and Events **********************************/
var qtek_popup_window = "QTekPopupWindow";
var qtek_content_popup = "DIV_ContentPopup";
var qtek_reload_opener = false;

function js_getWindowOpener()
{
	return (window.opener ? window.opener : (window.parent ? window.parent : window));
}
function js_getWindowOpenerUrl()
{
    var opener = js_getWindowOpener();
    return (opener ? opener.location.href : (arguments.length > 0 ? arguments[0] : ""));
}
function js_loadIframe(iframeName, url) 
{
    if (window.frames[iframeName]) 
    {
        window.frames[iframeName].location = url;   
        return false;
    }
    else return true;
}
function js_popupWindow(url, width, height)
{
    if (arguments.length > 3)
    {
        var control = js_findFormField(arguments[3]);
        if (control)
        {
            var valuelist = "";
            if (arguments[3].toLowerCase().indexOf("checker") > 0)
                valuelist = js_getCheckedValueList(control.form, arguments[3], ",");
            
            if (valuelist.length > 0)
            {
                if (url.indexOf("?") < 0) url += "?";
                else if (url.substr(url.length-1) != "?") url += "&";
            
                url += "keys=" + valuelist;
            }
            else
            {
                alert("Please select a value.");
                return false;
            }
        }
        else
        {
            alert("Could not find control to generate id");
            return false;
        }
    }

    if (width <= 0) width = 720;
    if (height <= 0) height = 360;
    var popup = window.open(url, qtek_popup_window, "menubar=0,toolbar=0,location=0,status=0,resizable=0,width="+width+",height="+height);
    popup.moveTo(window.screen.width/2 - width/2 - 25, window.screen.height/2 - height/2 - 25);
    
    return popup;
}
function js_reloadOpenerWindow()
{
    if (qtek_reload_opener && qtek_reload_opener == true)
    {
        var winobj = js_getWindowOpener();
        if (winobj != null && winobj != window) winobj.location.reload(); //= winobj.location.href;
    }
}
function js_reloadWindow()
{
    if (qtek_reload_opener || (arguments.length > 0 && arguments[0] == true)) //the child window must create this variable 'qtek_reload_opener' in order to force the opener window to reload
    {
        var winobj = js_getWindowOpener();
        if (winobj != null && winobj != window) winobj.location.reload(); //= winobj.location.href;
    }
    else
        window.location.reload(); //= window.location.href;
}
function js_closeWindow()
{
    window.close();
}
function js_redirect(url)
{
    if (arguments.length > 1 && arguments[1] == "opener")
    {
        var winobj = js_getWindowOpener();
        if (winobj != null && winobj != window) winobj.location.href = url;
    }
    else window.location.href = url;
}

function js_showContentPopup(contentUrl, eventElement)
{
    js_hideContentPopup();
    qtek_content_popup = js_getElementById(qtek_content_popup);
    if (qtek_content_popup == null)
    {
        alert("Cannot load content because of missing content container!");
        return false;
    }
    if (eventElement) js_moveLayerTo(qtek_content_popup, js_getLayerRight(eventElement) - 5, js_getLayerBottom(eventElement) - 5);
    qtek_content_popup.innerHTML = "Loading";

    ajaxRequest = null;
    js_initXmlHttpObject();
    if (ajaxRequest != null)
    {
        js_showLayer(qtek_content_popup);
        ajaxRequest.onreadystatechange = js_loadPopupContent;
        ajaxRequest.open("GET", contentUrl, true);
        ajaxRequest.send(null);
    }
    else js_hideLayer(qtek_content_popup);
}
function js_loadPopupContent()
{
    if (qtek_content_popup == null)
    {
        alert("Cannot load content because of missing container!");
        return false;
    }    
    if (ajaxRequest.readyState == 4) 
    {
	    if (ajaxRequest.status == 200) 
	    {
		    if (ajaxRequest.responseText == "") js_hideLayer(qtek_content_popup);
		    else
		    {
		        js_showLayer(qtek_content_popup);
			    qtek_content_popup.innerHTML = ajaxRequest.responseText;
			    ajaxRequest = null;
			    
			    if (js_getLayerBottom(qtek_content_popup) >= js_windowHeight)
			        js_moveLayerToY(qtek_content_popup, js_windowHeight - js_getElementHeight(qtek_content_popup) - 2);
			    if (js_getLayerRight(qtek_content_popup) >= js_windowWidth)
			        js_moveLayerToX(qtek_content_popup, js_windowWidth - js_getElementWidth(qtek_content_popup) - 2);
		    }
	    }
	    else qtek_content_popup.innerHTML= "Could not load content. Status: " + ajaxRequest.statusText;
    }
}
function js_hideContentPopup()
{
    if (qtek_content_popup) js_hideLayer(js_getElementById(qtek_content_popup));
}
/***************************** Form and Controls **********************************/
function js_findForm(formName)
{
	if (typeof(formName) == "object") return formName;
	
	var dforms = document.forms;
	if (!formName && dforms.length > 0) return dforms[0];
	if (typeof(formName) == "string")
	{		
		for (var i = 0; i < dforms.length; i++)
		{
			if (dforms[i].name.toUpperCase() == formName.toUpperCase()) return dforms[i];
		}
	}
	return null;
}
function js_findFormByField(fieldName)
{
	if (typeof(fieldName) == "object") return fieldName;
	var i = 0, j = 0;
	var dforms = document.forms;
	for (i = 0; i < dforms.length; i++)
	{
		for(j = 0; j < dforms[i].elements.length; j++)
		{
			if (dforms[i].elements[j].name.toUpperCase() == fieldName.toUpperCase()) return dforms[i];
		}
	}
	if (js_findFormByField.arguments.length == 1) alert("Web Application Error!\n\nCould not find control that has name " + fieldName + ".");
	return null;
}
function js_findFormField(fieldName, formName)
{
	if (typeof(fieldName) == "object") return fieldName;
	
	var i = 0, j = 0;
	if (formName)
	{
		formName = js_findForm(formName);
		for(j = 0; j < formName.elements.length; j++)
		{
			if (formName.elements[j].name.toUpperCase() == fieldName.toUpperCase()) return formName.elements[j];
		}
	}
	else
	{		
		var dforms = document.forms;
		for (i = 0; i < dforms.length; i++)
		{
			for(j = 0; j < dforms[i].elements.length; j++)
			{
				if (dforms[i].elements[j].name.toUpperCase() == fieldName.toUpperCase()) return dforms[i].elements[j];
			}
		}
	}	
	if (js_findFormField.arguments.length < 3) alert("Web Application Error!\n\nCould not find control that has name " + fieldName + ".");
	return null;
}

function js_validateMaxLength(textarea, maxlength)
{
    textarea = js_findFormField(textarea, null);
    if (textarea && textarea.value.length > maxlength)
        textarea.value = textarea.value.substring(0, maxlength);
}

//checkboxes
function js_countChecked(form, checkName)
{
	var fvcnt = 0;
	form = js_findForm(form);
	for (var i = 0; i < form.length; i++)
	{
		if (form[i].type == "checkbox" && form[i].name == checkName && form[i].checked) fvcnt++;
	}
	return fvcnt;
}
function js_getCheckedValues(checkName, delimiter) //delimiter can be passed as second parameter
{
    checkName = js_findFormField(checkName); 
    if (!checkName) return "";
    if (!delimiter) delimiter = ",";

	var fvcnt = 0;
	var fvresult = "";	
	var form = checkName.form;
	for (var i = 0; i < form.length; i++)
	{
		if (form[i].type == "checkbox" && form[i].name == checkName.name && form[i].checked)
		{
			if (fvcnt > 0) fvresult = fvresult + delimiter + form[i].value;
			else fvresult = form[i].value;
			fvcnt++;
		}
	}
	return fvresult;
}
function js_checkOnValue(checker, value)
{
    checker = js_findFormField(checker);
    if (checker)
    {
        for(var i = 0; i < checker.form.length; i++)
        {   
            if (checker.form[i].name == checker.name && checker.form[i].value.toString() == value.toString())
            {   
                checker.form[i].checked = true;
                return;
            }
        }
    }
}

//select boxes
function js_selectAll(selectName)
{
    selectName = js_findFormField(selectName);
    if (selectName)
    {
	    for (var i = 0; i < selectName.options.length; i++)
	    {
		    selectName.options[i].selected = true;
	    }
	}
}
function js_selectOnValue(selectbox, value)
{
    selectbox = js_findFormField(selectbox);
    if (selectbox)
    {
        for(var i = 0; i < selectbox.options.length; i++)
        {
            if (selectbox.options[i].value.toString() == value.toString())
            {
                selectbox.options[i].selected = true;
                return;
            }
        }
    }
}
function js_addSelectOption(selectName, selectValue, selectText, selected)
{
	var selectObject = js_findFormField(selectName);
	var option = new Option(selectText, selectValue);
	selectObject.options[selectObject.options.length] = option;
	if (selected) selectObject.options[selectObject.options.length - 1].selected = true;
}
function js_clearSelectOptions(selectName, fromIndex, toIndex)
{
    selectName = js_findFormField(selectName);
    if (selectName)
    {
        if (toIndex <= 0) toIndex = selectName.options.length - 1;
        if (fromIndex < 0 || fromIndex > selectName.options.length) fromIndex = 0;
        for (var i = fromIndex; i <= toIndex && i < selectName.options.length; i++)
	    {
		    selectName.options[i] = null;
	    }
    }
}
function js_removeSelectOption(selectName, values, removeByValue) //values is a list of indexes; if removeByValue=true then values is an option value list
{
	if (values == null || values.length == 0) return;
	var arrayValue = values.split(",");
	if (arrayValue.length > 1)
	{
		for (var i = 0; i < arrayValue.length; i++)
		{
			js_removeSelectOption(selectName, arrayValue[i], removeByValue);
		}
		return;
	}
	
	var selectObject = js_findFormField(selectName);
	var optionIndex = values; //values contains a single value and assuming it is an index value
	if (removeByValue)
	{
	    optionIndex = -1;
		for (var i = 0; i < selectObject.options.length; i++) 
		{
			if (selectObject.options[i].value.toUpperCase() == selectValue.toUpperCase()) 
			{
				optionIndex = i;
				break;
			}
		}
	}
	if (selectObject.options.length > 0 && optionIndex >= 0 && optionIndex < selectObject.options.length)
	{
		selectObject.options[optionIndex] = null;
		if (selectObject.options.length > optionIndex) selectObject.options[optionIndex].selected = true;
		else if (selectObject.options.length) selectObject.options[selectObject.options.length - 1].selected = true;
	}
}

function js_enableControls(controllist, valuelist, checkvalue)
{
    if (typeof(controllist) == "object")
    {
        valuelist = js_trim(valuelist);
        checkvalue = js_trim(checkvalue);
        var valueFound = false;
        if (valuelist == checkvalue) valueFound = true;
        else if (valuelist != "")
        {
            var values = valuelist.split(";");
            for(var i = 0; i < values.length && valueFound == false; i++)
            {
                if (values[i].toString() == checkvalue.toString())
                    valueFound = true;
            }
        }
        controllist.disabled = (valueFound ? false : true);
        return;
    }
    else if (controllist != "")
    {
        var controls = controllist.split(";");
        for(var i = 0; i < controls.length; i++)
        {
            var control = js_findFormField(controls[i]);
            if (control) js_enableControls(control, valuelist, checkvalue);
        }
    }
    return;
}
function js_disableControls(controllist, valuelist, checkvalue)
{
    if (typeof(controllist) == "object")
    {
        valuelist = js_trim(valuelist);
        checkvalue = js_trim(checkvalue);
        var valueFound = false;
        if (valuelist == checkvalue) valueFound = true;
        else if (valuelist != "")
        {
            var values = valuelist.split(";");
            for(var i = 0; i < values.length && valueFound == false; i++)
            {
                if (values[i].toString() == checkvalue.toString())
                    valueFound = true;
            }
        }
        controllist.disabled = (valueFound ? true : false);
        return;
    }
    else if (controllist != "")
    {
        var controls = controllist.split(";");
        for(var i = 0; i < controls.length; i++)
        {
            var control = js_findFormField(controls[i]);
            if (control) js_disableControls(control, valuelist, checkvalue);
        }
    }
    return;
}

//Control Focus
function js_focus(control)
{
    window.focus();
    control = js_findFormField(control, null);
    if (control) control.focus();
}
function js_focusNext(nextControl, eventKeyCode, e) //e is captured event
{
    if (js_getKeyCode(e) == eventKeyCode)
    {
        nextControl = js_findFormField(nextControl, null);
        if (nextControl)
        {
            js_trimForm(nextControl.form);            
            if (arguments.length > 3)
            {
                var doFocus = false;
                if (arguments[3] == "EMPTY" && nextControl.value == "")
                    doFocus = true;

                if (doFocus) nextControl.focus();
                else if (arguments.length > 4 && arguments[4] == "SUBMIT")
                    nextControl.form.submit();
            }
            else nextControl.focus();
        }
    }  
}

/***************************** Validation **********************************/
function js_trim(string) 
{
	var startpos = 0, endpos = string.length - 1;
	if (endpos == -1) return string;
	while (startpos <= string.length && string.substring(startpos, startpos + 1) == " ") startpos++; 
	while (endpos >= 0 && string.substring(endpos, endpos + 1) == " ") endpos--; 
	if (startpos > endpos) return "";
	else return string.substring(startpos, endpos + 1);
}
function js_isDigit(digit)
{
	var alldigit = new Array(0,1,2,3,4,5,6,7,8,9);
	for (var i = 0; i < alldigit.length; i++) 
	{
		if (digit == alldigit[i]) return true;
	}
	return false;
}
function js_isNumeric(value)
{
	value = js_trim(value);
	if (value == "") return false;
	var arrayChars = value.split("");
	var i = 0;
	if (arrayChars[0] == "-") i = 1;
	for (i = i; i < arrayChars.length; i++)
	{
		if (!js_isDigit(arrayChars[i])) return false;
	}	
	return true;
}

/***************************** Cultural/Language **********************************/
function js_getCultureIndex()
{
    var code = "EN";
    if (arguments.length > 0) code = arguments[0];
    if (code.toUpperCase() == "VI") return 1; //Vietnamese
    
    return 0;
}
function js_parseCurrency(inCurrency) //convert currency to a value in English
{
    var culture = js_getCultureIndex(gjsv_LanguageCulture);
	if (arguments.length > 1 && arguments[1]) culture = js_getCultureIndex(arguments[1]);
	
	var cents = 0, dollars = "";
    var cent_delim = (culture == 1 ? "," : ".");
    var dollar_delim = (culture == 1 ? "." : ",");
    var money_parts = inCurrency.toString().split(cent_delim);
    if (money_parts.length > 1)
    {
        if (js_isNumeric(money_parts[1]) == false)
        {
            alert("Invalid cent part in currency value " + inCurrency);
            return false;
        }
        cents = parseInt(money_parts[1], 10);
    }
    money_parts = money_parts[0].toString().split(dollar_delim);
    for(var i = money_parts.length - 1; i >= 0; i--)
    {
        if (js_isNumeric(money_parts[i]) == false || (i > 0 && money_parts[i].length != 3))
        {
            alert("Invalid dollar part in currency value " + inCurrency);
            return false;
        }
        else dollars = money_parts[i] + "" + dollars;
    }
    return parseFloat((dollars == "" ? "0" : dollars) + "" + (cents > 0 ? "." + cents : ""));
}

/***************************** Formats **********************************/
function js_paddingLeftZeros(input, finalLength)
{
    input = js_trim(input.toString());
    while(input.length < finalLength)
    {
        input = "0" + input;
    }
    return input;
}
function js_formatCurrency(inCurrency)
{
    var value = js_parseCurrency(inCurrency, (arguments.length > 1 ? arguments[1] : null));
    var culture = js_getCultureIndex(gjsv_LanguageCulture);
	if (arguments.length > 1 && arguments[1]) culture = js_getCultureIndex(arguments[1]);

	var cent_delim = (culture == 1 ? "," : ".");
    var dollar_delim = (culture == 1 ? "." : ",");    
	var money_parts = value.toString().split(cent_delim);
	var cents = (money_parts.length > 1 ? money_parts[1].toString() : "0");
	var dollars = money_parts[0].toString();

	value = "";
	for(var i = 1; i <= dollars.length; i++)
	{
	//alert(dollars + " -- " + dollars.length + " -- " + dollars.charAt(dollars.length - i));
	    value = dollars.charAt(dollars.length - i) + "" + value;
	    if (i % 3 == 0 && i < dollars.length)
	        value = dollar_delim + value;
	}
	return value + (parseInt(cents, 10) > 0 ? cent_delim + cents : "");
}