//----------------------------------------------------------------------------------
function getCookie(name)
{
  var cookies = document.cookie;
  var start = cookies.indexOf(name + '=');
  if (start == -1) 
  {
	return null;
  }
  var len = start + name.length + 1;
  var end = cookies.indexOf(';',len);
  if (end == -1) end = cookies.length;
  return unescape(cookies.substring(len,end))
}

//--------------------------------------------------------------------------

function setCookie(name, value, expires, path, domain, secure)
{
 // alert('setcookie');
  value = escape(value);

  expires = (expires) ? ';expires=' + expires.toGMTString() :'';
  path    = (path)    ? ';path='    + path                  :'';
  domain  = (domain)  ? ';domain='  + domain                :'';
  secure  = (secure)  ? ';secure'                           :'';

  document.cookie = name + '=' + value + expires + path + domain + secure;
}

//----------------------------------------------------------------------------
function checkCookieSet(cookieName,value)
{
    if (getCookie(cookieName) != value)
    {  
	//location.href='../perl_templates/TP_NoCookies';
	return false;
    }
    else return true;
}
//-----------------------------------------------------------------------------
function isCookieEnabled()
{
    if (document.all)
    {
        if (!navigator.cookieEnabled)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        setCookie('cookieSet',1);
        var temp = getCookie('cookieSet');
        if (!temp)
        {
           return false;
        }
        else
        {
            return true;
        }
    }
}
//----------------------------------------------------------------------------
function cookieTampered(cookieName)
{
    if (getCookie(cookieName) == null)
	return false;
    var cookieValue = getCookie(cookieName);
    var mid = cookieValue.indexOf(" ");
    if (mid == -1) return true;
    else
    {
        var string1 = cookieValue.substring(0,mid);
        var string2 = cookieValue.substring(mid+1,cookieValue.length);

	string1 = EncryptString(string1,cookieKey1);

	string2 = EncryptString(string2,cookieKey2);

	if (string1 == string2)
	{
	   return false;
	}
	else return true;
    }
}
//----------------------------------------------------------------------------
//Pre: cookieTampered returned false and cookieValue is the value returned by getCookie
//Returns: the decrypted value of the cookie (not including the checksum)

function decryptCookie(cookieValue)
{
    var mid = cookieValue.indexOf(" ");
    var value = cookieValue.substring(0,mid);
    value = EncryptString(value,cookieKey1);
    return value;
}
//----------------------------------------------------------------------------
function rc(timeout,param)
{

    if (timeout == -1)
    {
	return;
    }
  
    loginCookie = getCookie('userLoginCookie');

    if (loginCookie == null)
    {
        location.href='../cgi-bin/APSReLogin.pl?'+ param;
	return;
    } //if

    if (getCookie('userLoginCookie') == 0)
    {
   	var now = new Date();
        var nowTime = String(now.getTime());
	setCookie('timeout',nowTime);
  
	if (checkCookieSet('timeout',nowTime) == false)
	{  
            location.href='../perl_templates/TP_NoCookies';
 	    return;
   	}  //if

	setCookie('userLoginCookie','1');

        if (checkCookieSet('userLoginCookie','1') == false)
	{
            location.href='../perl_templates/TP_NoCookies';
	    return;
	}
    }  //if

      
    //If cookies are not accepted
    if (!isCookieEnabled())
    { 
        location.href='../perl_templates/TP_NoCookies';
	return;
    }  //if

    if (getCookie('timeout') == null)
    {
        location.href='../cgi-bin/APSReLogin.pl?'+ param;
	return;
    }  //if


    oldValue = getCookie('timeout');

    now = new Date();
    nowTime = now.getTime();
            
    intervalSecs = (nowTime - oldValue)/1000;
            
    if (intervalSecs > timeout)
    {
	setCookie('timeout','1');

   	if (checkCookieSet('timeout','1') == false)
	{
	    location.href='../perl_templates/TP_NoCookies';
  	    return;
	}
	else 
        {

            document.location.href='../cgi-bin/APSReLogin.pl?' + param;

            return;

	}  //else
    } //if

    setCookie('timeout',nowTime);
        
    if (checkCookieSet('timeout',nowTime) == false)
    {
        location.href='../perl_templates/TP_NoCookies';
	return;
    }  //if
}
//-----------------------------------------------------------------------------------


