//YOU CAN DELETE THESE COMMENTS FOR FASTER LOAD
/*---------------------------------------------------------------------
** 1.utility for tracking changes made to form elements via setSaved() [onChange or other element event]                                                          *
**     system will promt to save changes when onunload="PageExit()"                                             
** 2.utility for tracking session - and exiting after specified idle time - if SaveNeeded is included user will be prompted to save changes                
**                                                                     
** track changes for session requires SaveNeeded type hidden (any form)   
** timed session kill requires kill type hidden (any form), call initJavaScriptSession() to enable time out
** 
** on expiration the kill form will be submitted (if no changes made) or the form containing SaveNeeded and kill elements will be submitted
** if the SaveNeeded form contains a kill element as well kill will be set to value='kill' so you can deal with the server session based on that value

**---------------------------------------------------------------------*/
	
	var doTimer=false;
    var resetVals=false;

	var ck=0; 
	var tf=0;
	var timeUp=0;
	var timeLeft=0;
	var tcount=0;
	var SessionTime=130;
	var miscstr="";
	function initJavaScriptSession(sestime){
		SessionTime=sestime;
		doTimer = true;
		tf=window.setTimeout('TimerFunc()',1000);
		if(navigator.appName.substring(0,1)=="N"){
			window.document.captureEvents(Event.MOUSEDOWN);
			window.document.captureEvents(Event.KEYPRESS);
		}
		window.document.onkeypress=revivesession;
		window.document.onmousedown=revivesession;
	}
	function revivesession(e){
		tcount=0;
		if(getSaveNeeded()){
			miscstr="  |  Modified";
		}else{
			miscstr="  |  Unmodified";
		}
		window.status = getDefaultStatus() + miscstr + "  |  Session: Active";
	}
	function TimerFunc() {
   		tf=window.setTimeout("TimerFunc();",1000); 
   		tcount++;
  		timeLeft=SessionTime - tcount;
  		 
		if(timeLeft < 120) {
		    window.status = "Session:" + timeLeft + " Seconds remaining !" + "  |  " + getDefaultStatus();
		}
   		if(timeLeft == 0) {
      			window.clearTimeout(tf);
     			timeUp=1;
				window.status = "Session Status: Timed Out";
      			alert("Your session has expired.  You will need to log in again.");
				//check SaveNeeded and ask to save if changes made
			if(PromtToSave()){
				Savefrm=FindForm("kill");
				if(Savefrm){
					Savefrm.elements.kill.value='kill';
					if(validateform(Savefrm)){
						setSaveOnExit();
						Savefrm.submit();
					}
				}else{
					alert ("Unable to save changes");
					window.document.frmkillsession.submit();
				}	
			}else{
				window.document.frmkillsession.submit();
			}
   		}
	}
	function FindForm(HasField){
   		var numberForms = window.document.forms.length;
  	 	var formIndex;
   		for (formIndex = 0; formIndex < numberForms; formIndex++)
   		{
	      	if(eval(window.document.forms[formIndex].elements[HasField])){
				return window.document.forms[formIndex];
   			}	
		}
		return false;
	}
	function PageExit(){
		if(PromtToSave()){
			thefrm=FindForm('SaveNeeded');
			if(thefrm){
				if(validateform(thefrm)){
					setSaveOnExit();
					thefrm.submit();
				}else{
					alert("Unable to save changes at this time");
					return false;
				}
			}
			return false;
		}
		return true;
	}
	function PromtToSave(){
		thefrm=FindForm('SaveNeeded');
		
		if((getSaveNeeded()) && (confirm("Click OK to save changes to this form"))){
			   thefrm.SaveNeeded.value=0; //assume some other function will submit our form
			   return true;
		}else{
			//setSaveNeeded(0);
			return false;
		}
	}
	function getDefaultStatus(){
		thefrm=FindForm('DefaultStatus');
		if(thefrm){
			return thefrm.DefaultStatus.value;
		}else{
			return '';
		}
		return '<?=$DefaultStatus?>';
	}
	function getSaveNeeded(){
		thefrm=FindForm('SaveNeeded');
		if((thefrm) && (thefrm.SaveNeeded.value==1)){
			return true;
		}else{
			return false;
		}
	}
	function setSaveOnExit(){
		thefrm=FindForm('SaveOnExit');
		if(thefrm){
			thefrm.elements.SaveOnExit.value=1;
		}
		newfrm=FindForm('SaveOnBak');
		if(newfrm){
			newfrm.elements.SaveOnBak.value=1;
		}
	}
	function setSaveNeeded(needed){
		revivesession();
		thefrm=FindForm('SaveNeeded');
		if(thefrm){
			thefrm.elements.SaveNeeded.value=needed;
		}
	}
	function CheckSomethingonSubmit(frm){
		frm.elements.SaveNeeded.value=0;
	}

