
function Cookie(document, name, days, path, domain, secure)
{
	var when = new Date();
	
	this._document = document;
	this._name = name;

	if (!days) {
		// transient cookie
		this._expiration = null;
	} else {
		this._expiration = new Date();
		
		if (days < 0) {
			// make it 10 years in the future
			this._expiration.setFullYear(this._expiration.getFullYear() + 10);
		} else {
			this.expiration.setDate(this.expiration.getDate() + days);
		}
	}
		
	this._path = path ? path : null;
	this._domain = domain ? domain : null;
	this._secure = secure ? true : false;
}

Cookie.prototype.store = function() 
{
	var cookieval = "";
	
	for (var prop in this) {
		// ignore properties that begin with '_' and also methods
		if ((prop.charAt(0) == '_') || ((typeof this[prop]) == 'function'))
			continue;
		if (cookieval != "")
			cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}
	
	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';
		
	// now store it
	this._document.cookie = cookie;
}

Cookie.prototype.load = function()
{
	// get list of all cookies for this document
	var allCookies = this._document.cookie;
	if (allCookies == "")
		return false;
		
	// now get our cookie from the list
	var start = allCookies.indexOf(this._name + '=');
	if (start == -1)
		return false;
	start += this._name.length + 1;
	
	var end = allCookies.indexOf(';', start);
	if (end == -1)
		end = allCookies.length;
		
	var cookieval = allCookies.substr(start, end);
	
	// now break out the individual attributes
	var a = cookieval.split('&');
	for (var i = 0; i < a.length; i++) {
		// break each pair into an array
		a[i] = a[i].split(':');
	}
	
	// now import the values
	for (var i = 0; i < a.length; i++) {
		this[a[i][0]] = unescape(a[i][1]);
	}
	
	return true;
}

Cookie.prototype.remove = function()
{
	var 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;
}

