// $Header: /MASweb_MMSQL/6.22.01/web_server/source/dealer/client/mascls.js 146   10/03/07 8:15a Jwood $
// © 2007 copyright GE Security all rights reserved 
// For Legal Notice, Terms & Conditions see www.ge.com/en/ge/gl_terms.htm 

var sendlook, LookupField, LookupForm, lk_winref, HelpWinRef, ReportWinRef, scrollPos;
var DropDownMenu, DropDownEvent, PullUpMenu, PullUpEvent;
var ScreenHeight, ScreenWidth, ScreenLeft, ScreenTop, ContentLeftMargin, ContentWidth, ContentHeight, ContentBottom, ColNamePrefix;
var DateFormat="MM/dd/yyyy";
var PageAdjustCount=0;
var PageAdjustDone=false;
var LockFocus=true;
var Scrolling=false;
var RTFActive=false;
var WindowWidth, WindowHeight;
var BrowserType=new Browser();

function lookup_rtn(columnValue, returnString){		// executed in lookup window
	self.opener.LookupReturn(columnValue, returnString);
	
	window.close();
}

function LookupReturn(columnValue, returnString){	// executed from calling window
	if (returnString==null || returnString==undefined){returnString="";}
	
	while (returnString.indexOf("$#039;")!=-1){returnString=returnString.replace("$#039;", "'");}  // character substitution in MASwebutil.dll
	while (columnValue.indexOf("$#039;")!=-1){columnValue=columnValue.replace("$#039;", "'");}
	
	var columnName=LookupField;
	var formName=LookupForm;
	var returnFields=returnString.split(",");  // character substitution in MASwebutil.dll
	var xx, returnValue, returnField, scrap;
	var returnPair=new Array();

	try {eval("document."+formName+"."+columnName).value=columnValue;}
	catch (error){}
	
	try {document.getElementById(columnName).innerHTML=columnValue;}
	catch (error){}
	
	try {eval("document."+formName+".xX_"+columnName).value=columnValue;}
	catch (error){}
	
	for (xx=0; xx<returnFields.length; xx++){
		scrap=returnFields[xx];
		
		if (scrap!=null && scrap!=""){
			returnPair=scrap.split("=");
			returnField=ColNamePrefix+returnPair[0];
			returnValue=unescape(returnPair[1]);
			
			while (returnValue.indexOf("$#044;")!=-1){returnValue=returnValue.replace("$#044;", ",");}  // character substitution in MASwebutil.dll
			
			switch (returnField){
				case "colnameprefix": // assumes this is the first value in the return string
					//colNamePrefix=returnValue; obsolete??
					break;
				case "err_msg":  // used by httpRequest lookup returns
					window.alert(returnValue);

					try {eval("document."+formName+"."+columnName).value="";}
					catch (error){}
					
					break;
				default:
					try {document.getElementById(returnField).innerHTML=returnValue;} // prefered form format (Text)
					catch (error){}
					
					try {eval("document."+formName+"."+returnField).value=returnValue;} // old form format or hidden form field
					catch (error){}
					
					try {eval("document."+formName+".xX_"+returnField).value=returnValue;} // support for form submission errors
					catch (error){}
					
					break;
			}
		}
	}
	
	if (columnName.indexOf("dispage_no")>-1 || columnName.indexOf("_open_page")>-1 || columnName.indexOf("_close_page")>-1){dispPageToggle(columnName);}
	if (columnName.indexOf("gldisp_id")>-1){dispPageToggle(columnName);}
	if (columnName=="fail_to_open_event_id" || columnName=="fail_to_close_event_id"){failurePageToggle(eval("document."+formName+"."+columnName));}
	
	return;
}

function lookup_cancel(){
	var columnName=self.opener.LookupField;
	var formName=self.opener.LookupForm;
	
	eval("self.opener."+formName+"."+columnName).focus();
	
	window.close();
	
	return;
}

function checkit(arg) {
	var foo = arg;
	return;
}

function do_lookup(colName, windowURL, formName){
	try {lk_winref.close();} catch (error){};
	
	if (formName=="undefined" || formName==null || formName==""){formName="form1";}
	
	try{
		if 	(eval("document."+formName+"."+colName).getAttribute("disabled")){return;}
	}
	catch (error){}
	
	var leftPos=ScreenWidth-575-15;
	var height=300;
	var width=575;
	var parts=new Array();
	var addFields=new Array();
	var firstPart=null;
	var secondPart=null;
	var thirdPart=null;
	var qString="";
	var splitter, scrap, xx, getSeconds, openString, rootColName;
	var dateSelect=(windowURL.indexOf("dateselector.aspx")!=-1);
	var timeSelect=(windowURL.indexOf("timeselector.aspx")!=-1);
	var lookupWindowName="lookup";
	var lookupSeqno=0;
	var openerWindowRef=window.opener;
	
	while (openerWindowRef!=undefined){
		lookupSeqno++;
		openerWindowRef=openerWindowRef.opener;
	}
	
	if (lookupSeqno>0){lookupWindowName+=lookupSeqno;}
	
	ColNamePrefix="";
	
	if (dateSelect){height=400; width=350;}
	if (timeSelect){height=150; width=300;}
	
	openString="top=30,left="+leftPos+",height="+height+",width="+width+",resizable,scrollbars";
	
	try {
		xx=colName.indexOf("_");
		scrap=colName.substring(0, 3);
		
		if (scrap=="Row" && xx>4){
			rootColName=colName.substring(xx+1);
			ColNamePrefix=colName.substring(0, xx+1);
			//qString+="&colnameprefix="+ColNamePrefix;
		}
		else {rootColName=colName;}
	}
	catch (error){rootColName=colName;}
	
	try {scrap=eval(rootColName+"_Lookup");}
	catch (error){scrap="";}
	
	if (scrap!=""){
		addFields=scrap.split(",");
		
		for (xx=0; xx<addFields.length; xx++){
			qString+="&"+addFields[xx]+"="+escape(eval("document."+formName+"."+ColNamePrefix+addFields[xx]).value);
		}
	}
	
	try {scrap=eval(rootColName+"_Return");}
	catch (error){scrap="";}
	
	if (scrap!=""){qString+="&return="+scrap;}
	
	if (dateSelect){splitter="/";}
	if (timeSelect){splitter=":";}
	
	if (dateSelect || timeSelect){
		LockFocus=false;  // prevents the following focus event from screwing things up
		
		eval("document."+formName+"."+colName).focus();
		
		try {parts=eval("document."+formName+"."+colName).value.split(splitter);}
		catch (error){}
		
		if (parts.length>=1){firstPart=parts[0];}
		if (parts.length>=2){secondPart=parts[1];}
		if (parts.length>=3){thirdPart=parts[2];}
		
		if (dateSelect && parts.length==3){
			if (firstPart!=null){qString+="&part1="+firstPart;}
			if (secondPart!=null){qString+="&part2="+secondPart;}
			if (thirdPart!=null){qString+="&part3="+thirdPart;}
		}
		
		if (timeSelect && parts.length>=2){
			try {getSeconds=eval("document."+formName+"."+colName).getAttribute("seconds");}
			catch (error){}
			
			if (firstPart!=null){qString+="&hour="+firstPart;}
			if (secondPart!=null){qString+="&minute="+secondPart;}
			
			if (getSeconds){
				if (thirdPart==null){thirdPart="00";}
				
				qString+="&second="+thirdPart;
			}
		}
	}
	
	if (qString!=""){
		if (windowURL.indexOf("?")==-1){windowURL+="?"+qString.substring(1);}
		else {windowURL+=qString;}
	}
	
	LookupField=colName;
	LookupForm=formName;
	lk_winref=window.open(windowURL, lookupWindowName, openString);
	
	if (LockFocus){lk_winref.focus();}
	
	return;
}

