

/******************************************************************************
function write_cookie (name, value);
function write_cookie (name, value, days);

write_cookie creates a cookie with the name, value, and days given in the
parameters.  If no length parameters are supplied, write_cookie uses the
default expiration time of 365 days.
******************************************************************************/

function write_cookie (name, value, days)
{
	// Build the expiration date string:
	if (!days) {
		days = 365;
	} 
		
	var expiration_date = new Date();
	expiration_date.setDate(expiration_date.getDate () + days);
	expiration_date = expiration_date.toGMTString ();

	// Build the set-cookie string:
	var cookie_string = escape (name) + "=" + escape (value) +
		"; expires=" + expiration_date;

	// Set path to root for all cookies
	cookie_string += "; path=/";

	// Create/update the cookie:
	document.cookie = cookie_string;
}


/******************************************************************************
function read_cookie (key)
function read_cookie (key, skips)

read_cookie searches through the current document's cookie string (i.e., the
concatenation of all cookies readable by the current document) for a cookie
whose name is identical to key.  If read_cookie finds a matching cookie, it
returns a string containing the value of cookie.  If read_cookie cannot find a
match, it returns null instead.

An optional skips parameter may be supplied if there is a need to select among
multiple cookies with the same name.  If a skips parameter is supplied,
read_cookie will skip that many occurrences of matching cookies and then return
the next one it finds, or null if there aren't any more.
******************************************************************************/

function read_cookie (key, skips)
{
	// Set skips to 0 if parameter was omitted:
	if (skips == null)
		skips = 0;

	// Get cookie string and separate into individual cookie phrases:
	var cookie_string = "" + document.cookie;
	var cookie_array = cookie_string.split ("; ");

	// Scan for desired cookie:
	for (var i = 0; i < cookie_array.length; ++i)
	{
		var single_cookie = cookie_array [i].split ("=");
		if (single_cookie.length != 2)
			continue;
		var name  = unescape (single_cookie [0]);
		var value = unescape (single_cookie [1]);

		// Return cookie if found:
		if (key == name && skips -- == 0)
			return value;
	}

	// Cookie was not found:
	return null;
}

// better "create cookie" function. if no days are passed, session ends when browser closes.// 
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}

	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie (name) {
write_cookie (name,"",-1);
}