//---------------------------------------------------------------------
// Author:		Marc van Heugten
// Date:		06-02-2004
// Description: This Script file will be used for redirecting the
//				user to the RetryLogin page
// Reference:	TD Webshop 300
//---------------------------------------------------------------------

var blnShowSessionPopup;
blnShowSessionPopup = false;

function getCorrectFrame(FrameName)
{
var Frame;
	if(parent.frames[FrameName] == null)
	{
		if(parent.parent.frames[FrameName] == null)
		{
			if(parent.parent.parent.frames[FrameName] == null)
			{
				Frame = this;
			}
			else
			{
				Frame = parent.parent.parent.frames[FrameName];
			}
		}
		else
		{
			Frame = parent.parent.frames[FrameName];
		}
	}
	else
	{
		Frame = parent.frames[FrameName];
	}
	return Frame;
}

function findTopDoc()
{
	if(parent.EnableClock != null)
		return parent;
	
	return this;
}

function resetClock()
{
	//--------------------------------------------------------------------- 
	// Reset the variable that is declared in Default.aspx
	//--------------------------------------------------------------------- 
	var topDoc = findTopDoc();
	TimeOutDateCounter = new Date ();
	topDoc.TimeOutSeconds = Date.parse(TimeOutDateCounter);
}

function disableClock()
{
	//--------------------------------------------------------------------- 
	// Turns the clock off
	//--------------------------------------------------------------------- 
	var topDoc = findTopDoc();
	
	resetClock();
	topDoc.EnableClock = false;
}


function startClock(TimoutInMinutes) 
{
	var topDoc = findTopDoc();
	
	if (topDoc.EnableClock)
	{	
		//--------------------------------------------------------------------- 
		// Calculate the new value of the clock
		//--------------------------------------------------------------------- 
		TimeOutDateCounter = new Date ();

		if ((Date.parse(TimeOutDateCounter) - topDoc.TimeOutSeconds) > (TimoutInMinutes * 60000))
		{
			//--------------------------------------------------------------------- 
			// Build JavaScript for redirecting the user to the RetryLogin screen
			// 1098 = value of (int)Constants.enumLoginMessage.ExpiredSession
			//--------------------------------------------------------------------- 
			disableClock();
			getCorrectFrame('Top').location.href = '/Home/LoginHeader.aspx';
			getCorrectFrame('MainFrame').location.href = '/Login/RetryLogin.aspx?ErrorCode=1098&LoginErrorCode=0';
		}

		setTimeout ("startClock(" + TimoutInMinutes + ")", 1000);

		//--------------------------------------------------------------------- 
		// One minute before the session expires display a popup to ask
		// the user if he wants to continue his session or not. This popup
		// will be visoble for one minute before it will close and go to
		// login screen.
		//--------------------------------------------------------------------- 		
		if ((Date.parse(TimeOutDateCounter) - topDoc.TimeOutSeconds) > ((TimoutInMinutes - 1) * 60000))
		{
			if (!blnShowSessionPopup)
			{
				window.open("/Messages/SessionExpires.aspx","","width=300px,height=150px,left=250,top=225");
				blnShowSessionPopup = true;	
			}
		}
	}
}