function AdvancedSearch(){
	window.open("search.aspx", "popup", "top=150,left=350,height=160,width=500,status,resizable,scrollbars");
}

function clear_lookup()	// Deprecated - Use CloseLookup() instead
{
	CloseLookup();
}

function lk_foc(ctl) {
	if (LockFocus){
		if (LookupField!=""){ 
			clear_lookup(); 
			LookupField="";	
		}
	}
	else {
		try {lk_winref.focus();}
		catch (error){}
	}
	
	LockFocus=true;
	
	return;
}

function CheckAutoApply(){
	if 	(autoApply=="Y")
	{
		window.location.href="custadjustselect.aspx?recordmode=add";
	}
	else
	{
		OpenPopupWindow("autoapply.aspx", 575, 250);
	}
}

function IMouseOver(imgObject, option){
	var styleClass, srcFile, dotPos, srcExt, newSrc;
	
	if (option=="style"){
		styleClass=imgObject.className;
		imgObject.className=styleClass+"Hover";
	}
	else if (option=="image"){
		srcFile=imgObject.src;
		dotPos=srcFile.lastIndexOf(".");
	
		if (dotPos>-1){
			srcExt=srcFile.substring(dotPos);
			newSrc=srcFile.replace(srcExt, "_hover"+srcExt);
			imgObject.src=newSrc;
		}
	}
}

function IMouseOut(imgObject, option){
	var styleClass, srcFile;
	
	if (option=="style"){
		styleClass=imgObject.className;
		imgObject.className=styleClass.replace("Hover", "");
	}
	else if (option=="image"){
		srcFile=imgObject.src;
		imgObject.src=srcFile.replace("_hover", "");
	}
}

function SetCookie(colValue, colName){
	if (colName!="" && colName!=null){
		document.cookie=colName+"="+escape(colValue);
	}
}

function GetHelp(helpPage){
	try {HelpWinRef.close();} catch(error){};
	
	var xPos=document.URL.indexOf("/report/");
	var destination="help.aspx?helppage="+helpPage;
	var windowWidth=575;
	var left=window.screen.availWidth-windowWidth-15;
	var height=Math.max(600, window.screen.availHeight/2);
	
	if (xPos>-1){destination="../"+destination;}
	
	HelpWinRef=window.open(destination, "help", "top=0,left="+left+",height="+height+",width="+windowWidth+",resizable,scrollbars");
	HelpWinRef.focus();
}

function PageSizeAdjust(){
	var contentRef=document.getElementById("Content");
	var contentSRef=document.getElementById("ContentSpace");
	var headerRef=document.getElementById("PageHeader");
	var headerSRef=document.getElementById("HeaderSpace");
	var footerRef=document.getElementById("PageFooter");
	var footerSRef=document.getElementById("FooterSpace");
	var xx, yy, mName, hName, xIncr, xNumber, yPos;
	
	if (WindowWidth!=undefined && WindowHeight!=undefined){
		window.resizeTo(WindowWidth, WindowHeight);
		WindowWidth=xx; // set to undefined value
		WindowHeight=xx; // set to undefined value
	}

	ScreenHeight=window.screen.availHeight;
	ScreenWidth=window.screen.availWidth;
	ScreenLeft=window.screenLeft ? (window.screenLeft-ScreenWidth>0 ? window.screenLeft : 0) : window.screenX;
	ScreenTop=window.screenTop ? (window.screenTop>0 ? window.screenTop : 0) : window.screenY;
	
	if (contentRef.clientWidth && contentRef.offsetWidth){ // New Version
		ContentHeight=contentRef.clientHeight;
		ContentBottom=contentRef.offsetHeight-ContentHeight;
		ContentWidth=DocContentWidth();
		ContentLeftMargin=0;
	}
	else { // Old Version  (provided for downward compatibility to older browsers)
		ContentHeight=contentRef.offsetHeight;
		ContentBottom=-1;
		ContentWidth=contentSRef.width;
		ContentLeftMargin=contentSRef.offsetLeft;
	}

	headerRef.style.width=ContentWidth+"px";
	headerRef.style.left=ContentLeftMargin+"px";
	headerSRef.style.width=ContentWidth+"px";
	headerSRef.style.left=ContentLeftMargin+"px";
	footerRef.style.width=ContentWidth+"px";
	footerRef.style.left=ContentLeftMargin+"px";
	footerRef.style.bottom=ContentBottom+"px";
	footerSRef.style.width=ContentWidth+"px";
	footerSRef.style.left=ContentLeftMargin+"px";

	headerSRef.innerHTML=headerRef.innerHTML;
	footerSRef.innerHTML=footerRef.innerHTML;

	if (BrowserType.isFF){
		headerSRef.className+=" HeaderSpace2";
		contentRef.style.overflow="auto";
	}

	scrollPos=contentRef.scrollTop;

	FormScroll();

	if (DocContentWidth()!=ContentWidth){PageSizeAdjust();}  // happens on some pages after headerSRef and footerSRef get changed
}

function DocContentWidth()
{
	var contentRef=document.getElementById("Content");

	return BrowserType.isIE ? contentRef.clientWidth : window.innerWidth;
}

function FormScroll(){
	var formItem, currentPos, xx, formxx, thisForm, fieldType, fieldVisibility, topPos, bottomPos, topCheck, bottomCheck;
	var element, prevElement, elementPos, testInfo;
	var includePos=true;
	
	if (document.forms.length>0){
		currentPos=document.getElementById("Content").scrollTop;

		if (currentPos==scrollPos && document.forms.length>0){ // scrolling has stopped
			topCheck=document.getElementById("PageHeader").offsetTop+document.getElementById("PageHeader").offsetHeight;
			bottomCheck=document.getElementById("PageFooter").offsetTop;
			Scrolling=false;

			for (formxx=0; formxx<document.forms.length; formxx++){
				thisForm=document.forms[formxx];
				
				for (xx=0; xx<thisForm.length; xx++){
					fieldType=thisForm[xx].type;
					
					if (fieldType.indexOf("select")==0){
						topPos=-currentPos;
						element=thisForm[xx];
						prevElement="";
						
						//if (element.name=="alarm_type"){testInfo="Start:"+topPos+"<br>";}
						
						while(element.id!="Content"){	// calculate the element's top-edge (y-coordinate) in the window
							includePos=true;
							elementPos=element.offsetTop;

							//if (element.tagName=="FORM" && prevElement=="TABLE"){includePos=false;}
							//if (element.tagName=="FORM" && prevElement=="DIV"){includePos=false;}
							if (element.tagName=="FORM"){includePos=false;}
							if (element.tagName=="P"){includePos=false;}
							if (element.tagName=="TBODY"){includePos=false;}
							if (element.tagName=="TR"){includePos=false;}
							if (element.tagName=="DIV" && element.currentStyle.styleFloat=="none"){includePos=false;}
							
							if (includePos){topPos=topPos+elementPos;}
							
							//if (thisForm[xx].name=="alarm_type"){testInfo+=element.tagName+": "+elementPos+" ("+includePos+", "+element.currentStyle.styleFloat+")<br>";}
							
							prevElement=element.tagName;
							element=element.offsetParent;
						}
						/*
						if (thisForm[xx].name=="alarm_type"){
							testInfo+="topPos: "+topPos+"<br>topCheck: "+topCheck;
							document.getElementById("testReport").innerHTML=testInfo;
						}
						*/
						bottomPos=topPos+thisForm[xx].offsetHeight-2;
						
						if (topPos>topCheck && (bottomPos<bottomCheck || bottomCheck==0)){
							thisForm[xx].style.visibility="visible";
						}
						else {
							fieldType=thisForm[xx].type;
							fieldVisibility=thisForm[xx].style.visibility;
							
							if (fieldType.indexOf("select")==0 && fieldVisibility!="hidden"){
								thisForm[xx].style.visibility="hidden";
							}
						}
					}
				}
			}
		}
		else {
			if (!Scrolling){
				for (formxx=0; formxx<document.forms.length; formxx++){
					thisForm=document.forms[formxx];
					
					for (xx=0; xx<thisForm.length; xx++){
						fieldType=thisForm[xx].type;
						
						if (fieldType.indexOf("select")==0){
							thisForm[xx].style.visibility="hidden";
						}
					}
				}
			}
			
			Scrolling=true;
			setTimeout("FormScroll()", 500);
		}
	}
	
	scrollPos=document.getElementById("Content").scrollTop;
}

