var arCompareProperties = new Array();
var comparePropertiesForm = document.forms[0];

function property(t,v,i,im,h) {
	this.text = t;
	this.value = v;
	this.id = i;
	this.img = im;
	this.href = h;
}

//function called on onlick event from checkboxes
function checkCompare(t,r) {
	var p = getComparePropertiesArray();
	if (t.checked) {
		var foundMatch = false;
		var s = "";
		for (var k=0; k<p.length; k++) {
			s += (s.length > 0 ? "|" : "") + p[k];
			if (p[k] == t.value)
				foundMatch = true;
		}
		if (!foundMatch) {
			s += (s.length > 0 ? "|" : "") + t.value;
			p[p.length] = t.value;
			arCompareProperties[arCompareProperties.length] = new property(eval('t.form.propertyText'+r+'.value'),t.value,r,eval('t.form.propertyImg'+r+'.value'),eval('t.form.propertyLink'+r+'.value'));
		}
		if (p.length > 4) {
			alert('You can only compare up to four properties.\n\nPlease deselect a property before adding another.');
			return;
		} else if (p.length == 4)
			disableRest();
		document.cookie = "CompareProperties=" + s;
	} else {
		removePropertyFromCookie(p,t.value);
	}
	//var buffer = "";
	//for (var x=0; x<arCompareProperties.length; x++)
	//	buffer += arCompareProperties[x].text + '\n';
	//alert(buffer);
	displayCompareProperties();
}

var separator = "<table class='compareSeparator'><tr><td><img src='/images/spacer.gif' width=1 height=1></td></tr></table>";

function displayCompareProperties() {
	if (document.getElementById("idComparePropertiesButton")) {
		if (arCompareProperties.length == 0) {
			for (var i=0; i<4; i++) {
				var row = "idCompareProperty"+(i+1)+"Row";
				document.getElementById(row).style.display = "none";
			}
			if (document.getElementById("idCompareSelectedPropertiesButton1"))
				document.getElementById("idCompareSelectedPropertiesButton1").style.display = "none";
			if (document.getElementById("idCompareSelectedPropertiesButton2"))
				document.getElementById("idCompareSelectedPropertiesButton2").style.display = "none";
			document.getElementById("idComparePropertiesButton").style.display = "none";
			document.getElementById("idComparePropertiesNone").style.display = "";
		} else {
			for (var i=0; i<4; i++) {
				var row = "idCompareProperty"+(i+1)+"Row";
				var cell = "idCompareProperty"+(i+1);
				if (i < arCompareProperties.length) {
					document.getElementById(cell).innerHTML = (i > 0 ? separator : "") + renderProperty(arCompareProperties[i]);
					document.getElementById(row).style.display = "";
				} else
					document.getElementById(row).style.display = "none";
			}
			if (document.getElementById("idCompareSelectedPropertiesButton1"))
				document.getElementById("idCompareSelectedPropertiesButton1").style.display = "";
			if (document.getElementById("idCompareSelectedPropertiesButton2"))
				document.getElementById("idCompareSelectedPropertiesButton2").style.display = "";
			document.getElementById("idComparePropertiesButton").style.display = "";
			document.getElementById("idComparePropertiesNone").style.display = "none";
		}
	}
}

function renderProperty(p) {
	return "<table border=0 cellpadding=2 cellspacing=0 width=100%><tr><td valign=top width=48><a href='" + p.href + "'><img src='" + p.img + "' class=\'comparePropImageBorder\'></a></td><td valign=top class=\'CompareText\' width=100%>" + p.text + "</td><td valign=top width=13><a href=\"javascript:removeCompareProperty('" + p.value + "'," + p.id + ")\"><img src='/images/buttons/blue/button_x.gif' border=0 alt='Remove' title='Remove'></a></td></tr></table>";
}

function removeCompareProperty(n,r) {
	var prop = comparePropertiesForm.property;
	if (prop) {
		if (r == 0) {
			if (prop.length) {
				var foundMatch = false;
				for (var j=0; j<prop.length; j++)
					if (prop[j].value == n) {
						//alert('found property at index ' + j);
						foundMatch = true;
						prop[j].click();
						break;
					}
				//alert("found match? " + foundMatch);
				if (!foundMatch)
					removePropertyFromCookie(getComparePropertiesArray(),n);
				displayCompareProperties();
			} else {
				if (prop.value == n) {
					if (prop.checked)
						prop.click();
				} else {
					removePropertyFromCookie(getComparePropertiesArray(),n);
					displayCompareProperties();
				}
			}
		} else {
			if (prop.length) {
				if (prop[r-1].checked)
					prop[r-1].click();
				else
					alert('Property is either already unchecked or the checkbox does not exist.');
			} else if (prop.checked) {
				prop.click();
			} else
				alert('Property is either already unchecked or the checkbox does not exist.');
		}
	} else {
		removePropertyFromCookie(getComparePropertiesArray(),n);
		displayCompareProperties();
	}
}

