var timeoutVal = 0;
var minutesTimeout = 19;
var tOut = -1;
//document.onmousedown = new Function("timeoutVal = 0; return true;");

// ##### Check if in IFrame #####
if (document.location.href.indexOf('iframe=true') == -1)
{
    var strURL = top.location.href;
    strURL = strURL.substring(strURL.lastIndexOf('/')+1, strURL.length); 
    strURL = strURL.substring(0, strURL.indexOf('.')); 
    //alert(strURL);

    if ( (strURL.substring(0, 8) != 'frmLogin') && (strURL != '') )
    {
        InitTimer();
    }
}

function InitTimer ()
{
	//alert('inittimer');
	
	try
	{
	    //alert('clear: ' + tOut);
		//clearTimeout(tOut);
		timeoutVal = 0;
		//alert('reset timeoutval');
	}
	catch(err)
	{
		//alert('no timer');
	}
	
	try
    {
        //alert('opener = ' + opener);
    	if (opener)
		{
		    opener.InitTimer();
		}
    }
    catch(err)
    {
    }
	
	if (tOut == -1)
	{
	    tOut = setTimeout('countDown()', 1000);
	    //alert('new: ' + tOut);
	}
	
	window.status = (minutesTimeout - ((timeoutVal-1) / 60).toFixed(0)) + " minutes remaining to Logout.";
}

function countDown ()
{
	timeoutVal++;

	if (timeoutVal > (minutesTimeout * 60))
	{
	    var strURL = top.location.href;
	    strURL = strURL.substring(strURL.lastIndexOf('/')+1, strURL.length); 
	    strURL = strURL.replace("?", "&");
	    //alert("frmLoginSessionTimeout.aspx?redirect=" + strURL);
		top.location.href = "frmLoginSessionTimeout.aspx?redirect=" + strURL + '&jstimeout=true';
		//top.location.href = unescape(top.location.pathname + '&jstimeout=true');
	}
	else
	{
		tOut = setTimeout('countDown()', 1000);
		//alert(tOut);
		if (timeoutVal > ((minutesTimeout-1) * 60))
		{
			window.status = ((minutesTimeout*60) - (timeoutVal-1) ) + " seconds remaining to Logout.";
		}
		else
		{
			if ((timeoutVal % 60) == 1)
			{
				window.status = (minutesTimeout - ((timeoutVal-1) / 60).toFixed(0)) + " minutes remaining to Logout.";
			}
		}
	}

	if (timeoutVal == ((minutesTimeout-1) * 60))
	{
		TimeoutWindow();
	}
}

function TimeoutWindow() 
{ 
	var TimeWin = open('','Timeout','DEPENDENT=YES,RESIZEABLE=YES,ALWAYSRAISED=YES,TOOLBAR=NO,TITLEBAR=NO,LOCATION=NO,MENUBAR=NO,SCROLLBARS=NO,HEIGHT=150,WIDTH=200'); 
	TimeWin.document.writeln('<html>'); 
	TimeWin.document.writeln('<head>'); 
	TimeWin.document.writeln('<title>ConnX</title>'); 
	TimeWin.document.writeln('<link rel=stylesheet type=text/css href="StyleSheets/Styles.CSS">'); 
	TimeWin.document.writeln('</head>'); 
	TimeWin.document.writeln('<body>'); 
	TimeWin.document.writeln('<center>'); 
	TimeWin.document.writeln('ConnX will timeout in one minute unless you continue your activity.'); 
	TimeWin.document.writeln('<br><br><br><input type=button value=OK onClick=window.close()>'); 
	TimeWin.document.writeln('</center>'); 
	TimeWin.document.writeln('<script>setTimeout("window.close();", 60000);</script>'); 
	TimeWin.document.writeln('</body>'); 
	TimeWin.document.writeln('</html>'); 
	TimeWin.document.close(); 
	TimeWin.focus(); 
} 

