/*
	CookieHandler.js - read/write cookies
    
		Constructor
			CookieClass(void)
		
		Properties
			bool valid
			
		Methods
			void set(key,val)
			string|false get(key)
			void del(key[,path,domain])
			
	Example
		
		var C = new CookieHandler();
		C.set(‘mykey’, ‘myvalue’);
		alert(C.get(‘mykey’));
			
*/

function CookieHandler () {
	this.valid = false;
	this.val = null;
	this.path = "/";
	this.domain = "";
}

new CookieHandler();

CookieHandler.prototype.get = function (key) {
	this.getv(key);
	return this.valid ? this.val : false;
}

CookieHandler.prototype.set = function (key,val) {

	var today = new Date()
	expires = new Date(today.getTime() + 365*24*60*60*1000);
	document.cookie = key + "=" + escape(val) + "; expires=" + expires.toGMTString() + 
		(this.path=="" ? "" : "; path=" + this.path) + 
		(this.domain=="" ? "" : "; domain=" + this.domain);
}

CookieHandler.prototype.del = function (key,path,domain) {
	document.cookie= key + "=" +
		((path) ? "; path=" + path : this.path) +
		((domain) ? "; domain=" + domain : this.domain) +
		"; expires=Thu 01-Jan-70 00:00:01 GMT";
}

CookieHandler.prototype.getv = function (key) { // sets valid and val, returns true/false on success/failure

	var endstr;
	var arg = key + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0, j=0;
	
	while (i < clen) {
		j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			endstr = document.cookie.indexOf(";", j);
			if (endstr == -1) {
				endstr = document.cookie.length;
			}
			this.val = unescape(document.cookie.substring(j, endstr));
			this.valid = true;
			return true;
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) {
			break;
		}
	}
	this.valid = false;
	return false;
}