function PreSubmit(thisForm){	// disabled fields are not posted back to the server.
	var xx, disabledValue;		// use this function as an onSubmit event handler to remove the disabled attribute
	var eventType=null;			// from the form fields before submitting.
	var proceed=false;			// If this is called outside of an onSubmit event it will do nothing (important
								// for form security).
	try {eventType=event.type;} // Important: This function will only be called if a Submit button is clicked.
	catch (error){}				// If you submit the form using form1.submit() then call FormSubmit() instead.
	
	if (eventType="submit"){
		proceed=true;
		
		for (xx=0; xx<thisForm.length; xx++){
			disabledValue=thisForm[xx].getAttribute("disabled");
			
			if (disabledValue){thisForm[xx].setAttribute("disabled", !disabledValue);}
		}
	}
	
	return proceed;
}

function FormSubmit(){			// This function replaces calls to "form1.submit()".  It is needed whenever a form contains
	var xx, disabledValue;		// disabled fields and is not submitted using a submit button.
	
	for (xx=0; xx<document.form1.length; xx++){
		disabledValue=document.form1[xx].getAttribute("disabled");
		
		if(document.form1[xx].className.indexOf("MLFDeleted")==-1){
			if (disabledValue){document.form1[xx].setAttribute("disabled", !disabledValue);}
		}
	}
	
	document.form1.submit();
	
	return true;
}

function OpenReportWindow()
{
	var outputType;
	
	try {outputType=document.form1.output_type.value;}
	catch (Error){outputType="pdf";}
	
	if (outputType=="xls")
	{
		FormSubmit();
	}
	else
	{
		if (ReportWinRef!=undefined && ReportWinRef!=null)
		{
			try {ReportWinRef.close();}
			catch (error){}
		}

		ReportWinRef=window.open("../checkwin.htm", "ReportViewerWindow", "status,resizable");
		//ReportWinRef=window.open("", "ReportViewerWindow", "status,resizable");

		if (ReportWinRef.document.URL.indexOf("getreport.aspx")!=-1) // || ReportWinRef.document.URL=="about:blank")
		{  // about:blank is the URL for a previous report in IE7 (a bug?)
			ReportWinRef.close();
			ReportWinRef=window.open("", "ReportViewerWindow", "status,resizable");
		}
		else {
			ReportWinRef=window.open("about:blank", "ReportViewerWindow", "status,resizable", true);
		}

		// In IE7 open window again to prevent automation error (a bug)
		if (BrowserType.isIE && BrowserType.version>=7){ReportWinRef=window.open("../checkwin.htm", "ReportViewerWindow", "status,resizable");}

		document.form1.target="ReportViewerWindow";
		FormSubmit();
		document.form1.target="";
		ReportWinRef.focus();
	}
}

function Trim(textToTrim){
	return textToTrim.replace(/^\s+/, "").replace(/\s+$/, "");
}

function ToPascalCase(transText){
	transText=Trim(transText).replace(/[\s_]+([a-z])/g, function($1){return $1.toUpperCase();});
	transText=transText.replace(/[\s_]/g, "");
	
	return transText.replace(/^([a-z])/, function($1){return $1.toUpperCase();});
}

function ToCamelCase(transText){
	transText=Trim(transText).replace(/[\s_]+([a-z])/g, function($1){return $1.toUpperCase();});
	transText=transText.replace(/[\s_]/g, "");
	
	return transText.replace(/^([A-Z])/, function($1){return $1.toLowerCase();});
}

function ToTitleCase(transText){
	transText=Trim(transText).replace(/([A-Z][a-z])/g, " $1").replace(/[\s_]+/g, " ");
	transText=transText.replace(/( [a-z])/g, function($1){return $1.toUpperCase();});
	
	return transText.replace(/^([a-z])/, function($1){return $1.toUpperCase();});
}

function ViewDispPage(fieldName, pageID){
	var windowURL;
	
	if (fieldName.indexOf("gldisp_id")>-1){
		windowURL="globaldispview.aspx?gldisp_id="+pageID;
	}
	else {
		windowURL="sitedispview.aspx?dispage_no="+pageID;
	}
	
	LockFocus=false;
	
	OpenPopupWindow(windowURL, 575, 400);
	
	return;
}

function ViewOnlyLookup(pageName, width, height){
	if (pageName.indexOf(".aspx")==-1){
		pageName+=".aspx";
	}
	
	LockFocus=false;
	
	OpenPopupWindow(pageName, width, height);
	
	return;
}

function CheckNumber(inputField){
	var inputValue=inputField.value;
	var fCall;
	
	if (inputValue!="" && isNaN(inputValue)){
		alert("Invalid Entry - Only numeric values are allowed for this field.");
		inputField.value="";
		
		if (inputField.type.toLowerCase()!="hidden"){
			fCall="form1."+inputField.name+".focus()";
			setTimeout(fCall, 1);
		}
	}
}

function SetHiddenValue(formField){
	var hiddenFields=formField.getAttribute("hField").split(",");
	var hiddenValues=formField.getAttribute("hValue").split(",");
	var hiddenValue, xx;
	
	for (xx=0; xx<hiddenFields.length; xx++){
		if (hiddenValues[xx]=="this"){hiddenValue=formField.value;}
		else {hiddenValue=hiddenValues[xx];}
		
		try {eval("document.form1."+hiddenFields[xx]).value=hiddenValue;}
		catch (error){}
	}
}

function SetFieldAccess(formField){  // only valid for checkboxes and radio buttons
	var accessFields=formField.getAttribute("aField").split(",");
	var fieldType=formField.type.toLowerCase();
	var enable=false;
	var xx;
	
	if (formField.tagName=="INPUT"){
		if (fieldType=="checkbox"){
			enable=(formField.getAttribute("aEnable").toLowerCase()=="true");
			
			if (!formField.checked){enable=!enable;}
		}
		else if (fieldType=="radio"){
			for (xx=0; xx<formField.length; xx++){
				if (formField[xx].checked){
					enable=(formField[xx].getAttribute("aEnable").toLowerCase()=="true");
					break;
				}
			}
		}
		else {return;}
	}
	else {return;}
	
	for (xx=0; xx<accessFields.length; xx++){
		if (enable){
			try {eval("document.form1."+accessFields[xx]).removeAttribute("disabled");} catch (error){}
			try {eval("document.form1."+accessFields[xx]).removeAttribute("readOnly");} catch (error){}
			try {eval("document.form1."+accessFields[xx]).className="";} catch (error){}
			
			if (xx==0){
				try {eval("document.form1."+accessFields[xx]).focus;} catch (error){}
			}
		}
		else {
			try {eval("document.form1."+accessFields[xx]).value="";} catch (error){}
			try {eval("document.form1."+accessFields[xx]).setAttribute("disabled", true);} catch (error){}
			try {eval("document.form1."+accessFields[xx]).setAttribute("readOnly", true);} catch (error){}
			try {eval("document.form1."+accessFields[xx]).className="ReadOnly";} catch (error){}
		}
	}
}

