
// The following code sets up a timeout alert and a timeout redirect.

// Insert the following code into the HTML of a page to disable these timeouts: 
// (e.g. index.aspx)

// <!--
// 	<SCRIPT Language="javascript">
// 		clearTimeout(idTimer);
// 		clearTimeout(idAlert);
// 	</SCRIPT>
// -->

var idTimer = '';
var idAlert = '';
var dateNow = '';
var strHour = '';
var strMinute = '';
var strTime = '';
var strAlertMessage = '', strConfirmMessage = '';

// time limits in minutes
var numTimeoutMinutes = 120;
var numAlertMinutes =  110;

// time limits in milliseconds
var intTimerDelay = (numTimeoutMinutes * 60 * 1000);
var intAlertDelay = (numAlertMinutes * 60 * 1000);

// where to go if the session does time out
var constTimeoutDestination = 'http://www.southlandtube.com/index.aspx?SessionTimedOut';
// var constTimeoutDestination = 'http://localhost/www-southlandtube-com/index.aspx?SessionTimedOut';

var boolAlertDisplayed = false;


function ResetTimer() 
    {
    // This function will not normally need to be called, since 
    //   the user can cause a reset by forcing a page reload.
    clearTimeout(idTimer);                                          // reset the timer
    clearTimeout(idAlert);                                          // reset the alert
    idTimer = setTimeout('ExecuteTimer()',intTimerDelay); 
    idAlert = setTimeout('ExecuteAlert()',intAlertDelay);
    boolAlertDisplayed = false;
    }


function ExecuteTimer() 
    {
    // Timer will redirect to the following page
    if( (new Date()).getHours() >= strHour && (new Date()).getMinutes() >= parseInt(strMinute) )
        location.href = constTimeoutDestination; 
    }


function ExecuteAlert() 
    {
	if( ! boolAlertDisplayed )                                      // display timeout alert; timeout will occur 'numAlertMinutes' minutes thereafter.
	    {
		// Get the current hour and minute
		dateNow = new Date(); 
		strHour = dateNow.getHours();
		strMinute = dateNow.getMinutes();

		// Calculate the hour and minute that the timeout will occur.
        // These calculations are provided for "short" timeout periods (i.e., less than 1 hr.)
		if( parseInt(dateNow.getMinutes()) + numAlertMinutes >= 60 )
		    {
			strHour = dateNow.getHours() + 1;
			strMinute = dateNow.getMinutes() + numAlertMinutes - 60;
		    }
		else
		    {
			strMinute = dateNow.getMinutes() + numAlertMinutes;
		    }

		if( parseInt(strMinute) < 10 )
		    {
			strMinute = "0" + strMinute;
		    }

		strTime = strHour + ":" + strMinute;                        // assemble the time

		// Assemble the timeout notice.
		strAlertMessage = 'PLEASE NOTE\nTo conserve server resources, your session will be automatically terminated after 120 minutes of inactivity.\nYour current session has been idle for the past 110 minutes and will be ended at the time shown below if it remains idle.\nIf this occurs, any information entered or changed on this page will not be saved.\n\n\n\nTo keep your session active: \n\t- Edit information on this page (if applicable) OR\n\t- Refresh the current page, or navigate to another page. \n\nSession timeout will occur at ' + strTime + '.';

		strConfirmMessage = 'PLEASE NOTE\nTo conserve server resources, your session will be automatically terminated after 120 minutes of inactivity.\nYour current session has been idle for the past 110 minutes and will be ended at the time shown below if it remains idle.\nIf this occurs, any information entered or changed on this page will not be saved.\n\nTo keep your session active, click the "OK" button below.\n\nSession timeout will occur at ' + strTime + '.';

		boolAlertDisplayed = true;                                  // 'message displayed' flag
		// alert(strAlertMessage);                                  // display the alert as an 'alert'
		if( confirm(strConfirmMessage) )                            // display the alert as a 'confirm'
            {
            if( (new Date()).getHours() <= strHour && (new Date()).getMinutes() <= parseInt(strMinute) )
                window.location.reload(true);                       // force a reload from the server
            else
                location.href = constTimeoutDestination; 
            }
	    }
    }

// Schedule the timeout & message display.
idTimer = setTimeout('ExecuteTimer()', intTimerDelay);
idAlert = setTimeout('ExecuteAlert()', intAlertDelay);


