// JavaScript 

var count = 0;
var name = "Pure";
var separator = "|";

function propertyAction(propertyID) {
	var str
	var cookie = getCookie(name);
	var propertyFound = findProperty(cookie, propertyID);
	if (propertyFound == -1) {
		str = saveProperty(cookie, propertyID);
		count++;
		document.cookie = name + "=Properties:" + str;
	}
	//alert("propertyAction\r\nAfter insertion/deletion: " + str + "\r\nCookie Saved.");
}
// Works
function getCookie(name) {
	var thecookie = new String();
	var cookieList = document.cookie;
	var cookieIdx = cookieList.indexOf(name + "=");
	if (cookieIdx != -1) {
		thecookie = cookieList.substring(cookieIdx + name.length + 12, cookieList.indexOf(";", cookieIdx));
	}
	//alert("getCookie\r\nCookie Index: " + cookieIdx + "\r\nThe Cookie: " + thecookie);
	return thecookie
}
// Works
function saveProperty(str, propertyID) {
	if (str.length > 0) {
		str = str + "|" + propertyID;
		var props = str.split("|");
		props.sort(compare);
		str = props.join("|");
	}
	else
		str = propertyID;
	alert("Property " + propertyID + " Saved.");
	return str;
}
// Works
function deleteCookie(name) {
	var cookieList = document.cookie;
	var cookieIdx = cookieList.indexOf(name) + name.length + 1;
	if (cookieIdx != -1) {
		document.cookie = name + "=";
	}
}
// Works
function compare(a, b) {
	return a - b;
}
// Works
function deleteProperty(str, pos) {
	var i;
	alert("deleteProperty\r\nData string: " + str);
	var props = str.split("|");
	alert("deleteProperty\r\nNumber of Properties: " + props.length);
	props.splice(pos, 1);
	str=props.join("|");
	//alert("deleteProperty\r\nString after deletion: " + str);
	return str;
}

// Works
function findProperty(str, PID) {
	var i, propertyIDs;
	var found;
	var propertyIDs = str.split(separator);
	var properties = propertyIDs.length
	if (properties == 1)
		if (propertyIDs[0] == "") {
			//alert("findProperty\r\nNo Properties in this cookie");
			properties = 0;
			found = -1;
		}
		else
			properties = propertyIDs.length;
	//alert("findProperty\r\nNumber of Properties: " + properties);
	for (i=0; i < properties; i++) {
		if (propertyIDs[i]==PID) {
			found=i;
			return found;
		}
		else
			found=-1;
	}
	return found;
} // end of function: findProperty

function listProperties() {
	var str = getCookie(name);
	var properties = str.split("|");
	var i;
	alert("listProperties\r\nProperty String: " + str);
	for (i=0;i<properties.length;i++) {
		document.writeln("<a href='home_details.asp?PID=" + properties[i] + "'>" + properties[i] + "</a><br>");
	}
	
}
function retrieveProperties() {
	var str = getCookie(name);


}
