var secs
var timerID = null
var timerRunning = false
var delay = 1000

var maxtime=5  // 5400 need the change if want to change the time out time it is in second
var alerttime=120 // 300 need the change if want to change the time out alert time it is in second
var serverpath="";

function InitializeTimer(serverurl)
{
	serverpath=serverurl
    // Set the length of the timer, in seconds
    secs = maxtime	   
    StopTheClock()
    StartTheTimer()
	
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==alerttime) 
    {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        //alert("You have just wasted 10 seconds of your life.")
		var d = new Date();
        var curr_hour = d.getHours();
        var curr_min = d.getMinutes();
        var curr_sec = d.getSeconds();
        var curre=curr_hour*60*60+curr_min*60+curr_sec;

		if(confirm('Your session will expire in 5 minutes.\n Click "OK" to continue working.'))
		  {
		     var d1 = new Date();
             var curr_hour1 = d1.getHours();
             var curr_min1 = d1.getMinutes();
             var curr_sec1 = d1.getSeconds();
             var curre1=curr_hour1*60*60+curr_min1*60+curr_sec1;
			 if(curre1-curre>alerttime-5)
	           { 
	                document.location.href=serverpath+'include/destroysession.php';
	           }
			 else
			   {			  
		   			//location.reload(true);
					InitializeTimer();
					window.open(serverpath+'include/sessionrenew.php','','height=100,width=100');
					
			   }
		  }
		else
		  {
		        self.status = secs
		        secs = secs - 1
		        timerRunning = true
        		timerID = self.setTimeout("StartTheTimer()", delay)
			  
             var d1 = new Date();
             var curr_hour1 = d1.getHours();
             var curr_min1 = d1.getMinutes();
             var curr_sec1 = d1.getSeconds();
             var curre1=curr_hour1*60*60+curr_min1*60+curr_sec1;
			 
			 if(curre1-curre>alerttime-5) 
	           {
	                document.location.href=serverpath+'include/destroysession.php';
	           }
			/* elseif(curre1-curre<0)
	           {
	                document.location.href='../index.php?err=01';
	           }*/
		  }
		
    }
	else if (secs==0)
    { 
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        //alert("You have just wasted 10 seconds of your life.")		
		   document.location.href=serverpath+'include/destroysession.php';
		
    }
    else
    {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}
