// Used in:
//		1. /clients/meamanatt			No opts on write.
//		2. /clients/winstonstrawngp		No opts on write.
//		3. /clients/keesalyoung

var Cookie = {
	// Checks for cookie's existance.
	has: function(key) {
		var cookies = document.cookie.split(/;\s*/);
		var cookie;
		
		key = escape(key);	// Remove naughty characters.

		for (var i = 0; i < cookies.length; ++i) {
			cookie = cookies[i].split("=");

			if (cookie.length > 0 && cookie[0] == key) {
				return true;
			}
		}

		return false;
	},
	// Reads the value of a cookie.  An empty string is returned if no cookie.
	read: function(key) {
		var cookies = document.cookie.split(/;\s*/);
		var ret = "", cookie, i, st = cookies.length;
		
		key = escape(key);	// Remove naughty characters.

		for (i = 0; i < st; ++i) {
			cookie = cookies[i].split("=");

			if (cookie.length && cookie[0] == key) {
				if (cookie.length > 1) {
					ret = unescape(cookie[1]);	// Return string to its normal form.
				}
			}
		}

		return ret;
	},
	// Removes a cookie.
	remove: function(key) {
		this.write({key: key, value: "", days: -1});
	},
	// Writes a cookie.  Only key is required.
	write: function(opts) {
		var msInDay = 86400000;				// Number of milliseconds in a day.
		
		if (opts.key) {
			var cookie;
			
			// Set cookie value.
			cookie = escape(opts.key)		// Remove naughty characters.
				+ "="
				+ escape(opts.value || "")	// Remove naughty characters.
				+ "; ";
			
			// Set expire date.
			if (opts.date) {
				cookie += "expires="
					+ opts.date.toGMTString()
					+ "; ";
			}
			else if (opts.days) {
				var today = new Date();

				today.setTime(today.getTime() + (opts.days * msInDay));
				cookie += "expires="
					+ today.toGMTString()
					+ "; ";
			}

			// Set path to restrict to.
			if (opts.path) {
				cookie += "path="
					+ escape(opts.path)		// Remove naughty characters.
					+ "; ";
			}

			// Set domains to restrict to.
			if (opts.domain) {
				cookie += "domain="
					+ escape(opts.domain)	// Remove naughty characters.
					+ "; ";
			}

			// Restrict cookie to https.
			if (opts.secure === true) {
				cookie += "secure; ";
			}

			document.cookie = cookie;
		}
	}
};