function TranslatePage(aHREF){
	if (!aHREF){aHREF="";}

	LockFocus=true;
	
	OpenPopupWindow(aHREF+"translate.aspx", 800, 600);
	
	return;
}

function OpenPopupWindow(pageName, width, height, popupName, center){
	if (width==null || width==undefined){width=575;}
	if (height==null || height==undefined){height=400;}
	if (center==null || center==undefined || center!=true){center=false;}
	
	ScreenLeft=window.screenLeft-ScreenWidth>0 ? ScreenWidth : 0;
	
	var leftPos, topPos;
	var openerWindowRef=window.opener;
	var lookupSeqno=0;
	
	if (popupName==undefined || popupName==null){popupName="lookup";}
	
	if (center)
	{
		leftPos=ScreenLeft+ContentWidth/2-width/2;
		topPos=ScreenTop+ContentHeight/2-height/2;
		
		if (topPos>40){topPos-=40;}
	}
	else
	{
		leftPos=ScreenLeft+ScreenWidth-width-20;
		topPos=10;
	}

	openString="top="+topPos+",left="+leftPos+",height="+height+",width="+width+",resizable,scrollbars,status";
	
	while (openerWindowRef!=undefined){
		lookupSeqno++;
		openerWindowRef=openerWindowRef.opener;
	}
	
	if (lookupSeqno>0){popupName+=lookupSeqno;}

	if (lk_winref!=undefined){
		try {lk_winref.close();} catch (error){};
	}
	
	lk_winref=window.open(pageName, popupName, openString);

	if (LockFocus){lk_winref.focus();}
	
	return;
}

function ParseDate(inString){
	var outDate=null;
	var month=null;
	var day=null;
	var year=null;
	var validDate=false;
	var dateParts, scrap;
	var formatParts=DateFormat.split("/");
	
	if (inString.indexOf("/")==-1 && (inString.length==8 || inString.length==6) && !isNaN(inString)){
		month=Number(inString.substring(0, 1));
		day=Number(inString.substring(2, 3));
		year=Number(inString.substring(4, inString.length-4));
	}
	else if (inString.indexOf("/")>-1){
		dateParts=inString.split("/");
		
		try {
			month=Number(dateParts[0]);
			day=Number(dateParts[1]);
			year=Number(dateParts[2]);
		}
		catch (error){}
	}
	
	if (month!=null && day!=null && year!=null){
		if (year<100){
			if (year>70){year+=1900;}
			else {year+=2000;}
		}
		
		if (formatParts[0]=="dd"){
			scrap=month;
			month=day;
			day=scrap;
		}
		
		outDate=new Date(year, month-1, day);
	}
	
	return outDate;
}

function CheckAllRange(startField, endField, allField, selectNFocus){
	var startRef=eval("document.form1."+startField);
	var endRef=eval("document.form1."+endField);
	var allRef=eval("document.form1."+allField);
	var startValue=startRef.value;
	var endValue=endRef.value;
	
	if (selectNFocus==undefined){selectNFocus=false;}

	if (startValue=="" && endValue=="" && !startRef.getAttribute("disabled")){
		if (allRef.checked){allRef.click();}
		
		allRef.click();
		
		if (selectNFocus){
			allRef.select();
			allRef.focus();
		}
	}
	else if (selectNFocus){
		if (allRef.checked){allRef.click();}
		
		startRef.select();
		startRef.focus();
	}
}

function EnableFormField(fieldName, formName){
	var fieldRef, disabled, readOnly, className;
	
	if (formName==undefined){formName="form1";}
	
	try {
		fieldRef=eval("document."+formName+"."+fieldName);
		
		if (fieldRef==undefined){return;}
	}
	catch (Exception){return;}
	
	disabled=fieldRef.getAttribute("disabled");
	readOnly=fieldRef.getAttribute("readOnly");
	className=fieldRef.className;
	
	if (disabled){fieldRef.removeAttribute("disabled");}
	if (readOnly){fieldRef.removeAttribute("readOnly");}

	if (className.indexOf("ReadOnly")>-1){
		className=className.replace("ReadOnly", "").replace("  ", " ");

		if (className.substring(className.length-1)==" "){className=className.substring(0, className.length-1);}
		if (className.substring(0, 1)==" "){className=className.substring(1);}
		
		fieldRef.className=className;
	}
	
	return;
}

function DisableFormField(fieldName, formName){
	var fieldRef, disabled, readOnly, className;
	
	if (formName==undefined){formName="form1";}
	
	try {
		fieldRef=eval("document."+formName+"."+fieldName);
		
		if (fieldRef==undefined){return;}
	}
	catch (Exception){return;}
	
	disabled=fieldRef.getAttribute("disabled");
	readOnly=fieldRef.getAttribute("readOnly");
	className=fieldRef.className;
	
	if (!disabled){fieldRef.setAttribute("disabled", true);}
	if (readOnly){fieldRef.setAttribute("readOnly", true);}

	if (className.indexOf("ReadOnly")==-1){
		className=(className=="" || className==undefined) ? "ReadOnly" : className+" ReadOnly";
		
		fieldRef.className=className;
	}
	
	return;
}

function CreateHttpRequest(method, serverPage, aSync, contentLength){
	var httpRequest=null;
	
	if (window.ActiveXObject){	// IE
		httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest){	// Not IE
		httpRequest=new XMLHttpRequest();
		httpRequest.overrideMimeType('text/xml');
	}
	
	if (aSync==undefined || (aSync!=true && aSync!=false)){aSync=true;}
	if (!contentLength){contentLength=0;}

	if (httpRequest){
		httpRequest.open(method, serverPage, aSync);
		httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-Length", contentLength);
		
		return httpRequest;
	}
}

function GetOthers(event){
	var fieldRef, fieldName, fieldValue, fieldType, formName, lookupField;
	var returnList, httpRequest, queryString, xx, scrap, rootColName, rootPath;
	var wEvent=event==undefined ? window.event : event;  // in IE the value will be undefined.
	
	if (!wEvent){return;}

	fieldRef=wEvent.srcElement;

	if (fieldRef==undefined){return;}

	fieldName=fieldRef.name;
	formName=fieldRef.form.name;
	LookupField=fieldName;
	LookupForm=formName;
	ColNamePrefix="";
	
	if (fieldRef.tagName=="INPUT"){fieldType=fieldRef.type.toLowerCase();}
	else {fieldType="";}
	
	try
	{
		switch (fieldRef.type.toLowerCase())
		{
			case "radio":
				fieldValue=GetRadioValue(fieldRef);
				break;
			case "checkbox":
				fieldValue=GetCheckboxValue(fieldRef);
				break;
			default:
				fieldValue=fieldRef.value;
				break;
		}
	}
	catch (error){fieldValue="";}

	try {
		xx=fieldName.indexOf("_");
		scrap=fieldName.substring(0, 3);

		if (scrap=="Row" && xx>4){
			rootColName=fieldName.substring(xx+1);
			ColNamePrefix=fieldName.substring(0, xx+1);
		}
		else {rootColName=fieldName;}
	}
	catch (error){rootColName=fieldName;}

	try {returnList=eval(rootColName+"_Return");}
	catch (error){return;}

	if (returnList=="" || returnList==null){return;}

	scrap=BuildLookupQueryString(rootColName, ColNamePrefix, formName);
	queryString="?field="+fieldName+"&value="+escape(fieldValue)+(scrap=="" ? "" : "&"+scrap)+"&return="+returnList;

	if (document.URL.indexOf("report/")==-1){rootPath="";}
	else {rootPath="../";}

	httpRequest=CreateHttpRequest("POST", rootPath+"dynamicvalue.aspx"+queryString, true);

	httpRequest.onreadystatechange=function(){
		if (httpRequest.readyState==4){
			LookupReturn(fieldValue, httpRequest.responseText);
		}
	}

	httpRequest.send("");

	return true;
}
	
