// Constructor for the Cookie object for a specified document, with a
// specified name and optional attributes.
// Arguments:
//   document: The Document object for which the cookie is stored. Required.
//   name:     String with cookie name. Required.
//   hours:    Number of hours after which Cookie expires. Optional.
//   path:     Cookie file path string. Optional.
//   domain:   Cookie domain string. Optional.
//   secure:   Boolean value to request a secure cookie. Optional.
//
function Cookie(document, name, hours, path, domain, secure) {

	// Distinguish between attributes and values by prefacing
	// a '$' in front of the attribute name.

	this.$document = document;
	this.$name     = name;

	if(hours) {
	    this.$expiration = new Date((new Date()).getTime() + hours*3600000);
        }
	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;
}  

// The store() method of the cookie object
Cookie.prototype.store = function() {
	var cookieval = "";
	for(var prop in this) {

	   // Ignore properties with names that begin with '$' and also methods
	   if((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
   	       continue;

	   if(cookieval != "") cookieval += '&';
	   
           // escape() converts all whitespace, commas, semicolons
	   // into escaped/legal characters. Have to unescape to get 
	   // values back out.
	   cookieval += prop + ':' + escape(this[prop]);
        }


// With the cookie value, now put together the complete cookie string,
// which includes the name and various attributes set during cookie construction.

        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';
		
// Store the cookie by setting the magic Document.cookie property
        this.$document.cookie = cookie;
}
	

// Cookie object's load() method
Cookie.prototype.load = function() {

	// Get list of all cookies for this document
	var allcookies = this.$document.cookie;

	if(allcookies == "") return false;

	// Now extract our named cookie from the list
	var start = allcookies.indexOf(this.$name + '=');
	if(start == -1) return false; // Cookie not defined for this page

	start += this.$name.length + 1; // Skip name and equals sign

	var end = allcookies.indexOf(';', start);
	if(end == -1) end = allcookies.length;

	var cookieval = allcookies.substring(start, end);

	// Parse the name/value pairs out of the cookieval string.
	// '&' delimits name/value pairs.

        // Creat array of name/value pairs
	var a = cookieval.split('&');

        // Break each pair into an array
	for(var i=0; i < a.length; i++) { 
	   a[i] = a[i].split(':');
        }


	// Set all name and values of the state variables in
	// this Cookie Object. 

	for(var i = 0; i < a.length; i++) {
	   this[a[i][0]] = unescape(a[i][1]);
        }
	
	return true;
}

// The remove() method of the Cookie Object
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;
}

