if(!document.hasDefinedTimeout){
<!--#if expr="$to_warn_millis=''" -->
var to_warn_millis=24*60*1000; //24 mins timeout before displaying warning window
<!--#else -->
var to_warn_millis=<!--#echo var="to_warn_millis" -->; //from apache environment variable!
<!--#endif -->
<!--#if expr="$to_close_secs=''" -->
var to_close_secs=5*60; //5 mins timeout to respond to the warning window
<!--#else -->
var to_close_secs=<!--#echo var="to_close_secs" -->; //from apache environment variable!
<!--#endif -->
var orig_to_close_secs = to_close_secs;
var t1=null;var t2=null;var ww=null;
var width=620;var height=320;

var lastFocus = null;
var executeOldFocus = true;
var timeoutDiv = document.getElementById('timeoutDiv');


function getFocusElement(){
	return document.getElementById("timeoutFocus");
}

function getPingElement(){
	return document.getElementById("timeoutImg");
}

function rememberFocus(){
	if(this==getFocusElement()){
		return;
	}
	lastFocus = this;
	if(this.onblurOld){
		this.onblurOld();
	}
}


function setFocus(){
}


function clearTimers()
{
	if(t2 != null)clearTimeout(t2);
	if(t1 != null)clearInterval(t1);
	to_close_secs = orig_to_close_secs;
	t1=null;t2=null;
}

function TimeTrack(){
	clearTimers(); // clear timers, hideDiv(false) doesn't ping server but also doesn't clear timers...
	hideDiv(false); // do not ping server, it is done by dynacall already
}	


function countdown(){
	if(to_close_secs>0){
		setCountdownText();
		window.focus();
	} else {
		clearTimers();
		logoutForce('?timeout=1'); 
	}
}


function logout(){
	wireInputs();
	var autoExtend = (document.autoExtendSession && document.autoExtendSession > 0);
	if( autoExtend ){
		if(!document.autoExtendSessionOrig){
			document.autoExtendSessionOrig = document.autoExtendSession;
		}
		document.autoExtendSession = document.autoExtendSession - 1;
	}
	showDiv(autoExtend);
	if(autoExtend){
		clearTimers();
		pingServer();
		t1=setTimeout("logout()",to_warn_millis);
	} else {
		t2=setInterval("countdown()",1000);
	}
}

function logoutForce(str)
{
	clearTimers();
	if(!str){
		str="";
	}	
	logAction("logoutForce");
	
	// allow a hook to clean things up before logging off.
	if( typeof(window.timeoutCleanupCallback) != 'undefined'  ) {
		window.timeoutCleanupCallback();
	}
	
	window.location="/home/logoff.do"+str;
}

function pingServer(){
	logAction("pingServer");
}


function setCountdownText(){
		if(to_close_secs>60){
			document.getElementById("timeoutCountdown").innerText = (Math.floor(to_close_secs/60)+1) + " MINUTE(S)";
		} else {
			document.getElementById("timeoutCountdown").innerText = to_close_secs + " SECOND(S)";
		}
		to_close_secs--;
}

function logAction(action){
	var time = new Date();
	getPingElement().src="/home/img.do?action="+action+"&url=/images/empty.gif&time="+time;
}

function showDiv(justWarning){
	if(justWarning){
		logAction("showDivAutoExtend");
		document.getElementById('toJW').style.display="block";
		document.getElementById('toCW').style.display="none";
	} else {
		logAction("showDivCountdown");
		document.getElementById('toJW').style.display="none";
		document.getElementById('toCW').style.display="block";
	}
	timeoutDiv.style.display="block";
	setCountdownText();
	window.focus();
	try{
		getFocusElement().focus();
	} catch (e) {
	}
}

function hideDiv(ping){
	if(ping){
		if(document.autoExtendSessionOrig){
			document.autoExtendSession = document.autoExtendSessionOrig;
		}
		clearTimers();
		pingServer();
	}
	timeoutDiv.style.display="none";
	t1=setTimeout("logout()",to_warn_millis);
	if(lastFocus && lastFocus!=getFocusElement()){
		executeOldFocus = false;
		try{
			lastFocus.focus();
		} catch(e){}
		executeOldFocus = true;
	}
}


var inputsWired = false;
function wireInputs(){
	if(inputsWired) return;

	var focusElement = getFocusElement();
	focusElement.skip = true;
	var Inputs = new Array('INPUT','TEXTAREA','SELECT');
	for(var i=0; i<Inputs.length; i++){
		var elems = document.getElementsByTagName(Inputs[i]);
		for(var j=0; j<elems.length; j++){
			if(elems[j]!=focusElement && !elems[j].wired && !elems[j].skip ){
				elems[j].onblurOld=elems[j].onblur;
				elems[j].onblur=rememberFocus;
				elems[j].wired=true;
			}
		}
	}	
	inputsWired = true;
}

function setTime(time_out){
to_warn_millis = time_out; 
}

function restartTimeout(){
	TimeTrack();
	// do not wire it out again...
	document.hasDefinedTimeout = true;
}


TimeTrack();

// do not wire it out again...
document.hasDefinedTimeout = true;

}

