
var remTimeOut;
var remTimeOut2;
var blnTimeOutStopped;
var thisTimeText1;
var thisTimeText2;

var intSetTimeHour
var intSetTimeMinute
var intSetTimeSeconds

var timer;
var timer2;

var blnDONOTUSETIMEOUT;
	blnDONOTUSETIMEOUT = false;	
		
	StartClock();

	// Get the Div Name/ID from Pageheader
	var follower = xbGetElementById("divTimeOut");
	var followerBG = xbGetElementById("divTimeOutBG");
	
	// Make sure we're able to see the div when it shows
	window.onscroll = do_follow;
	window.onresize = do_follow;

	function do_follow() {
		//divTimeOutBG.style.top = xScrollTop('',this);
		//divTimeOutBG.style.top = document.body.scrollTop;
		
		var el = xGetElementById("divTimeOutBG");
		var el2 = xGetElementById("body");
		var el3 = xGetElementById("childbody");
		if(el != null && typeof(el) != 'undefined') {
			if(el2 != null && typeof(el2) != 'undefined') {
				xTop(el,xScrollTop(el2));
			} else {
				if(el3 != null && typeof(el3) != 'undefined') {
					xTop(el,xScrollTop(el3));
				}
			}
			
		}
	}
	
	function restartTimeOut() {
		if (typeof(intSetTimeHour) !=  'undefined' && typeof(intSetTimeMinute) != 'undefined' && typeof(intSetTimeSeconds) != 'undefined' ) {
			setRemainingTimeBeforeTimeout(intSetTimeHour, intSetTimeMinute, intSetTimeSeconds);
		}
	}
			
	function setRemainingTimeBeforeTimeout(varHour, varMin, varSec)
	{
				
      if(typeof(timer) != 'undefined') {
		//KeepSessionActive();	
		clearTimeout(timer);
      }
      
      StartClock();
      
	  intSetTimeHour = varHour;
	  intSetTimeMinute = varMin;
	  intSetTimeSeconds = varSec;
	  remTimeOut = new Date(00, 00, 00,varHour,varMin,varSec, 00);
	  startCountdownForTimeOut();
	  
		 //capture keypresses as well as page loads
	  
		if (navigator.appName == 'Netscape') {
		    window.captureEvents(Event.KEYPRESS);
		    window.onKeyPress = netscapeKeyPress;
		}	  
	}

	function netscapeKeyPress(e) {
	    setRemainingTimeBeforeTimeout(intSetTimeHour, intSetTimeMinute, intSetTimeSeconds)
	}

	function captureKeyPress() {
		setRemainingTimeBeforeTimeout(intSetTimeHour, intSetTimeMinute, intSetTimeSeconds)
	}

	// recursive call to
	function startCountdownForTimeOut()
	{
	  zeroDate = new Date(00,00,00,00,00,00);
					
	  if((remTimeOut - 1000) >= zeroDate){
	    var varNewRemTimetimeout;
	    var H;
	    var M;
	    var S;

	    //decrement remaining time by 1000ms
	    if(!blnTimeOutStopped) {
			remTimeOut = new Date(remTimeOut - 1000);
		}
		
		H = remTimeOut.getHours();
		M = (remTimeOut.getMinutes() > 9)? remTimeOut.getMinutes(): "0" + remTimeOut.getMinutes();
		S = (remTimeOut.getSeconds() > 9)? remTimeOut.getSeconds(): "0" + remTimeOut.getSeconds();

		timer = setTimeout('startCountdownForTimeOut()',1000);
	  }
	  else
	  {
		// call the Preliminary Logout Script (2 min warning)
		Logout(); 
	  }
	}

function Logout()
{
	// set another 2 min countdown so we automatically logoff if we don't answer
	
	setRemainingTimeBeforeTimeout2(0,2,00);
	
	StartClock();
	var thisTimeOut;

	
	// Get the hidden div to display
	thisTimeOut = xbGetElementById("divTimeOut");
	thisTimeOutBG = xbGetElementById("divTimeOutBG");

	// Make the Hidden Div Visible ...
	thisTimeOutBG.style.visibility = "visible";
	thisTimeOut.style.visibility = "visible";
}

