// ********************************************************************
// Copyright (C) 2001 Microsoft Corporation. All rights reserved.
//
//	IMPORTANT.  Please read the legal.txt file, located in the 
//	"<CMS Install Directory>\Server\httpexec\WBC\Internals" 
//	directory, governing the use of this internal file."
// ********************************************************************


var g_nClientTimeAheadOfServer = WBC_getClientTimeAheadOfServer();


// ---- [Public] Check if AuthToken cookie has expired
function WBC_ensureCookieValidity(strReasonText)
{
	// retrieve CMSAUTHTOKENEXPIRY cookie value which is in the format of "yyyy;mm;dd;hh;nn;ss"
	var strCookieExpiryTime = WBC_getCookieValue( "CMSAUTHTOKENEXPIRY" );

	if (strCookieExpiryTime)
	{
		var aExpiryDate = strCookieExpiryTime.split(":");
		var dateExpiry = new Date( aExpiryDate[0], aExpiryDate[1], aExpiryDate[2], aExpiryDate[3], aExpiryDate[4], aExpiryDate[5] );

		// compare cookie expiry time to current time (both in UTC)
//		var dateClientUTC = WBC_convertLocal2UTC( new Date() );
		var dateClientUTC = new Date();
		
		alert( "dateClientUTC = " + dateClientUTC + ", g_nClientTimeAheadOfServer = " + g_nClientTimeAheadOfServer + ", dateExpiry = " + dateExpiry );
		
		if ( (dateClientUTC.getTime() - g_nClientTimeAheadOfServer) > (dateExpiry.getTime() - IDC_TIMEOUT_RELAX_CHECK) )
		{
			// --------------------------------------------------------------------
			// glenbu:  As of May 28, 2002. This file is not being used and will be
			//          removed from SD.  If it is ever reactivated, the code below
			//          must be modified so that it does not hard-code 
			//          ManualLogin.asp.  See ManualLogin.inc for details.
			// --------------------------------------------------------------------
			// cookies it has expired; we need to pop up dialog for ManualLogin.asp 
			var strURLBase = "/NR/System/Access/ManualLogin.asp?"
			var strReason = "REASONTXT=" + escape(strReasonText);
			var strReturnURL = "ReturnUrl=" + escape("/NR/System/WBC/Internals/PageTemplate/Authoring/AfterSessionExtended.htm");
			WBC_openWindowOnFocus( strURLBase + strReason + "&" + strReturnURL, IDS_WIN_SESSION_TIMEOUT_LOGIN, IDS_WIN_SESSION_TIMEOUT_LOGIN_FEATURES);

			return false;
		}
	}

	// by default, return true since we don't have conclusive
	// evidence that says we have a cookie that has expired.
	return true;
}


// ---- [Private] Retrieve the value of a named cookie
function WBC_getCookieValue( strCookieName )
{
	var strValue = "";
	var colCookies = unescape(document.cookie);
	var strCookieNamePlusEqual = strCookieName + "=";
	var nPosBegin = colCookies.indexOf( strCookieNamePlusEqual );

	if (nPosBegin != -1)
	{
		// defaine the nPosBegin and nPosEnd for the cookie we are looking for
		nPosBegin += strCookieNamePlusEqual.length;
		var nPosEnd = colCookies.indexOf( ";", nPosBegin );
		if (nPosEnd == -1)
		{
			nPosEnd = colCookies.length;
		}

		// extract the cookie value
		strValue = colCookies.substring( nPosBegin, nPosEnd );
		strValue = unescape( strValue );

	}

	return strValue;
}


// ---- [Private] Figure out the client time ahead of server time in terms of milliseconds
function WBC_getClientTimeAheadOfServer()
{
	// get the system's central UTC time
	var strServerUTCTime = WBC_getCookieValue( "NR_SYSTEM_UTC_TIME" );
	var aServerUTCTime = strServerUTCTime.split(":");
	var dateServerUTCTime = new Date( aServerUTCTime[0], aServerUTCTime[1], aServerUTCTime[2], aServerUTCTime[3], aServerUTCTime[4], aServerUTCTime[5] );

	// get the client UTC time
//	var dateClientUTCTime = WBC_convertLocal2UTC( new Date() );
	var dateClientUTCTime = new Date();
	
	return dateClientUTCTime.getTime() - dateServerUTCTime.getTime();
}


