/********************************************************************************************************************
** Purpose      : Countdown controller
** Author       : Julian Kato
** Date         : 23-OCT-07
** Re-engineered: 12/5/2007, Robert Harlow
********************************************************************************************************************/

var _iCountStart = 1200;								// The initial value to countdown in seconds (Default value is 20 minutes)
var _iCountSessionAboutToExpire = _iCountStart / 10;	// At what time should we show the message 'This session is about to expire'
var _iCountdown = 0;									// The countdown value in seconds
var _cartEmpty;											// The flag to start or stop the countdown
var _t;													// timer

/// <Summary>
/// This function starts the countdown
/// </Summary>
function CountdownStart()
{
	if(_cartEmpty == true)
	{
		CountdownReset();
		Countdown();
	}
}

/// <Summary>
/// This function resets the initial value of the countdown
/// If the countdown is running it will start over again
/// </Summary>
function CountdownReset()
{
    _iCountdown = _iCountStart;
}

/// <Summary>
/// This function stops the countdown
/// </Summary>
function CountdownStop()
{
    clearTimeout(_t);
}

/// <Summary>
/// This is the countdown function
/// It counts the seconds backwards by calling back to itself each second.  At 15 minutes the user
/// is alerted.  At 20 minutes, the session times out and returns to the homepage, cart is emptied.
/// </Summary>
function Countdown()
{
    if(_cartEmpty)
    {
		_iCountdown--;
		
        if(_iCountdown == _iCountSessionAboutToExpire)
            CountdownSessionAboutToExpire();

        if(_iCountdown <= 0)
        {
            CountdownStop();
            CountdownSessionEnds();
        }
        else
			_t = setTimeout('Countdown()', 1000);
    }
}

/// <Summary>
/// This function pops up a temporary message saying that the session is about to expire
/// </Summary>
function CountdownSessionAboutToExpire()
{
    document.getElementById('layerSessionExpiring').style.visibility = 'visible';
}

/// <Summary>
/// This function ends the current session asking the user if they wants to open another session
/// </Summary>
function CountdownSessionEnds()
{
    document.getElementById('layerSessionExpiring').style.visibility = 'hidden';
    document.getElementById('layerSessionExpired').style.visibility = 'visible';
}

/// <Summary>
/// This function is called by the Session control when the user clicks "Ok" to keep the session
/// from expiring
/// </Summary>
function ResetSession()
{
    CountdownReset();
    CountdownStart();
    
    document.getElementById('layerSessionExpiring').style.visibility = 'hidden';
}

/// <Summary>
/// This function is called by the Session control when the user clicks "Ok" and ends the session
/// </Summary>
function EndSession()
{
    document.getElementById('layerSessionExpired').style.visibility = 'hidden';
    window.location.href = applicationRoot + 'Default.aspx?ExpireCart=true';
}