/* Javascript for the homepage forms */

$(document).ready(function(){
	// Put label text into text fields
	$("input.labelInside").putLabelInside();
	
	// Split the min/max price drop-down value and put them in two hidden inputs
	$("select.minMaxSelect").minMaxSelect();
	
	// Change the advanced search and browse link urls based on radio button selected
	advancedLink = $("a#forsale_advanced");
	browseLink = $("a#forsale_browse");
	homesForm = $("form[name=quickSearchHomes]")[0];
	
	// First check the radio buttons on page load and set the links accordingly
	if ($("input#homes_for_sale")[0].checked) {
		setLinksToHomesForSale();
	} else {
		setLinksToNewHomes();
	}
	
	// Then setup event handlers to set the links when the radio buttons are changed
	$("input#homes_for_sale").click(function(e) {
		if (this.checked) {
			setLinksToHomesForSale();
		}
	});
	
	$("input#new_homes").click(function(e) {
		if (this.checked) {
			setLinksToNewHomes();
		}
	});
	
	$("form[name=quickSearchHomes]").bind("submit", cleanInputHomes);
	$("form[name=quickSearchRentals]").bind("submit", cleanInputRentals);
});

function setLinksToHomesForSale() {
	advancedLink.attr("href","http://nwhomes.nwsource.com/properties/search/searchForm.php");
	browseLink.attr("href","/realestate/browse_neighborhoods.html");
	homesForm.action = homesForm.action.replace(/newhomes/, "nwhomes");
}

function setLinksToNewHomes() {
	advancedLink.attr("href","http://newhomes.nwsource.com/properties/search/searchForm.php");
	browseLink.attr("href","/realestate/newhomes/browse_neighborhoods.html");
	homesForm.action = homesForm.action.replace(/nwhomes/, "newhomes");
}

function cleanInputHomes(e) {	
	if ($("input#city").val() == "City")
		$("input#city").val("");
		
	if ($("input#zip").val() == "Zip code")
		$("input#zip").val("");
}

function cleanInputRentals(e) {	
	if ($("input#rent_city").val() == "City")
		$("input#rent_city").val("");
		
	if ($("input#rent_zip").val() == "Zip code")
		$("input#rent_zip").val("");
	
	if ($("select#prop_types").val() == "")
		$("select#prop_types").attr("name","noProperyTypes")
}