
//###################
//#   cookie code   #
//###################

var bikky = document.cookie;

function getCookie(name) { // use: getCookie("name");
	bikky = document.cookie;
	var index = bikky.indexOf(name + "=");
	if (index == -1) return null;
	index = bikky.indexOf("=", index) + 1;
	var endstr = bikky.indexOf(";", index);
	if (endstr == -1) endstr = bikky.length;
	return unescape(bikky.substring(index, endstr));
}

var today = new Date();
var expiry = new Date(today.getTime() + 36500 * 24 * 60 * 60 * 1000); // plus 100 years

function setCookie(name, value, action,saveHTML,removeHTML) { // use: setCookie("name", value, action);
	if (value != null && value != "") {

		//box = eval("document.ResultsForm.Shortlist" + value);

		var contents = getCookie(name) || "";
		//check if value in contents

		var each_cookie = contents.split(",");
		var inCookie = false;

		for (i = 0; i < each_cookie.length-1; i++){
			if (each_cookie[i]==value) {

				//if (box.checked == true)
				if (action == "createOrReplace")
				{
					//remove from cookie
					contents = contents.replace(each_cookie[i] + ",", "");
					document.cookie=name + "=" + escape(contents) + "; expires=" + expiry.toGMTString();
			 		bikky = document.cookie; // update bikky

			 		document.getElementById('saveOrRemove' + value).innerHTML='Save to myAccount';

				}
				else if (action == "check")
				{
					document.getElementById('saveOrRemove' + value).innerHTML='Remove from myAccount';
				}
					i=each_cookie.length;
					inCookie = true;
			}
		}//end FOR

		if (inCookie == false && action == "createOrReplace") {

			//add to cookie
			contents = value + "," + contents;
			document.cookie=name + "=" + escape(contents) + "; expires=" + expiry.toGMTString();
			bikky = document.cookie; // update bikky

			document.getElementById('saveOrRemove' + value).innerHTML='Remove from myAccount';
		}

		if (inCookie == false && action == "check") {
			document.getElementById('saveOrRemove' + value).innerHTML='Save to myAccount';
		}


	}//end if

}//end function