function CheckZipCode(doAfter, zipRef){
	var zipQS, queryString, httpRequest, rootPath
	var returnText, returnPair, returnFields, errorMsg, xx, scrap, fieldName, fieldValue, errMsg;
	var errored=false;
	
	if (zipRef){zipQS="?zipname="+zipRef.name+"&zip="+escape(zipRef.value);}
	else {return true;}
	
	if (document.URL.indexOf("report/")==-1){rootPath="";}
	else {rootPath="../";}
	
	queryString=zipQS;
	httpRequest=CreateHttpRequest("POST", rootPath+"checkzipcode.aspx"+queryString, false);
	
	httpRequest.send("");
	
	returnText=httpRequest.responseText;
	
	while (returnText.indexOf("$#039;")!=-1){returnText=returnText.replace("$#039;", "'");}
	
	returnFields=returnText.split(',');

	for (xx=0; xx<returnFields.length; xx++){
		scrap=returnFields[xx];
		
		if (scrap!=null && scrap!=""){
			returnPair=scrap.split('=');
			fieldName=returnPair[0];
			fieldValue=returnPair[1];
			
			if (fieldName=="error"){
				errMsg=unescape(fieldValue);
				errored=true;
				errorRef=document.getElementById("ErrorMsg");

				if (!errorRef)
				{
					errorRef=document.createElement("p");
					errorRef.id="ErrorMsg";
					scrap=startTimeRef.form;
					
					scrap.offsetParent.insertBefore(errorRef, scrap);
				}

				errorRef.innerHTML=errMsg;
				scrap=errorRef.className;
				
				if (scrap && scrap.indexOf("NoDisplay")>-1){scrap=scrap.replace(" NoDisplay", "");}
				else {scrap="ErrorMsg";}
				
				errorRef.className=scrap;
			}
			else if (fieldName!=""){
				zipRef.form[fieldName].value=unescape(fieldValue);
			}
		}
	}
	
	if (!errored){
		try {
			document.getElementById("ErrorMsg").innerHTML="";
			scrap=document.getElementById("ErrorMsg").className;
			
			if (scrap && scrap.indexOf("NoDisplay")==-1){scrap+=" NoDisplay";}
			else {scrap="ErrorMsg NoDisplay";}
			
			document.getElementById("ErrorMsg").className=scrap;
		}
		catch (error){}
		
		if (doAfter){
			while(doAfter.indexOf("$#039;")!=-1){doAfter=doAfter.replace("$#039;", "'");}
			
			eval(unescape(doAfter));
		}
	}
	
	return !errored;
}

function CheckDateRange(doAfter, startDateRef, endDateRef, startTimeRef, endTimeRef){
	var startDateQS, endDateQS, startTimeQS, endTimeQS, queryString, httpRequest, rootPath;
	var returnText, returnPair, returnFields, errorMsg, xx, scrap, fieldName, fieldValue, errMsg;
	var errored=false;
	
	if (startDateRef){startDateQS="?sdatename="+startDateRef.name+"&sdate="+escape(startDateRef.value);}
	else {return true;}
	
	if (startTimeRef){startTimeQS="&stimename="+startTimeRef.name+"&stime="+escape(startTimeRef.value);}
	if (endDateRef){endDateQS="&edatename="+endDateRef.name+"&edate="+escape(endDateRef.value);}
	if (endTimeRef){endTimeQS="&etimename="+endTimeRef.name+"&etime="+escape(endTimeRef.value);}

	if (document.URL.indexOf("report/")==-1){rootPath="";}
	else {rootPath="../";}

	queryString=startDateQS+(startTimeQS ? startTimeQS : "")+(endDateQS ? endDateQS : "")+(endTimeQS ? endTimeQS : "");
	httpRequest=CreateHttpRequest("POST", rootPath+"checkdaterange.aspx"+queryString, false);
	
	httpRequest.send("");
	
	returnText=httpRequest.responseText;
	
	while (returnText.indexOf("$#039;")!=-1){returnText=returnText.replace("$#039;", "'");}
	
	returnFields=returnText.split(',');

	for (xx=0; xx<returnFields.length; xx++){
		scrap=returnFields[xx];
		
		if (scrap!=null && scrap!=""){
			returnPair=scrap.split('=');
			fieldName=returnPair[0];
			fieldValue=returnPair[1];
			
			if (fieldName=="error"){
				errMsg=unescape(fieldValue);
				errored=true;
				errorRef=document.getElementById("ErrorMsg");

				if (!errorRef)
				{
					errorRef=document.createElement("p");
					errorRef.id="ErrorMsg";
					scrap=startTimeRef.form;
					
					scrap.offsetParent.insertBefore(errorRef, scrap);
				}

				errorRef.innerHTML=errMsg;
				scrap=errorRef.className;
				
				if (scrap && scrap.indexOf("NoDisplay")>-1){scrap=scrap.replace(" NoDisplay", "");}
				else {scrap="ErrorMsg";}
				
				errorRef.className=scrap;
			}
			else if (fieldName!=""){
				startDateRef.form[fieldName].value=unescape(fieldValue);
			}
		}
	}
	
	if (!errored){
		try {
			document.getElementById("ErrorMsg").innerHTML="";
			scrap=document.getElementById("ErrorMsg").className;
			
			if (scrap && scrap.indexOf("NoDisplay")==-1){scrap+=" NoDisplay";}
			else {scrap="ErrorMsg NoDisplay";}
			
			document.getElementById("ErrorMsg").className=scrap;
		}
		catch (error){}
		
		if (doAfter){
			while(doAfter.indexOf("$#039;")!=-1){doAfter=doAfter.replace("$#039;", "'");}
			
			eval(unescape(doAfter));
		}
	}
	
	return !errored;
}

function CheckTimeRange(doAfter, startTimeRef, endTimeRef){
	var startTimeQS, endTimeQS, queryString, httpRequest, rootPath, errorRef;
	var returnText, returnPair, returnFields, errorMsg, xx, scrap, fieldName, fieldValue, errMsg;
	var errored=false;
	
	if (startTimeRef){startTimeQS="?stimename="+startTimeRef.name+"&stime="+escape(startTimeRef.value);}
	else {return true;}

	if (endTimeRef){endTimeQS="&etimename="+endTimeRef.name+"&etime="+escape(endTimeRef.value);}

	if (document.URL.indexOf("report/")==-1){rootPath="";}
	else {rootPath="../";}

	queryString=startTimeQS+(endTimeQS ? endTimeQS : "");
	httpRequest=CreateHttpRequest("POST", rootPath+"checktimerange.aspx"+queryString, false);
	
	httpRequest.send("");
	
	returnText=httpRequest.responseText;
	
	while (returnText.indexOf("$#039;")!=-1){returnText=returnText.replace("$#039;", "'");}
	
	returnFields=returnText.split(',');

	for (xx=0; xx<returnFields.length; xx++){
		scrap=returnFields[xx];
		
		if (scrap!=null && scrap!=""){
			returnPair=scrap.split('=');
			fieldName=returnPair[0];
			fieldValue=returnPair[1];
			
			if (fieldName=="error"){
				errMsg=unescape(fieldValue);
				errored=true;
				errorRef=document.getElementById("ErrorMsg");

				if (!errorRef)
				{
					errorRef=document.createElement("p");
					errorRef.id="ErrorMsg";
					scrap=startTimeRef.form;
					
					scrap.offsetParent.insertBefore(errorRef, scrap);
				}

				errorRef.innerHTML=errMsg;
				scrap=errorRef.className;
				
				if (scrap && scrap.indexOf("NoDisplay")>-1){scrap=scrap.replace(" NoDisplay", "");}
				else {scrap="ErrorMsg";}
				
				errorRef.className=scrap;
			}
			else if (fieldName!=""){
				startTimeRef.form[fieldName].value=unescape(fieldValue);
			}
		}
	}
	
	if (!errored){
		try {
			document.getElementById("ErrorMsg").innerHTML="";
			scrap=document.getElementById("ErrorMsg").className;
			
			if (scrap && scrap.indexOf("NoDisplay")==-1){scrap+=" NoDisplay";}
			else {scrap="ErrorMsg NoDisplay";}
			
			document.getElementById("ErrorMsg").className=scrap;
		}
		catch (error){}
		
		if (doAfter){eval(unescape(doAfter));}
	}
	
	return !errored;
}

