
var sessWarningWin, interval, warningURL, redirectPageName, idletimeoutPage, idletimeoutUrl, logOffUrl, refreshURL;


function MaintainChildFocus()
{
  try
  {
		if(sessWarningWin)
		{
			if(sessWarningWin.closed)
			{	window.clearInterval(interval);	}
			else
			{	sessWarningWin.focus();	}
		}
  } catch (e) {   }
}

function RegisterTimeout(timeOutSeconds, timeoutPage, logoutUrl, refreshUrl, warningurl, redirectPage, securePrefix, unSecurePrefix)
{
	if( timeOutSeconds == 0 || timeOutSeconds == '')
		timeOutSeconds = 1740;
	msTillPopup = timeOutSeconds * 1000;
	var urlPrefix = securePrefix;
	var reqType = "secure";
	if( window.location.protocol == "http:")
	{
		urlPrefix = unSecurePrefix;
		reqType = "unsecure";
	}
	
	redirectPageName = redirectPage;
	
	idletimeoutPage = timeoutPage;
	refreshURL = urlPrefix + refreshUrl;
	warningURL = urlPrefix + warningurl + "&reqType="+ reqType;
	
	logOffUrl = urlPrefix + logoutUrl + "&RedirectPage="+redirectPageName;//Will be used with window.confirm log off
	
	window.setTimeout('FireWarning()', msTillPopup);
}

function HandleFocus()
{
  if (sessWarningWin)
  {
    if (!sessWarningWin.closed)
    {sessWarningWin.focus();}
    else
    {window.top.onfocus = "";}
  }
  return false;
}

function Unload()
{
  if (sessWarningWin)
  {	sessWarningWin.close();}
}

function FireWarning()
{	
	var leftOffset = (screen.width - 578) / 2;
	var topOffset = (screen.height - 185) / 2;
	window.onfocus = HandleFocus;
	window.onunload = Unload;
	
	var mine = window.open('','','width=1,height=1,left=0,top=0,scrollbars=no');
	var popUpsBlocked = true;//Pops are blocked
	if(mine)
	{
		popUpsBlocked = false;//Pops are not blocked
		mine.close()
	}
	mine="";

	if(popUpsBlocked)
	{		
			var x = window.confirm("Your session has been inactive for some time and will expire within 60 seconds. \n\nWould you like to keep this session active and continue using this site? Click OK to refresh session or Cancel to end session.");
			
			if(x)
			{ SessionRefresh(); }
			else
			{ LogOff(); }
	}
	else
	{
		var popupURL = warningURL +"&IdletimeoutPage="+idletimeoutPage+"&RedirectPage="+redirectPageName;
		if( sessWarningWin == null || sessWarningWin.closed)
		{
			sessWarningWin = window.open(popupURL, "warning", 'toolbar=no,height=228,width=395,status=no,scrollbars=no,resize=no,menubar=no,top' + topOffset + ',left=' + leftOffset);
		}
		else
		{sessWarningWin.focus();}
	}
	
	interval = window.setInterval("MaintainChildFocus()",1);
}

function SessionRefresh()
{	
	setTimeout('FireWarning()', msTillPopup);
	var myFrame = document.createElement("IFRAME");
	myFrame.src = refreshURL;
	myFrame.height = "0px";
	myFrame.width = "0px";
	document.body.appendChild(myFrame);
	if(sessWarningWin)
	{sessWarningWin.close();}
}

function LogOff()
{
		hasRefreshed = true;
		window.location.href = logOffUrl;
}

//Pop up functions
function ShowLogOffMessage()
{	
	try
	{
		hasRefreshed=true; self.opener.location=idletimeoutUrl;
	}
	catch(Error)
	{
		window.close();
	}
}
function HandleRefresh()				
{
	hasRefreshed = true;
	try
	{
		self.opener.SessionRefresh();
	}
	catch(Error)
	{
		window.close();
	}
}

function HandleLogOff( )
{
	try
	{
		hasRefreshed = true;
		self.opener.location= logOffUrl;
		self.close();
	}
	catch(Error)
	{
		window.close();
	}	
}

function Exit()
{	if(hasRefreshed==false){ShowLogOffMessage();}}

