// JavaScript Document
$(document).ready(function()	{

      
	$('#simple_search').click(function() {
 		d = $('#id_district').val()
		if($("input[@name='property_type']").is(":checked"))
		    t = 'True';
		else
		    t = '';
 		l = $('#id_location').val()
 		
 		if(d != '0' || t || l)
 		{
 		  $('#home_form').submit()
 		}
 		else
 		{
 		   alert('Please fill at least one field to search')
 		}
	});
	
	$('#listing_id_search').click(function() {
 		l = $('#listing_id').val()
		
 		
 		if(l)
 		{
 		  $('#listing_id_form').submit()
 		}
 		else
 		{
 		   alert('Please fill Listing Id field to search')
 		}
	});
	
	// ajax that will handle change of location
	$('#id_district').change(function(){
		val = $(this).val();
	    if(val != '0')
	    {
	    $.ajax({
		type: "GET",
		url: "/ajax/location-district/",
		data: "district="+val,
		dataType: "json",
		success: function(json){
		    if(json.error)
		    {
			$("#loc_div").show("slow");
			$("#id_location").html('<option value=0>'+json.error+'</option>');
			$("#id_location").attr('disabled', 'disabled');
		    }
		    else
		    {
			$("#id_location").html('')
			$("#id_location").append("<option value=''>---All--</option>");
			$.each(json, function(i,item){
			    $("#id_location").append('<option value=' + item.pk + "> " + item.fields.name + "</option>");
			});
			$("#id_location").removeAttr('disabled');
			$("#loc_div").show("slow");
		    //$('#gallons_cont').html(json['gallons']);
		    }
		}
            });
	    }
	    else
		alert('Please select district');
	});
	
	
});