// JavaScript Document
// Session extension/kill script file

// Page timer and session expiration date/time
var objTimer = null;
var dtSessionExpires = null;
var nSessionTime = 60*60*1000;
var nWarning = 2*60*1000;

function startTimer()
{
	// The user's session expires after 30 minutes on the same page, warn the user @ the 2 min mark.
	//alert("timer started");
	objTimer = setTimeout("extendSession();",(nSessionTime - nWarning));
}

function extendSession()
{
	var strMsg = "Session about to go poof @ " + getSessionExpiry() + "!!! Do something.\n\n";
	
	// Pop up an ok-cancel dialog with the message
	if( confirm( strMsg ) )
	{
		// If the user chooses to stay, see if they did it in time
		var now = new Date();
		if( dtSessionExpires > now )
		{
			// If there's still time to save their session, keep the page open.
			// This causes the page to be reloaded - so users filling out long forms
			// are going to be upset
			location.replace( window.top.location.href );
		}
		else
		{
			// Send them back to the logon page
			//location.replace( "" );
		}
	}
	else
	{
		// If they choose to leave
		var now = new Date();
		if( dtSessionExpires > now )
		{
			// force them to logoff, killing their session since it's currently still active
		}
		else
		{
			// Send them back to the logon page
			//location.replace( "" );
		}
	}
}

function getSessionExpiry()
{
	// Session expires @ now + warning
	var now = new Date();
	// Get's the time in 24-hr format
	dtSessionExpires = new Date( Date.parse(now) + nWarning );
	// Build a string with the expiry time
	var hours = dtSessionExpires.getHours();
	var mins = dtSessionExpires.getMinutes();
	var ampm = "am";
	
	// Set am/pm indicator
	if( hours > 12 )
	{
		ampm = "pm";
	}
	// Convert from 24-hr clock to 12-hr clock
	if( hours > 12 )
	{
		hours = hours - 12;
	}
	// Add a leading 0 to minute values less than 10
	if( mins < 10 )
	{
		mins = "0" + mins;	
	}
	return hours + ":" + mins + " " + ampm;
}