/*
 * Cookies.js
 *
 * Utility class for handling cookies.
 *
 * (c) Copyright 2006 Future Medium Pty Ltd - http://www.futuremedium.com.au/
 *
 * @author scampbell
 */


/**
 * The Cookie class is responsible for creating and deleting cookies.
 *
 * @param The name of the cookie.
 */
function Cookie(cookieName) {

	/** The name of the cookie. */
	this._cookieName = cookieName;

	/**
	 * Saves a cookie value.
	 *
	 * @param value  The cookie value.
	 * @param expiry The Date when the cookie expires.
	 */
	this.setValue = function(value, expiry) {
		var cookieStr = escape(this._cookieName) + "=" + escape(value);

		if (expiry) {
			cookieStr += "; expiry=" + expiry.toGMTString();
		}

		cookieStr += "; path=/";

		document.cookie = cookieStr;
	}

	/**
	 * Loads a cookie.
	 *
	 * @return The value of the cookie (or null if not found).
	 */
	this.getValue = function() {
		var cookies = document.cookie.split("; ");
		for (var ii = 0; ii < cookies.length; ii++) {
			var aCookie = cookies[ii].split("=");
			if (aCookie[0] == unescape(this._cookieName)) {
				return unescape(aCookie[1]);
			}
		}
		return null;
	}

	/**
	 * Deletes this cookie from the user's browser.
	 */
	this.unload = function(name) {
		var date = new Date();
		date.setTime(-1);

		this.setValue("", date);
	}
}