function glOpen(pageIndex, query, forcedWindowType) {
	if (glPages[pageIndex] == null) {
		glHndlError('Page [' + pageIndex + '] not available.', '', '');
		return;
	}
	var title = glPages[pageIndex].title;
	var relUrl = glPages[pageIndex].relFolder + pageIndex + '.aspx';
	var width = glPages[pageIndex].width;
	var height = glPages[pageIndex].height;
	var leftPos = glPages[pageIndex].leftPos;
	var topPos = glPages[pageIndex].topPos;
	var windowType = glPages[pageIndex].windowType;

	relUrl = Trim(relUrl);
	query = Trim(query);
	if (relUrl.indexOf('?') < 0)
		relUrl += '?';

	if (arguments.length > 1) {
		if (query.charAt(0) == '?') {
			query = query.substring(1);
		}
		relUrl += query;
	}

	if (arguments.length > 2) {
		windowType = forcedWindowType;
	}

	if (glIsAuthorized(pageIndex)) {
		relUrl += "&r1=" + utGenerateRandomNumber(1000) + "&r2=" + utGenerateRandomNumber(1000);
		if (windowType == 0)
			document.location.href = relUrl;
		else if (windowType == 1) {
			if (leftPos == 0 && topPos == 0) {
				leftPos = (screen.width-width)/2;
				topPos = (screen.height-height)/2;
			}
			utPopUp(relUrl, width, height, leftPos, topPos);
		} else if (windowType == 2)
			utOpenInIFrame(relUrl);
	} else {
		alert("You are not authorized to access \'" + title + "\' screen.");
	} /* else */
} /* function glOpen( */

function glIsAuthorized(pageIndex) {
	return (glPages[pageIndex].authorized);
} /* function glIsAuthorized( */

function glBeforePrint() {
//	if (!is_ie5_5up) return;
	if (typeof(plBeforePrint) == 'function')
		plBeforePrint();
}
function glAfterPrint() {
//	if (!is_ie5_5up) return;
	if (typeof(plAfterPrint) == 'function')
		plAfterPrint();
}
function glLoad() {
//	if (!is_ie5_5up) return;
	try {
		glInitFormSubmit();
	} catch(e) {
		glHndlError("glInitFormSubmit() failed - " + e.description, '', '');
	}

	try {
		glInitFormAutoComplete();
	} catch(e) {
		glHndlError("glInitFormAutoComplete() failed - " + e.description, '', '');
	}

	try {
		if (!plHideContextSensitiveHelp)
			glInitHelp();
	} catch(e) {
		glHndlError("glInitHelp() failed - " + e.description, '', '');
	}

	try {
		glSetPageTitle();  //NEEDS ATTENTION
	} catch(e) {
		glHndlError("glSetPageTitle() failed - " + e.description, '', '');
	}

	try {
		glCreateFrameBrowser();
	} catch(e) {
		glHndlError("glCreateFrameBrowser() failed - " + e.description, '', '');
	}

	try {
		if (typeof(plLoad) == 'function')
			plLoad();
	} catch(e) {
		glHndlError("plLoad() failed - " + e.description, '', '');
	}

	try {
		for (var i = 1; i <= 10; i++) {
			eval("if (typeof(plLoad"+i+") == 'function') plLoad"+i+"();");
		}
	} catch(e) {
		glHndlError("plLoad...() failed - " + e.description, '', '');
	}

	try {
		glError();
	} catch(e) {
		glHndlError("glError() failed - " + e.description, '', '');
	}

	try {
		if (plIsPopUp) {
			glInitPageMemory();
			glInitPageMemoryHandlers();
		}
	} catch(e) {
		glHndlError("glInitPageMemory() failed - " + e.description, '', '');
	}

	try {
		glScrollInit();
	} catch(e) {
		glHndlError("glScrollInit() failed - " + e.description, '', '');
	}

	try {
		if (typeof(glAppLoad) == 'function')
			glAppLoad();
	} catch(e) {
		glHndlError("glAppLoad() failed - " + e.description, '', '');
	}

	try {
		glInitSetFocus();
	} catch(e) {
		glHndlError("InitSetFocus() failed - " + e.description, '', '');
	}

	try {
		glInitTextAreas();
	} catch(e) {
		glHndlError("glInitTextAreas() failed - " + e.description, '', '');
	}

}

function glInitFormSubmit() {
	for(var i=0; i < document.forms.length; i++) {
		var f = document.forms[i];
		f.onsubmit = glHndlSubmit;
	}
}

function glInitFormAutoComplete() {
	for(var i=0; i < document.forms.length; i++) {
		var f = document.forms[i];
		f.autoComplete = 'off';
	}
}

