/* ***************************************
* Javascript File: uberCookie.js
* Purpose: General cookie functions.
* Updated: 2005-04-03
* Authors: David McCracken
* .............. notes ..........................
* - Assigning a cookie name value pair to document.cookie only sets one cookie 
* but reading back document.cookie retrieves all of the domain's cookies. 
* Reading an individual cookies entails first parsing the cookie list to find 
* the given cookie and then parsing the value from the remainder of the list.
* - The functions here operate only on single cookie name value pairs. Various
* platforms limit the number of these, typically to 20 per domain.
* ****************************************************/
  
/*****************************************************
* Function: getCookie
* Description: Get a cookie's value.
* Returns: null if the cookie doesn't exist or has an empty value.
* Arguments: name (string) is the cookie name.
* ........... notes ...........................
* - Retrieve the entire domain's cookie list from document.cookie.
* Parse this for the given cookie name and, finding a match, parse
* the value from the remainder of the list.
* ............................................... */
function getCookie( name ) 
{
  cookies = document.cookie;
  //alert( "cookie list: " + cookies );
  var start = cookies.indexOf( name + "=" );
  if (start == -1)
    return null; // The named cookie doesn't exist.
    
  start = cookies.indexOf( "=", start) + 1; // First char of value.
  var end = cookies.indexOf( ";", start); // Find the end of the value string.
  if (end == -1) // If the cookie doesn't have additional parameters
    end = cookies.length; // then the end is the end of the string.
    
// Get the cookie value, restoring any escaped spaces to actual spaces.
  var value = unescape( cookies.substring( start, end ));
  if( value == null )
    return null;
  else
    return value;
}
/*****************************************************
* Function: delCookie
* Description: Delete one or all cookies in this domain.
* Returns: nothing
* Arguments: name (string) is the cookie name, 0 to delete all.
* .............. notes ..........................
* - Each cookie is deleted by assigning it a null value with
* expiration time of yesterday.
* - All cookies (of this domain) are deleted by parsing the cookie
* list backwards, deleting each name. Backwards parsing is used so
* that the cookie list only needs to be read once. Whether deleting
* causes the list to immediately change or not is irrelevant since
* the earlier parts won't change in any case. This makes the process
* immune to platform variations.
* ............................................... */
function delCookie( name )
{
  if( name )
    setCookie( name, "", -1, 3, false ); // Empty val and yesterday.
  else
  {
    var cookies = document.cookie;
    var end = cookies.length;  
    for( var lim = 0 ; lim < 30 ; lim++ ) // Limit iterations in case of error.
    {
      end = cookies.lastIndexOf( "=", end );
      if( end == -1 )
        break;
      start = cookies.lastIndexOf( ";", end ) + 1; // -1 -> 0, others ";" -> first char.
      // alert( "del " + cookies.substring( start, end ));
      setCookie( cookies.substring( start, end ), "", -1, 3, false );
      end = start - 1;
    }
  }
}
/*****************************************************
* Function: setCookie
* Description: Set the value of the given cookie.
* Returns: nothing
* Arguments: 
* - name (string) is the cookie name.
* - val (string) is the value to assign to the cookie. If 0, the cookie is in 
* effect deleted. It may not actually be deleted until it expires. To immediately
* really delete it, call delCookie.
* - lifetime (integer) is a life time count in seconds, minutes, hours, or days
* - interval (enum) tells lifetime units: 1 = milliseconds, 2 = seconds, 3 = days.
* - reload (bool) tells whether to reload the page.
* ............................................... */
function setCookie( name, val, lifetime, interval, reload )
{
  if( lifetime == 0 )
    document.cookie = name + "=" + val;
  else
  {
    var exp = new Date();
    var expiration = exp.getTime() + ( interval == 1 ? lifetime : 
      1000 * ( interval == 2 ? lifetime : 86400 * lifetime ));
    exp.setTime( expiration );
    document.cookie = name + "=" + val + "; expires=" + exp.toGMTString();
  }
  if( reload )
    location.reload( false ); /* Reload the page */
}
