<!--
/************************************************************************
File name		: TimeOutStatus.js
Application		: CleanFleet
Purpose			: This page allows the user to get an estimate of their
				: time remaining before their session times out.
Created on		: 12/02/2007
--------------------------------------------------------------------------

Developed By	: R Systems
--------------------------------------------------------------------------
Comments:
	
*************************************************************************/
	var seconds;
	var client;
	var timeStamp;
	var blnBool
	var intResetSetSeconds;
	var intResetSessionCnt;
	var strResetMessage;
	
	var dtTimeBeforeConfirm;
	blnBool = true
	
	//Main Function to start the count down
	function countDown(intSetSeconds, strMessage, intSessionCnt) 
	{
		var intMinutes, intSeconds
		var now = new Date();
		//set the Global Variable for the first time only
		if(blnBool)
		{
			intResetSetSeconds = intSetSeconds;
			intResetSessionCnt = intSessionCnt;
			strResetMessage = strMessage;
		}
		
		if(intSessionCnt > 1)
		{
			//this runs only when the page is loaded....
			if (parseInt(intSetSeconds) != -1 && parseInt(intSetSeconds) != '')
			{
				timeStamp = new Date();
				seconds = parseInt(intSetSeconds) * 60;
				timeStamp.setSeconds(timeStamp.getSeconds() + seconds);
			}
			
			//Reducing the Seconds
			seconds = (Date.parse(timeStamp)-Date.parse(now))/1000;
			if (seconds>0)
			{
				intMinutes=Math.floor(seconds/60);	// get minutes (60 seconds / minite)
      			intSeconds=(seconds%60);			// get seconds (total seconds modulus 60)
      			if (intSeconds < 10)
      			{
					intSeconds = "0" + intSeconds;
				}
				if (intMinutes < 10)
				{
					intMinutes = "0" + intMinutes;	
				}
				if (intMinutes=="00")
				{
					window.status = intMinutes + ':' + intSeconds + '  seconds remaining until timeout.';
				}
				else
				{
					window.status = intMinutes + ':' + intSeconds + '  minutes remaining until timeout.';
				}
				
				//A functioname object initialized
				var functionname = "countDown(-1" + ",'" +  strMessage + "'," + intSessionCnt + ")"
				//set the boolean for the gloabla variable to be false
				blnBool = false;
				//Call the function recursively
				window.setTimeout(functionname,1000);
				
				if (seconds==120)
				{	
					dtTimeBeforeConfirm = new Date()
					
					if (confirm("Do you wish to extend your session time?"))
					{
						//temporary date
						var dtTimeAfterClick = new Date()
						if (parseInt(dtTimeAfterClick.getTime() - dtTimeBeforeConfirm.getTime())<=120000)
						{
							document.getElementById("Defib").src = '../General/frmDefibrillator.aspx';
							countDown(intResetSetSeconds, strResetMessage, intResetSessionCnt);
						}
						else
						{
							window.status = 'Your Session has timed out.'
							//alert('Sorry! Your response was after the session timed out. You shall be logged off the system. Please Re-Login');
							//Close All Child indows
							CloseChildWindows();
							location.href = "../General/frmLogout.aspx?Param='SessionTimedOut'";
							//break;
				 		}
					}
					//Do Nothing
				}
			}
			else
			{
				window.status = 'Your Session has timed out.'
				alert('Your Session has expired. Please Re-Login');
				//Close All Child indows
				CloseChildWindows();
				location.href = "../General/frmLogout.aspx?Param='SessionTimedOut'";
			}
		}
		else
		{
			window.status = strMessage;
		}
	}
	
	
	/***** This method allows for resetting of the counter once its started *****/
	function countDownReset(intSetSeconds)
	{
		var intMinutes, intSeconds
		
		if (parseInt(intSetSeconds) != -1 && parseInt(intSetSeconds) != '')
		{
			timeStamp = new Date();
			seconds = parseInt(intSetSeconds) * 60
			timeStamp.setSeconds(timeStamp.getSeconds() + seconds)
		}
	}
	
	function countDownSave(intSetSeconds, strMessage, intSessionCnt,strFormName,strFieldName,strControlName) 
	{
		var intMinutes, intSeconds
		var now = new Date();
		var el
		
		if(blnBool)
		{
			intResetSetSeconds = intSetSeconds;
			intResetSessionCnt = intSessionCnt;
			strResetMessage = strMessage;
		}
		
		if(intSessionCnt > 1)
		{
			//this runs only when the page is loaded....
			if (parseInt(intSetSeconds) != -1 && parseInt(intSetSeconds) != '')
			{
				timeStamp = new Date();
				seconds = parseInt(intSetSeconds) * 60
				timeStamp.setSeconds(timeStamp.getSeconds() + seconds)
			}
		
			seconds = (Date.parse(timeStamp)-Date.parse(now))/1000;
			//seconds--;
			if (seconds>0) 
			{
			
				//if less than 5 minutes are left then save the page,
				// also perform the check every 120 seconds after less than 
				//6 minutes are left and reminder after 2 minutes.
				if (seconds==360) 
				{
					alert("Your session is about to expire. Please Save your entries.")
					if (typeof(Page_ClientValidate) == 'function') 
					{
						if(Page_ClientValidate())
						{
							el = eval('document.' + strFormName + '.' + strFieldName)
							//set value of hidden field
							if(el!=null)
							{
								el.setAttribute("value","1")	
							}
							el = eval('document.' + strFormName)
							//Call function to save template data
							//SavePageContents(strControlName)
							//el.submit();					
						}
					}
				}
				
				intMinutes=Math.floor(seconds/60); // get minutes (60 seconds / minite)
      			intSeconds=(seconds%60); // get seconds (total seconds modulus 60)
      			if (intSeconds < 10)
					intSeconds = "0" + intSeconds;
				if (intMinutes < 10)
					intMinutes = "0" + intMinutes;	
				if (intMinutes=="00")
					window.status = intMinutes + ':' + intSeconds + '  seconds remaining until timeout.'	
				else
					window.status = intMinutes + ':' + intSeconds + '  minutes remaining until timeout.'	
				var functionname = "countDownSave(-1" + ",'" +  strMessage + "'," + intSessionCnt + ",'" + strFormName +"','" + strFieldName + "','" + strControlName +"')"
				blnBool = false
				window.setTimeout(functionname,1000);
				
				if (seconds==120)
				{	
					dtTimeBeforeConfirm = new Date()
					
					if (confirm("Do you wish to extend your session time?"))
					{
						//temporary date
						var dtTimeAfterClick = new Date()
						if (parseInt(dtTimeAfterClick.getTime() - dtTimeBeforeConfirm.getTime())<=120000)
						{
							document.getElementById("Defib").src = '../General/frmDefibrillator.aspx';
							countDown(intResetSetSeconds, strResetMessage, intResetSessionCnt);
						}
						else
						{
							window.status = 'Your Session has timed out.'
							//alert('Sorry! Your response was after the session timed out. You shall be logged off the system. Please Re-Login');
							//Close All Child indows
							CloseChildWindows();
							location.href = "../General/frmLogout.aspx?Param='SessionTimedOut'";
							//break;
				 		}
					}
					//Do Nothing
				}
			}
			else
			{
				window.status = 'Your Session has timed out.'
				alert('Your Session has expired. Please Re-Login');
				//Close All Child indows
				CloseChildWindows();
				location.href = "../General/frmLogout.aspx?Param='SessionTimedOut'";
			}
		}
		else
		{
			window.status = strMessage;
		}
	}
	
	function CloseChildWindows()
	{
		//Search Window Popup Window
		if (SearchWindow && SearchWindow.open && !SearchWindow.closed)
		{
			SearchWindow.close()
		}
		//Select Combo Popup Window
		if (ComboSelectWindow && ComboSelectWindow.open && !ComboSelectWindow.closed)
		{
			ComboSelectWindow.close()
		}
		//Select Combo Popup Window
		if (PASWindow && PASWindow.open && !PASWindow.closed)
		{
			PASWindow.close()
		}
		//ListCntrlWindow
		if (ListCntrlWindow && ListCntrlWindow.open && !ListCntrlWindow.closed)
		{
			ListCntrlWindow.close()
		}
		//PageHelp_CollapseWindow
		if (PageHelp_CollapseWindow && PageHelp_CollapseWindow.open && !PageHelp_CollapseWindow.closed)
		{
			PageHelp_CollapseWindow.close()	
		}
		//Export Window
		if (ExportWindow && ExportWindow.open && !ExportWindow.closed)
		{
			ExportWindow.close()
		}
		//Import Window
		if (ImportWindow && ImportWindow.open && !ImportWindow.closed)
		{
			ImportWindow.close()
		}
		//DGPopUpWindow Window
		if (DGPopUpWindow && DGPopUpWindow.open && !DGPopUpWindow.closed)
		{
			DGPopUpWindow.close()
		}
	}
-->