var ErrorArr = new Array();
function glError() {
	try {
		for (var i=0; i < ErrorArr.length; i++) {
			var id = ErrorArr[i].id;
			var description = ErrorArr[i].description;
			var htmlTagName = ErrorArr[i].htmlTagName;
			var index = ErrorArr[i].index;
			if (htmlTagName != "") {
				var e = document.getElementById(htmlTagName);
				if (e != null) {
					if (e.xClassName == null) {
						e.xClassName = e.className;
					}
					e.className = e.xClassName + " " + "glError";
					document.getElementById("errDesc" + index).childNodes[0].nodeValue = e.title + ": ";
				}
			}
		}
	} catch(e) {
		glHndlError('Global.js:glError: Caught exception: ' + e.description, '', '');
	}
}

function glInitSetFocus() {
	if (document.location.href.indexOf('noInitFocus=1') >= 0)
		return; //bypass setfocus if no init focus requested

	//if special focus is requested, grant it
	if (typeof(plFocusOnId) == 'string') {
		if (plFocusOnIdIndexed) {
			var b = -1;
			var oF = document.getElementById(plFocusOnId + "_0");
			if (oF == null) {
				oF = document.getElementById(plFocusOnId + "_1");
				if (oF != null)
					b = 1;
			} else {
				b = 0;
			}
			if (b >= 0) {
				var nL = b;
				for (var i = b+1; i <= 26; i++) {
					oF = document.getElementById(plFocusOnId + "_" + i);
					if (oF == null)
						break;
					else
						nL = i;
				}
				oF = document.getElementById(plFocusOnId + "_" + nL);
				if (oF != null && nL < 26) oF.focus();
			}
		} else {
			var oF = document.getElementById(plFocusOnId);
			if (oF != null) oF.focus();
		}
		return;
	}

	//bypass focus if window is scrolled by glScrollInit
	//to avoid browser puship up to show the focused element
	if (utWindowGetScrollX() || utWindowGetScrollY())
		return;

	//if there are errors focus on the first field
	if (ErrorArr.length > 0) {
		for (var i=0; i < ErrorArr.length; i++) {
			var htmlTagName = ErrorArr[i].htmlTagName;
			if (htmlTagName != "") {
				var o = document.getElementById(htmlTagName);
				if (o != null) {
					if (o.type == null)
						break; //first error field must be special, break from for
					if (o.type.toUpperCase() == 'TEXT') {
						if (!o.disabled && !o.readOnly) {
							try {
								o.focus();
							} catch (e) {}
							return;
						}
					}
				}
			}
		}
	}

	var inputs = document.getElementsByTagName("input");
	for (var i = 0; i < inputs.length && i < 100; i++) { //limit loop to 100, to avoid extreme delays
		var o = inputs[i];
		if (o.type.toUpperCase() == 'TEXT') {
			if (!o.disabled && !o.readOnly) {
				try {
					o.focus();
				} catch (e) {}
				return;
			}
		}
	}
}

function glSetPageTitle() {
	if (document.getElementById('pageTitle') == null)
		return;
	if (typeof(plPageHeader) == 'string')
		document.getElementById('pageTitle').innerHTML = '&nbsp;' + plPageHeader + '&nbsp;';
//	else
//		document.getElementById('pageTitle').parentNode.style.display='none';
} /* function glSetPageTitle() */

function glHndlROCheckBox() {
	var o = event.srcElement;
	if (o._checked == null)
		o._checked = o.checked;
	o.checked = o._checked;
}

