/** Return a Cookie with the given name, or if one doesn't exist, a Cookie where isEmpty=true */
function getCookie(name) {
	var nameStr = name + "=";
	var cookieComponents = document.cookie.split(';');
	var value;
	for(var i=0;i < cookieComponents.length;i++) {
		var c = cookieComponents[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameStr) == 0) {
			value = unescape(c.substring(nameStr.length,c.length));
		}
	}
	return new Cookie(name, value);
}

function getAllCookies() {
	var cookieComponents = document.cookie.split(';');
	var cookies = [];
	for(var i=0;i < cookieComponents.length;i++) {
		var c = cookieComponents[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		var elements = c.split('=');
		if (elements.length == 2) {
			var cookie = new Cookie(elements[0],unescape(elements[1]));
			cookies.push(cookie);
		}
	}
	return cookies;
}

/** An object representing a cookie, including methods to set the value, expiration 
 *  date, path and domain.  In general, Cookies should be created using the getCookie
 *  method defined above. */
function Cookie(name, value) {
	var mName = name;
	var mValue;
	var mPath;
	var mDomain;
	var mDate;
	
	this.getName = function() {
		return mName;
	}
	
	this.isEmpty = function() {
		return (mValue=="");
	}
	
	this.setValue = function(newValue) {
		if (!newValue) {
			mValue = "";
		} else {
			mValue = newValue;
		}
	}
	/* Set the value with the function we just created */
	this.setValue(value);
	
	this.getValue = function() {
		if(!mValue) {
			return "";
		}
		return mValue;
	}
	
	this.setExpiration = function(hoursFromNow) {
		mDate = new Date();
		mDate.setTime(mDate.getTime()+(hoursFromNow*60*60*1000));
	}
	
	this.setPath = function(newPath) {
		mPath = newPath;
	}
	
	this.setDomain = function(newDomain) {
		mDomain = newDomain;
	}
	
	this.write = function() {
		if (mDate) {
			var expires = "; expires="+mDate.toGMTString();
		} else {
			var expires = "";
		}
		if (mPath) {
			var pathStr = "; path="+mPath;
		} else {
			var pathStr = "";
		}
		if (mDomain) {
			var domainStr = "; domain="+mDomain;
		} else {
			var domainStr = "";
		}
		document.cookie = mName+"="+escape(mValue)+expires+pathStr+domainStr;
	}
	
	this.kill = function() {
		// set cookie to expire yesterday.
		this.setExpiration(-1000);
		this.setValue("");
		this.write();
	}
}