function InitRTFEditor(){
	var editorRef=document.getElementById("RTFEditor");
	var editDocRef=editorRef.contentWindow.document;
	
	editDocRef.onkeypress=function(){UpdateRTF();}
	editDocRef.onkeyup=function(){UpdateRTF();}
	editDocRef.onmouseup=function(){UpdateRTF();}
	editDocRef.body.onblur=function(){UpdateRTF();}
	
	// Delay because these events fire before they occur
	editDocRef.body.ondrop=function(){setTimeout("UpdateRTF()", 100);}
	editDocRef.body.oncut=function(){setTimeout("UpdateRTF()", 100);}
	editDocRef.body.onpaste=function(){setTimeout("UpdateRTF()", 100);}
	
	RTFSetSelectRange();
}

function SetRTFAction(controlRef){
	var controlName=controlRef.name;
	var command=controlName.substring(3);
	var editorRef=document.getElementById("RTFEditor");
	var editDocRef=editorRef.contentWindow.document;
	var controlIndex=controlRef.selectedIndex;
	var scrap, xPos, yPos, screenWidth, screenHeight, dWidth, dHeight;
	
	RTFSetSelectRange();
	
	if (controlIndex!=null){  // <select ...> controls
		scrap=controlRef[controlIndex].value;
		
		editDocRef.execCommand(command, false, scrap);
	}
	else if (command=="ForeColor" || command=="BackColor"){
		scrap=DecimalToRGB(editDocRef.queryCommandValue(command));
		xPos=event.screenX;
		yPos=event.screenY;
		dWidth=180;
		dHeight=186;
		
		if (xPos+dWidth>ScreenWidth){xPos=ScreenWidth-dWidth-10;}
		if (yPos+dHeight>ScreenHeight-30){yPos=ScreenHeight-dHeight-40;}
		
		scrap=showModalDialog("colorselector.aspx", scrap, "dialogWidth:"+dWidth+"px; dialogHeight:"+dHeight+"px; dialogLeft:"+xPos+"; dialogTop:"+yPos+"; resizable:yes; help:no; status:no; scroll:no");
		
		if (scrap!=null){editDocRef.execCommand(command, false, scrap);}
	}
	else {
		// undo superscript and subscript when in conflict
		if ((command=="Superscript" || command=="Subscript") && editDocRef.queryCommandState(command)){editDocRef.execCommand(command);}
		
		editDocRef.execCommand(command);
	}
	
	UpdateRTF();
}

function UpdateRTF(){
	if (RTFActive){
		setTimeout("UpdateRTF()", 333);
		return;
	}

	RTFActive=true;
	
	var editorRef=document.getElementById("RTFEditor");
	var editDocRef=editorRef.contentWindow.document;
	var editEventRef=editorRef.contentWindow.event;
	var isActiveCmd, buttonRef, className, fontName, fontSize, fonts, found, fontRef, sizeRef, xx, yy, scrap;
	var commands=new Array("BackColor", "Bold", "ForeColor", "Indent", "InsertHorizontalRule", "InsertOrderedList", "InsertUnorderedList", 
		"Italic", "JustifyCenter", "JustifyLeft", "JustifyRight", "Outdent", "StrikeThrough", "Underline");
	
	document.form1.RTFText.value=editDocRef.body.innerHTML;
	
	for (xx=0; xx<commands.length; xx++){
		try {
			buttonRef=document.form1["RTF"+commands[xx]];
			
			if (buttonRef==null || buttonRef==undefined){continue;}
		}
		catch (error){continue;}
		
		isActiveCmd=editDocRef.queryCommandState(commands[xx]);
		className=buttonRef.className;
		
		if (isActiveCmd){
			if (className==""){className="ButtonDown";}
			else if (className.indexOf("ButtonDown")==-1){className=className.replace("Button", "ButtonDown");}
			
			if (className.indexOf("ButtonDown")==-1){className="ButtonDown "+className;}
		}
		else {
			if (className==""){className="Button";}
			else if (className.indexOf("ButtonDown")!=-1){className=className.replace("ButtonDown", "Button");}
			
			if (className.indexOf("Button")==-1){className="Button "+className;}
		}
		
		buttonRef.className=className;
	}
	
	fontName=editDocRef.queryCommandValue("FontName");
	fontSize=editDocRef.queryCommandValue("FontSize");
	found=false;
	
	if (fontName!=null){fontName=fontName.toLowerCase();}
	
	try {
		fontRef=document.form1["RTFFontName"];
		
		if (fontRef){
			if (fontName==null){fontRef.value=fontName;}
			else {
				for (xx=0; xx<fontRef.length; xx++){
					scrap=fontRef[xx].value.toLowerCase();
					fonts=scrap.split(/\s*,\s*/);
					
					for (yy=0; yy<fonts.length; yy++){
						if (fonts[yy]==fontName){
							found=true;
							fontRef[xx].selected=true;
							break;
						}
					}
					
					if (found){break;}
				}
				
				if (!found){fontRef.value=fontName;}
			}
		}
	}
	catch (error){}
	
	found=false;
	
	try {
		sizeRef=document.form1["RTFFontSize"];
		
		if (sizeRef){
			if (fontSize==null){sizeRef.value=fontSize;}
			else {
				for (xx=0; xx<sizeRef.length; xx++){
					scrap=sizeRef[xx].value;
					
					if (scrap==fontSize){
						found=true;
						sizeRef.value=fontSize;
						break;
					}
				}
				
				if (!found){sizeRef.value=fontSize;}
			}
		}
	}
	catch (error){}
	
	RTFActive=false;
}

function RTFSetSelectRange()
{
	var editorRef=document.getElementById("RTFEditor");
	var editDocRef=editorRef.contentWindow.document;
	var editRange=editDocRef.body.createTextRange();
	var selectedRange=editDocRef.selection.createRange();

	if (selectedRange.boundingHeight==0 || !editRange.inRange(selectedRange))
	{
		editRange.collapse();  // set insertion point at the beginning
		editRange.select();  // ensures the selection is highlighted
		selectedRange=editRange;
	}
	else if (editRange.inRange(selectedRange))
	{
		editDocRef.focus(); // Deselect the button just pressed by the user
		selectedRange.select(); // Ensures that the insertion point is set to the current cursor position or selected text
	}
}

