
/**********************************************************************************
 *				Cookies library - Jon Brewster
 **********************************************************************************/

/**********************************************************************************
 * string = GetValues(string, int)
 *
 * Return cookie values or null if not set, or wrong version.
 **********************************************************************************/

function GetValues(name, version) {
    var value;		// a cookie value
    var varray;		// an array of strings

    // check cookie fmt version
    if (!(value = GetCookie(name + "v")))
        return(null);
    if (eval(value) != version)
        return(null);
    if (!(value = GetCookie(name)))
        return(null);
    varray = value.split("|");

    return(varray);
}

/**********************************************************************************
 * string = GetCookie(string)
 *
 * Return cookie value or null if not set.
 **********************************************************************************/

function GetCookie(name) {
    var search;
    var offset, end;

    search = name + "=";
    if (document.cookie.length > 0) {		// if there are any names in cookie
        offset = document.cookie.indexOf(search);
        if (offset != -1) {			// if name exists in cookie
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) 
                end = document.cookie.length;
            return(unescape(document.cookie.substring(offset, end)));
        } 
    }
    return(null);
}

/**********************************************************************************
 * SetValues(string, int, string)
 *
 * Take a name, version, and value set and store into the cookie.
 **********************************************************************************/

function SetValues(name, version, values) {

    SetCookie(name + "v", "" + version);
    SetCookie(name, values);
}

/**********************************************************************************
 * SetCookie(string, string)
 *
 * Take a name / value pair and store into the cookie.
 **********************************************************************************/

function SetCookie(name, value) {
    var expires;

    // set value to expire 1 year out
    expires = new Date();
    expires.setYear(expires.getYear() + 1);

    document.cookie = name + "=" + escape(value) + "; expires=" +
        expires.toGMTString();
}

/**********************************************************************************
 *				End of cookies library
 **********************************************************************************/
