/* ---------------------------------------------------------
 * Copyright (c) 2004 Participate Systems, Inc.  All rights reserved.
 * ---------------------------------------------------------
 */
/******** START Get Cookie Function ********
******** REQUIRED PARAMS
******** cookieName = name of the cookie whose value we are attempting to access */
function getCookie(cookieName)
{
  //Determine starting position of cookie value
  var cookieStart = document.cookie.indexOf(cookieName + "=");
  
  //Determine length of cookie
  var cookieLength = cookieStart + cookieName.length + 1;
  
  /*Determine if cookie value exists, return null if not found*/
  if ((!cookieStart) && (cookieName != document.cookie.substring(0,cookieName.length)))
    return null;
  if (cookieStart == -1) return null;
  
  /*Cookie exists, get value*/
  //Find end position of cookie value
  var cookieEnd = document.cookie.indexOf(";",cookieLength);
  
  if (cookieEnd == -1)
    cookieEnd = document.cookie.length;
    
  //Return cookie value
  return unescape(document.cookie.substring(cookieLength,cookieEnd));
}
/******** END Get Cookie Function ********/

/******** START Set Cookie Function ********
******** REQUIRED PARAMS
******** cookieName  = name of the cookie whose value we are attempting to set
******** cookieValue = value of the cookie to set
******** OPTIONAL PARAMS
******** cookieExpires = number of days before cookie expires
******** cookiePath    = sets the path to the directory (and subdirectories that are allowed to read the cookie; default is current
******** cookieDomain  = sets the valid domain(s) that are allowed to use the cookie; default is current
******** cookieSecure  = Boolean indicator requiring SSL for transmission of the cookie; default is not required */
function setCookie(cookieName,cookieValue,cookieExpiresDays,cookiePath,cookieDomain,cookieSecure)
{
  var expDateGMT;

  /* Convert the number of days after which a cookie will expire to milliseconds for conversion to Greenwich Mean Time */
  if (cookieExpiresDays)
  {
    var currentDate;
    
    currentDate = new Date();
    //24 hours in a day, 60 minutes in an hour, 60 seconds in a minute, 1000 milliseconds in a second
    currentDate.setTime(currentDate.getTime() + (cookieExpiresDays * 24 * 60 * 60 * 1000));
    expDateGMT = currentDate.toGMTString();     
  }
  else
    expDateGMT = ""

  /* Set the cookie */
  //Set the cookie name and value
  document.cookie = cookieName + "=" + escape(cookieValue) +
  //Set the cookie expiration date
  ( (expDateGMT) ? ";expires=" + expDateGMT : "") +
  //Set the cookie valid path
  ( (cookiePath) ? ";path=" + cookiePath : "") + 
  //Set the valid cookie domain
  ( (cookieDomain) ? ";domain=" + cookieDomain : "") +
  //Set Boolean
  ( (cookieSecure) ? ";secure" : "");
}
/******** END Set Cookie Function ********/

/******** START Delete Cookie Function ********
******** REQUIRED PARAMS
******** cookieName = name of the cookie to delete */
function deleteCookie(cookieName)
{
  setCookie(cookieName,"",-1)
}
/******** END Delete Cookie Function ********/