function DecimalToRGB(decimalNo){
	var rgbValue="#";
	var xx, firstByte, firstNybble, secondNybble;
	
	for (xx=0; xx<3; xx++){
		firstByte=decimalNo&0xFF;	// get low byte
		secondNybble=firstByte&0x0F;	// get low nybble (4 bits)
		firstNybble=(firstByte>>4)&0x0F;	// get high nybble
		
		rgbValue+=firstNybble.toString(16);	// convert nybbles to hex
		rgbValue+=secondNybble.toString(16);
		
		decimalNo>>=8;	// drop first byte for next loop
	}
	
	return rgbValue.toUpperCase();
}

function ValidateEmail(emailAddress){
     var emailReg="^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex=new RegExp(emailReg);
     
     return regex.test(emailAddress);
}

function CheckAll(checked, fieldList){
	var fields=fieldList.split(",");
	try {
		for (xx=0; xx<fields.length; xx++){
			try {
				document.getElementById(fields[xx]).checked=checked;
			}
			catch (error) {}
		}
	}
	catch (error) {}
}

function JSLinkAction(){
	// do nothing - place the actual JS call in an OnClick event
}

function GetArrayFieldValue(fieldRef)
{
	var fieldValue="";
	var xx;

	for (xx=0; xx<fieldRef.length; xx++)
	{
		if (fieldRef[xx].checked)
		{
			if (fieldValue!=""){fieldValue+=",";}
			
			fieldValue+=fieldRef[xx].value;
			
			if (fieldRef.type.toLowerCase()=="radio"){break;}
		}
	}

	return fieldValue;
}

function GetRadioValue(fieldRef)
{
	  // go through fieldRef.form to be sure we are referencing the array of radio choices instead of a single choice
	return GetArrayFieldValue(fieldRef.form[fieldRef.name]);
}

function GetCheckboxValue(fieldRef)
{
	var fieldRef=fieldRef.form[fieldRef.name];
	var fieldValue;

	if (fieldRef.length==1)
	{
		if (fieldRef.checked){fieldValue=fieldRef.value;}
		else {fieldValue="";}
	}
	else {fieldValue=GetArrayFieldValue(fieldRef);}

	return fieldValue;
}

function GetSelectMultiValue(fieldRef)
{
	var choices=fieldRef.options;
	var fieldValues=new Array();
	var xx;
	
	for (xx=0; xx<choices.length; xx++)
	{
		if (choices[xx].selected){fieldValues.push(escape(choices.value));}
	}
	
	return fieldValues.join(',');
}

function GetFieldValue(fieldRef){
	var fieldRef=fieldRef.form[fieldRef.name];  // ensure that reference is to the field and not one single radio or checkbox option
	var fieldValue;
	
	switch (fieldRef.type)
	{
		case "radio":
			fieldValue=GetRadioValue(fieldRef);
			break;
		case "checkbox":
			fieldValue=GetCheckboxValue(fieldRef);
			break;
		case "select-multiple":
			fieldValue=GetSelectMultiValue(fieldRef);
			break;
		default:
			fieldValue=fieldRef.value;
			break;
	}
	
	return fieldValue;
}

function GetLookupData(fieldRef)
{
	var fieldName=fieldRef.name;
	var lookupList="";
	var fieldType, fieldValue, lookupRef, lookupType, queryString, colNamePrefix, rootPath, httpRequest, scrap, xx;
	
	colNamePrefix="";

	try {
		xx=fieldName.indexOf("_");
		scrap=fieldName.substring(0, 3);

		if (scrap=="Row" && xx>4){
			colNamePrefix=fieldName.substring(0, xx+1);
			fieldName=fieldName.substring(xx+1);
		}
	}
	catch (error){}
	
	if (fieldRef.tagName=="INPUT"){fieldType=fieldRef.type.toLowerCase();}
	else {fieldType="";}
	
	try
	{
		switch (fieldRef.type.toLowerCase())
		{
			case "radio":
				fieldValue=GetRadioValue(fieldRef);
				break;
			case "checkbox":
				fieldValue=GetCheckboxValue(fieldRef);
				break;
			default:
				fieldValue=fieldRef.value;
				break;
		}
	}
	catch (error){fieldValue="";}
	
	scrap=BuildLookupQueryString(fieldName, colNamePrefix, fieldRef.form.name);
	queryString="?field="+fieldName+"&value="+escape(fieldValue)+(scrap=="" ? "" : "&"+scrap);

	if (document.URL.indexOf("report/")==-1){rootPath="";}
	else {rootPath="../";}

	httpRequest=CreateHttpRequest("POST", rootPath+"dynamicvalue.aspx"+queryString, false);

	httpRequest.send("");
	
	return httpRequest.responseText;
}

function BuildLookupQueryString(fieldName, colNamePrefix, formName)
{
	if (formName==undefined){formName="form1";}
	if (colNamePrefix==undefined){colNamePrefix="";}
	
	var formRef=document.forms[formName];
	var fieldRef=formRef[colNamePrefix+fieldName];
	var queryString="";
	var lookupList, lookupFields, lookupRef, lookupType, lookupField, lookupValue, xx, scrap;
	
	try {
		lookupList=eval(fieldName+"_Lookup");
		
		if (lookupList!=null)
		{
			lookupFields=lookupList.split(',');
			
			for (xx=0; xx<lookupFields.length; xx++)
			{
				lookupField=lookupFields[xx];
				
				if (lookupField==""){continue;}
				
				try
				{
					lookupRef=formRef[colNamePrefix+lookupField];
					
					if (lookupRef.tagName=="INPUT"){lookupType=lookupRef.type.toLowerCase();}
					else {lookupType="";}
					
					switch (lookupType)
					{
						case "radio":
							lookupValue=GetRadioValue(lookupRef);
							break;
						case "checkbox":
							lookupValue=GetCheckboxValue(lookupRef);
							break;
						default:
							lookupValue=lookupRef.value;
							break;
					}
				}
				catch (error){
					try {lookupValue=document.getElementById(ColNamePrefix+lookupField).innerHTML;}
					catch (error){continue;}
				}

				if (queryString!=""){queryString+="&";}
				
				queryString+=lookupField+"="+escape(lookupValue);
			}
		}
	}
	catch (error){}
	
	return queryString;
}

function RebuildSelectOptions(fieldRef, delimData)
{
	var fieldValue=fieldRef.value;
	var rows=delimData.replace("$#039;", "'").split('$$');
	var option, columns, nameValuePair, optionValue, optionDescr, scrap, xx, yy;
	
	fieldRef.value="";
	fieldRef.options.length=0;
	
	for (xx=0; xx<rows.length; xx++)
	{
		scrap=rows[xx].replace("$#036;", "$");
		columns=scrap.split(',');
		optionValue=null;
		optionDescr=null;
		
		for (yy=0; yy<columns.length; yy++)
		{
			scrap=columns[yy].replace("$#044", ",");
			nameValuePair=columns[yy].split('=');
			
			if (nameValuePair.length==2)
			{
				if (yy==0){optionValue=unescape(nameValuePair[1]);}
				else {optionDescr=unescape(nameValuePair[1]);}
			}
			
			if (yy==1){break;}  // only use the first two columns
		}
		
		if (optionValue!=null && optionDescr!=null){
			option=document.createElement("OPTION");
			option.text=optionDescr;
			option.value=optionValue;
			fieldRef.options.add(option);
			
			if (optionValue==fieldValue){fieldRef.value=optionValue;}
		}
	}
}

