// $Header: /MASweb_MMSQL/version300/web_server/source/dealer/client/mascls.js 42    9/21/04 1:59p Jwood $
// © 2004 Copyright GE Monitoring Automation Systems All Rights Reserved 
// For Legal Notice and Terms & Conditions see www.monauto.com/legal.htm 

var sendlook, lookupField, lookupForm, lk_winref, HelpWinRef, scrollPos, DropDownMenu, DropDownEvent, PullUpMenu, PullUpEvent;
var ScreenHeight, ScreenWidth, ContentLeftMargin, ContentWidth, ContentHeight, colNamePrefix;
var PageAdjustCount=0;
var LockFocus=true;
var Scrolling=false;

function lookup_rtn(columnValue, returnString){
	var columnName=self.opener.lookupField;
	var formName=self.opener.lookupForm;
	var returnFields=returnString.replace("$#039;", "'").split(",");  // character substitution in MASwebutil.dll
	var xx, returnValue, returnField, scrap;
	var returnPair=new Array();
	
	colNamePrefix=self.opener.colNamePrefix;
	
	try {eval("self.opener.document."+formName+"."+columnName).value=columnValue;}
	catch (error){}
	
	try {self.opener.document.getElementById(columnName).innerText=columnValue;}
	catch (error){}
	
	try {self.opener.document.getElementById("xX_"+columnName).innerText=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]).replace("$#044;", ",");  // character substitution in MASwebutil.dll
			
			if (returnField=="colnameprefix"){ // assumes this is the first value in the return string
				//colNamePrefix=returnValue; obsolete??
			}
			else {
				try {self.opener.document.getElementById(returnField).innerText=returnValue;} // prefered form format (Text)
				catch (error){}
				
				try {eval("self.opener.document."+formName+"."+returnField).value=returnValue;} // old form format or hidden form field
				catch (error){}
				
				try {eval("self.opener.document."+formName+".xX_"+returnField).value=returnValue;} // support for form submission errors
				catch (error){}
			}
		}
	}
	
	if (columnName.indexOf("dispage_no")>-1 || columnName.indexOf("_open_page")>-1 || columnName.indexOf("_close_page")>-1){self.opener.dispPageToggle(columnName);}
	if (columnName.indexOf("gldisp_id")>-1){self.opener.dispPageToggle(columnName);}
	if (columnName=="fail_to_open_event_id" || columnName=="fail_to_close_event_id"){self.opener.failurePageToggle(eval("self.opener.document."+formName+"."+columnName));}
	
	window.close();
	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){};
	
	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);
	
	colNamePrefix="";
	
	if (dateSelect){height=400; width=350;}
	if (timeSelect){height=150; width=300;}
	
	openString="top=30,left="+leftPos+",height="+height+",width="+width+",resizable,scrollbars";
	
	if (formName=="undefined" || formName==null || formName==""){formName="form1";}
	
	try {
		if 	(eval("document."+formName+"."+colName).getAttribute("disabled")){return;}
	}
	catch (error){}
	
	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, "lookup", 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() {
	try {
	//		window.parent.Frame1.document .write("<html></html>"); 
		lk_winref.close();
	} catch (error) {}
	//	window.parent.Frame1.location = "blank.htm";
	return;
}

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="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 xx, yy, mName, hName, xIncr, xNumber, yPos;
	
	ContentHeight=document.getElementById("Content").offsetHeight;
	ContentWidth=document.getElementById("ContentSpace").width;
	ContentLeftMargin=document.getElementById("ContentSpace").offsetLeft;
	ScreenHeight=window.screen.availHeight;
	ScreenWidth=window.screen.availWidth;
	
	document.getElementById("PageHeader").style.width=ContentWidth+"px";
	document.getElementById("PageHeader").style.left=ContentLeftMargin+"px";
	document.getElementById("HeaderSpace").style.width=ContentWidth+"px";
	document.getElementById("HeaderSpace").style.left=ContentLeftMargin+"px";
	document.getElementById("PageFooter").style.width=ContentWidth+"px";
	document.getElementById("PageFooter").style.left=ContentLeftMargin+"px";
	document.getElementById("FooterSpace").style.width=ContentWidth+"px";
	document.getElementById("FooterSpace").style.left=ContentLeftMargin+"px";
	
	if (PageAdjustCount<2){
		document.getElementById("HeaderSpace").innerHTML=document.getElementById("PageHeader").innerHTML;
		document.getElementById("FooterSpace").innerHTML=document.getElementById("PageFooter").innerHTML;
	}
	
	scrollPos=document.getElementById("Content").scrollTop;
	PageAdjustCount++;

   	if (document.readyState!="complete"){
   		setTimeout("PageSizeAdjust()", 500);
	}
	else {
		FormScroll();
		
		if (PageAdjustCount==2)
		{
			document.getElementById("HeaderSpace").innerHTML=document.getElementById("PageHeader").innerHTML;
			document.getElementById("FooterSpace").innerHTML=document.getElementById("PageFooter").innerHTML;
			PageAdjustCount=0;
		}
	}
}

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.parentElement;
						}
						/*
						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){
							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 (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{
		window.open("", "reportviewerwindow", "status,resizable");
		document.form1.target="reportviewerwindow";
		FormSubmit();
		document.form1.target="";
	}
}

function ToPascalCase(transText){
	var xPos=transText.indexOf("_");
	var scrap;
	
	if (transText.length>1){transText=transText.substring(0, 0).toUpper()+transText.substring(1);}
	else {transText=transText.toUpper();}
	
	while (xPos>-1){
		scrap=transText.substring(xPos, xPos+1);
		transText=transText.replace(scrap, scrap.substring(1).toUpper());
		
		xPos=transText.indexOf("_");
	}
	
	return transText;
}

function ToCamelCase(transText){
	transText=ToPascalCase(transText);
	
	if (transText.length>1){transText=transText.substring(0, 0).toLower()+transText.substring(1);}
	else {transText=transText.toLower();}
	
	return transText;
}

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 MenuDropDown(menuName, parentItem){
	var menuDiv=document.getElementById(menuName);
	var currentX=menuDiv.offsetLeft;
	var currentY=menuDiv.offsetTop;
	var targetX=parentItem.parentElement.offsetLeft;
	var targetY=parentItem.parentElement.offsetTop+parentItem.parentElement.offsetHeight;
	var moveBy, fCall;
	
	DropDownMenu=null;
	DropDownEvent=null;
	
	if (PullUpMenu==menuDiv.id && PullUpEvent!=null){  // if current menu is pulling up, cancel event;
		PullUpEvent.cancelBubble;
		PullUpMenu=null;
		PullUpEvent=null;
	}
	
	if (currentX!=targetX){menuDiv.style.left=targetX;}
	
	if (currentY!=targetY){
		moveBy=Math.ceil((targetY-currentY)/2);
		moveBy=Math.max(moveBy, 5);
		moveBy=Math.min(moveBy, targetY-currentY);
		
		menuDiv.style.top=currentY+moveBy;
		
		fCall="MenuDropDown('"+menuName+"', document.all["+parentItem.sourceIndex+"])";
		DropDownMenu=menuName;
		DropDownEvent=setTimeout(fCall, 100);
	}
}

function MenuPullUp(menuName){
	var menuDiv=document.getElementById(menuName);
	var currentY=menuDiv.offsetTop;
	var bottomPos=currentY+menuDiv.offsetHeight;
	var fCall;
	
	PullUpMenu=null;
	PullUpEvent=null;
	
	if (DropDownMenu==menuDiv.id && DropDownEvent!=null){  // if current menu is dropping down, cancel event
		DropDownEvent.cancelBubble;
		DropDownMenu=null;
		DropDownEvent=null;
	}
	
	if (bottomPos>0){
		menuDiv.style.top=currentY-10;
		
		fCall="MenuPullUp('"+menuName+"')";
		PullUpMenu=menuName;
		PullUpEvent=setTimeout(fCall, 100);
	}
}

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(){
	LockFocus=true;
	
	OpenPopupWindow("translate.aspx", 800, 600);
	
	return;
}

function OpenPopupWindow(pageName, width, height){
	if (width==null || width==undefined){width=575;}
	if (height==null || height==undefined){height=400;}
	
	var leftPos=ScreenWidth-width-15;

	openString="top=10,left="+leftPos+",height="+height+",width="+width+",resizable,scrollbars,status";

	if (lk_winref==self){
		try {lk_winref.close();} catch (error){};
	
		lk_winref=window.open(pageName, "lookup", openString);
	
		if (LockFocus){lk_winref.focus();}
	}
	else {
		window.open(pageName, "lookup2", openString);
	}
	
	return;
}