﻿// declare global variables and set default values

var gstrPrevBgrColor = "";

var garrOpacity = new Array(9);
var garrTimerID = new Array(9);

for (var i = 1; i <= 9; i++) {
	garrOpacity[i] = 100;
	garrTimerID[i] = 0;
}

function gAddOnLoadFunction(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function gInitFB() {
	FB.init("114799241873649");
}

function gShowFader() {
	var oDiv = document.getElementById("divFader");
	oDiv.style.display = "inline";
}

function gHideFader() {
	var oDiv = document.getElementById("divFader");
	oDiv.style.display = "none";
}

//*************************************************************************************************
//* FUNCTION: gGetClient*() // Cross-browser client dimensions and scroll parser.
//*************************************************************************************************
function gGetClientWidth() {
	return gGetClientFilter(window.innerWidth ? window.innerWidth : 0, document.documentElement ? document.documentElement.clientWidth : 0, document.body ? document.body.clientWidth : 0);
}

function gGetClientHeight() {
	return gGetClientFilter(window.innerHeight ? window.innerHeight : 0, document.documentElement ? document.documentElement.clientHeight : 0, document.body ? document.body.clientHeight : 0);
}

function gGetClientScrollLeft() {
	return gGetClientFilter(window.pageXOffset ? window.pageXOffset : 0, document.documentElement ? document.documentElement.scrollLeft : 0, document.body ? document.body.scrollLeft : 0);
}

function gGetClientScrollTop() {
	return gGetClientFilter(window.pageYOffset ? window.pageYOffset : 0, document.documentElement ? document.documentElement.scrollTop : 0, document.body ? document.body.scrollTop : 0);
}

function gGetClientScrollWidth() {
	return document.body.scrollWidth;
}

function gGetClientScrollHeight() {
	return document.body.scrollHeight;
}

function gGetClientFilter(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;

	if (n_docel && (!n_result || (n_result > n_docel))) {
		n_result = n_docel;
	}

	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function gMenuFadeIn(iPageIndex) {
	var oImg = document.getElementById("img-" + iPageIndex);
	//alert("fading in: " + oImg.src);
	clearTimeout(garrTimerID[iPageIndex]);
	garrTimerID[iPageIndex] = setTimeout("gMenuFade(" + iPageIndex + ", 2, 1)", 1);
}

function gMenuFadeOut(iPageIndex) {
    var oImg = document.getElementById("img-" + iPageIndex);
    //alert("fading out: " + oImg.src);
    clearTimeout(garrTimerID[iPageIndex]);
	garrTimerID[iPageIndex] = setTimeout("gMenuFade(" + iPageIndex + ", -4, 1)", 1);
}

function gMenuFade(iPageIndex, iValue, iRate) {
	var oImg = document.getElementById("img-" + iPageIndex);

	garrOpacity[iPageIndex] = garrOpacity[iPageIndex] + iValue;

	if (garrOpacity[iPageIndex] < 0) {
		garrOpacity[iPageIndex] = 0;
	}
	else if (garrOpacity[iPageIndex] > 100) {
		garrOpacity[iPageIndex] = 100;
	}
	else {
		garrTimerID[iPageIndex] = setTimeout("gMenuFade(" + iPageIndex + ", " + iValue + ", " + iRate + ")", iRate);
	}

	gSetOpacity(oImg, garrOpacity[iPageIndex]);
}

function gSetOpacity(oObject, iOpacity) {
	oObject.style.opacity = iOpacity / 100;
	oObject.style.MozOpacity = iOpacity / 100;
	oObject.style.filter = "alpha(opacity=" + (iOpacity) + ")";
}

function gOpenReportPopup(sURL) {
	gOpenPopupWinCenter("popup_report.aspx?url=" + escape(sURL), "", 550, 438, "");
	return false;
}

//*************************************************************************************************
//* FUNCTION: gOpenPopupCalendar() // 
//*************************************************************************************************
function gOpenPopupCalendar(sFunction) {
	var intXSize = 240;
	var intYSize = 180;
	var xpos = ((screen.width / 2) - (intXSize / 2)) - 5;
	var ypos = ((screen.height / 2) - (intYSize / 2)) - 40;
	var sPosition = "width=" + intXSize + ",height=" + intYSize + ",left=" + xpos + ",top=" + ypos;
	var sOptions = "";

	if (sOptions != "") sOptions = "," + sOptions
	window.open("/popup_calendar.aspx?function=" + sFunction, "calendar", sPosition + sOptions);
	return false;
}

//*************************************************************************************************
//* FUNCTION: gOpenPopupWinCenter() // 
//*************************************************************************************************
function gOpenPopupWinCenter(strURL, strName, intXSize, intYSize, strOptions) {
	var xpos = ((screen.width / 2) - (intXSize / 2)) - 5;
	var ypos = ((screen.height / 2) - (intYSize / 2)) - 40;
	var strCenter = "width=" + intXSize + ",height=" + intYSize + ",left=" + xpos + ",top=" + ypos;

	if (strOptions != "") strOptions = "," + strOptions
	return window.open(strURL, strName, strCenter + strOptions);
}

//*************************************************************************************************
//* FUNCTION: gGetXMLHTTPObject() // 
//*************************************************************************************************
function gGetXMLHTTPObject() {

	try {
		// Firefox, Opera 8.0+, Safari
		oXML = new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			oXML = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			oXML = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return oXML;
}

//*************************************************************************************************
//* FUNCTION: gSendPwd() // 
//*************************************************************************************************
function gSendPwd(sCellPhone, iPartyID) {
	location.href = "register_auth.aspx?cellphone=" + sCellPhone + "&partyid=" + iPartyID;
}

//*************************************************************************************************
//* FUNCTION: gCheckKey() // 
//*************************************************************************************************
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 = "";
	}

	if ((sFieldName == "txtLoginUsername" || sFieldName == "txtLoginPassword") && iKeyCode == 13) {
		gValidateLoginForm();
		return false;		
	}
}

//************************************************************************************************************
//* FUNCTION: gSwitchUser() //
//************************************************************************************************************
function gSwitchUser(iPartyID) {
	var oForm = document.getElementById("form1");

	oForm.action = "/login.aspx?cmd=switch&partyid=" + iPartyID;
	oForm.__VIEWSTATE.value = "";
	oForm.submit();

	return false;
}

//************************************************************************************************************
//* FUNCTION: gValidateLoginForm() //
//************************************************************************************************************
function gValidateLoginForm() {
	var oForm = document.getElementById("form1");

	if (oForm.txtLoginUsername.value == "") {
		return gFieldError("", oForm.txtLoginUsername);
	}

	if (oForm.txtLoginPassword.value == "") {
		return gFieldError("", oForm.txtLoginPassword);
	}

	oForm.action = "/login.aspx?cmd=login";
	oForm.__VIEWSTATE.value = "";
	oForm.submit();
}

//************************************************************************************************************
//* FUNCTION: gOpenNewPwdPage() //
//************************************************************************************************************
function gOpenNewPwdPage(sUsername) {
	var oForm = document.getElementById("form1");

	if (sUsername == undefined) {
		sUsername = oForm.txtLoginUsername.value;
	}

	//oForm.action = "login_newpwd.aspx";
	//oForm.__VIEWSTATE.value = "";
	//oForm.submit();

	location.href = "login_newpwd.aspx?user=" + sUsername;
}

//*************************************************************************************************
//* FUNCTION: gReplace() //
//*************************************************************************************************
function gReplace(sSource, sFind, sReplace) {
	var strLowerCase = sSource.toLowerCase();
	var lookForLower = sFind.toLowerCase();

	while (strLowerCase.indexOf(lookForLower) != -1) {
		posStart = strLowerCase.indexOf(lookForLower);
		posEnd = posStart + sFind.length;
		strLowerCase = strLowerCase.substring(0, posStart) + sReplace + strLowerCase.substring(posEnd, strLowerCase.length);
		sSource = sSource.substring(0, posStart) + sReplace + sSource.substring(posEnd, sSource.length);
	}

	return sSource;
}

//*************************************************************************************************
//* FUNCTION: gIsNumeric() //
//*************************************************************************************************
function gIsNumeric(sValue) {

	sValue = gReplace(String(sValue), ",", ".");

	if (sValue == "" || isNaN(sValue)) {
		return false;
	}
	else {
		return true;
	}
}

//*************************************************************************************************
//* FUNCTION: gIsObject() //
//*************************************************************************************************
function gIsObject(sName) {
	var oTest = document.getElementsByName(sName);
	if (oTest.length > 0) return true;
	else return false;
}

//*************************************************************************************************
//* FUNCTION: gFieldError() //
//*************************************************************************************************
function gFieldError(sErrorMsg, oField) {
	if (sErrorMsg != "") {
		alert(sErrorMsg);
	}

	if (oField != undefined) {
		oField.focus();
		if ((oField.type == "text" || oField.type == "textarea") && oField.value != "") oField.select();
	}

	return (false);
}

//************************************************************************************************************
//* FUNCTION: gSortGridView() // Client call to sort a gridview control.
//************************************************************************************************************
function gSortGridView(sGridViewID, sSortExpression, sDivID) {
	var oDiv;

	if (sDivID != "" && sDivID != undefined) {
		oDiv = document.getElementById(sDivID)

		if (oDiv != null) {
			oDiv.scrollIntoView();
		}
	}

	__doPostBack(sGridViewID, "Sort$" + sSortExpression);
}

//************************************************************************************************************
//* FUNCTION: gScrollIntoView() // 
//************************************************************************************************************
function gScrollIntoView(sElementID) {

	var oElement;

	if (sElementID != "" && sElementID != undefined) {
		oElement = document.getElementById(sElementID)

		if (oElement != null) {
			oElement.scrollIntoView();
		}
	}

	return true;
}

//************************************************************************************************************
//* FUNCTION: gM() // Mark object by setting it's background color.
//************************************************************************************************************
function gM(oObject, sColor) {
	if (sColor == undefined) {
		sColor = "#e3c38c"
	}
	gstrPrevBgrColor = oObject.style.backgroundColor;
	oObject.style.backgroundColor = sColor;
}

//************************************************************************************************************
//* FUNCTION: gU() // Unmark object by unsetting it's background color.
//************************************************************************************************************
function gU(oObject) {
	oObject.style.backgroundColor = gstrPrevBgrColor;
}

//************************************************************************************************************
//* FUNCTION: gCheckSwitchbox() // 
//************************************************************************************************************
function gCheckSwitchbox() {
	if (CheckBoxIDs != null) {
		for (var i = 1; i < CheckBoxIDs.length; i++) {
			var cb = document.getElementById(CheckBoxIDs[i]);
			if (!cb.checked) {
				gChangeCheckBoxState(CheckBoxIDs[0], false);
				return;
			}
		}

		gChangeCheckBoxState(CheckBoxIDs[0], true);
	}
}

//************************************************************************************************************
//* FUNCTION: gChangeCheckBoxState() // 
//************************************************************************************************************
function gChangeCheckBoxState(id, checkState) {
	var cb = document.getElementById(id);

	if (cb != null) {
		cb.checked = checkState;
	}
}

//************************************************************************************************************
//* FUNCTION: gChangeAllCheckBoxStates() // 
//************************************************************************************************************
function gChangeAllCheckBoxStates(checkState) {
	if (CheckBoxIDs != null) {
		for (var i = 0; i < CheckBoxIDs.length; i++) {
			gChangeCheckBoxState(CheckBoxIDs[i], checkState);
		}
	}
}

//************************************************************************************************************
//* FUNCTION: gCountChecked() // 
//************************************************************************************************************
function gCountChecked() {
	var iChecked = 0;

	if (CheckBoxIDs != null) {
		for (var i = 1; i < CheckBoxIDs.length; i++) {
			var cb = document.getElementById(CheckBoxIDs[i]);
			if (cb.checked) {
				iChecked++;
			}
		}
	}

	return iChecked;
}

//*************************************************************************************************
//* FUNCTION: gGetChecked() // 
//*************************************************************************************************
function gGetChecked(sElementName, iIndex) {
	var sList = "";
	var j = 0;
	var n = 0; // #/checkboxs on page

	if (iIndex == undefined) {
		iIndex = 0;
	}

	if (gIsObject(sElementName)) {
	    n = document.forms.item(0)[sElementName].length;

		if (n == undefined) {
		    if (document.forms.item(0)[sElementName].checked) {
		        sList = "," + document.forms.item(0)[sElementName].value;
			}
		}
		else {
			for (var i = 0; i < n; i++) {
			    var e = document.forms.item(0)[sElementName][i];
				if ((e.type == "checkbox" || e.type == "radio") && 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;
}
