// Cookie() constructor
function Cookie(document, name, hours, path, domain, secure) {
	this.$document = document;
	this.$name = name;
	if (hours)
		this.$expiration = new Date((new Date()).getTime() + hours * 3600000);
	else
		this.$expiration = null;
	if (path) this.$path = path; else this.$path = null;
	if (domain) this.$domain = domain; else this.$domain = null;
	if (secure) this.$secure = true; else this.$secure = false;
}
// store() method
Cookie.prototype.store = function() {
	// Loop through the properties of the Cookie object and
	// build the values of the cookie. Since cookies use the
	// equal sign and semicolons as delimeters, we'll use
	// colons and ampersands. We escape() the value of each
	// state var in case it contains punctuation or other funky chars.
	var cookieval = "";
	for (var prop in this) {
		// Ignore properties with names that begin with '$' and also methods.
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
			continue;
		if (cookieval != "") cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}

	// Build the complete cookie string
	var cookie = this.$name + '=' + cookieval;
	if (this.$expiration) cookie += '; expires=' + this.$expiration.toGMTString();
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	if (this.$secure) cookie += '; secure';

	// Store the cookie
	this.$document.cookie = cookie;
}

// load() method
Cookie.prototype.load = function() {
	var allcookies = this.$document.cookie;	// Get all cookies in this document.
	if (allcookies == "") return false;

	// Extract the named cookie from the list.
	var start = allcookies.indexOf(this.$name + '=');
	if (start == -1) return false;		// Cookie not found for this page.
	start += this.$name.length + 1;		// Skip name and equal sign.
	var end = allcookies.indexOf(';', start);
	if (end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start, end);

	var a = cookieval.split('&');		// Split into array of name/value pairs.
	for (var i = 0; i < a.length; i++)	// Split each pair into an array.
		a[i] = a[i].split(':');

	for (var i = 0; i < a.length; i++)	// Set all the state vars in this Cookie object.
		this[a[i][0]] = unescape(a[i][1]);

	return true;	// Success
}

// remove() method
Cookie.prototype.remove = function() {
	var cookie;
	cookie = this.$name + '+';
	if (this.$path) cookie += '; path=' + this.$path;
	if (this.$domain) cookie += '; domain=' + this.$domain;
	cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';

	this.$document.cookie = cookie;
}
