var CurrentCategory = "";
var refreshMap = false;
var alertShown = false;
var compareListings = new Array();
var http_request = false;
var http_request2 = false;
var ZoomSub = 0;

function Change(Feature, RefreshMap) {
	refreshMap = RefreshMap;
	if (Feature != "Sub_Div") 
		document.getElementById("SearchForHeader").innerHTML = Feature;
	else
		document.getElementById("SearchForHeader").innerHTML = "Subdivision";
		
	document.getElementById("ctl00_PrimaryContent_valSearchBy").value = Feature;
	CurrentCategory = Feature;
	updateDDL('XML.aspx?OpenHouseDate=' + document.getElementById("ctl00_PrimaryContent_OHDate").value + '&OpenHouses=1&Feature=' + Feature);
}


function Update() {
	refreshMap = true;
	var i=document.getElementById("ctl00_PrimaryContent_Values");
	document.getElementById("ctl00_PrimaryContent_valValues").value = "";
	for (x=0; x < i.options.length; x++) {
		if (i.options[x].selected == true) {
			var CategoryNoSpaces = new String(i.options[x].value);
			CategoryNoSpaces = CategoryNoSpaces.replace(/ /g, '_');
			if (document.getElementById("ctl00_PrimaryContent_valValues").value == "") {
				document.getElementById("ctl00_PrimaryContent_valValues").value = i.options[x].value;
			} else {
				document.getElementById("ctl00_PrimaryContent_valValues").value += "~" + i.options[x].value;
			}
		}	
	}
	CountHomes();
}

function UpdatePrice(doCountHomes) {
	if (doCountHomes == 1) {
	refreshMap = true;
		CountHomes();
	}
}

function UpdateBeds(doCountHomes) {
	if (doCountHomes == 1) {
		refreshMap = true;
		CountHomes();
	}
}

function UpdateBaths(doCountHomes) {
	if (doCountHomes == 1) {
		refreshMap = true;
		CountHomes();
	}
}

function UpdateGarages(doCountHomes) {
	if (doCountHomes == 1) {
		refreshMap = true;
		CountHomes();
	}
}

function UpdateOpenHouseDate(doCountHomes) {
	document.getElementById("ctl00_PrimaryContent_valValues").value = "";
	ZoomSub = 0;
	Change('City', 1);
}

function CountHomes() {
	http_request2 = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request2 = new XMLHttpRequest();
			if (http_request2.overrideMimeType) {
				http_request2.overrideMimeType('text/xml');
			}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request2 = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request2 = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request2) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	var url = new String("NumHomes.aspx")
	url += "?SearchType=OpenHouses&OpenHouseDate=" + document.getElementById("ctl00_PrimaryContent_OHDate").value;
	url += "&OpenHouseSearchType=" + document.getElementById("ctl00_PrimaryContent_valSearchBy").value;
	url += "&Values=" + encodeURIComponent(document.getElementById("ctl00_PrimaryContent_valValues").value);
	url += "&MinPrice=" + document.getElementById("ctl00_PrimaryContent_MinPrice").value;
	url += "&MaxPrice=" + document.getElementById("ctl00_PrimaryContent_MaxPrice").value;
	url += "&MinBeds=" + document.getElementById("ctl00_PrimaryContent_MinBeds").value;
	url += "&MinBaths=" + document.getElementById("ctl00_PrimaryContent_MinBaths").value;
	url += "&MinGarages=" + document.getElementById("ctl00_PrimaryContent_MinGarages").value;
	//window.open(url);
	http_request2.onreadystatechange = showNumberOfHomes;
	http_request2.open('GET', url, true);
	http_request2.send(null);
}

function hideCover() {
	document.getElementById("coverDiv").style.display="none";
}

function showNumberOfHomes() {
	document.getElementById("coverDiv").style.display="";
	if (http_request2.readyState == 4) {
		if (http_request2.status == 200) {
			var hxmlobject = (new DOMParser()).parseFromString(http_request2.responseText, "text/xml");
			var root = hxmlobject.getElementsByTagName("NumHomes")[0];
			var items = root.getElementsByTagName("Count");
			var item = items[0];
			var description = item.getElementsByTagName("Value")[0].firstChild.nodeValue;
			document.getElementById("NumHomes").innerHTML = description;
			if (refreshMap) {
				document.getElementById("MapFrame").src = GoogleMapURL + "?ZoomSub=" + ZoomSub + "&OpenHouseDate=" + document.getElementById("ctl00_PrimaryContent_OHDate").value + "&MinGarages=" + document.getElementById("ctl00_PrimaryContent_MinGarages").value + "&MinBaths=" + document.getElementById("ctl00_PrimaryContent_MinBaths").value + "&MinBeds=" + document.getElementById("ctl00_PrimaryContent_MinBeds").value + "&MaxPrice=" + document.getElementById("ctl00_PrimaryContent_MaxPrice").value + "&MinPrice=" + document.getElementById("ctl00_PrimaryContent_MinPrice").value + "&OpenHouseSearchType=" + document.getElementById("ctl00_PrimaryContent_valSearchBy").value + "&Values=" + encodeURIComponent(document.getElementById("ctl00_PrimaryContent_valValues").value) + "&Width=" + iWidth + "&Height=" + iHeight;
			}
			refreshMap = false;
			ZoomSub = 1;
		}
	}
}

function AddToCompare(mlsnum) {
	compareListings.push(mlsnum);
	if (!(alertShown))
		alert("Click the \'Show Compared Properties\' Button on the lower right side to compare and print selected properties side-by-side.");
	document.getElementById("p_compare").style.display="";
	document.getElementById("p_clear").style.display="";
	alertShown = true;
}

function RemoveFromCompare(mlsnum) {
	for (var x = 0; x < compareListings.length; x++)
		if (compareListings[x] == mlsnum)
			compareListings.splice(x,1);
	if (compareListings.length == 0) {
		document.getElementById("p_compare").style.display="none";
		document.getElementById("p_clear").style.display="none";
	}
}

function showCompare() {
	window.open("OpenHouseCompare.aspx?MlsNums=" + compareListings.join(","));
}

function clearCompare() {
	compareListings = [];
	document.getElementById("p_compare").style.display="none";
	document.getElementById("p_clear").style.display="none";
}


function updateDDL(url) {
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = showNewDDLValues;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function showNewDDLValues() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var i=document.getElementById("ctl00_PrimaryContent_Values");
			for (x=i.length; x > -1; x--) {
				i.remove(x);
			}
			var xmlobject = (new DOMParser()).parseFromString(http_request.responseText, "text/xml");
			var root = xmlobject.getElementsByTagName("Category")[0];
			var items = root.getElementsByTagName("Item");
			document.getElementById("ctl00_PrimaryContent_valValues").value = "";
			for (var i = 0 ; i < items.length ; i++) {
				var item = items[i];
				var name = new String(item.getElementsByTagName("Value")[0].firstChild.nodeValue).replace(/_/g, ' ');
				var description = item.getElementsByTagName("Name")[0].firstChild.nodeValue;
				var myOpt = new Option;
				myOpt.value = description;
				myOpt.text  = description;
				document.getElementById("ctl00_PrimaryContent_Values").options[i] = myOpt;
			}
			document.getElementById("ctl00_PrimaryContent_Values").options[0].selected = true;
			CountHomes();
		} else {
			alert('There was a problem with the request.');
		}
	}
}
