// Notable behaviours of document.cookie as observed in IE 6 / Firefox 3.5 /
// Opera 9.27 / Iron 4.0.280
//  When writing to document.cookie:
//   - Only one cookie can be set with each write. Anything after the first
//     semi-colon in a particular write is interpreted as attributes to the cookie.
//   - If "name=value" has a blank value, ie. "name=",
//     the cookie name is "name" and the cookie value is "".
//   - If "name=value" has a blank name, ie. "=value",
//      In IE 6, Firefox 3.5, Iron 4.0.280:
//       the cookie name is "" and the cookie value is "value".
//      In Opera 9.27:
//       nothing happens
//   - If "name=value" has no equals sign, ie. "word",
//      In IE 6, Firefox 3.5, Iron 4.0.280:
//       it is interpreted as it if were "=word", ie.
//       the cookie name is "" and the cookie value is "word".
//      In Opera 9.27:
//       it is interpreted as it if were "word=", ie.
//       the cookie name is "word" and the cookie value is "".
//  When reading from document.cookie:
//   - The string is "name=value" for each cookie, a semi-colon seperating each pair of cookies.
//     Cookie attributes are not accessible.
//   - Cookies without a value are represented
//      in IE 6: as "name"
//      in Firefox 3.5, Opera 9.27, Iron 4.0.280: as "name="
//   - The cookie with a blank name is represented
//      in IE 6, Firefox 3.5, Iron 4.0.280: as "value"
//      (in Opera 9.27: can't have a cookie with a blank name, see above)


function getCookie(a_name)
// Check if a cookie exists and get its value.
//
// Args:
//  a_name:
//   (string) Name of cookie.
// Returns:
//  (string or undefined)
//  If a cookie with the specified name exists and has a value, that value is returned.
//  If a cookie with the specified name doesn't exist, undefined is returned.
// Caveats:
//  This doesn't work exactly as described in IE 6, for the cookie with the name "" and
//  for cookies without a value. (Later IE versions not yet tested.)
{
    // Split cookies up into "name=value" strings
    var cookies = document.cookie.split(";");

    // Loop through cookies looking for one with a_name
    for (var cookieNo = 0; cookieNo < cookies.length; cookieNo++)
    {
	// Split name and value
	var nameAndValue = cookies[cookieNo].split('=');
	var name = nameAndValue[0].replace(/^\s+|\s+$/g, '');  // trim whitespace on name
        var value;
        //   If there was no equals sign, assume name is "" and value is the present text
	if (nameAndValue.length == 1)
        {
            value = name;
            name = "";
        }
	else
        {
            value = nameAndValue[1].replace(/^\s+|\s+$/g, '');  // trim whitespace on value
        }

	
        //
	if (name == a_name)
	{
	    return unescape(value);
	}
    }

    return;
}

function setCookie(a_name, a_value, a_expires, a_path, a_domain, a_secure)
// Set a cookie's value and attributes.
//
// Args:
//  a_name:
//   (string)
//   Name of the cookie.
//  a_value:
//   (string)
//   Value for the cookie.
//   Required. If not specified you'll end up setting a cookie with no name to
//   the value of a_name.
//  a_expires:
//   (number, Date, string, or undefined)
//   When the cookie expires. This may be:
//    a number: The number of milliseconds relative to now.
//    a Date object: The date of expiry.
//    a string: The date of expiry in the format "Wdy, DD-Mon-YY HH:MM:SS GMT"
//     ("Wdy, DD Mon YY HH:MM:SS GMT", as returned by Date.toGMTString(), is also acceptable.)
//    undefined: cookie doesn't expire.
//  a_path:
//   (string, optional)
//   Path for the cookie.
//   If not specified, it takes on the path of the currently loaded page.
//  a_domain:
//   (string, optional)
//   Domain for the cookie.
//   If not specified, it takes on the domain of the currently loaded page.
//  a_secure:
//   (string, optional)
{
    var expiresEqualsValue = "";
    if (typeof(a_expires) == "number")
    {
        expiresEqualsValue = new Date(new Date().getTime() + a_expires);
        expiresEqualsValue = ";expires=" + expiresEqualsValue.toGMTString();
    }
    else if (typeof(a_expires) == "object")
    {
        expiresEqualsValue = ";expires=" + a_expires.toGMTString();
    }
    else if (typeof(a_expires) == "string")
    {
        expiresEqualsValue = ";expires=" + a_expires;
    }

    var equalsValue;
    if (a_value)
    {
        equalsValue = "=" + escape(a_value);
    }
    else
    {
        equalsValue = "";
    }

    document.cookie = a_name + equalsValue +
	expiresEqualsValue +
	(a_path  ?  ";path=" + path  :  "") + 
	(a_domain  ?  ";domain=" + domain  :  "") +
	(a_secure  ?  ";secure"  :  "");
}

function deleteCookie(a_name, a_path, a_domain)
{
    document.cookie = a_name + "=" +
	(a_path  ?  ";path=" + a_path  :  "") +
	(a_domain  ?  ";domain=" + a_domain  :  "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