function LogoutSession() {
	forceLogout();
}

function KeepSessionActive() {
	var thisSession
	var thisHTML
	var thisTimeOut;
	var thisKeepSessionActive
	
	StopClock();
	
	thisKeepSessionActive = xbGetElementById("keepSessionActive");
	thisKeepSessionActive.innerHTML = "";	
	
	thisKeepSessionActive.innerHTML = "<iframe frameborder='0' marginwidth='0' marginheight='0' border='1' width='1' height='1' src='/script/KeepSessionActive.asp'></iframe>";

		
	if(thisTimeOutBG = xbGetElementById("divTimeOutBG")){
		thisTimeOutBG.style.visibility = "hidden";
	}
	
	if(thisTimeOut = xbGetElementById("divTimeOut")){
		thisTimeOut.style.visibility = "hidden";
	}

	//restart the global page timer
	//Stop Timer2
      if(typeof(timer2) != 'undefined') {
		clearTimeout(timer2);
      }
      
	// alert(intSetTimeHour + ":" + intSetTimeMinute + ":" +  intSetTimeSeconds);
	setRemainingTimeBeforeTimeout(intSetTimeHour, intSetTimeMinute, intSetTimeSeconds)
	
}



function StopClock() {
	blnTimeOutStopped = true;
}
function StartClock() {
	blnTimeOutStopped = false;
}

function setRemainingTimeBeforeTimeout2(varHour, varMin, varSec)
{
      if(typeof(timer2) != 'undefined') {
		clearTimeout(timer2);
      }
	remTimeOut2 = new Date(00, 00, 00,varHour,varMin,varSec, 00);
	StartClock();
	startCountdownForTimeOut2();
}
  
function forceLogout() {
		// Proper Logout
		if (opener != null && typeof(opener) != 'undefined') {
			// we have a parent window - force the parent to logout
			opener.forceLogout();
			// close ourselves
			window.close();
			
		} else {
			window.location.href = "/script/logout.asp";
		}
}
  
function timerExists(){
	return true;
}  
  
function startCountdownForTimeOut2()
{
  zeroDate = new Date(00,00,00,00,00,00);

	if((remTimeOut2 - 1000) >= zeroDate){
	  var varNewRemTimetimeout;
	  var H;
	  var M;
	  var S;

		remTimeOut2 = new Date(remTimeOut2 - 1000);
		H = remTimeOut2.getHours();
		M = (remTimeOut2.getMinutes() > 9)? remTimeOut2.getMinutes(): "0" + remTimeOut2.getMinutes();
		S = (remTimeOut2.getSeconds() > 9)? remTimeOut2.getSeconds(): "0" + remTimeOut2.getSeconds();

		varNewRemTimetimeout =  M + ":" + S;

		//thisTimeText1 = xbGetElementById("countdowntimeText1");
		//if(thisTimeText1 != typeof "undefined") {
		//	thisTimeText1.innerHTML = "Automatic Logout in: " + varNewRemTimetimeout;
		//}
		
		thisTimeText2 =  xbGetElementById("countdowntimeText2");
	
		if(thisTimeText2 != null) {
			if(typeof(thisTimeText2) != 'undefined') {
				thisTimeText2.innerHTML = "Automatic Logout in: " + varNewRemTimetimeout;	
			}
		} else {
				thisTimeText2 = document.getElementById('timeoutcontent').contentWindow.document.getElementById('countdowntimeText2')
				if(typeof(thisTimeText2) != 'undefined' && thisTimeText2 != null) {
					thisTimeText2.innerHTML = "Automatic Logout in: " + varNewRemTimetimeout;	
				}
				
		}
		
		if (!blnTimeOutStopped) {
			timer2 = setTimeout('startCountdownForTimeOut2()',1000);
		}
	} else {
		LogoutSession();
	}
}
