var secs
var timerID = null
var timerRunning = false
var delay = 1000
var applicationName = "";
var timeExpired
var currentTime

function InitializeTimer(appName){
    // Set the length of the timer, in seconds
    secs = 900;
    StopTheClock();
    applicationName = appName;	
    StartTheTimer();
}

function StopTheClock(){
    if(timerRunning){
        clearTimeout(timerID);
    }	
    timerRunning = false;
}

function StartTheTimer(){

    if (secs==0){
     displayTimer(secs);
        StopTheClock();
        sessionTimerExpired();
	var jsConfirm = null;
        if (typeof(jsSaveCurrentActivity) == "function"){
	    //this message will come out from a page that has form data that needs to be saved.
            jsConfirm = confirm("When this alert appears you have 5 minutes to save your information before you are logged out and the information you entered is cleared. Click OK within the 5 minute window to continue, otherwise you will lose your information.");
        }else if (typeof(jsContinueServices) == "function"){
            //this message will come out when jsContinueServices function exists in the page
	    jsConfirm = confirm("When this alert appears you have 5 minutes to save your information before you are logged out and the information you entered is cleared. Click OK within the 5 minute window to continue, otherwise you will lose your information.");
        }else{
	    //this message will come out from a page with no form data that needs to be saved.
            jsConfirm = confirm("When this alert appears you have 5 minutes to save your information before you are logged out and the information you entered is cleared. Click OK within the 5 minute window to continue, otherwise you will lose your information.");
	}

        if (jsConfirm){//if "ok" button was clicked
           currentTime = new Date();
           if (currentTime.getTime() > timeExpired.getTime()){
 	     window.location = "/"+applicationName+"/housing/indexHousingUnit.do?logoff=true";
           }else{   
	      if (typeof(jsSaveCurrentActivity) == "function"){
        	jsSaveCurrentActivity();// this will save the current transaction (form data)
              }else if (typeof(jsContinueServices) == "function"){        	
                jsContinueServices();
	      }else{
		location.reload();//this will refresh the page (page that needs no saving)
	      }
           }
	}else{
	   window.location = "/"+applicationName+"/housing/indexHousingUnit.do?logoff=true";// if "cancel" button was clicked
        }

    }else{
    displayTimer(secs);
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout("StartTheTimer()", delay);
    }
}


function displayTimer(sSec){
		min=""+(secs/60);
     	if(min.indexOf(".")!=-1){
     		min=min.substring(0,min.indexOf("."));
     	}
     	if(min.length==1){
			min="0"+min;     	
     	}
     	secsRemain=""+(secs % 60);
     	if(secsRemain.length==1){
			secsRemain="0"+secsRemain;     	
     	}
     	window.status ="Session Timer: 00:"+ min +":"+secsRemain;
}

function sessionTimerExpired(){

timeExpired = new Date();
var curr_minutes = timeExpired.getMinutes()+5;// added 5 minutes from the time the comfirm message show
                                              // for the 5 minutes remaining for the user to response
timeExpired.setMinutes(curr_minutes);         

}

