//---- copyright by Phil Barnard; phil@perite.com

/* ----------------------/  notes

Version info ---

Started: 09012000  last mod 03042001
Case: this version is case sensitive.
Regex: used only to extract the named cookie string
Returns: "" when fetching invalid cookies, and saves named cookie as "" when saving invalid input

Usage ---

var myCookie = new cookie("myCookie"); // creates a new cookie named myCookie and save that cookie obj

myCookie.value returns the entire named cookie string
myCookie.save(duration,path,domain) // saves the current cookie values; duration in hours
myCookie.kill() sets the cookie value to "" and expiry to Thu, 01-Jan-70 00:00:01 GMT

myCookie.key(keyName,keyvalue) replaces or creates a keyname=keyvalue pair

myCookie.keys.length returns the number of keys
myCookie.keys.keyName returns the value for keyName

myCookie.keys[indx].name returns the name of the key with that index
myCookie.keys[indx].value returns the value of the key with that index

------------------------ */

// NB to self: do not use the "for .. in" construct with the keys array as it is multi-dimensional


function kill(){
	this.value = ""; this.keys = new Array();  // clean out the cookie value and the keys array
	document.cookie = this.name + "=;expires=Thu, 01-Jan-70 00:00:01 GMT";  // expire the cookie
	// for(var i in this.keys){this.keys[i]=null;}
	this.save();
}

function killKey(deadkey){
	for(var i in this.keys){if(this.keys[i].name == deadkey){this.keys[i]=null; break;}}
	this.save();
}

function save(duration,path,domain){
	var cstr="";
	for(var i=0; i<this.keys.length; i++){
		if(!this.keys[i]) continue;
		cstr += "&" + this.keys[i].name + "=" + escape(this.keys[i].value);
	}
	cstr = this.name + "=" + cstr.substring(1);  // get rid of the leading &
	if(duration){
		var tempDate = new Date();
		tempDate.setHours(tempDate.getHours() + duration);
		cstr += ";expires=" + tempDate.toGMTString();
	}
	cstr += ";path=" + (path ? path : "/");
	cstr += domain ? ";domain=" + domain : "";
	document.cookie = cstr;
}

function key(k,v){
	var re = new RegExp(k,"i");  // get a key value on a case insentive basis
	for(var i=0; i<this.keys.length; i++){
		if(re.test(this.keys[i].name)){
			this.keys[k]=v; this.keys[i].value=v;
			return;  // fall thru only to create a new key
		}
	}
	this.keys[k] = v;
	this.keys[this.keys.length]={name:k,value:v};
}

function cookie(name){
	// install object properties
	this.name=name; this.value=""; this.keys=new Array();
	
	// install object methods
	this.save=save; this.key=key; this.kill=kill; this.killKey=killKey;
	
	if(!document.cookie) return "";

	re = new RegExp ("^"+name+"=([^;]*).*|.+;\\s?"+name+"=([^;]*).*|.*","i"); // ^phil=([^;]*).*|.+;\s?phil=([^;]*).*|.*
	this.value = document.cookie.replace(re,"$1$2");
	if(!this.value) return "";  // avoid error in IE when splitting an empty string
	
	var keyData = this.value.split("&"); // parse cookie value for key/value pairs
	for(var i in keyData){
		var keyNV = keyData[i].split("=");
		keyNV[1] = keyNV[1] ? unescape(keyNV[1]) : "";  // otherwise NN returns undefined
		this.keys[keyNV[0]]=keyNV[1];  // so that obj.keys.keyName1 -> value1
		this.keys[i]={name:keyNV[0],value:keyNV[1]};  // so that obj.keys[1].name -> keyName1 and obj.keys[1].value -> keyValue1
	}
}