var timerID;
var timerID2;
var timeout;
var loggedIn;
var secondsToWarn;
var warningWindow;
var cancelRefresh = false;
var siteName;
var keypress = false;

document.onkeypress = function kPress(e){keypress = true;}
document.onclick = function kPress(e){keypress = true;}

function StartUp(strTimeout, strLoggedIn, strSecondsToWarn, strSiteName)
{
	SetTimeout (strTimeout);
	loggedIn = eval(strLoggedIn);
	secondsToWarn = eval(strSecondsToWarn);
	
	siteName = strSiteName;
	
	if (loggedIn)
	{
		timerID = window.setInterval("CheckLogoutTime();", 5000);		
		timerID2 = window.setInterval("CheckKeyPress();", 10000);
	}
}

function CheckKeyPress()
{
	if(keypress)
	{
		keypress = false;
		cancelRefresh = true;
		RefreshSession();
	}
}

function CheckLogoutTime ()
{
	if (!cancelRefresh)
	{			
		var timeRemaining = (timeout - new Date ())/1000;		
		
		if (loggedIn &&	timeRemaining > 0 && timeRemaining < secondsToWarn)
		{	
			if (warningWindow == null || warningWindow.closed)
			{			
				var pagePath = "/global/components/desktop/libraries/Web.Components.Form/logout_notice/logout_notice.aspx";	
				if (window.location.toString ().indexOf('localhost') >= 0)
					pagePath = '/intranet' + pagePath;
								
				var height = 200;
				var width = 300;				
				
				warningWindow = window.open(pagePath,'',"height="+height+",width="+width);					
				
				// popup blocker
				if(warningWindow == null)
				{
					showWindow();								
				}
				else
				{
					warningWindow.opener = self;
					warningWindow.window.focus();					
				}
			}			
		}
	}
//	else if (warningWindow != null && warningWindow.closed)
//	{
//		window.clearInterval (timerID);		
//	}
}

//child window can't set a reference variable because after it closes the reference will be gone
//use this to ensure the value of timeout even if the child window closes
function SetTimeout (strTimeout)
{			
	var now = new Date();
	now.setTime(now.getTime() + (parseInt(strTimeout) * 60000));
	timeout = now;
}

function disableForm()
{
	document.forms[0].disabled = true;
	document.body.style.opacity = 0.5;
	document.body.style.filter = "alpha(opacity=50)";
}

function enableForm()
{
	document.forms[0].disabled = false;
	document.body.style.opacity = 1;
	// This filter will distort the font/style of the page
	if(document.body.style.filter != "")
		document.body.style.filter = "alpha(opacity=100)";	
}

function RefreshSession ()
{
	var objHTTP = getHTTPObject();			
	var resultText;

	var sessionRefreshURL = "/global/components/desktop/libraries/Web.Components.Form/session_refresh/session_refresh.aspx";
	if (window.location.toString ().indexOf('localhost') >= 0)
		sessionRefreshURL = '/intranet' + sessionRefreshURL;
	
	objHTTP.open('POST',sessionRefreshURL,false);				
	objHTTP.send('');
	
	if (objHTTP.readyState == 4) {
		if(objHTTP.status == 200)
		{
			resultText = objHTTP.responseText;
			SetTimeout(resultText);
			cancelRefresh = false;
		}
		else
		{			
			alert(objHTTP.statusText);
		}		
	}
	enableForm();
}

function getHTTPObject() 
{
	var xmlhttp;
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

function showWindow()
{
	disableForm();
	
	warningWindow = document.getElementById('logoutwindow');
	warningWindow.style.display = 'inline';	
	warningWindow.style.pixelTop = document.body.scrollTop;				
	warningWindow.style.opacity = 1;
	warningWindow.style.filter = "alpha(opacity=100)";	
	
	StartUpWindow();	
}

function closeWindow()
{
	warningWindow.style.display = 'none';		
	warningWindow = null;
}
