﻿var Timer;
Timer = '20:00';

// this function is called every time a page is loaded
function timeIt() 
{
	// set the second and minute variables to 20 minutes and zero seconds initially
	min = Minutes(Timer);
	sec = Seconds(Timer);
	// call the Down function to monitor the time remaining and display a javascript alert
	// when there are three minutes before the session will expire 
	Down();
}

// this function keeps a count of minutes and seconds, and calls itself recursively
// every 1 second (1000 milliseconds), and once there are three minutes left before
// the session timeout will occur, it displays a javascript alert
function Down()
{ 
	// bump the second counter down by 1
	sec--;
	// if the seconds counter is -1 (we need to bump the minute counter down by 1)...
	if (sec == -1) 
	{ 
		// set the seconds counter to 59 and bump the minute counter down by 1
		sec = 59; 
		min--; 
	}
	Timer = Display(min, sec);
	// if there are three minutes left until the session times out...
	if (min == 3 && sec == 0) 
	{
		// alert the user that there are three minutes left until the session times out
		alert("Your session will time out in 3 minutes due to inactivity.");
	}
	// call this function recursively in one second (1000 milliseconds)
	else down = setTimeout("Down()", 1000);
}

function Display(min, sec) 
{
	var varDisplay;

	// if the minute counter is less than or equal to nine...
	if (min <= 9) 
	{
		// set the display counter to 0 (for a leading zero since the minutes are less than ten)
		varDisplay = " 0";
	}
	// if the minute counter is greater than nine...
	else 
	{
		// clear the display counter (no need to add a leading zero - the minute counter is at least ten)
		varDisplay = " ";
	}

	// append a semicolon to the minute counter
	varDisplay = varDisplay + min + ":";

	// if the second counter is less than or equal to nine...
	if (sec <= 9) 
	{
		// append the seconds (with a leading zero) to the display counter
		varDisplay += "0" + sec;
	}
	// if the second counter is greater than nine...
	else 
	{
		// append the seconds (without a leading zero) to the display counter
		varDisplay += sec; 
	}

	// return the time calculated above to the calling function
	return (varDisplay);
}

// this function is called from the timeIt function (called once each time a page is loaded)
// this function loops through a time (mm:ss format) handed in and returns the minutes (mm) of the string
function Minutes(data) 
{
	// loop through the string handed in ("20:00")
	for (var i = 0; i < data.length; i++)
	{
		// if the current character in the string handed in is a colon...
		if (data.substring(i, i + 1) == ":")
		{
			// exit the loop
			break;
		}
	}
	
	// return the string UP TO the colon (i.e. if 20:00 was handed in, return 20)
	return (data.substring(0, i));
}

// this function is called from the timeIt function (called once each time a page is loaded)
// this function loops through a time (mm:ss format) handed in and returns the seconds (ss) of the string
function Seconds(data) 
{
	// loop through the string handed in ("20:00")
	for (var i = 0; i < data.length; i++)
	{
		// if the current character in the string handed in is a colon...
		if (data.substring(i, i + 1) == ":")
		{
			// exit the loop
			break;
		}
	}

	// return the string AFTER the colon (i.e. if 20:00 was handed in, return 00)
	return (data.substring(i + 1, data.length));
}

