/**
 * Class for working with name|value pair cookies.
 * @param      string       cookie name we will be working on.
 */
function Cookie(sCookieName)
{
    this.sCookieName   = sCookieName || 'Jazz-Cookie';
    this.nExpires      = (3*365*24*60*60*1000); // default expires 3 years
    this.sPath         = '/';                   // default path
    this.sDomain       = '';                    // default domain
    this.sDelimiter    = '|';                   // default delimiter
    this.bValidCookie  = false;                 // set true if cookie found
    this.aData         = new Array();           // Associative array of cookie Array[name] => value
}    
    
/**
 * Read in the cookie into an associative array cData[name] => value
 * @return     aReturn
 */   
Cookie.prototype.readCookie = function () 
{  
    // reset vars in case we are re-reading the cookie
    this.bValidCookie = false;
    this.aData        = Array();
    
    var aAll     = document.cookie.split(';');
    var sFull    = '';
    var aFull    = new Array();

    if (aAll) 
    {
        // set up the regex to get the correct cookie
        var sPattern   = '(^' + this.sCookieName + '=)';
        var oCookieReg      = new RegExp(sPattern, 'g');
        
        // loop through all the cookies to get the one we want
        for (i = 0; i < aAll.length; i++) 
        {
            // trim each value so the RegExp matches the beginning of the line correctly
            aAll[i] = aAll[i].replace(/^\s+|\s+$/g, '');
            
            if (oCookieReg.test(aAll[i]))
            {
                sFull = unescape(aAll[i]);
                continue;  
            }
        }
        
        // if we found our cookie put into the aData array
        if (sFull) 
        {
            aFull = sFull.split('=');
            aFull = aFull[1].split('|');
            for(i = 0; i < aFull.length; i += 2) 
            {
                this.aData[aFull[i]] = aFull[i + 1];
            }
            
            this.bValidCookie = true;
        }
    }
}  

/**
 * Set when the cookie expires
 * @param       int      microseconds till cookies should expire
 */
Cookie.prototype.setExpires = function (nExpires) 
{
    this.nExpires = nExpires;
}    

/**
 * Set the path for the cookie
 * @param     sPath
 */
Cookie.prototype.setPath = function (sPath)
{
    this.sPath = sPath;
}

/**
 * Set the domain for the cookie
 * @param     sDomain
 */
Cookie.prototype.setDomain = function (sDomain)
{
    this.sDomain = sDomain;
}    

/**
 * Set the delimeter for the cookie
 * @param     sDelimiter
 */
Cookie.prototype.setDelimiter = function (sDelimiter)
{
    this.sDelimiter = sDelimiter;
}      

/**
 * Return the cookie as a string
 * @return    sReturn
 */
Cookie.prototype.cookieToString = function ()
{
    var sReturn = this.sCookieName + ":\n";
    for (i in this.aData)
    {
        sReturn += i + ' => ' + this.aData[i] + "\n";
    }
    return sReturn;
}

/**
 * Set the value for a given param name
 * @param      string      param name
 * @param      string      param value
 */
Cookie.prototype.setCookieData = function (sName, sValue) 
{
    this.aData[sName] = sValue;
    this.writeCookie();
}

/**
 *   Return the value for the given name
 *   @param      string      cookie param name
 *   @return     string      cookie value
 */
Cookie.prototype.getCookieData = function (sName) 
{
    return(this.aData[sName]);
}

/**
 *   Check if cookie is valid
 *   @return     boolean
 */
Cookie.prototype.isValidCookie = function () 
{
    return this.bValidCookie;
}    
  
/**
 * Write the cookie
 */
Cookie.prototype.writeCookie = function () 
{
    // build the cookie from the array
    var aTemp   = new Array();
    var sValue = '';
    var k      = 0;
    for (key in this.aData) 
    {
        aTemp[k] = key + this.sDelimiter + this.aData[key];
        sValue = aTemp.join(this.sDelimiter);
        k++;
    }
    
    // set expiration        
    var oExpdate = new Date();
    oExpdate.setTime (oExpdate.getTime() + this.nExpires);

    var aCookie  = new Array();        
    aCookie.push(this.sCookieName + "=" + escape (sValue) + ';');
    aCookie.push('expires=' + oExpdate.toGMTString() + ';');
    aCookie.push('path=' + this.sPath + ';');
    if (this.sDomain.length > 0) aCookie.push('domain=' + this.sDomain + ';');

    document.cookie = aCookie.join('');

    this.bValidCookie = true;
}

/**
 * Clear the cookie by setting expires to beginning of UNIX time
 */
Cookie.prototype.clearCookie = function ()
{
    aCookie = new Array();
    aCookie.push(this.sCookieName + '=;');
    aCookie.push('expires=Thu, 01-Jan-70 00:00:01 GMT;');
    aCookie.push('path=' + this.sPath + ';');
    if (this.sDomain.length > 0) aCookie.push('domain=' + this.sDomain + ';');
    
    document.cookie = aCookie.join('');

    // re-read the cookie
    this.readCookie();
}
     