/*
 *----------------------------------------------------------------------
 *
 * Cookie functions:
 *   Programs call these functions to read/write data into the cookie
 *   associated with this web site.  The cookie, which is stored at the
 *   reader's site, associates a data item (e.g., a reader's name) with
 *   a keyword (e.g., the string "username").  Thus, cookies allow you
 *   to customize a site per user.  For example, you can use cookies
 *   to detect that someone has never visited your site before, or
 *   to customize a site based on user preferences (e.g. to set a
 *   background color).
 *
 * Retrictions on cookies:
 *   Each data value has a lifetime.  By default, cookies last for
 *   the duration of a browser session, but this behavior can be
 *   over-ridden by explictly setting a lifetime.
 *
 *   The keyword with which you associate a data value may not
 *   contain semicolons, commas, or white space.
 *
 *   A cookie keyword/value pair may not be more than 4 KBytes in
 *   length
 *
 * Cookie functions:
 *   SetCookie    -- Set a cookie value
 *                   CS 130 student's web site.
 *   GetCookie    -- Retrieve a previously set cookie value
 *   DeleteCookie -- Delete a cookie value
 *
 *----------------------------------------------------------------------
 */


/*
 *----------------------------------------------------------------------
 * SetCookie --
 *
 *   This function sets a keyword/value pair in the cookie.
 *   It allows you to specify the lifetime of the cookie,
 *   which is given in days and may be fractional.  After that
 *   time period, the cookie will be deleted.  If the lifetime
 *   is less than 0, the cookie will only last for the duration
 *   of the browser session.
 * 
 * Examples:
 *   To associated the name "bob" with the keyword
 *   "user" for the next 30 days, use
 *      SetCookie ("user", "bob", 30);
 *
 *   To associated the name "dave" with the keyword
 *   "bsmith_user" for the next 12 hours, use
 *      SetCookie ("bsmith_user", "dave", 0.5);
 *
 * Parameters:
 *   name      -- the keyword used to find the value
 *   value     -- the value associated with name
 *   lifetime  -- the time (in days) until the datum is deleted.
 *
 *----------------------------------------------------------------------
 */
function SetCookie (name, value, lifetime) {
    var expires, tmp;
    var expiresStr = "";

    /*
     * Set expiresStr to lifetime of the data
     */
    if (lifetime >= 0) {
        expires = new Date();
        tmp = Math.ceil (1000 * 60 * 60 * lifetime);
        expires.setTime (tmp + expires.getTime());
        expiresStr = "; expires=" + expires.toGMTString();
    }

    
    /*
     * Set the cookie value to store the data
     */
    document.cookie = name + "=" + escape(value) + expiresStr;
}   

/*
 *----------------------------------------------------------------------
 *
 * GetCookie --
 *
 * Programs call this function to retrieve a keyword/value
 * pair that previously stored in the cookie (using the SetCookie
 * function).
 * 
 * Examples:
 *    To retrieve the data associated with the keyword "user" and
 *    assign it to the variable 'id':
 *        var id = GetCookie ("user");
 *
 * Parameters:
 *    name      -- the keyword used to find the value
 *----------------------------------------------------------------------
 */
function GetCookie (name) {
    var cookie = document.cookie; 
    var cookieLen = cookie.length;
    var cookieName, start, end;

    /*
     * Check for three cases:
     *    value we want is at the beginning of the cookie
     *    value we want is in middle of the cookie
     *    value we want isn't present.
     * Set start to index of beginning of cookie, 
     * or return null if value isn't present
     */
    cookieName = "; " + name + "=";
    start = cookie.lastIndexOf(cookieName);
    if (start == -1) {
        cookieName = name + "=";
        start = cookie.indexOf(cookieName, 0);
        if (start != 0) {
            return null;
        }
    }
    start += cookieName.length;
    end = cookie.indexOf (";", start);
    if (end == -1) {
        end = cookieLen;
    }
    return unescape(cookie.substring(start, end));
}

/*
 *----------------------------------------------------------------------
 * DeleteCookie --
 *
 * Programs call this function to delete the value 
 * associated with a keyword previously stored in the cookie
 * (using the SetCookie function).
 * 
 * Examples:
 *        To delete the data in the cookie associated with the keyword "user":
 *                DeleteCookie ("user");
 *
 * Parameters:
 *        name      -- the keyword used to find the value
 *----------------------------------------------------------------------
 */
function DeleteCookie (name) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
