//$("form.searchform input[@type=text]").remove();
//$("form.searchform label.searchlabel").after("<select name='areatop' disabled='disabled'><option value=''>Select a Province...</option></select>");

var areatop = $("form.searchform select[@name=areatop]");
//areatop.width((areatop.width() + 5) + "px");

//areatop.after("&nbsp;&nbsp;&nbsp;<select name='areasub' disabled='disabled'><option value=''>Select a Town...</option></select>");
var areasub = $("form.searchform select[@name=areasub]");
//areasub.width((areasub.width() + 5) + "px");

//areasub.after("&nbsp;&nbsp;&nbsp;<select name='category' disabled='disabled'><option value=''>Select a Category...</option></select>");
var cat = $("form.searchform select[@name=category]");
//cat.width((cat.width() + 5) + "px");

//cat.after("&nbsp;&nbsp;&nbsp;<select name='price' disabled='disabled'><option value=''>Select a Price...</option></select>");
var price = $("form.searchform select[@name=price]");
//price.width((price.width() + 5) + "px");


$("form.searchform select[@name=areatop] option").remove();
areatop.append("<option value=''>Loading...</option>");
$.ajax({
	type: "GET",
	url: "index.php",
	data: "comp=property&op=optionareatop&popup=xml",
	success: function(options) {
		$("form.searchform select[@name=areatop] option").remove();
		areatop.append("<option value=''>Select a Town...</option><option value=''>All Towns</option>");
		areatop.append(options);
		var areatopsel;
		if (curAreaTop != "-1") {
			areatopsel = $("form.searchform select[@name=areatop] option[@value=" + curAreaTop + "]");
		} else {
			areatopsel = $("form.searchform select[@name=areatop] option").contains("Select a Town...");
		}
		areatopsel.attr("id", "blkPropSearchBox-areatopsel");
		var e = document.getElementById("blkPropSearchBox-areatopsel");
		if (e) {
			var selects = e.form.getElementsByTagName("select");
			if(selects[0]) selects[0].selectedIndex = e.index;
		}
		areatop.attr("disabled", false);
		if (curAreaTop == areatop.val() && curAreaSub != "-1") {
			areatop.change();
		}
	}
	
	
});

areatop.change(function() {
	window.setTimeout(remSelectAreaTop, 200);
	areasub.attr("disabled", "disabled");
	cat.attr("disabled", "disabled");
	price.attr("disabled", "disabled");
	$("form.searchform select[@name=areasub] option").remove();
	areasub.append("<option value=''>Loading...</option>");
	$.ajax({
		type: "GET",
		url: "index.php",
		data: "comp=property&op=optionareasub&popup=xml&areatop=" + areatop.val(),
		success: function(options) {
			$("form.searchform select[@name=areasub] option").remove();
			areasub.append("<option value=''>Select a Suburb...</option><option value='0'>All Suburbs</option>");
			areasub.append(options);
			var areasubsel;
			if (curAreaTop == areatop.val() && curAreaSub != "-1") {
				areasubsel = $("form.searchform select[@name=areasub] option[@value=" + curAreaSub + "]");
			} else {
				areasubsel = $("form.searchform select[@name=areasub] option").contains("Select a Suburb...");
			}
			areasubsel.attr("id", "blkPropSearchBox-areasubsel");
			var e = document.getElementById("blkPropSearchBox-areasubsel");
			e.form.getElementsByTagName("select")[1].selectedIndex = e.index;
			areasub.attr("disabled", false);
			if (curAreaSub == areasub.val() && curCat != "-1") {
				areasub.change();
			}
		}
	});
});

areasub.change(function() {
	window.setTimeout(remSelectAreaSub, 200);
	cat.attr("disabled", "disabled");
	price.attr("disabled", "disabled");
	$("form.searchform select[@name=category] option").remove();
	cat.append("<option value=''>Loading...</option>");
	$.ajax({
		type: "GET",
		url: "index.php",
		data: "comp=property&op=optioncategory&popup=xml&areatop=" + areatop.val() + "&areasub=" + areasub.val(),
		success: function(options) {
			$("form.searchform select[@name=category] option").remove();
			cat.append("<option value=''>Select a Category...</option><option value='0'>All Categories</option>");
			cat.append(options);
			var catsel;
			if (curAreaSub == areasub.val() && curCat != "-1") {
				catsel = $("form.searchform select[@name=category] option[@value=" + curCat + "]");
			} else {
				catsel = $("form.searchform select[@name=category] option").contains("Select a Category...");
			}
			catsel.attr("id", "blkPropSearchBox-catsel");
			var e = document.getElementById("blkPropSearchBox-catsel");
			e.form.getElementsByTagName("select")[2].selectedIndex = e.index;
			cat.attr("disabled", false);
			if (curCat == cat.val() && curPrice != "-1") {
				cat.change();
			}
		}
	});
});

cat.change(function() {
	window.setTimeout(remSelectCat, 200);
	price.attr("disabled", "disabled");
	$("form.searchform select[@name=price] option").remove();
	price.append("<option value=''>Loading...</option>");
	$.ajax({
		type: "GET",
		url: "index.php",
		data: "comp=property&op=optionprice&popup=xml&areatop=" + areatop.val() + "&areasub=" + areasub.val() + "&category=" + cat.val(),
		success: function(options) {
			$("form.searchform select[@name=price] option").remove();
			price.append("<option value=''>Select a Price...</option><option value='0'>All Prices</option><option value='250'>Up to 500 000</option><option value='500'>500 000 - 1 Million</option><option value='1000'>1 - 3 Million</option><option value='2000'>3 - 10 Million</option><option value='3000'>10 Million +</option>");
//			price.append(options);
			var pricesel;
			if (curCat == cat.val() && curPrice != "-1") {
				pricesel = $("form.searchform select[@name=price] option[@value=" + curPrice + "]");
				$("form.searchform select[@name=price] option").contains("Select a Price...").remove();
		} else {
				pricesel = $("form.searchform select[@name=price] option").contains("Select a Price...");
			}
			pricesel.attr("id", "blkPropSearchBox-pricesel");
			var e = document.getElementById("blkPropSearchBox-pricesel");
			e.form.getElementsByTagName("select")[3].selectedIndex = e.index;
			price.attr("disabled", false);
		}
	});
});

function searchSubmit() {
	var area = areasub.val() == "" ? areatop.val() : areasub.val();
	$("form.searchform").append("<input type='hidden' name='area' value='" + area + "' />");
	$("form.searchform").attr("id", "blkPropSearchBox-form");
	document.getElementById("blkPropSearchBox-form").submit();
}

$("form.searchform").submit(searchSubmit);
price.change(searchSubmit);

function remSelectAreaTop() {
	$("form.searchform select[@name=areatop] option").contains("Select a Province...").remove();
}

function remSelectAreaSub() {
	$("form.searchform select[@name=areasub] option").contains("Select a Town...").remove();
}

function remSelectCat() {
	$("form.searchform select[@name=category] option").contains("Select a Category...").remove();
}

var searchtxt = $("#propsearchtxt");
searchtxt.focus(function() { if (searchtxt.val() == "Ref Num") searchtxt.val(""); });
searchtxt.blur(function() { if (searchtxt.val() == "") searchtxt.val("Ref Num"); });

