//General purpose Cookie class (typically saved in separate cookie.js file).
var Cookie = function(name, hours, path, domain, secure) {
	this.$name = name;
	this.$expiration = (hours) ? new Date((new Date()).getTime() + hours*3600000) : null;
	this.$path = (path) ? path : null;
	this.$domain = (domain) ? domain : null;
	this.$secure = (secure) ? true : false;
	this.store = function() {
		var prop;
		var cookievalArray = [];
		for(prop in this){
			if((prop.charAt(0)!=='$') && (typeof this[prop]!=='function')) {
				cookievalArray.push( prop + ':' + escape(this[prop]) );
			}
		}
		var cookie = [ this.$name + '=' + cookievalArray.join('&') ];
		cookie.push( (this.$expiration) ? 'expires=' + this.$expiration.toGMTString() : '' );
		cookie.push( (this.$path)       ? 'path='    + this.$path                     : '' );
		cookie.push( (this.$domain)     ? 'domain='  + this.$domain                   : '' );
		cookie.push( (this.$secure)     ? 'secure'                                    : '' );
		document.cookie = cookie.join('; ');
	};
	this.load = function() {
		var i;
		var allcookies = 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)===-1)? allcookies.length : allcookies.indexOf(';',start);
		var cookieval = allcookies.substring(start,end);
		var a = cookieval.split('&');
		for(i=0; i<a.length; i++){
			a[i] = a[i].split(':');
			this[a[i][0]] = unescape(a[i][1]);
		}
		return true;
	};
	this.legacyLoad = function(c_name) {
		if (document.cookie.length>0) {
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1) {
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				var val = unescape(document.cookie.substring(c_start,c_end));
				if(val) { this[c_name] = val; }
			}
		}
		return "";
	};
	this.remove = function() {
		var cookie = [this.$name + '='];
		if(this.$path)   { cookie.push( 'path=' + this.$path ); }
		if(this.$domain) { cookie.push( 'domain=' + this.$domain ); }
		cookie.push( 'expires=Fri, 02-Jan-1970 00:00:00 GMT' );
		document.cookie = cookie.join('; ');
	};
	this.clear = function() {
		var prop;
		for(prop in this){
			if((prop.charAt(0)!=='$') && (typeof this[prop]!=='function')) {
				delete this[prop];
			}
		}
	};
	
};
