/* ***************************************
* Javascript File: uberCookie.js
* Purpose: General cookie functions.
* Updated: 2005-05-06
* 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.
      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.
* - reloadAlert (string): if not null then reload the page and also if not empty
* string then it is the ID of an alert box (usually div) that is display:none when
* the page loads but will now be changed to block so that if the page doesn't 
* automatically reload the alert remains saying e.g. "Please reload page for style 
* change".
* - path (string) tells the cookie's scope. If null then the cookie can be read 
* only from pages in the directory in which it is created. If "/" then the same 
* cookie can be accessed throughout the domain and should usually have a rather
* specific name to avoid collisions. 
* ............................................... */
function setCookie( name, val, lifetime, interval, reloadAlert, path )
{
  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() +
     ( path == null ? "" : "; path=" + path );
  }
  if( reloadAlert != null )
  { 
    if( reloadAlert != "" )
    {
      var ref = document.getElementById( reloadAlert );
      if( ref != null )
        ref.style.display = "block";
    }
/* Every browser requires a unique means of forcing page reload and most are 
* mutually exclusive. Using the following two different means works for most 
* Browsers that have any means at all and gets some, e.g. FF, to return to the 
* current URL fragment after reload. */
    history.go(0);
    location.reload( true ); 
  }
}
