function SetDefaultValues(){
	var querystring = decodeURI(location.search);
	if (!querystring)
		return;
	
	var queryStringDictionary = {};
	querystring = querystring.substring(1);
	var pairs = querystring.split("&");
	 for (var i = 0; i < pairs.length; i++) {
		var keyValuePair = pairs[i].split("=");
			if (!queryStringDictionary[keyValuePair[0]])
				queryStringDictionary[keyValuePair[0]] = keyValuePair[1];
			else
				if ($.isArray(queryStringDictionary[keyValuePair[0]]))
					queryStringDictionary[keyValuePair[0]].push(keyValuePair[1]);
				else
					queryStringDictionary[keyValuePair[0]] = [queryStringDictionary[keyValuePair[0]], keyValuePair[1]];
	}

	for (var key in queryStringDictionary) {
		var value = queryStringDictionary[key];
		var foundEl = $("[name='" + key + "']");
		if (foundEl.length > 1){	//probably a checkbox or radiobutton list
			foundEl.each(function(){
				this.checked = false;
			});
			var vals = ($.isArray(value)) ? value : [value];
			$.each(vals, function(idx, val){
				$("[name='" + key + "'][value='" + val +"']").each( function(){
					this.checked = true;
				});
			});
		}else{
			foundEl.val(value);
		}
	}
	
	GetCounts();
}

function ApplySummary(){
	var SearchSummary = $("#SearchSummary");
	SearchSummary.empty();
	
	var propertyStatus = $("input:radio[name='PropertyStatus']:checked").val();
	var propertyTypes = [];
	$("input[name='PropertyTypes']:checked").each(function(){propertyTypes.push($(this).val());});
	var counties = [];
	$("#Location :selected").each(function(){counties.push($(this).text().replace(/\(\d*\)/, ""));});
	var cities = [];
	$("#City :selected").each(function(){cities.push($(this).text().replace(/\(\d*\)/, ""));});
	var priceRange =  $("#PriceRange :selected").text().replace(/\(\d*\)/, "");
	var buildingSize = $("#SizeRange :selected").text().replace(/\(\d*\)/, "");
	var lotSize = $("#LotRange :selected").text().replace(/\(\d*\)/, "");
	var leasableSF = $("#LeaseRange :selected").text().replace(/\(\d*\)/, "");
	var leaseRate = $("#LeaseRateRange :selected").text().replace(/\(\d*\)/, "");
	var enteredWithinDays = $("#DateRange :selected").text().replace(/\(\d*\)/, "");
	

	if (propertyStatus != '')
		SearchSummary.append("<span>Property Status: " + propertyStatus + "</span><br/>");
	if (propertyTypes.length > 0)
		SearchSummary.append("<span>Property Types: " + propertyTypes.join(", ") + "</span><br/>");
	if (counties.length > 0)
		SearchSummary.append("<span>Counties: " + counties.join(", ") + "</span><br/>");
	if (cities.length > 0)
		SearchSummary.append("<span>Cities: " + cities.join(", ") + "</span><br/>");
	if (priceRange != '')
		SearchSummary.append("<span>Price Range: " + priceRange + "</span><br/>");
	if (buildingSize != '')
		SearchSummary.append("<span>Building Size: " + buildingSize + "</span><br/>");
	if (lotSize != '')
		SearchSummary.append("<span>Lot Size: " + lotSize + "</span><br/>");
	if (leasableSF != '')
		SearchSummary.append("<span>Leaseable SF: " + leasableSF + "</span><br/>");
	if (leaseRate != '')
		SearchSummary.append("<span>Lease Rate: " + leaseRate + "</span><br/>");
	if (enteredWithinDays != '')
		SearchSummary.append("<span>Date Entered: " + enteredWithinDays + "</span><br/>");
}

