// \s*\S\s*\/\/.+\.$
// CookieMaster.action v1.3.4 - October, 2008
// Copyright © 2001 by Walter Blady
// All rights reserved
//Set the globals.
var CMTimeout = 0, CMCookieObject, CMJumpURL, CMPairsDelim, CMFieldDelim, CMAction;
//Create a new Cookie class.
new CMCookie();
CMCookie.prototype.storeCookie = CMstoreCookie;
CMCookie.prototype.loadCookie = CMloadCookie;
CMCookie.prototype.removeCookie = CMremoveCookie;
CMCookie.prototype.removeProperty = CMremoveProperty;
//Main function.
function WBCookieMaster(action) {
	if (CMVersion()) {
		var appendDelim = action[19];
		CMPairsDelim = action[21];
		CMFieldDelim = action[22];
		CMAction = action;
		//Set a Cookie.
		var cookieCrumbs = "", formPairs = action[20];
		//Set a new cookie.
		if (action[1] == 0 && action[2] != "") {
			cookieCrumbs = CMGatherValues(action, formPairs);
			CMSetCookie(action, cookieCrumbs);
			//See if an action should be performed after these options.
			CMCookieAction(action, cookieCrumbs);

		}
		//Append CookieMaster's data to an existing Cookie.
		else if (action[1] == 1 && action[2] != "") {
			//Append the new value to the end of the Cookie, with a delimiter between them.
			//A pairs value of 1 strips the cookie's name.
			cookieCrumbs = CMGetCookie(action[2], 1);
			//Don't start with a delimiter if the cookie value is blank.
			cookieCrumbs += cookieCrumbs != "" ? appendDelim : cookieCrumbs;
			cookieCrumbs += CMGatherValues(action, formPairs);
			CMSetCookie(action, cookieCrumbs);
			//See if an action should be performed after these options.
			CMCookieAction(action, cookieCrumbs);
		}
		//Append a Cookie's value to a second Cookie.
		else if (action[1] == 2 && action[2] != "") {
			//Append the second Cookie value to the first Cookie, with a delimiter between them.
			//A pairs value of 1 strips the cookie's name.
			cookieCrumbs = CMGetCookie(action[2], 1);
			//If there is a Cookie.
			if (cookieCrumbs) {
				cookieCrumbs += appendDelim + CMGetCookie(action[14], 1);
			}
			CMSetCookie(action, cookieCrumbs);
			//See if an action should be performed after these options.
			CMCookieAction(action, cookieCrumbs);
		}
		//Get and search all cookies.
		else if (action[1] == 3) {
			cookieCrumbs = CMGetCookie("all", 0);
			if (cookieCrumbs) {
				cookieCrumbs = CMSearchCookie(action, cookieCrumbs);
				if (cookieCrumbs) {
					cookieCrumbs = CMDelimitCookie(action, cookieCrumbs);
					//See if an action should be performed after these options.
					CMCookieAction(action, cookieCrumbs);
				}
			}
		}
		//Get and search a specific cookie.
		else if (action[1] == 4 && action[2] != "") {
			cookieCrumbs = CMGetCookie(action[2], 0);
			if (cookieCrumbs) {
				cookieCrumbs = CMSearchCookie(action, cookieCrumbs);
				if (cookieCrumbs) {
					cookieCrumbs = CMDelimitCookie(action, cookieCrumbs);
					//See if an action should be performed after these options.
					CMCookieAction(action, cookieCrumbs);
				}
			}
		}
		//Get and search a specific cookie, or set a new one if not found.
		else if (action[1] == 5 && action[2] != "") {
			cookieCrumbs = CMGetCookie(action[2], 0);
			//If the Cookie does exist.
			if (cookieCrumbs) {
				cookieCrumbs = CMSearchCookie(action, cookieCrumbs);
				if (cookieCrumbs) {
					cookieCrumbs = CMDelimitCookie(action, cookieCrumbs);
				}
			}
			else {
				//If not, set a cookie.
				cookieCrumbs = CMGatherValues(action, formPairs);
				CMSetCookie(action, cookieCrumbs);
			}
			//See if an action should be performed after these options.
			CMCookieAction(action, cookieCrumbs);
		}
		//Remove a cookie.
		else if (action[1] == 6 && action[2] != "" && document.cookie != "") {
			CMCookieObject = new CMCookie(document, action[2], "0", action[4], action[5], action[6]);
			CMCookieObject.removeCookie();
		}
		//Remove a property from a cookie.
		else if (action[1] == 7 && action[2] != "" && action[7] != "" && document.cookie != "") {
			CMCookieObject = new CMCookie(document, action[2], parseInt(action[3]), action[4], action[5], action[6]);
			CMCookieObject.loadCookie(false);
			CMCookieObject.removeProperty(action[7]);
			//See if an action should be performed after these options.
			CMCookieAction(action, cookieCrumbs);
		}
		//View the properties of a cookie.
		else if (action[1] == 8 && action[2] != "" && document.cookie != "") {
			cookieCrumbs = CMGetCookie(action[2], 0, true);
			//See if an action should be performed after these options.
			CMCookieAction(action, cookieCrumbs);
		}
	}
	return;
}
//Set a cookie.
function CMSetCookie(action, cookieCrumbs){
	cookieCrumbs = cookieCrumbs.replace(/\;/, escape(";"));
	cookieCrumbs = cookieCrumbs.replace(/\,/, escape(","));
	cookieCrumbs = cookieCrumbs.replace(/\ /, escape(" "));
	CMCookieObject = new CMCookie(document, action[2], parseInt(action[3]), action[4], action[5], action[6]);
	CMCookieObject.storeCookie(cookieCrumbs);
	return;
}
//Get cookies.
function CMGetCookie(cookieTarget, stripName, view) {
	//Get all the Cookies for this domain.
	var cookieCrumbs = unescape(document.cookie);
	//If cookies and asking for one particular cookie.
	if (cookieCrumbs && cookieTarget != "all") {
		var cookieArray = cookieCrumbs.split("; ");
		//Locate the specific Cookie.
		for (var i = 0; i < cookieArray.length; i++) {
			if (cookieArray[i].indexOf(cookieTarget) != -1) {
				//Create the Cookie object.
				CMCookieObject = new CMCookie(document, cookieTarget);
				CMCookieObject.loadCookie(view);
				//Get the full Cookie string.
				cookieCrumbs = cookieArray[i];
				//Strip the Name from the Name=Value pair, if asked.
				if (stripName) {
					cookieCrumbs = cookieArray[i].substr(cookieArray[i].indexOf("=")+1, (cookieArray[i].length - cookieArray[i].indexOf("=")+1)); 
				}
				break;
			}
		}
	}
	return cookieCrumbs;
}
//Search for and parse Cookies.
function CMSearchCookie(action, cookieCrumbs) {
	if (cookieCrumbs) {
		var bool = "";
		if (action[10] == 0) bool = " && ";
		else if (action[10] == 1) bool = " || ";
		else if (action[10] == 2) bool = " ! ";
		//Split the Cookies and search each Cookie string.
		var cookieArray = unescape(cookieCrumbs).split("; ");
		cookieCrumbs = "";
			for (var i = 0; i < cookieArray.length; i++) {
			//Not used.
			var s0 = true;
			//Contains.
			var s1 = eval("cookieArray[i].indexOf(action[9]) > -1");
			//Does not contain.
			var s2 = eval("cookieArray[i].indexOf(action[9]) < 0");
			//Starts with.
			var s3 = eval("action[9] == cookieArray[i].substr(0, action[9].length)");
			//Equals.
			var s4 = eval("cookieArray[i] == action[9]");
			//Not used.
			var s5 = true;
			//Contains.
			var s6 = eval("cookieArray[i].indexOf(action[12]) > -1");
			//Does not contain.
			var s7 = eval("cookieArray[i].indexOf(action[12]) < 0");
			//Starts with.
			var s8 = eval("action[12] == cookieArray[i].substr(0, action[12].length)");
			//Equals.
			var s9 = eval("cookieArray[i] == action[12]");
			CMCorrectFlag = eval("s" + action[8] + bool + "s" + (action[11]+5));
			if (CMCorrectFlag) {
				cookieCrumbs += cookieCrumbs != "" ? "; " : cookieCrumbs;
				cookieCrumbs += cookieArray[i];
			}
		}
	}
	return cookieCrumbs;
}
//Cookie transmission functions.
function CMCookieAction(action, cookieCrumbs) {
	//Get the jump-to URL.
	CMJumpURL = action[15];
	//Just display the Cookie value if "Not used" is selected.
	if (action[13] == 0) {
		//var internal = false;.
		if (CMAction[38]) alert(cookieCrumbs);
		//if (cookieCrumbs) CMSendCookie(action, cookieCrumbs, internal);.
	}
	//Jump to a Javascript Extension.
	else if (action[13] == 1 && action[14] != "") {
		var script = action[14], cookieName = action[2];
		script = script.replace(/\(|\)/g, "");
		if (CMAction[38]) alert(cookieCrumbs);
		var doIt = eval(script + "(cookieName, cookieCrumbs)");
	}
	//Jump to a URL.
	else if (action[13] == 2 && action[15].indexOf("Reference!") == -1) {
		if (CMAction[38]) alert(cookieCrumbs);
		CMGoURL();
	}
	//E-mail the data.
	else if (action[13] == 3) {
		var internal = true;
		if (CMAction[38]) alert(cookieCrumbs);
		if (cookieCrumbs) CMSendCookie(action, cookieCrumbs, internal);
	}
	//E-mail the data then load the URL.
	else if (action[13] == 4 && action[15].indexOf("Reference!") == -1) {
		var internal = true;
		if (CMAction[38]) alert(cookieCrumbs);
		if (cookieCrumbs) CMSendCookie(action, cookieCrumbs, internal);
		CMTimeout = setTimeout("CMGoURL()", parseInt(action[16]) * 1000);
	}
	return;
}
//Gather data from the form and put the results into a hidden field.
//Also returns with the results.
function CMGatherValues(action, formPairs) {
	var value = action[7];
	//Add a cookie value delimiter if action[7] had data.
	//value += value != "" ? CMAppendDelim : "";.
	//Must strip this delimiter if there is no form data.
	//...so set the cookie value delimiter in a RegEx.
	//var re = new RegExp("\\" + CMAppendDelim + "+$");.
	if (action[23] != "") {
		var myForm = window.document[action[23]];
		//Loop through the fields and gather data.
		for (var i = 25; i <= 36; i++) {
			//If there is a field name.
			if (action[i] != "") {
				var field = myForm[action[i]];
				var type = field.type;
				value += value != "" ? CMPairsDelim : "";
				//Check text, hidden and password fields.
				//Adds the value of the field.
				if (type == "text" || type == "textarea" || type == "hidden" || type == "password") {
					value += formPairs ? field.name + CMFieldDelim + field.value : field.value;
				}
				//Check check boxes and a single radio button.
				//Add the wor true for checked or false for unchecked.
				else if (type == "checkbox" || type == "radio") {
					value += formPairs ? field.name + CMFieldDelim + field.checked : field.checked;
				}
				//Check list.
				//Add the list number of the selected item.
				else if (type == "select-one") {
					//Get the value/list number of the list item - lists begin with zero.
					var fieldVal = action[37] ? field.selectedIndex : field.options[field.selectedIndex].value;
					value += formPairs ? field.name + CMFieldDelim + fieldVal  : fieldVal;
				}
				//Check multiple selection lists.
				//Add the list number of the selected item.
				else if (type == "select-multiple") {
					for (var j = 0; j < field.length; j++) {
						if (field[j].selected) {
							value += value.lastIndexOf(CMPairsDelim) == (value.length-1) ? "" : CMPairsDelim;
							var fieldVal = action[37] ? j : field[j].value;
							//Get the value/list number of the list item - lists begin with zero.
							value += formPairs ? field.name+j + CMFieldDelim + fieldVal : fieldVal;
						}
					}
				}
				//Check for groups of radio buttons.
				else if (field.length > 0) {
					for (var j = 0; j < field.length; j++) {
						var fieldVal = action[37] ? j : field[j].value;
						if (field[j].checked) {
							//Get the name/number of the radio button.
							value += formPairs ? field[j].name + CMFieldDelim + fieldVal : fieldVal;
							break;
						}
					}
				}
			}
		}
	}
	//If there is a hidden field, put the final string in there.
	if (action[24].length != "") {
		myForm[action[24]].value = value;
	}
	return value;
}
//Parse the Cookie data.
function CMDelimitCookie(action, cookieCrumbs) {
	var data = "", quotes = "", CMCorrectFlag = false, endLine = "";
	if (action[17] == 0) delim = "";
	else if (action[17] == 1) delim = " ";
	else if (action[17] == 2) delim = "; ";
	else if (action[17] == 3) delim = escape("\n");
	else if (action[17] == 4) delim = escape("\t");
	else if (action[17] == 5) delim = ", ";
	else if (action[17] == 6) {
		delim = ', ';
		quotes = '"';
	}
	//Split up the Cookies.
	var cookieArray = cookieCrumbs.split("; "), formPairs = action[20];
	for (var i = 0; i < cookieArray.length; i++) {
		//If Name=Value pairs.
		if (action[18]) {
			data += data != "" ? delim : data;
			//Delimit the Cookie/value pairs.
			data += quotes + cookieArray[i] + quotes;
		}
		//else delimit the name/value pairs.
		else {
			//Get rid of the Cookie name in prep for field splitting.
			cookieArray[i] = cookieArray[i].substring(cookieArray[i].indexOf("=")+1, cookieArray[i].length);
			//Split up the fields if there are delimiter.
			var pairsArray = cookieArray[i].split(CMPairsDelim);
			for (var j = 0; j < pairsArray.length; j++) {
				data += data != "" ? delim : data;
				pairsArray[j] = pairsArray[j].replace(CMFieldDelim, "=");
				//Split up the name/value pairs and save only the value.
				if (formPairs) {
					data += quotes + pairsArray[j] + quotes;
				}
				//else delimit the name/value pairs.
				else {
					data += quotes + pairsArray[j].substring(pairsArray[j].indexOf(action[22])+1, pairsArray[j].length) + quotes;
				}
			}
		}
	}
	return data;
}
//Re-direct to a URL.
function CMGoURL() {
	clearTimeout(CMTimeout);
	window.location.href = CMJumpURL;
	return;
}
//Mail the Cookie data.
function CMSendCookie(action, data, internal) {
	//If a form name has been entered.
	if (action[23].length > 0) {
		var myForm = window.document[action[23]];
		if (action[24].length != "") {
			myForm[action[24]].value = unescape(data);
		}
		if (internal) {
			document.forms[action[23]].submit();
		}
	}
	return;
}
//Create the Cookie object.
function CMCookie(document, name, hours, path, domain, secure) {
	this.$document = document;
	this.$name = name;
	//Set the time as number of days.
	if (hours) this.$expires = new Date(new Date().getTime() + hours*86400000);
	else this.$expires = null;
	if (path) this.$path = path; else this.$path = null;
	if (domain) this.$domain = domain; else this.$domain = null;
	if (secure) this.$secure = secure; else this.$secure = false;
	return;
}
//Set a Cookie.
function CMstoreCookie(value) {
	if (value == null) value = "";
	//Skip internal properties.
	for (var prop in this) {
		if (prop.charAt(0) == "$" || (typeof this[prop]) == "function") {
			continue;
		}
		//Put a delimiter at the end.
		if (value != "") value += CMPairsDelim;
		//If the prop is a generated name, don't include it.
		if (prop.substr(0, 5) != "CMval") value += prop + CMFieldDelim + escape(this[prop]);
		else value += escape(this[prop]);
	}
	//Append the Cookie parameters.
	var cookie = this.$name + "=" + value;
	if (this.$expires) cookie += "; expires=" + this.$expires.toGMTString();
	if (this.$path) cookie += "; path=" + this.$path;
	if (this.$domain) cookie += "; domain=" + this.$domain;
	if (this.$secure) cookie += "; secure=" + secure;
	if (CMAction[38]) alert(cookie);
	this.$document.cookie = cookie;
	return;
}
//Get a Cookie.
function CMloadCookie(view) {
	if (view == null) view = false;
	var cookies = this.$document.cookie;
	if (cookies == "") return false;
	var start = cookies.indexOf(this.$name + "=");
	if (start == -1) return false;
	start += this.$name.length + 1;
	var end = cookies.indexOf(";", start);
	if (end == -1) end = cookies.length;
	var value = cookies.substring(start, end);
	//Return if there is no value.
	if (value == "") return true;
	var subArray = value.split(CMPairsDelim);
	for (var i = 0; i < subArray.length; i++) {
		subArray[i] = subArray[i].split(CMFieldDelim);
	}
	var nam, val, n = 0, display = "";
	display = 'These are the properties of the Cookie Object "' + this.$name + '":\n';
	for (var i = 0; i < subArray.length; i++) {
		//If no name, make a generic one - CMval(n).
		if (subArray[i][1] != null) nam = subArray[i][0];
		else { nam = "CMval" + n; n++; }
		val = subArray[i][1] != null ? unescape(subArray[i][1]) : unescape(subArray[i][0]);
		this[nam] = val;
		display += nam + " = " + val + "\n";
	}
	if (view) alert(display);
	return true;
}
//Remove a Cookie.
function CMremoveCookie() {
	var cookie = this.$name + "=";
	if (this.$path) cookie += "; path=" + this.$path;
	if (this.$domain) cookie += "; domain =" + this.$domain;
	cookie += "; expires=Thu, 01-Jan-1970 00:00:01 GMT";
	if (CMAction[38]) alert(cookie);
	this.$document.cookie = cookie;
	return;
}
//Remove a Cookie property.
function CMremoveProperty(property) {
	if (property != null) {
		//Locate the property to delete.
		for (var prop in this) {
			if (prop.charAt(0) == "$" || typeof this[prop] == "function") {
				continue;
			}
			//If a match is found, void the property.
			if (property == prop) {
				if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
					delete this[prop];
				}
				else {
					this[prop] = void 0;
				}
				this.storeCookie("");
			}
		}
	}
	return;
}
//Browser check.
function CMVersion() {
	return true;
}
//

