/*
** cookies.js
**   Functions for retrieving/setting cookies.
**   Johnny Cuervo <cuervo@digits.zerokarma.sleepers.net>
**
**   This code is public domain! :-)
**   (Caution: overdose of word "cookie"...)
**
**   http://web.zerokarma.sleepers.net/~cuervo/js/
*/
var RCSID = new Array();
RCSID["cookies.js"] = '$Id: cookies.js,v 1.1 2002/12/10 16:00:14 cuervo Exp cuervo $';

var cookieBase64Magic = new String();
/*
** If you want Base64-encoded cookie capability (the optional fourth argument
** to setCookie()), set this to something unlikely to appear in the first X
** bytes of your cookie values, where X is the length of the string -- the
** default should be okay.
**
** When you set this, your cookies will go from this:
**    wantFortunes=true;
**
** to something like this:
**    wantFortunes=$B64$d1JHXL===;
**
** ...which ought to make the user sort of wonder what the hell is going on
** with your cookies. :-) But fuck 'em, anyway, users suck.
*/
cookieBase64Magic = '$B64$';

/*
** Cookies[] is a hash that holds all the current cookies in the document;
** e.g., Cookies["hello_world"] = "true", Cookies["did_something"] = "false"...
**
** The first run through getCookie() initializes this hash; subsequent calls
** do not modify it unless document.cookie has changed.
**
** setCookie(x,y) will append to this array and return getCookie(x).
*/
var Cookies = new Array();
Cookies     = [];

var CookiesInitialized = new String();
CookiesInitialized     = false;

/* getCookie(cookieName, raw)
**    Retrieves a cookie value. Returns the value of the named cookie, or null
**    if the cookie was not found. If no cookie is given, the internal cookie
**    hash is initialized, if it wasn't already, and null is returned.
**
**    If the second parameter is true, base64-encoded cookies are returned
**    as-is -- that is, base64-encoded.
*/
function getCookie(name, raw)
{
	var allCookies = new String();
	var acookies   = new Array();
	var ac         = new Array();
	var v          = new String();
	var i, sc;

	/*
	** Check our comparison variable to see if the cookie
	** string has changed since our last run (new/deleted cookies)
	*/
	if(CookiesInitialized && document.cookies != CookiesInitialized)
	{
		CookiesInitialized = false;
	}

	if(! document.cookie)
	{
		return false;  /* document has no cookies */
	}
	else
	if(!CookiesInitialized)
	{
		/* First run, or changed cookie table */
		allCookies = document.cookie;
		acookies   = allCookies.split("; ");

		for(i = 0; i != acookies.length; i++)
		{
			ac = acookies[i].split("=");

			if(!ac[1])
				v = true;    /* simple cookie, no value */
			else
			{
				v  = ac[1].substr(0,
					(sc=ac[1].indexOf(";")) != -1
					? sc : ac[1].length);

				/* Check if it's a base64-encoded cookie */
				if((!raw)
					&& v.substr(0,cookieBase64Magic.length)
						== cookieBase64Magic)
				{
					/* Yup */
					v = atob(v.substr(cookieBase64Magic.length));
				}
			}

			Cookies[ac[0]] = v;
		}

		/* Update our comparison variable */
		CookiesInitialized = document.cookie;
	}

	/* At this point, the cookie hash is initialized */

	if(! Cookies[name])
	{
		return false;
	}
	else
	if(Cookies[name]
		&& Cookies[name].length
		&& Cookies[name].substr(0,cookieBase64Magic.length)==cookieBase64Magic)
	{
		return (raw ? Cookies[name]
			: atob(Cookies[name].substr(cookieBase64Magic.length)));
	}
	else
	{
		return Cookies[name];
	}

	alert("NOT REACHED");
	return false;
}

function b64_cookie(s)
{
	var str = new String(s);
	return str.substr(0, cookieBase64Magic.length) == cookieBase64Magic;
}

/* setCookie()
**    Tells the browser to set a cookie, optionally encoding the value in
**    base64 first. (Default is to store as plaintext.) If the third argument
**    ("properties") is given, this is sent as the optional cookie modifier
**    string -- e.g., "path=/; domain=sleepers.net".
*/
function setCookie(name, value, properties, encode)
{
	var cVal = new String();

	if(encode)
	{
		/*
		** It seems there aren't a lot of browsers this works with
		** (that implement btoa()); maybe only Netscape. I need to
		** do some looking into this.
		*/
		encode = false;

		/*
		if(navigator.appName == "Microsoft Internet Explorer")
		{
			encode = false;
		}
		*/
	}

	if(encode)
	{
		cVal = cookieBase64Magic + "" + btoa(value);
	}
	else
	{
		cVal = value;
	}

	document.cookie = name + '=' + cVal +
		(properties ? ("; "+ properties) : "");

	return getCookie(name);	/* update */
}

/* unsetCookie()
**    Deletes a cookie (by setting the same cookie with an expired
**    timestamp -- FSR, 02 Jan 1970 seems to be the Right Thing to do
**    here, but I have no idea why).
*/
function unsetCookie(name)
{
	setCookie(name, 'deleted',
		'expires=Fri, 02-Jan-1970 00:00:00 GMT;', 0);
	getCookie(name);
}
