/*************************************************
**
**	Script:		cookie.js
**	Created: 	03 January 2000
**	Language:	JavaScript1.1
**
**	Description:
**	This library script provides functions for
**	creating, storing, reading, and deleting
**	cookies
**
**	Usage:
**	var c = new Cookie(document,'theCookie',240);
**	c.newprop = somevalue; // add new data
**	c.store(); // store new values to the cookie
**	c.retrieve(); // read the cookie
**	c.erase(); // delete the cookie
**
*///**********************************************

function Cookie(document, name, hours, path, domain, secure) {
	this._document = document;
	this._name = name;
	this._expiration = (hours) ? new Date((new Date()).getTime() + (hours*1000*60*60)) : null;
	this._path = (path) ? path : null;
	this._domain = (domain) ? domain : null;
	this._secure = (secure) ? true : false;
}

function store() {
	var properties = "";
	for (var p in this)	{
		if ((p.charAt(0) == '_') || (typeof this[p] == 'function'))
			continue;
		if (properties != "") properties += '&';
		properties += p + '::' + escape(this[p]);
	}
	
	var cookie = this._name + '=' + properties;
	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';
	
	this._document.cookie = cookie;
}

function retrieve() {
	var allcookies = this._document.cookie;
	if (allcookies == "") return false;
	
	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 cookie = allcookies.substring(start, end);
	
	var a = cookie.split('&');
	for (var i = 0; i < a.length; i++) {
		a[i] = a[i].split('::');
	}	
	
	for (var i = 0; i < a.length; i++) {
		this[a[i][0]] = unescape(a[i][1]);
	}
	
	return true;
}

function erase() {
	var cookie;
	cookie = this._name + "=";
	if (this._path) cookie += '; path=' + this._path;
	if (this._domain) cookie += '; _domain=' + this._domain;
	cookie += '; expires=Thu, 01-Jan-70 00:00:01 GMT';
	
	this._document.cookie = cookie;
}

new Cookie;
Cookie.prototype.store = store;
Cookie.prototype.retrieve = retrieve;
Cookie.prototype.erase = erase;
