SessionRenew = function() {}
SessionRenew.m_timeoutTimer = null;
SessionRenew.m_promptTimeoutTimer = null;
SessionRenew.m_confirmDialog = false;
SessionRenew.m_mainDivId = null;
SessionRenew.m_mainDivCssClass = null;

SessionRenew.onLoad = function()
	{
	SessionRenew.startTimer(); // start our timer

	try
		{
		// call other onLoad javascript
		if ( "" != "")
			{
			eval("");
			}
		}
	catch(e)
		{
		}
	}

SessionRenew.startTimer = function()
	{	
    if (SessionRenew.m_timeoutTimer)
        window.clearTimeout(SessionRenew.m_timeoutTimer);
    SessionRenew.m_timeoutTimer = window.setTimeout(SessionRenew.keepSession, 1080000); 
	}

SessionRenew.displayPrompt = function()
	{
	try
		{
		// start a timer for the prompt (so we can automatically clear it and redirect
		// to the timeout page when it is too late)
		if (SessionRenew.m_promptTimeoutTimer)
			window.clearTimeout(SessionRenew.m_promptTimeoutTimer);

		SessionRenew.m_promptTimeoutTimer = window.setTimeout(SessionRenew.endSession, 120000);

		// create the div used to prompt
		if (!SessionRenew.m_confirmDialog)
			{
			var confirmDialog = null;
	        
			// if there is one already in the document, use it
			if (null != SessionRenew.m_mainDivId)
				{
				confirmDialog = document.getElementById(SessionRenew.m_mainDivId);
				}

			// if there isn't one in the document, we have to try to make our own
			if (!confirmDialog)
				{
				var text;

				// create a div
				confirmDialog = document.createElement('div');
				confirmDialog.id = 'confirmDialog';
				confirmDialog.style.top = '5px';
				confirmDialog.style.left = '5px';
				confirmDialog.style.zIndex = '100';
				confirmDialog.style.position = 'absolute';
				confirmDialog.style.cursor = 'default';

				// if we have a predermined CssClass, use it
				if (null != SessionRenew.m_mainDivCssClass)
					{
					confirmDialog.className = SessionRenew.m_mainDivCssClass;
					}
				else
					{
					// no style, so make an ugly little div
					confirmDialog.style.width = '280px';
					confirmDialog.style.height = '120px'; 
					confirmDialog.style.border = 'solid 2px black';
					confirmDialog.style.textAlign = 'center';
					confirmDialog.style.backgroundColor = 'Silver';
					confirmDialog.style.fontFamily = 'Verdana';
					confirmDialog.style.fontSize = '10pt';
					confirmDialog.style.color = 'Black';
					}

				// make a div for the "titlebar"
				var promptTitleDiv = document.createElement('div');
				promptTitleDiv.id = 'confirmDialogTitle';
				if (SessionRenew.m_mainDivCssClass == null)
					{
					promptTitleDiv.style.height = '15px';
					promptTitleDiv.style.borderBottom = 'solid 1px black';
					promptTitleDiv.style.backgroundColor = 'Navy';
					promptTitleDiv.style.color = 'White';
					promptTitleDiv.style.fontWeight = 'bold';
					}
				text = document.createTextNode('Renew Session');
				promptTitleDiv.appendChild(text);
				confirmDialog.appendChild(promptTitleDiv);

				// make a div for the actual text of the prompt
				var promptDiv = document.createElement('div');
				promptDiv.id = 'confirmDialogPrompt';
				promptDiv.style.height = '60px';
				promptDiv.style.padding = '5px';
				text = document.createTextNode('Your session is about to expire.  Click OK to renew your session or Cancel to end it now.');
				promptDiv.appendChild(text);
				confirmDialog.appendChild(promptDiv);

				// make a div to hold the OK/Cancel buttons
				var buttonsDiv = document.createElement('div');
				buttonsDiv.id = 'confirmDialogButtons';
				buttonsDiv.style.height = '30px';
				buttonsDiv.style.padding = '5px';

				// add the OK button
				var okayButton = document.createElement('button');
				text = document.createTextNode('OK');
				okayButton.appendChild(text);
				okayButton.id = 'confirmDialogOkayButton';
				okayButton.onclick = SessionRenew.onClickOk;
				buttonsDiv.appendChild(okayButton);

				// add the cancel button
				var cancelButton = document.createElement('button');
				text = document.createTextNode('Cancel');
				cancelButton.appendChild(text);
				cancelButton.id = 'confirmDialogCancelButton';
				cancelButton.onclick = SessionRenew.endSession;
				buttonsDiv.appendChild(cancelButton);

				// button styles
				if (null == SessionRenew.m_mainDivCssClass)
					{
					okayButton.style.backgroundColor = 'Silver';
					okayButton.style.color = 'Black';
					cancelButton.style.backgroundColor = 'Silver';                
					cancelButton.style.color = 'Black';
					}
				else
					{   
					okayButton.style.cursor = 'hand';
					cancelButton.style.cursor = 'hand';
					cancelButton.style.marginLeft = '10px';
					} 
				
				// append buttons div
				confirmDialog.appendChild(buttonsDiv);
	            
				// append div to the document
    			document.body.appendChild(confirmDialog);    			
				}
				
				// hold on to div
				SessionRenew.m_confirmDialog = confirmDialog;
			}

		if ( SessionRenew.m_confirmDialog )
			{
			// display the div		
			SessionRenew.m_confirmDialog.style.display = 'block';
			SessionRenew.positionMainDiv();
			}
		else
			{
			SessionRenew.fallbackPrompt();
			}
		}
	catch(e)
		{
		SessionRenew.fallbackPrompt();
		}
	
	return true;
	}

