function eURI(s,full) {
	if(typeof(encodeURIComponent)=='function') {
		if(full) { return encodeURI(s); } // for complete URIs (incl http://)
		else { return encodeURIComponent(s); } // for components of a URI, incl. query strings
	} else {
		return escape(s);
	}
}

function dURI(s) {
	if (typeof(decodeURIComponent) == 'function') {
		return decodeURIComponent(s);
	} else {
		return unescape(s);
	}
}

function Cookie(document, name, seconds, path, domain, secure)
{
	this.$document = document;
	this.$name = name;
	if(seconds) this.$expiration = new Date( (new Date()).getTime() + seconds*1000);
	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;
}

Cookie.prototype.store = function() {
	var cookieval = "";
	for(var prop in this) {
		if( (prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') )
			continue;
		if(cookieval != "") cookieval += '&';
		cookieval += prop + ':' + this[prop];
	}
	//alert('storing js cookie... Name: "'+this.$name+'" '+' Val:'+cookieval);
	var cookie = this.$name + '=' + eURI(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';
	this.$document.cookie = cookie;
}

Cookie.prototype.set = function() {
	this.store();
}

Cookie.prototype.load = function() {
	var allcookies = this.$document.cookie;
	if(allcookies == "") return false;
	var a, a2, i, j, found=false, cookieval;
	a = allcookies.split(';');
	for(i=0; i<a.length; i++) {
		a2 = a[i].split('=');
		a2[0]=a2[0].replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); // trim
		//alert(a2[0]+ '==' + this.$name + "\n" + (a2[0]==this.$name));
		if(a2[0]==this.$name) {
			found = true;
			cookieval = a2[1];
			break;
		}
	}
	if(!found) { return false; }
	cookieval = dURI(cookieval);
	this.$cookie_value = cookieval;
	//alert('entire stored cookie val for "'+this.$name+'": '+cookieval);
	var a = cookieval.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]] = dURI(a[i][1]);
	}
	return true;
}

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;
}