function ApplyCounts(o){
	var elCities = $("#City");
	var elCounties = $("#Location");
	var elPriceRange = $("#PriceRange");
	var elSizeRange = $("#SizeRange");
	var elLotRange = $("#LotRange");
	var elLeaseRange = $("#LeaseRange");
	var elLeaseRateRange = $("#LeaseRateRange");
	var elDateRange = $("#DateRange");
	
	var selectedCitiesValues = elCities.val() || [];
	elCities.dropdownchecklist("destroy");
	elCities.empty();
	for(var i = 0, l = o.CityCounts.length; i < l; ++i)
	{
		var city = o.CityCounts[i];
		var opt = '<option '
			+ ' value=\"' + city.City + '\" '
			+ ((jQuery.inArray(city.City, selectedCitiesValues) == -1) ? '' : ' selected ' )
			+ ' >&nbsp;' + city.City + '&nbsp;(' + city.Count + ')</option>';
		elCities.append(opt);
	}
	elCities.dropdownchecklist({ maxDropHeight: 100 });
	
	var selectedCountiesValues = elCounties.val() || [];
	elCounties.dropdownchecklist("destroy");
	elCounties.empty();
	for(var i = 0, l = o.CountyCounts.length; i < l; ++i)
	{
		var county = o.CountyCounts[i];
		var opt = '<option '
			+ ' value="' + county.County + '" '
			+ ((jQuery.inArray(county.County, selectedCountiesValues) == -1) ? '' : ' selected ' )
			+ ' >&nbsp;' + county.County + '&nbsp;(' + county.Count + ')</option>';
		elCounties.append(opt);
	}
	elCounties.dropdownchecklist({ maxDropHeight: 100 });
	
	var selectedPriceRange = elPriceRange.val();
	elPriceRange.empty();
	elPriceRange.append('<option value="PriceRange_All">All&nbsp;(' + o.PriceRange_All + ')</option>');
	elPriceRange.append('<option value="PriceRange_0_300000">$0 to $300,000&nbsp;(' + o.PriceRange_0_300000 + ')</option>');
	elPriceRange.append('<option value="PriceRange_300001_750000">$300,001 to $750,000&nbsp;(' + o.PriceRange_300001_750000 + ')</option>');
	elPriceRange.append('<option value="PriceRange_750001_1500000">$750,001 to $1,500,000&nbsp;(' + o.PriceRange_750001_1500000 + ')</option>');
	elPriceRange.append('<option value="PriceRange_1500001_Up">Over $1,500,000&nbsp;(' + o.PriceRange_1500001_Up + ')</option>');
	elPriceRange.val(selectedPriceRange);
	
	var selectedSizeRange = elSizeRange.val();
	elSizeRange.empty();
	elSizeRange.append('<option value="BuildingSize_All">All&nbsp;(' + o.BuildingSize_All + ')</option>');
	elSizeRange.append('<option value="BuildingSize_0_10000">0 to 10,000 SF&nbsp;(' + o.BuildingSize_0_10000 + ')</option>');
	elSizeRange.append('<option value="BuildingSize_10001_20000">10,001 to 20,000 SF&nbsp;(' + o.BuildingSize_10001_20000 + ')</option>');
	elSizeRange.append('<option value="BuildingSize_20001_50000">20,001 to 50,000 SF&nbsp;(' + o.BuildingSize_20001_50000 + ')</option>');
	elSizeRange.append('<option value="BuildingSize_50001_Up">Over 50,001 SF&nbsp;(' + o.BuildingSize_50001_Up + ')</option>');
	elSizeRange.val(selectedSizeRange);
	
	var selectedLotRange = elLotRange.val();
	elLotRange.empty();
	elLotRange.append('<option value="LotSize_All">All&nbsp;(' + o.LotSize_All + ')</option>');
	elLotRange.append('<option value="LotSize_0_5">0 to 5 Acres&nbsp;(' + o.LotSize_0_5 + ')</option>');
	elLotRange.append('<option value="LotSize_5_10">5.1 to 10&nbsp;(' + o.LotSize_5_10 + ')</option>');
	elLotRange.append('<option value="LotSize_10_50">10.1 to 50 Acres&nbsp;(' + o.LotSize_10_50 + ')</option>');
	elLotRange.append('<option value="LotSize_50_Up">Over 50 Acres&nbsp;(' + o.LotSize_50_Up + ')</option>');
	elLotRange.val(selectedLotRange);
	
	var selectedLeaseRange = elLeaseRange.val();
	elLeaseRange.empty();
	elLeaseRange.append('<option value="LeaseableSF_All">All&nbsp;(' + o.LeaseableSF_All + ')</option>');
	elLeaseRange.append('<option value="LeaseableSF_0_1000">0 to 1000 SF&nbsp;(' + o.LeaseableSF_0_1000 + ')</option>');
	elLeaseRange.append('<option value="LeaseableSF_1001_1500">1001 to 1500 SF&nbsp;(' + o.LeaseableSF_1001_1500 + ')</option>');
	elLeaseRange.append('<option value="LeaseableSF_1501_3000">1501 to 3000 SF&nbsp;(' + o.LeaseableSF_1501_3000 + ')</option>');
	elLeaseRange.append('<option value="LeaseableSF_3001_Up">Over 3000 SF&nbsp;(' + o.LeaseableSF_3001_Up + ')</option>');
	elLeaseRange.val(selectedLeaseRange);
	
	var selectedLeaseRateRange = elLeaseRateRange.val();
	elLeaseRateRange.empty();
	elLeaseRateRange.append('<option value="LeaseRate_All">All&nbsp;(' + o.LeaseRate_All + ')</option>');
	elLeaseRateRange.append('<option value="LeaseRate_0_5">$0 to $5.00&nbsp;(' + o.LeaseRate_0_5 + ')</option>');
	elLeaseRateRange.append('<option value="LeaseRate_5_10">$5.01 to $10.00&nbsp;(' + o.LeaseRate_5_10 + ')</option>');
	elLeaseRateRange.append('<option value="LeaseRate_10_20">$10.01 to $20.00&nbsp;(' + o.LeaseRate_10_20 + ')</option>');
	elLeaseRateRange.append('<option value="LeaseRate_20_Up">Over $20.00&nbsp;(' + o.LeaseRate_20_Up + ')</option>');
	elLeaseRateRange.val(selectedLeaseRateRange);
	
	var selectedDateRange = elDateRange.val();
	elDateRange.empty();
	elDateRange.append('<option value="DateEnteredInLast_AllDays">All&nbsp;(' + o.DateEnteredInLast_AllDays + ')</option>');
	elDateRange.append('<option value="DateEnteredInLast_1Days">Within the last 24 hours&nbsp;(' + o.DateEnteredInLast_1Days + ')</option>');
	elDateRange.append('<option value="DateEnteredInLast_3Days">Within the last 3 Days&nbsp;(' + o.DateEnteredInLast_3Days + ')</option>');
	elDateRange.append('<option value="DateEnteredInLast_7Days">Within the last week&nbsp;(' + o.DateEnteredInLast_7Days + ')</option>');
	elDateRange.append('<option value="DateEnteredInLast_30Days">Within the last month&nbsp;(' + o.DateEnteredInLast_30Days + ')</option>');
	elDateRange.append('<option value="DateEnteredInLast_90Days">Within the last 3 months&nbsp;(' + o.DateEnteredInLast_90Days + ')</option>');
	elDateRange.append('<option value="DateEnteredInLast_365Days">Within the last year&nbsp;(' + o.DateEnteredInLast_365Days + ')</option>');
	elDateRange.val(selectedDateRange);
}

