/**
 * Cookie handler Javascript object
 *
 * Brian Gillespie 2006
 */

var CookieHandler = {
	
	DELIM_RE: new RegExp(/;/),
	WEEK_MILLIS: 604800000,
	DAY_MILLIS:   86400000,
	
	/**
	 * Create a cookie.
	 * @param name the name for this cookie
	 * @param value value for the cookie
	 * @param millis duration for this cookie (optional)
	 * @returns false if there was a semi-colon in either the name or the value
	 *   of the cookie, which is not allowed.
	**/
	create: function(name, value, millis) {
		
		// check for semi-colons (disallowed)
		if (CookieHandler.DELIM_RE.test(name) || CookieHandler.DELIM_RE.test(value))
			return false;
		
		var expires = "";
		if (millis)	{
			var date = new Date();
			date.setTime(date.getTime() + millis);
			expires = "; expires=" + date.toGMTString();
		}
		
		document.cookie = name + "=" + value + expires + "; path=/";
		return true;
	},
	
	/**
	 * Return the value of a cookie as a string.
	 * @param name the name of the cookie to search for.
	 * @returns NULL if the cookie doesn't exist.
	**/
	read: function(name) {
		var c, tokens = document.cookie.split(';'),
				matching = new RegExp('^\s*' + name + '\s*=')
		;
		for(var i in tokens) {
			c = tokens[i];
			if (matching.test(c)) return c.replace(matching, '');
		}
		return null;
	},
	
	/**
	 * Clear a cookie.
	 * @param name the name of the cookie.
	**/
	erase: function(name) {
		CookieHandler.create(name, "", -1);
	}

}
