﻿var NN4 = (document.layers) ? true : false;
var IE4 = (document.layers) ? false : true;

// The timeout remainder should be set 30 seconds
// before the real time out (10 minutes).
var timeoutLimit = 9.5 ; // 9.5 minutes - TIMEOUT

timeoutLimit *= 60 * 1000; // convert to miliseconds

var lastEvent;

function display_dialog_box() {
	var date = new Date();
	var date2;
	var hours = convertTimeDisplay(date.getHours());
	var minutes = convertTimeDisplay(date.getMinutes());
	var seconds = convertTimeDisplay(date.getSeconds());

	if(confirm("Dear MOL Member:\nYour session will expire in 30 seconds starting from "+hours+":"+minutes+":"+seconds+".\nPress OK to continue, or Cancel to logout.")) {
		date2 = new Date();
		var timeAfterResponse = date2.getTime();
		lastEvent = document.getElementById("hidlastEvent").value;
		if((parseInt(timeAfterResponse) - parseInt(lastEvent)) >= (timeoutLimit+30000)) {
			go_to_logout();
			setTimeout("go_to_logout()",4000);
		}
		var strCurrentURL = window.location.href;
		var strNewURL = window.location.href;
		strNewURL = Mid(strCurrentURL, 0, InStr(strCurrentURL, '#'));
		if (strNewURL == "")
		{
		    strNewURL = strCurrentURL;
		}
		window.location.href = strNewURL;
	} else {
		date2 = new Date();
		var timeAfterResponse = date2.getTime();
		lastEvent = document.getElementById("hidlastEvent").value;
		if((parseInt(timeAfterResponse) - parseInt(lastEvent)) >= (timeoutLimit+30000)) {
			go_to_logout();
			setTimeout("go_to_logout()",4000);
		}
		go_to_logout();
	}
}

function check_for_timeout() {
	var date = new Date();
	var currentTime = date.getTime();

	lastEvent = document.getElementById("hidlastEvent").value;
	if((parseInt(currentTime) - parseInt(lastEvent)) < timeoutLimit) {
		setTimeout('check_for_timeout()', 10000);
	} else {
		display_dialog_box();
	}

}

function convertTimeDisplay(num) {
	if (num < 10)
		return new String("0"+num);
	else
		return new String(num);
}

function updateLastEvent() {
	var date = new Date();
	var time = date.getTime();
	if(document.getElementById("hidlastEvent") != null)
	{
		document.getElementById("hidlastEvent").value = time;
	}	
    //alert(document.getElementById("hidlastEvent").value);
	setTimeout('check_for_timeout()', 10000);
	
}


//-----------------------------------

function Left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function Mid(str, start, len)
/***
        IN: str - the string we are LEFTing
            start - our string's starting position (0 based!!)
            len - how many characters from start we want to get

        RETVAL: The substring from start to start+len
***/
{
        // Make sure start and len are within proper bounds
        if (start < 0 || len < 0) return "";

        var iEnd, iLen = String(str).length;
        if (start + len > iLen)
                iEnd = iLen;
        else
                iEnd = start + len;

        return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}

function Len(str)
/***
        IN: str - the string whose length we are interested in

        RETVAL: The number of characters in the string
***/
{  return String(str).length;  }