/**
 *  Javascript cookie object, that is fully compatible with the PxCookie object in PHP
 *  This object can be constructed to access cookies just like PHP object.
 *  This is the constructor that should be called to create the cookie object.
 *  The parameter is the name of the cookie
 *  @param  Name        Name of the cookie
 *  @param  Domain      Optional domain
 *  @param  Path        Optional apath
 */
function PxCookie(name, domain, path)
{
    // store name and other parameters
    this.name = name;
    this.domain = domain;
    this.path = path;

    // the key=value pairs
    this.vars = []; 
    
    // get the parts
    if (document.cookie == '') return;
    var parts = document.cookie.split(/;/);
    
    // loop through the part
    for (var i=0; i<parts.length; i++)
    {
        // remove whitespace and split the key=value pair
        var keyvalue = unescape(parts[i].replace(/^\s+/, '').replace(/\s+$/, '')).split(/=/,2); 
    
        // skip the other cookies
        if (keyvalue[0] != name) continue;
        
        // all variables are seperated by ampersnads
        var pairs = keyvalue[1].split(/&/);
        for (var j=0; j<pairs.length; j++)
        {
            // keyvalue is seperated by a colons
            var keyvalue2 = unescape(pairs[j]).split(/:/,2);
            this.vars[keyvalue2[0]] = unescape(keyvalue2[1]);
        }
    }
}

/**
 *  Member function to retrieve a value from the cookie
 *  @param  Key         Name of the item to retrieve
 *  @param  Value       Option default value if item does not exist
 *  @return string      The requested value
 */
PxCookie.prototype.load = function(key, value)
{
    // lookup the specified key
    if (this.vars[key] == undefined) return value;
    return this.vars[key];
}

/**
 *  Store a new value in the cookie object
 *  This method can be used to store a value in the cookie,
 *  but this does not yet store the cookie in the browser,
 *  after this method, you must also call the method save()
 *  @param  Name        Name of the item to store
 *  @param  Value       Value to store
 */
PxCookie.prototype.store = function(key, value)
{
    // store the key
    return this.vars[key] = value;
}

/**
 *  Remove a value from the cookie
 *  You need to call the method save() to store the cookie
 *  in the browser as well
 *  @param  Key      Key to remove
 */
PxCookie.prototype.remove = function(key)
{
    delete this.vars[key];
}

/**
 *  Store the cookie in the browser
 *  @param Seconds         Number of seconds to store the cookie, 0 to store only for this session
 */
PxCookie.prototype.save = function(seconds)
{
    // initialize cookie text
    var cookietext = this.name + "=";
    
    // concatenate array elements into cookie string
    var first = true;
    for (var key in this.vars)
    {
        // store cookie text
        if (!first) cookietext += '&';
        cookietext += key + ':' + escape(this.vars[key]);
        first = false;
    }
    
    // Set expiry parameter, if specified
    if (seconds)
    {
        var today = new Date();
        var expiredate = new Date();
        expiredate.setTime(today.getTime() + seconds * 1000);
        cookietext += "; expires=" + expiredate.toGMTString();
    }

    // add domain and path if specified
    if (this.domain) cookietext += '; domain='+this.domain;
    if (this.path) cookietext += '; path='+this.path;
 
    // write cookie to browser
    document.cookie = cookietext 
}