SessionRenew.fallbackPrompt = function()
	{
	try
		{
		// we couldn't create the fancy confirmation dialog
		// so see if we can get it done with javascript.confirm
		var stay=confirm("Your session is about to expire.  Click OK to renew your session or Cancel to end it now.")
		if (!stay)
			{
			SessionRenew.endSession();
			}
		else
			{
			SessionRenew.keepSession();
			}
		}
	catch(e)
		{
		alert("There was an error renewing your session.  Please contact the application administrator.");
		SessionRenew.endSession();		
		}
	}

SessionRenew.onClickOk = function()
	{
    SessionRenew.m_confirmDialog.style.display = 'none';
    
    // clear prompt timeout timer
    window.clearTimeout(SessionRenew.m_promptTimeoutTimer);
    SessionRenew.m_promptTimeoutTimer = null;
    
    // Renew session
    SessionRenew.keepSession();
    
    // restart timer
    SessionRenew.startTimer();
	}

SessionRenew.onClickCancel = function()
	{
    SessionRenew.endSession();
	}

SessionRenew.keepSession = function()
	{	
	SessionRenew.loadXmlDoc("/SessionManager.ashx?n=" + (new Date()).getTime());
	}

// end the session by redirection to the timeout url
SessionRenew.endSession = function()
	{
	try
		{
		if ( "" != "/lost_session.aspx" )
			{
			window.location.href = "/lost_session.aspx";
			}
		}
	catch(e)
		{
		}
	
	return true;
	}

var oXmlRequest;
SessionRenew.loadXmlDoc = function(url)
	{
	oXmlRequest = false;
	// native XMLHttpRequest object
	if(window.XMLHttpRequest) 
		{
		try 
			{
			oXmlRequest = new XMLHttpRequest();
			}
		catch(e) 
			{
			oXmlRequest = false;
			}
		} 
	// IE/Windows ActiveX version
	else if(window.ActiveXObject) 
		{
		try 
			{
			oXmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} 
		catch(e) 
			{
			try 
				{
				oXmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} 
			catch(e) 
				{
  				oXmlRequest = false;
				}
			}
		}

	// execute request
	if(oXmlRequest) 
		{
		try
			{
			oXmlRequest.open("GET", url, false); //<-- no async (to iffy)
			oXmlRequest.send(null);
			if (200 == oXmlRequest.status)  // status == "OK"
				{
				// success
				SessionRenew.startTimer();//<-- start the timer again
				}
			else
				{
				// bad status value returned from web server
				alert("There was an error renewing your session.  Please contact the application administrator.");
				SessionRenew.endSession();
				}
			return;	//<-- don't forget it!  fall through displays error message
			}
		catch(e)
			{
			// fall through
			}
		}
		
	// if we fell through, then we failed to send the request
	alert("Could not initialize communications with server; ending session.  Please contact the application administrator.");
	SessionRenew.endSession();	
	}

// center the confirmation dialog in the client window
SessionRenew.positionMainDiv = function()
	{
	try
		{
		var div = SessionRenew.m_confirmDialog;
		
		// bail out if no div
		if ( !div || !div.style || 'block' != div.style.display )
			{
			return;
			}

		// variables for width and height of client window
		var my_width = 0;
		var my_height = 0;

		// get width and height of client window as appropriate for whatever browser this is
		if ( 'number' == typeof( window.innerWidth ) )
			{
			my_width = window.innerWidth;
			my_height = window.innerHeight;
			}
		else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
			{
			my_width = document.documentElement.clientWidth;
			my_height = document.documentElement.clientHeight;
			}
		else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
			{
			my_width = document.body.clientWidth;
			my_height = document.body.clientHeight;
			}

		var scrollY = 0;

		if ( document.documentElement && document.documentElement.scrollTop )
			{
			scrollY = document.documentElement.scrollTop;
			}
		else if ( document.body && document.body.scrollTop )
			{
			scrollY = document.body.scrollTop;
			}
		else if ( window.pageYOffset )
			{
			scrollY = window.pageYOffset;
			}
		else if ( window.scrollY )
			{
			scrollY = window.scrollY;
			}
		
		if ( div.offsetWidth )
			{
			div.style.left = ((my_width - div.offsetWidth) / 2) + 'px';
			div.style.top = ((my_height - div.offsetHeight) / 2 + scrollY) + 'px';
			}
		else
			{
			div.style.left = '10px';
			div.style.top = (10 + scrollY) + 'px';
			}
		return;
		}
	catch(e)
		{
		// alert(e.description);
		}
		
	// only here if we fell throught the catch
	try
		{
        if (document.all)
			{
			SessionRenew.m_confirmDialog.style.top = (document.documentElement.scrollTop + document.body.scrollTop + 10) + 'px';
			}
		else
			{
			SessionRenew.m_confirmDialog.style.top = (pageYOffset + 10) + 'px';
			}
		
		SessionRenew.m_confirmDialog.style.left = '10px';
		}
	catch(e)
		{
		//hopeless
		}
	}

// add our onLoad event, also add the onscroll and resize events
// if we are using a confirmation dialog
try
	{
	window.onload = SessionRenew.onLoad;
	if ( 'SessionRenew.keepSession' == 'SessionRenew.displayPrompt') 
		{
		window.onscroll = SessionRenew.positionMainDiv;
		window.onresize = SessionRenew.positionMainDiv;
		}
	}
catch(e)
	{
	// not much we can do -- timeouts (like other things) are just gonna happen
	}