$(document).ready(function() {
	
	$('form.home-search #select-state').change(function(e){
		if ($(this).val().toLowerCase() != 'select a state')
		{
			$('form.home-search #select-count, form.home-search #select-price').removeAttr('disabled');
		}
		else
		{
			$('form.home-search #select-count, form.home-search #select-price').val('ALL');
			$('form.home-search #select-count, form.home-search #select-price').attr('disabled', true);
		}
	});

	$('form.home-search button').click(function(e){
		e.preventDefault();

		if ($('form.home-search #select-state').val().toLowerCase() == 'select a state')
		{
			// If nothing is selected, send them to the generic "finding your home" page
			window.location = "/finding-your-home";
		}
		else
		{
			var url = "/finding-your-home/" + $('form.home-search #select-state').val();
			
			var count = $('form.home-search #select-count').val();
			var price = $('form.home-search #select-price').val();
			
			// If only the state is selected, just send them to the state's finding page
			if ((count == '' || count.toLowerCase() == 'all') && (price == '' || price.toLowerCase() == 'all'))
			{
				// Send visitor to the state's finding page
				window.location = url;
			}
			else
			{
				// Do search results
				$("form.home-search").attr('action',url);
				$("form.home-search").submit();
			}
		}
	});
	
	$('#select-community').change(function(e){
		if ($(this).val() != '')
		{
			var url = $('#select-community').val();
			window.location = url;
		}		
	});
	
});
