﻿var scriptname = 'search_results.php?';
var runQuery = false;
var queryUrl = '';
var regionsUrl = '';
var timer;
var timer2;
var categoryId;

function runQueryFunc() {
	 queryUrl1 = scriptname + 'category_id=' + categoryId + queryUrl;
	
	downloadUrl(queryUrl1, function(data) {
		//alert('data');
		var markers = data.documentElement.getElementsByTagName("marker");
			nonev = 0;
		$('#search_properties_form input[name=regions[]]').each(function() {
		if($(this).attr('checked') == true) {
			nonev = 1;
		
		}
		});
		if(nonev==0)
		{
			$('#results_found_count').html('0');
		}
		else
		{
			if(markers.length==0)
			{
			timer2 = setTimeout("iii()", 0);
			}
			else
			{
				if(timer2) clearTimeout(timer2);
				
			$('#results_found_count').html(''+markers.length);
			}
			
			}
	});
}

function iii()
{
	$('#results_found_count').html('0');
}
function setTimer() {
	timer = setTimeout("runQueryFunc()", 0);
}

/*
Bind functions to the HTML form controls to find the "results found" number
*/
function resultsFoundInit(in_categoryId) {
	categoryId = in_categoryId;
	$('#search_properties_form input[name=regions[]]').each(function() {
		$(this).bind('change', buildUrl2);
		$(this).bind('click', buildUrl2);
	});
	$('#search_properties_form input[name=price_from]').bind('keyup', buildUrl2);
	$('#search_properties_form input[name=price_to]').bind('keyup', buildUrl2);
	$('#search_properties_form select[name=type_id]').bind('change', buildUrl2);
	$('#search_properties_form select[name=bedrooms]').bind('change', buildUrl2);
	$('#search_properties_form select[name=status_id]').bind('change', buildUrl2);
	$('#search_properties_form select[name=bathrooms]').bind('change', buildUrl2);
	$('#search_properties_form select[name=building_size]').bind('change', buildUrl2);
	$('#search_properties_form select[name=land_size]').bind('change', buildUrl2);
	$('#search_properties_form .selectall, #search_properties_form .deselectall').bind('click', function() {
		setTimeout("buildUrl2()", 350);
	});
}

/*
All the magic that gets the forms data and compiles the feed url
*/
function buildUrl() {
	queryUrl = '';
	
	if(timer) clearTimeout(timer);
	//get all the region values
	$('#search_properties_form input[name=regions[]]').each(function() {
		if($(this).attr('checked') == true) {
			queryUrl += ',';
			queryUrl += $(this).val();
		}
	});
	if(queryUrl != '') {
		queryUrl = '&regions=' + queryUrl.slice(1);
	}
	
	//get the price form value
	var priceFrom = $('#search_properties_form input[name=price_from]').val();
	if(priceFrom != '') {
		queryUrl += '&price_from=' + priceFrom;
	}
	//get the price to value
	var priceTo = $('#search_properties_form input[name=price_to]').val();
	if(priceTo != '') {
		queryUrl += '&price_to=' + priceTo;
	}
	//get the type id value
	var typeId = $('#search_properties_form select[name=type_id]').val();
	if(typeId != '') {
		queryUrl += '&type_id=' + typeId;
	}
	//get the bedrooms value
	var bedrooms = $('#search_properties_form select[name=bedrooms]').val();
	if(bedrooms != '') {
		queryUrl += '&bedrooms=' + bedrooms;
	}
	//get the status id value
	var status_id = $('#search_properties_form select[name=status_id]').val();
	if(status_id != '') {
		queryUrl += '&status_id=' + status_id;
	}
	//get the bathrooms value
	var bathrooms = $('#search_properties_form select[name=bathrooms]').val();
	if(bathrooms != '') {
		queryUrl += '&bathrooms=' + bathrooms;
	}
	//get the building size value
	var building_size = $('#search_properties_form select[name=building_size]').val();
	if(building_size != '') {
		queryUrl += '&building_size=' + building_size;
	}
	//get the land size value
	var land_size = $('#search_properties_form select[name=land_size]').val();
	if(land_size != '') {
		queryUrl += '&land_size=' + land_size;
	}
	setTimer();
}


function buildUrl2() {
	queryUrl = '';
	var tokyoNum = 0;
	var count1 = 0;
	if(timer) clearTimeout(timer);
	var mymarkers = []; 
	//get all the region values
	LatLngList = [];
	$('#search_properties_form input[name=regions[]]').each(function() {
		if($(this).attr('checked') == true) {
			queryUrl += ','; 
			queryUrl += $(this).val();
		}
	});
	//$('#results_found_count').html(''+count1);
	if(queryUrl != '') {
		
		queryUrl = '&regions=' + queryUrl.slice(1);
	
 }
//alert('done');
	
	//get the price form value
	var priceFrom = $('#search_properties_form input[name=price_from]').val();
	if(priceFrom != '') {
		queryUrl += '&price_from=' + priceFrom;
	}
	//get the price to value
	var priceTo = $('#search_properties_form input[name=price_to]').val();
	if(priceTo != '') {
		queryUrl += '&price_to=' + priceTo;
	}
	//get the type id value
	var typeId = $('#search_properties_form select[name=type_id]').val();
	if(typeId != '') {
		queryUrl += '&type_id=' + typeId;
	}
	//get the bedrooms value
	var bedrooms = $('#search_properties_form select[name=bedrooms]').val();
	if(bedrooms != '') {
		queryUrl += '&bedrooms=' + bedrooms;
	}
	//get the status id value
	var status_id = $('#search_properties_form select[name=status_id]').val();
	if(status_id != '') {
		queryUrl += '&status_id=' + status_id;
	}
	//get the bathrooms value
	var bathrooms = $('#search_properties_form select[name=bathrooms]').val();
	if(bathrooms != '') {
		queryUrl += '&bathrooms=' + bathrooms;
	}
	//get the building size value
	var building_size = $('#search_properties_form select[name=building_size]').val();
	if(building_size != '') {
		queryUrl += '&building_size=' + building_size;
	}
	//get the land size value
	var land_size = $('#search_properties_form select[name=land_size]').val();
	if(land_size != '') {
		queryUrl += '&land_size=' + land_size; 
	}
setTimer();
}
