//==================================================================================
// cookie functions, to store and retrieve HTTP cookies
// These cookie functions also use escape/unescape of the cookie value (not needed)
// author: Sam Sultan
//==================================================================================

//-------------------------------------------------------------------------------
//setCookie:  sets an HTTP cookie
//     args:  name  - the name of the cookie to be set
//            value - the value of the cookie to be set
//            exp   - the number of days from today, to expire the cookie
//                        0 to make cookie persist for session only
//                        any negative number to delete the cookie
//   return:  none
//-------------------------------------------------------------------------------
function setCookie(name, value, expiration) {

    if (name == "")				//if no cookie name, return
        return;

    today   = new Date();                        //today's date
    elapse  = today.getDate() + expiration;      //elapse  = today + exp days
    expDate = new Date();                        //today's date
    expDate.setDate(elapse);                     //set expDate to elapsed days

    if (expiration == 0)    
        document.cookie = name + "=" + escape(value) + "; path=/";
    else
        document.cookie = name + "=" + escape(value) + "; path=/;" +
                         " expires=" + expDate.toGMTString();    
}

//-------------------------------------------------------------------------------
//getCookie:  retrieves the value of a given HTTP cookie
//     args:  name  - the name of the cookie to retrieve
//   return:  value - the value of the cookie
//-------------------------------------------------------------------------------
function getCookie(name) {
    var value = '';

    var httpCookies = document.cookie;		    //get all the cookies
    var allCookies  = httpCookies + "; "            //append '; ' at the end

    var found = allCookies.indexOf(name);            //find the cookie name 
    if (found >=0) {                                 //if cookie name is found
        var beg = allCookies.indexOf("=",found) +1   //get the beginning of value
        var end = allCookies.indexOf(";",found)      //get the end of value            
        value = allCookies.substring(beg,end);       //extract of the value
        value = unescape(value);                     //convert URL encoding to chars
    }
    return(value);				     //return the cookie value
} 

//-------------------------------------------------------------------------------
//getCookies:  retrieves all HTTP cookies
//      args:  none
//    return:  cookies - an object containing all the cookies in name/value pairs
//-------------------------------------------------------------------------------
function getCookies() {
    if (document.cookie == '')
        return;

    var cookies = new Object();

    var httpCookies = document.cookie;		    //get all the cookies
    var allCookies  = httpCookies.split("; ");      //split all cookies at '; '

    for (i=0; i < allCookies.length; i++) {         //start a loop
        nameValue = allCookies[i].split("=");       //split each cookie at '='
        var name  = nameValue[0];                   //the name part
        var value = nameValue[1];                   //the value part
        value     = unescape(value);                //convert URL encoding to chars
        cookies[name] = value;                      //build the cookies object
    }           
    return(cookies);				    //return the cookies object
}
//-------------------------------------------------------------------------------