/******************************************************************************************
 * JavaScript functions -- setParameters (utility class for working with cookies          *
 * Purpose: Set cookies with url parameters - unions.org / other advertising campaigns    *
 * Created: 09/11/08                                                                      *
 ******************************************************************************************/
 
 // contructor function
 function Cookie (document, name, hours, path, domain, secure) {
 	//predeifned properties start with $
	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;
 }
 // store() method of Cookie object
 function _Cookie_store () {
 	var cookievalue = "";
	for(var prop in this) {
		//ignore properties whose names begin with '$' and methods
		if((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function'))
			continue;
		//concantanate parameters into one cookie value
		if(cookievalue != "") cookievalue += '&';
		cookievalue += prop + ':' + escape(this[prop]);
	}
	//put together complete cookie string with name passed in
	var cookie = this.$name + '=' + cookievalue;
	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';
	//alert(cookie);
	//store cookie setting magic Document.cookie property
	this.$document.cookie = cookie;
 }
 // load() method of Cookie object - retrieves/reads cookies
 function _Cookie_load() { 
 	// get list of cookies pertaining to document
	var allcookies = this.$document.cookie;
	if(allcookies == "") return false;
	//alert(allcookies);
	// extract named cookie
	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
	//alert(start);
	var end = allcookies.indexOf(';',start);
	if(end == -1) end = allcookies.length;
	//alert(end);
	var cookievalue = allcookies.substring(start, end);
	//alert(cookievalue);	
	// break into individual name/value pairs
	var cookiearray = cookievalue.split('&');		//break into name/value pair array
	//alert(cookiearray.length);
	for(var i=0; i < cookiearray.length; i++) {		
		cookiearray[i] = cookiearray[i].split(':');
		//alert("1 " + cookiearray[i]);
	}
	// sett names/values of state variables
	for(var i = 0; i < cookiearray.length; i++) {
		this[cookiearray[i][0]] = unescape(cookiearray[i][1]);
		//alert("2 " + cookiearray[i] + ": " + cookiearray[i][0]);
	}
	return true;
 }
 

