﻿var g_sessionTimoutWatcher = null;
//--------------------------------------------------------------
function OnExpiring()
{
	g_sessionTimoutWatcher.Expire();
}
//--------------------------------------------------------------
function OnPolling()
{
	g_sessionTimoutWatcher.Poll();
}
//--------------------------------------------------------------
function OnWarning()
{
	g_sessionTimoutWatcher.Warn();
}
////////////////////////////////////////////////////
//Class SessionTimeoutWatcher
function SessionTimeoutWatcher(timout,pollTime,warningTimout,redirectUrl,labelId,warningText,formatText,expireText,postbackFunction)
{
	g_sessionTimoutWatcher = this;	
	this.formatText =formatText;
	this.warningText = warningText;
	this.redirectUrl = redirectUrl;
	this.labelId = labelId;
	this.timeout = timout;
	this.pollTime = pollTime;
	this.remaingTime = this.timeout;
	this.warningTimout = warningTimout;
	this.warnTimerId = window.setTimeout("OnWarning()",(timout-warningTimout)*60000);
	window.setTimeout("OnExpiring()",timout*60000);
	this.pollTimerId = window.setInterval("OnPolling()",pollTime*60000);
	this.expireText = expireText;
	this.Expire = SessionTimeoutWatcher_Expire;
	this.Poll = SessionTimeoutWatcher_Poll;
	this.Warn = SessionTimeoutWatcher_Warn;
	this.SetText = SessionTimeoutWatcher_SetText;
	this.postbackFunction =postbackFunction;
	
	this.SetText(this.remaingTime);
}
//--------------------------------------------------------------
function SessionTimeoutWatcher_SetText(text)
{
	var label = document.getElementById(this.labelId);
	if(label)
	{
		if(this.formatText.length > 0)
		{
			var re = /@0/g;
			label.innerHTML = this.formatText.replace(re,text);
		}
		else
		{
			label.innerHTML = text;
		}
	}
}
//--------------------------------------------------------------
function SessionTimeoutWatcher_Expire()
{
	window.clearInterval(this.pollTimerId);
	alert(this.expireText);
	window.location.href = this.redirectUrl;
}
//--------------------------------------------------------------
function SessionTimeoutWatcher_Poll()
{
	this.remaingTime -=this.pollTime;
	this.SetText(this.remaingTime);
}
//--------------------------------------------------------------
function SessionTimeoutWatcher_Warn()
{
	var expireDate = new Date();
	expireDate.setMinutes(expireDate.getMinutes()+ this.warningTimout);
	
	var result = window.confirm(this.warningText);
	if(result &&  new Date() < expireDate )
	{
		window.clearInterval(this.pollTimerId);
		eval(this.postbackFunction);
	}
	else if(new Date() >= expireDate)
	{
		this.Expire();
	}	
}