function GetCounts(wait){
	wait = (typeof wait == undefined) ? true : wait;
	var propertyStatus = $("input:radio[name='PropertyStatus']:checked").val();
	var propertyTypes = [];
	$("input[name='PropertyTypes']:checked").each(function(){propertyTypes.push($(this).val());});

	var searchParam = {
		'propertyStatus': propertyStatus,
		'propertyTypes': propertyTypes,
		'counties': ($("#Location").val() || []),
		'cities': ($("#City").val() || []),
		'priceRange': $("#PriceRange").val(),
		'buildingSize': $("#SizeRange").val(),
		'lotSize': $("#LotRange").val(),
		'leasableSF': $("#LeaseRange").val(),
		'leaseRate': $("#LeaseRateRange").val(),
		'enteredWithinDays': $("#DateRange").val()
	};
	
	$.ajax({
		async: wait,
		type: 'POST',
		url: '/helpers/search.asmx/GetPropStatusCounts',
		data: JSON.stringify(searchParam),
		dataType: 'json',
		contentType: 'application/json; charset=utf-8',
		success: function(msg) {
			ApplyCounts(msg.d);
		},
		error: function(error, textStatus) {
			alert('Ajax Error' + error.responseText);
		}
	});
}

$(document).ready(function(){
	GetCounts(false);
	SetDefaultValues();
	ApplySummary();
	
	$('#reset').click(function(){
		window.location.href = 'PropertySearch.aspx';
	});
	
	$('#PropStatusBoth').click(function() {			
		$('#PriceRange,#LotRange').attr('disabled','');
		$('#LeaseRange,#LeaseRateRange').attr('disabled','');
	});
	$('#PropStatusLease').click(function() {
		$('#PriceRange,#LotRange').attr('disabled','disabled');
		$('#LeaseRange,#LeaseRateRange').attr('disabled','');
	});
	$('#PropStatusSale').click(function() {
		$('#PriceRange,#LotRange').attr('disabled','');
		$('#LeaseRange,#LeaseRateRange').attr('disabled','disabled');
	});

	var propertySearchElements = "#City,#Location,#PriceRange,#SizeRange,#LotRange,#LeaseRange,#LeaseRateRange,#DateRange";
	$(propertySearchElements).change(function(){
		//$.blockUI({ message: '<h1><img src="images/ajax-loader.gif" /><br/>Just a moment...</h1>' });
		window.setTimeout(ApplySummary, 1);
		window.setTimeout(GetCounts, 1);
	});
	$("input:radio[name='PropertyStatus'],input[name='PropertyTypes']").click(function(){
		//$.blockUI({ message: '<h1><img src="images/ajax-loader.gif" /><br/>Just a moment...</h1>' });
		window.setTimeout(ApplySummary, 1);
		window.setTimeout(GetCounts, 1);
	});
});

	//$(document).ajaxStop($.unblockUI);