function selectCountry(countryid, regionid, locationid) {	
	if(countryid) {
		$("#searchregion").
		load('/ajax.php?function=selectregions&countryid='+countryid+'&selected='+regionid,
		function() {
			$("#searchlocation").html('<select name="locationid" id="locationid"><option value="">Location</option></select>');
			if(regionid) {
				$("#searchlocation").load('/ajax.php?function=selectlocations&regionid='+regionid+'&selected='+locationid);	
			}
			$('#regionid').change(function() {
				var regionid = $(this).val();
				if(regionid) {
					$("#searchlocation").load('/ajax.php?function=selectlocations&regionid='+regionid+'&selected='+locationid);
				}
				else {
					$("#searchlocation").html('<select name="locationid" id="locationid"><option value="">Location</option></select>');
				}
			});	
		});

	}
	else {
		$("#searchregion").html('<select name="regionid" id="regionid"><option value="">Region</option></select>');
		$("#searchlocation").html('<select name="locationid" id="locationid"><option value="">Location</option></select>');
	}
}

function selectSaleType(saletype, minval, maxval) {
	if(saletype) {
		$("#searchminprice").load('/ajax.php?function=selectprices&field=min&saletype='+saletype+'&selected='+minval);
		$("#searchmaxprice").load('/ajax.php?function=selectprices&field=max&saletype='+saletype+'&selected='+maxval);
	}
	else {
		$("#searchminprice").html('<select name="minprice" id="minprice"><option value="">Min. Price</option></select>');
		$("#searchmaxprice").html('<select name="maxprice" id="maxprice"><option value="">Max. Price</option></select>');
	}	
}

function selectType(typeid) {
	$.get('/ajax.php?function=checkbedrooms&typeid='+typeid,function(usebedrooms){
		if(usebedrooms) {
			$("#searchbedroommin").load('/ajax.php?function=selectbedrooms&field=min');
			$("#searchbedroommax").load('/ajax.php?function=selectbedrooms&field=max');
		}
		else {
			$("#searchbedroommin").html('');
			$("#searchbedroommax").html('');
		}
	});
}


$(document).ready(function() {
						   
	$('#countryid').change(function() {
		selectCountry($(this).val(), false, false);
	});	


	$('#saletype').change(function() {
		selectSaleType($(this).val(), false, false);
	});	


	$('#typeid').change(function() {
		selectType($(this).val());
	});	
});