function glHndlTblMouseOver(e) {
	var o = utGetEventTarget(e, window.event);
	var eventType = utGetEventType(e, window.event);

	if (o.nodeName != 'TD')
		return;

	var tr = o.parentNode;

	if (tr._HLIgnore == null)
		return;

	if (tr._HLIgnore != "false") {
		return;
	}

	if (eventType == 'mouseout') {
		tr.className = tr._xClassName;
	} else if (eventType == 'mouseover') {
		if (tr._xClassName == null)
			tr._xClassName = tr.className;
		tr.className = 'glHighlightedRow';
	}
}
//----------------------------------------------------------------------------------
function glBuildDataTable(oT, rowClickHandler) {
	if (oT == null)
		return;
	if (arguments.length > 1) {
		utAttachEvent(oT, "mouseover", glHndlTblMouseOver);
		utAttachEvent(oT, "mouseout", glHndlTblMouseOver);
	}
	for (var i = 1; i < oT.rows.length; i++) {
		var R = oT.rows[i];
		utSetAttribute(R, "_HLIgnore", "false");
		if (arguments.length > 1) {
			utAttachEvent(R, "dblclick", rowClickHandler);
			R.style.cursor='pointer';
		}
		if (i % 2 == 0)
			R.className = 'glEvenRow';
		else
			R.className = 'glOddrow';
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//		Help functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function glInitHelp() {
	// This array stores the actual .href values of existing links.
	var oLinks = new Array();
	// This array stores the contents between the  tags
	var oLinksHTML = new Array();

	var nLinks = document.all.tags('A').length;
 
	// Store our current links in their proper arrays.
	for (var i = 0; i < nLinks; i++) {
		oLinks[i] = document.all.tags('A')[i].href;
		oLinksHTML[i] = document.all.tags('A')[i].innerHTML;
	}

	if (typeof(glHelpWordsArray) == 'undefined')
		return;

	for (var i = 0; i < glHelpWordsArray.length; i++) {
		var keyPhrase = glHelpWordsArray[i].keyPhrase;
		var docId = glHelpWordsArray[i].docId;

		//Recreate the text range every other keyword to get around bug:
		//that on larger pages, the collapse(true) didn't always reset the text range 
		//to the beginning again after processing several keywords
		if (i % 2 == 0) {
			var oRange = document.body.createTextRange();
		} else {
			oRange.collapse(true);
		}

		if (keyPhrase != null && LTrim(RTrim(keyPhrase)) != '') {
			for (var k=0; oRange.findText(keyPhrase); k++) { 
				try {
					//Stay away from input elements and <A href=
					var parentNodeName = "";
					if (oRange.parentElement() != null)
						parentNodeName = oRange.parentElement().nodeName.toUpperCase();
					if (parentNodeName != "TEXTAREA" && parentNodeName != "INPUT" && oRange.htmlText.toUpperCase().indexOf("HREF") < 0) {
						oRange.pasteHTML("<span onclick=\"glOpenHelpDocument(" + docId + ");\" class=glHelpPhrase title=\"Click to get help regarding \'" + utHtmlEncode(keyPhrase) + "\'\" >" + oRange.htmlText + "</span>"); 
					}
					oRange.collapse(false);
				} catch (e) {
					oRange.collapse(false);
				}
			}
		}

	} /* for */

//Restore links
	for (i=0; i< nLinks; i++) {
		document.all.tags('A')[i].href = oLinks[i];
		document.all.tags('A')[i].innerHTML = oLinksHTML[i];
	}




return;
	for (var i = 0; i < glHelpWordsArray.length; i++) {
		var tr = document.body.createTextRange();
		var k = 0;
		while (tr.findText(glHelpWordsArray[i].phrase) && k < 25) {
			tr.execCommand("CreateLink", false, "javascript:glOpenHelpDocument(" + glHelpWordsArray[i].docId + ");");
			tr.execCommand("ForeColor", false, "green");
			tr.execCommand("Underline", false, "");
			var n = tr.text.length;
			tr.moveStart("character", n);
			k++;
		} /* while */
	} /* for */
} /* function */

function glOpenHelpDocument(docId) {
	glOpen('HLP_HLP_01_OPN', "docId=" + docId)
} /* glOpenHelpDocument( */

function glContextMenuHelpCreate() {
	event.cancelBubble = true;
	var o = event.srcElement;
	var s = document.selection.createRange().text;
	if (o.nodeName != "INPUT" && 
		o.nodeName != "TEXTAREA" && 
		s != "") {
		var strTbl = 	"<table border=0px cellpadding=2px cellspacing=0px >" +
							"<tr>" +
								"<td align=\"left\" valign=\"middle\" nowrap class=\"menuItem\" _phrase=\"" + utHtmlEncode(s) + "\" _mnId=\"H\" onmouseover=\"utSwitchMenu();\" onmouseout=\"utSwitchMenu()\" >&nbsp;&nbsp;Assign help</td>" +
							"</tr>" +
						"</table>";
		utDisplayContextMenu(strTbl, 120, null, glOpenCreateHelp);
		return(false);
	} /* if */

	return(true);
} /* glContextMenuHelpCreate( */

function glOpenCreateHelp() {
	var o = event.srcElement;
	if (o._mnId==null) return;
	if (o._mnId == 'H') {
		glOpen('HLP_ADM_01_CRT', "keyPhrase=" + utUrlEncode(o._phrase) + "&pageName=" + utUrlEncode(plPageName));
	} /* if */
	utHideMenu();
} /* glOpenCreateHelp( */

function glHighlightText(phrase) {
	var tr = document.body.createTextRange();
	var k = 0;
	while (tr.findText(phrase) && k < 25) {
		tr.execCommand("Underline", false, "");
		var n = tr.text.length;
		tr.moveStart("character", n);
		k++;
	} /* while */
} /* function */

var formSubmitted = null;
function glHndlSubmit(e) {
	var f = utGetEventTarget(e, window.event);
	var cBut = f.getElementsByTagName('INPUT');
	for (var i=0; i < cBut.length; i++) {
		var c = cBut[i];
		if (c.type == 'submit' || c.type == 'button')
			c.style.display = 'none';
	}

	formSubmitted = f;
	self.setTimeout('glShowWait();', 1000);
} /* function */

function glShowWait() {
	document.body.style.cursor = "wait";
	document.title = "Please wait...";
	glHideAllSelectBoxes(); //so that when blanket is put over they (select boxes) do not stand out

	if (document.getElementById('divHideAll') != null) {
		document.getElementById('divHideAll').style.display = '';
		document.getElementById('divHideAll').style.height = utWindowGetHeight() + 'px';
		document.getElementById('divHideAll').style.width = utWindowGetWidth() + 'px';
	}

	if (document.getElementById("divWait") != null) {
		var w = document.getElementById("divWait");
		w.style.display = '';
		w.style.left = ((utWindowGetFrameWidth() - utWindowGetObjectWidth(w))/2 + utWindowGetScrollX()) + 'px';
		w.style.top = ((utWindowGetFrameHeight() - utWindowGetObjectHeight(w))/2 + utWindowGetScrollY()) + 'px';
	}
}

function glHideAllSelectBoxes(oParent) {
	if (oParent == null)
		oParent = document;
	var colS = oParent.getElementsByTagName('SELECT');
	for (var i = 0; i < colS.length; i++) {
		colS[i].style.display = 'none';
	}
}

function glShowAllSelectBoxes(oParent) {
	if (oParent == null)
		oParent = document;
	var colS = oParent.getElementsByTagName('SELECT');
	for (var i = 0; i < colS.length; i++) {
		colS[i].style.display = '';
	}
}

function glHidePrintButton() {
	var p = document.getElementById('btnPrint');
	if (p != null)
		p.style.display = 'none';
} /* glHidePrintButton */


function glAutoTab(n) {
	var o = event.srcElement;
	var k = event.keyCode;

	if (!((k >= 65 && k <= 90) || (k >= 48 && k <=57) || k == 32))
		return;

	m = o.maxLength;
	if (m == null)
		return;

	if (o.value.length >= m)
		n.focus();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Window memory
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function glInitPageMemory() {
	if (!plPageMemoryEnabled)
		return;
	if (typeof(plScreenTop) != 'number' ||
		typeof(plScreenLeft) != 'number' ||
		typeof(plClientHeight) != 'number' ||
		typeof(plClientWidth) != 'number' ) {
		return;
	}
	if (plClientWidth > 0) {
		//Make sure top is visible
		if (plScreenTop > (screen.height - 100))
			plScreenTop = screen.height - 100;
		if (plScreenTop < 30)
			plScreenTop = 30;
		//Make sure left is visible
		if (plScreenLeft > (screen.width - 100))
			plScreenLeft = screen.width - 100;
		if (plScreenLeft < 5)
			plScreenLeft = 5;

//		window.resizeBy(plClientWidth-document.body.clientWidth, plClientHeight-document.body.clientHeight);
//		window.moveBy(plScreenLeft-window.screenLeft, plScreenTop-window.screenTop);
		glLogPageMemory();
	}
}

function glInitPageMemoryHandlers() {
	if (!plPageMemoryEnabled)
		return;
	var tid = setInterval('glLogPageMemory()', 5000);
}

var glOldScreenX = window.screenLeft;
var glOldScreenY = window.screenTop;
var glOldWidth = 0;
var glOldHeight = 0;
function glLogPageMemory() {
	var leftPos = window.screenLeft;
	var topPos = window.screenTop;	
	var width = document.body.clientWidth;
	var height = document.body.clientHeight;

	if (	leftPos == glOldScreenX &&
			topPos == glOldScreenY &&
			width == glOldWidth &&
			height == glOldHeight
	) {
		return;
	}

	glOldScreenX = leftPos;
	glOldScreenY = topPos;
	glOldWidth = width;
	glOldHeight = height;

	//Reset values in the parent window
	if (window.opener != null && window.opener.glPages != null) {
		window.opener.glPages[plPageName].width = width;
		window.opener.glPages[plPageName].height = height;
		window.opener.glPages[plPageName].leftPos = leftPos;
		window.opener.glPages[plPageName].topPos = topPos;
	}

	//Save into database
	var query = "pageName=" + plPageName +
				"&leftPos=" + leftPos +
				"&topPos=" + topPos +
				"&width=" + width +
				"&height=" + height;

	glOpen('SYS_PAG_01_UPT', query);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//		JS Error handling
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function glHndlError(message, url, line) {
	try {
		if (plClientErrorsPrompted)
			alert("Client side error happened on the page\nAdministrative log has been created.\n\n\n [" + message + "]");
		if (plClientErrorsLogged) {
			var subject = glApplicationTitle + ' Client-Side Error';
			var description = 'Message:' + message + ' Href:' + document.location.href + ' Url:' + url + ' Line:' + line;
			glSubmitError(subject, description);
		} /* if */
	} catch(e) {
		//alert('failed - ' + e.description);
	}
	return (true); //Supress any browser alerts
}
function glSubmitError(subject, description) {
	var parameters =	"r1=" + utGenerateRandomNumber(1000) +
						"&r2=" + utGenerateRandomNumber(2000) +
						"&subject=" + utUrlEncode(subject) +
						"&description=" + utUrlEncode(description);
	glOpen('SYS_ERR_01_CRT', parameters);
}

function glRefreshParentIfRequired() {
	if (typeof(plParentRefreshOnClose) == 'boolean') {
		if (plParentRefreshOnClose) {
			if (window.opener && window.opener != null) {
				window.opener.glReset(true);
			} /* if */
		} /* if */
	} /* if */
} /* function glRefreshParent( */

function glClose()
{
	utClose();
}

function glReset(noInitFocus) {
	var cUrl = window.document.location.href;

	cUrl = utUrlAddQueryParam(cUrl, "r1", utGenerateRandomNumber(1000));
	cUrl = utUrlAddQueryParam(cUrl, "r2", utGenerateRandomNumber(2000));

	if (document.body && (document.body.scrollLeft || document.body.scrollTop)) { //Avoid non-compatible browser errors
		cUrl = utUrlAddQueryParam(cUrl, "sL", utWindowGetScrollX());
		cUrl = utUrlAddQueryParam(cUrl, "sT", utWindowGetScrollY());
	} else {
		cUrl = utUrlRemoveQueryParam(cUrl, "sL");
		cUrl = utUrlRemoveQueryParam(cUrl, "sT");
	} /* else */

	if (noInitFocus != null && noInitFocus) {
		cUrl = utUrlAddQueryParam(cUrl, "noInitFocus", "1");
	}

	if (typeof(plAddResetParams) == 'function') {
		cUrl = plAddResetParams(cUrl);
	}

	window.document.location.href = cUrl;
}

function glScrollInit() {
	var L = 0;
	var T = 0;
	if (typeof(sL) == 'number') {
		L = sL;
	} else if (typeof(sL) == 'string') {
		L = utParseInt(sL);
	}
	if (typeof(sT) == 'number') {
		T = sT;
	} else if (typeof(sT) == 'string') {
		T = utParseInt(sT);
	}
	if (L > 0 || T > 0) {
		utWindowScroll(L, T);
	}
} /* function glScrollInit */

/*	glSubmitNextButtonWithScroll:
		should be set as onlick event for the button (button only not submit) and the next
		element to this button should be the submit button which will be clicked by this function
		after SL & ST are set for auto-scrolling
*/
function glSubmitNextButtonWithScroll(e) {
	var o = utGetEventTarget(e, window.event);
	var aUrl = o.form.action;
	if (document.body && (document.body.scrollLeft || document.body.scrollTop)) { //Avoid non-compatible browser errors
		aUrl = utUrlAddQueryParam(aUrl, "sL", utWindowGetScrollX());
		aUrl = utUrlAddQueryParam(aUrl, "sT", utWindowGetScrollY());
	}
	o.form.action = aUrl;
	o.nextSibling.click();
}

function glHndlMouseOver(e) {
	var o = utGetEventTarget(e, window.event);
	o.className = "glHighlightItem";
}
function glHndlMouseOut(e) {
	var o = utGetEventTarget(e, window.event);
	o.className = "";
}
function glCreateFrameBrowser() {
	var objI = document.createElement('iframe');
	utSetAttribute(objI, "id", "glFrmBrowser");
	objI.style.display='none';
	document.body.appendChild(objI);
}

function glInitTextAreas() {
	var ts = document.getElementsByTagName("textarea");
	for (var i = 0; i < ts.length && i < 100; i++) {
		var t = ts[i];
		utAttachEvent(t, "keyup", utValidateTextArea);
	}
}