/**
 * CookieFactory
 *
 * The CookieFactory.js is a JavaScript class used for getting, creating, and 
 * removing cookies. All public methods are Static, so the user should never 
 * need to instantiate a new CookieFactory Object.
 *
 * @author Genworth Financial, Inc.
 * @created 19 April 2007 
 * @since 19 April 2007
 * @version 1.0
 * @modified 19 April 2007 : Birth
 */

/* CONSTRUCTOR */
  function CookieFactory () {}

/* CONSTANTS */
  CookieFactory.EPOCH_DATE = 'Thu, 01-Jan-1970 00:00:00 GMT';

/** 
 * public static createCookie method 
 *
 * @param name The cookie name
 * @param value The cookie value
 * @param days The number of days for the cookie to be active
 * @param path The path the cookie is valid for
 * @param domain The domain the cookie is valid for
 * @param secure If the cookie is secure (Use only 1|0 or true|false)
 */
  /*void*/ CookieFactory.createCookie = function (/*String*/ name, /*String*/ value, /*int*/ days, /*String*/ path, /*String*/ domain, /*boolean*/ secure)
  {
    /*String*/var expires = CookieFactory._calculateExpirationDate(days);
    CookieFactory._setCookie(name, value, expires, path, domain, secure);
  }

/**
 * public static deleteCookie method
 *
 * @param   name - The name of the cookie.
 * @param path - The string value of the cookie's path property.
 * @param domain - The string value of the cookie's domain property.
 */
  /*void*/ CookieFactory.deleteCookie = function (name, path, domain)
  {
    CookieFactory._setCookie(name, '', CookieFactory.EPOCK_DATE, path, domain);
  }

/**
 * public static readCookie method
 *
 * @param name The cookie name
 * @return String The cookie value 
 */	
  /*String*/ CookieFactory.readCookie = function (/*String*/ name)
  {
    if (document.cookie)
    {
      /*Cookie[]*/ var cookieArray = document.cookie.split(';');
      /*String*/ var nameEQ = name + "=";
      for (var i=0; i < cookieArray.length; i++)
      {
        /*Cookie*/ var c = cookieArray[i];
        while (c.charAt(0)==' ') { c = c.substring(1,c.length); }
        if (c.indexOf(nameEQ) == 0) { return unescape(c.substring(nameEQ.length, c.length)); }
      }
    }
    return null;
  }


/**
 * private static calculateExpirationDate method
 *
 * @param days The number of days to use to calculate the expiration date
 */
  /*String*/ CookieFactory._calculateExpirationDate = function (/*int*/ days)
  {
    /*Date*/ var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    return date.toGMTString();
  }

/**
 * Private method for setting a cookie.
 * @param   name The name of the cookie.
 * @param	value The value of the cookie.
 * @param	expires The GMT Date of when the the cookie expire on the client's 
 *          machine. If null, the cookie will only persist for the session.
 * @param	path The path the cookie is valid for.
 * @param	domain The domain the cookie is valid for.
 * @param	secure The boolean value used to request a secure cookie. The 
 *          value should be true, false, 1, 0, or null. 
 */
  /*void*/ CookieFactory._setCookie = function (/*String*/name, /*String*/value, /*String*/expires, /*String*/path, /*String*/domain, /*int*/secure)
  {
    var theCookie = name + '=' + value;
    if (expires) { theCookie += '; expires=' + expires; }
    if (path)    { theCookie += '; path=' + path; }
    if (domain)  { theCookie += '; domain=' + domain; }
    if (secure)  { theCookie += '; secure'; }
    
    document.cookie = theCookie;
  }