// Determine browser and version.
function Browser()
{
	var userAgent=navigator.userAgent;
	var xPos;

	this.isIE=false;  // These are not declared with "var" to expand scoping so this function can be queried as an object
	this.isNS=false;
	this.isFF=false;
	this.version=null;

	if (userAgent.indexOf("MSIE")>-1)
	{
		xPos=userAgent.indexOf("MSIE");
		
		this.isIE=true;
		this.version=parseFloat(userAgent.substr(xPos+4));
	}
	else if (userAgent.indexOf("Netscape6/")>-1)
	{
		xPos=userAgent.indexOf("Netscape6/");
		
		this.isNS=true;
		this.version=parseFloat(userAgent.substr(xPos+10));
	}
	else if (userAgent.indexOf("Firefox")>-1)
	{
		xPos=userAgent.indexOf("Firefox/");
		
		this.isFF=true;
		this.version=parseFloat(userAgent.substr(xPos+8));
	}
	else if (userAgent.indexOf("Gecko")>-1) // Treat any other "Gecko" browser as NS 6.1.
	{
		xPos=userAgent.indexOf("Gecko");
		this.isNS=true;
		this.version=6.1;
	}
}

function CloseLookup()
{
	if (lk_winref!=undefined)
	{
		try {lk_winref.close();}
		catch (error){}
	}
}

// Parses a form into a name=value&... string
function BuildFormString(formRef)
{
	var formItems=new Array();
	var xx;

	for (xx=0; xx<formRef.length; xx++)
	{
		formItems[xx]=formRef[xx].name+"="+escape(GetFieldValue(formRef[xx]));
	}

   return formItems.join('&');
}

function SubmitFormAjax(formRef, serverPage, doAfterRef)
{
	var formString=BuildFormString(formRef);
	var contentLength=formString.length;
	var httpRequest=CreateHttpRequest("POST", serverPage, true, contentLength);
	
	if (doAfterRef)
	{
		httpRequest.onreadystatechange=function()
		{
			if (httpRequest.readyState==4)
			{
				//if (objToFill){objToFill.innerHTML=httpRequest.responseText;} deprecated: still in use in Navlogix
				doAfterRef(httpRequest.responseText);
			}
		}
	}
	
	httpRequest.send(formString);
}

// Merges an HTML string into the current document
function InsertHTMLToPage(containerRef, htmlFrag)
{
	//var containerDoc=containerRef.document;
	var docFrag=window.document.createDocumentFragment();
	var htmlRef;
	var bodyRef;
	var xx, childRef, parentRef, parseRef;
	
	while (htmlFrag.indexOf("$#039;")!=-1){htmlFrag=htmlFrag.replace("$#039;", "'");}
	
	if (BrowserType.isIE)
	{
		htmlRef=docFrag.createElement("html");
		bodyRef=docFrag.createElement("body");
	}
	else  // super easy in FF - Nice, since it apparently doesn't support the DHTML commands on a document fragment
	{
		containerRef.innerHTML=htmlFrag;
		return;
	}
	
	if (containerRef.tagName=="TR")
	{
		htmlFrag="<table><tr id=\"TagParser\">"+htmlFrag+"</tr></table>";
	}
	else
	{
		bodyRef.id="TagParser";
	}

	bodyRef.innerHTML=htmlFrag;
	htmlRef.appendChild(bodyRef);
	docFrag.appendChild(htmlRef);
	
	parseRef=docFrag.getElementById("TagParser");
	
	for (xx=0; xx<parseRef.childNodes.length; xx++)
	{
		CopyNode(containerRef, parseRef.childNodes[xx]);
	}
}

// Copy One Node Hierarchy into Another Parent Hierarchy
function CopyNode(newParent, sourceNode)
{
	var tagType=sourceNode.tagName;
	var nodeType=sourceNode.nodeType;
	var newChild=null;
	var canHaveChildren=true;
	var xx, attribName, attribValue, newValue, fieldName, inputType, childHTML, childParts;
	
	if (nodeType==3)
	{
		newChild=newParent.document.createTextNode(sourceNode.data);
		newParent.appendChild(newChild);
		canHavChildren=false;
	}
	else if ((tagType=="TD" && newParent.tagName=="TR") || (tagType=="TR" && newParent.tagName=="TABLE"))
	{
		if (tagType=="TD"){newChild=newParent.insertCell(-1);}
		else {newChild=newParent.insertRow(-1);}
		
		if (sourceNode.className!=newChild.className){newChild.className=sourceNode.className;}  // not in the attributes collection

		for (xx=0; xx<sourceNode.attributes.length; xx++)
		{
			attribName=sourceNode.attributes[xx].name.toLowerCase();
			attribValue=sourceNode.getAttribute(attribName);
			newValue=newChild.getAttribute(attribName);
			
			if (newValue!=attribValue && attribName!="style")
			{
				newChild.setAttribute(attribName, attribValue);
			}
		}
	}
	else if (nodeType==1)
	{
		childParts=new Array();

		childParts.push(tagType.toLowerCase());
		
		if (tagType=="INPUT"){childParts.push("type=\""+sourceNode.type+"\"");}
		if (sourceNode.name){childParts.push("name=\""+sourceNode.name+"\"");}

		childHTML="<"+childParts.join(' ')+">";	// temporary assignment
		newChild=newParent.document.createElement(childHTML);	// temporary assignment
		
		if (sourceNode.className!=newChild.className){childParts.push("class=\""+sourceNode.className+"\"");}  // not in the attributes collection
		
		for (xx=0; xx<sourceNode.attributes.length; xx++)
		{
			attribName=sourceNode.attributes[xx].name.toLowerCase();
			
			if (attribName!="name" && attribName!="type" && attribName!="style")
			{
				attribValue=sourceNode.getAttribute(attribName);
				newValue=newChild.getAttribute(attribName);
				
				if (attribValue && IsEventHandlerAttribute(attribName))
				{
					attribValue=GetFunctionText(attribValue);
					newValue="";
				}
				
				if (newValue!=attribValue)
				{
					childParts.push(attribName+"=\""+attribValue+"\"");
				}
			}
		}
		
		newChild.removeNode(true);
		childHTML="<"+childParts.join(' ')+">";
		newChild=newParent.document.createElement(childHTML);
		newParent.appendChild(newChild);
	}

	if (canHaveChildren)
	{
		// copy child nodes (call this function on each child)
		for (xx=0; xx<sourceNode.childNodes.length; xx++)
		{
			CopyNode(newChild, sourceNode.childNodes[xx]);
		}
	}
}

// Gets the inner text from a function (preferably an anonymous function event handler)
function GetFunctionText(functRef)
{
	var innerText=null;
	var outerText, ePos, sPos;
	
	if (functRef)
	{
		outerText=functRef.toString();

		if (outerText.indexOf("function")==0)
		{
			sPos=outerText.indexOf("{");
			ePos=outerText.lastIndexOf("}");
			
			if (sPos>-1 && ePos>sPos)
			{
				innerText=Trim(outerText.substring(sPos+1, ePos));
			}
		}
	}
	
	if (innerText==null){innerText="";}
	
	return innerText;
}

function IsEventHandlerAttribute(attribName)
{
	var answer=false;
	
	switch (attribName.toLowerCase())	// more efficient than a huge if (...or...or...or...)
	{
		case "onabort":
			answer=true;
		case "onblur":
			answer=true;
		case "onchange":
			answer=true;
		case "onclick":
			answer=true;
		case "ondblclick":
			answer=true;
		case "onerror":
			answer=true;
		case "onfocus":
			answer=true;
		case "onkeydown":
			answer=true;
		case "onkeypress":
			answer=true;
		case "onkeyup":
			answer=true;
		case "onload":
			answer=true;
		case "onmousedown":
			answer=true;
		case "onmouseout":
			answer=true;
		case "onmouseover":
			answer=true;
		case "onmouseup":
			answer=true;
		case "onreset":
			answer=true;
		case "onresize":
			answer=true;
		case "onsubmit":
			answer=true;
		case "onunload":
			answer=true;
	}
	
	return answer;
}
