/**
 * Sets a cookie using the specified parameters.
 * 
 * @param name a name for the cookie
 * @param value the value to store in the cookie
 * @param expires the cookie's lifetime
 * @param path the path in which to store the cookie
 * @param domain the domain for which the cookie is being saved
 * @param secure whether or not the cookie data should be secure
 */
function setCookie(name, value, expires, path, domain, secure) {
    // Setup
    var today = new Date();
    var expireDate = null;
    var cookieString = "";
    
    // Set Details
    today.setTime(today.getTime());
    
    if(expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    
    expireDate = new Date(today.getTime() + expires);
    
    // Build Cookie String
    cookieString = name + "=" + escape(value);
    
    if(expires) {
        cookieString += ";expires=" + expireDate.toGMTString();
    }
    
    if(path) {
        cookieString += ";path=" + path;
    }
    
    if(domain) {
        cookieString += ";domain=" + domain;
    }
    
    if(secure) {
        cookieString += ";secure";
    }
    
    // Set the Cookie
    document.cookie = cookieString;
}

/**
 * Attempts to retrieve a cookie with the specified name.
 * 
 * @param name the name of the cookie to retrieve
 * @return the value of the requested cookie, or null if no cookie was found
 */
function getCookie(name) {
    // Setup
    var allCookies = document.cookie.split(";");
    var tempCookie = "";
    var cookieName = "";
    var cookieValue = "";
    var cookieFound = false;
    
    // Parse Cookie
    for(i = 0; i< allCookies.length; i++) {
        tempCookie = allCookies[i].split("=");
        cookieName = tempCookie[0].replace(/^\s+|\s+$/g, "");
        
        if(cookieName == name) {
            cookieFound = true;
            
            if(tempCookie.length > 1) {
                cookieValue = unescape(tempCookie[1].replace(/^\s+|\s+$/g, ""));
            }
            
            break;
        }
        
        tempCookie = null;
        cookieName = "";
    }
    
    if(!cookieFound) {
        cookieValue = null;
    }
    
    return cookieValue;
}

/**
 * Attempts to delete a cookie with the specified name/path/domain. Note that
 * the cookie will not actually be deleted, but will be set to be expired,
 * signalling the browser it is no longer needed and should be removed.
 * 
 * @param name the name of the cookie to delete
 * @param path the path within which to look for the cookie
 * @param domain the domain for which the cookie was set
 */
function deleteCookie(name, path, domain) {
    // Setup
    cookieString = "";
    
    // Attempt to Get Cookie
    if(getCookie(name)) {
        cookieString = name + "=";
        
        if(path) {
            cookieString += ";path=" + path;
        }
        
        if(domain) {
            cookieString += ";domain=" + domain;
        }
        
        cookieString += ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
        
        // Set the Cookie
        document.cookie = cookieString;
    }
}

/**
 * Sets a cookie with a serialized array.
 *
 * @param name a name for the cookie
 * @param value the value to store in the cookie
 * @param expires the cookie's lifetime
 * @param path the path in which to store the cookie
 * @param domain the domain for which the cookie is being saved
 * @param secure whether or not the cookie data should be secure
 */
function setArrayCookie(name, array, expires, path, domain, secure) {
    // Setup
    var delimiter = ",";
    var arrayString = "";
    
    // Serialize Array
    arrayString = array.join(delimiter);
    
    // Set the Cookie
    setCookie(name, arrayString, expires, path, domain, secure);
}

/**
 * Gets a cookie with a serialized array.
 *
 * @param name the name of the cookie to retrieve
 * @return the value of the requested cookie, or null if no cookie was found
 */
function getArrayCookie(name) {
    // Setup
    var array = null;
    var arrayString = new String("");
    
    // Retrieve Cookie
    arrayString = new String(getCookie(name));
    
    // Unserialize Array
    if(arrayString) {
        array = arrayString.split(",");
    }
    
    return array;
}
