//code to inform user of time remaining before session time-out.
//SETUP Instructions:
//1.In the calling application:
//	a. install a textbox named "txtClock" to show the CountDown timer.
//		in the page_load event enter statement 
//			this.txtClock.Text=Session.Timeout.ToString();
//	b. install a div named "AskUser" to show the user warnings and messages from this script
//2. Add onload attribute to body tag as example: <body onload="CountDown();">
//3.In the website, include page ResetSession.aspx.

var down;
var cmin;
var csec;
var resetNum;		//variable to keep track of the number of times the session is reset
var cminWarning=2;	//the minute at which the timeout warning will be displayed
var sessionTimeout; //the server-side session timeout setting [minutes]
var txtClock; 	//the textbox used to transfer and display timeout info.
var browser = navigator.appName;
var IsIE
IsIE = false;
if (browser.indexOf("Microsoft") == 0)
    IsIE = true;

function CountDown() 
{
//	if(document.getElementById("txtClock")!=undefined)
//	{
//		txtClock=document.getElementById("txtClock")
//		cmin=document.getElementById("txtClock").value;
//		sessionTimeout=cmin;
//		csec=0;
//		resetNum=1;
//		//debugger;
//		runclock();
//    }
//    if (document.getElementById("ctl00_ContentPlaceHolder1_CTS_WaypointReceivingGrid1_txtClock") != undefined) {
//        txtClock = document.getElementById("ctl00_ContentPlaceHolder1_CTS_WaypointReceivingGrid1_txtClock")
//        cmin = document.getElementById("ctl00_ContentPlaceHolder1_CTS_WaypointReceivingGrid1_txtClock").value;
//        sessionTimeout = cmin;
//        csec = 0;
//        resetNum = 1;
//        //debugger;
//        runclock();
//    }
    try     //bypass if locator is undefined
    {
        if (document.getElementById(locatorMasterPage + "txtClock") != undefined) {
            txtClock = document.getElementById(locatorMasterPage + "txtClock")
            cmin = document.getElementById(locatorMasterPage + "txtClock").value;
            sessionTimeout = cmin;
            csec = 0;
            resetNum = 1;
            //debugger;
            runclock();
        }
    } 
    catch(Error) {

    }

//    if (document.getElementById(locator + "txtClock") != undefined)
//	{
//	    txtClock = document.getElementById(locator + "txtClock")
//	    cmin = document.getElementById(locator + "txtClock").value;
//		sessionTimeout=cmin;
//		csec=0;
//		resetNum=1;
//		//debugger;
//		runclock();
//	}	
}
function ResetClock(time)
{
	//this.runclock=null;
	cmin=time;
	csec=1;
	runclock();		
}
	
function runclock() {
	
digit1 = parseInt(cmin/10);
digit2 = parseInt(cmin%10);
digit3 = parseInt(csec/10);
digit4 = parseInt(csec%10);
	
//document.getElementById("txtClock").value="Timeout in: " + cmin + ":" + digit3 + digit4;
txtClock.value="Timeout in: " + cmin + ":" + digit3 + digit4;
	
csec--;
	if(csec==-1) { csec=59; cmin--; }
  	if((cmin==0)&&(csec==0)) 
  	{
  	    //document.getElementById("txtClock").value="Session Timed-Out";
  	    //debugger;
  	    txtClock.value = "Session Timed-Out";
  	    //alert("IsIE=" + IsIE);
//  	    if (IsIE) {
//  	        txtClock.setAttribute("className", "clockTimedOutStyle");
//  	    }
//  	    else {
  	        txtClock.className = "clockTimedOutStyle";
//  	    }
  		document.getElementById("AskUser").style.backgroundColor="#ff0000";
  		document.getElementById("AskUser").style.visibility="visible";
  		document.getElementById("AskUser").innerHTML="<a href='#' onclick='hide()';><img src='close.gif' alt='Close this messagebox.'></a>Your session has timed-out.<br>You may want to copy any info on this screen<br>so that you can login again and<br>paste your info back into the application<br>for submission to the database."
  		alert("Your session has Timed-Out.  Please login again.");
  		return;
  	}
  	if((cmin==cminWarning)&&(csec==0))
  	{
  		
  		//document.body.bgColor="#ffff00";
  		
  		document.getElementById("AskUser").style.position="absolute"; 
  		document.getElementById("AskUser").style.zIndex="1000";  		
  		document.getElementById("AskUser").style.top=(screen.availHeight)/2;
  		document.getElementById("AskUser").style.left="200px";
		document.getElementById("AskUser").style.backgroundColor="#ffff00";
		document.getElementById("AskUser").style.fontFamily="Arial";
		document.getElementById("AskUser").style.padding="5px";
		document.getElementById("AskUser").style.border='0.2cm ridge red';
		/*
	  	document.getElementById("AskUser").className="Timeout";
	  	*/
  		document.getElementById("AskUser").style.visibility="visible";
  		document.getElementById("AskUser").innerHTML="<a href='#' onclick='hide()';><img src='close.gif' alt='Close this messagebox.'></a><br>Your session will expire within " + cminWarning + " minutes.<br>Do you want to maintain your session and continue working?<br><a href='ResetSession.aspx' class='timeout' onclick='ResetCountDown();' target='_blank'>Click Here to Reset Session</a>";
  		
		//cmin=19;
		//csec=48;
		//runclock();
  	}
	else 
	{
		//down=setTimeout("runclock()",1000); 
	}
	down=setTimeout("runclock()",1000*resetNum); 
}

function ResetCountDown()
{
	document.getElementById("AskUser").style.visibility="hidden";
	resetNum++;
	ResetClock(sessionTimeout);
}
function hide()
{
	document.getElementById("AskUser").style.visibility="hidden";
	document.getElementById("AskUser").style.width="0px";
	document.getElementById("AskUser").style.height="0px";
}