function removePropertyFromCookie(p,n) {
	//alert('arCompareProperties.length = ' + arCompareProperties.length + '\n\nremoving property [' + n + '] from cookie\n\n[' + getComparePropertiesCookie() + ']');
	resetCompareProperties();
	var s = "";
	for (var i=0; i<p.length; i++) {
		//alert('checking ' + (i+1) + '/' + p.length + ' if\n[' + p[i] + '] equals\n[' + n + ']');
		if (p[i] != n) {
			s += (s.length > 0 ? "|" : "") + p[i];
		} else {
			//alert("found property at index " + i);
			for (var k=i; k<arCompareProperties.length-1; k++)
				arCompareProperties[k] = arCompareProperties[k+1];
			arCompareProperties.length--;
		}
	}
	//alert('arCompareProperties.length = ' + arCompareProperties.length + '\nsaving cookie\n\n[' + s + ']');
	if (s.length > 0)
		document.cookie = "CompareProperties=" + s;
	enableAll();
}

function resetCompareProperties() {
	document.cookie = "CompareProperties=;Expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function removeAllCompareProperties() {
	resetCompareProperties();
	if (comparePropertiesForm.property) {
		if (comparePropertiesForm.property.length) {
			for (var d=0; d < comparePropertiesForm.property.length; d++) {
				comparePropertiesForm.property[d].disabled = false;
				comparePropertiesForm.property[d].checked = false;
			}
		} else {
			comparePropertiesForm.property.disabled = false;
			comparePropertiesForm.property.checked = false;
		}
	}
	arCompareProperties = new Array();
	displayCompareProperties();
}

function checkIfCompareShouldBeDisabled() {
	var a = getComparePropertiesArray();
	if (a.length >= 4)
		disableRest();
}

function countChars(str,chr) {
	var counter = 0;
	for (var j=0; j<str.length; j++)
		if (str.substring(j,j+1) == chr)
			counter++;
	return counter;
}

function getComparePropertiesCookie() {
	var c = document.cookie.split(';');
	var s = "";
	for (var i=0; i<c.length; i++)
		if (c[i].indexOf("CompareProperties=") != -1) {
			s = trimstr(c[i]).substring("CompareProperties=".length);
			break;
		}
	return s;
}

function getComparePropertiesArray() {
	var s = getComparePropertiesCookie();
	var p = s.split("|");
	if (p.length == 1 && p[0].length == 0)
		p = new Array();
	return p;
}

function compareProperties() {
	var p = getComparePropertiesArray();
	
	//var buffer = 'you have ' + p.length + ' properties\n';
	//for (var j=0; j<p.length; j++)
	//	buffer += '[' + p[j] + ']\n';
	//alert(buffer);
	
	if (p.length < 1) {
		alert('Select at least 1 property to be compared.');
		return;
	} else if (p.length > 4) {
		alert('Maximum 4 properties can be selected for comparision.');
		return;
	}
	
	var args = "";
	for (var i=0; i<p.length; i++)
		args += (i > 0 ? "&" : "" ) + parseProperty(p[i],i+1);
	
	//alert(args);
	
	CompareWin = window.open("","CompareWin","toolbar=0,top=50,left=50,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=744,height=670");
	CompareWin.location.href = "compareProperties.asp?" + args;
	CompareWin.focus();
}

//function to disable the rest of the checkboxes if 3 are selected
function disableRest() {
	if (comparePropertiesForm.property.length) {
		for (var i = 0; i < comparePropertiesForm.property.length; i++)
			if (!comparePropertiesForm.property[i].checked)
				comparePropertiesForm.property[i].disabled = true;
	} else if (!comparePropertiesForm.property.checked)
		comparePropertiesForm.property.disabled = true;
}

//function to enable the checkboxes if < 3 are selected
function enableAll() {
	if (comparePropertiesForm.property) {
		if (comparePropertiesForm.property.length) {
			for (var i = 0; i < comparePropertiesForm.property.length; i++)
				comparePropertiesForm.property[i].disabled = false;
		} else
			comparePropertiesForm.property.disabled = false;
	}
}

function parseProperty(p,i){
	var pa = p.split("~");
	return "lst" + i + "_listingID=" + pa[0] + "&lst" + i + "_MLSProviderID=" + pa[1] + "&lst" + i + "_MLSID=" + pa[2];
}

function trimstr(s) {
	while(''+s.charAt(0)==' ')
		s=s.substring(1,s.length);
	while(''+s.charAt(s.length-1)==' ')
		s=s.substring(0,s.length-1);	
	return s
}
