// "Global" vars
var selected_areas = [];
var running_total = 0;
var last_keywords = '';
var current_keywords = '';
var timer;

var last_location_keyword = '';
var current_location_keyword = '';
var timer2;


function toggle_search_area(area){
	
	var area_name = area.id;
	//alert(area_name);
	
	if(area.src.match("_off.png")){	// Area has been selected, so add in
		new_file = "_on.png";
		selected_areas.push(area_name);
		
		$(area_name).src = '/images/layout/' + theme + '/search/' + area_name + new_file;
	}
	else{								// Area has been de-selected, so remove
		new_file = "_off.png";	
		selected_areas.splice(selected_areas.indexOf(area_name), 1);
		//get_results_preview_1();
		$(area_name).src = '/images/layout/' + theme + '/search/' + area_name + new_file;
	}
	//alert(area_name + new_file);
	

	//alert(selected_areas);
	if(selected_areas.size()>0){
		appear_if_not_visible("next");
		appear_if_not_visible("info");
		get_results_preview_1();
	}
	else{
		toggle_div("total_1", false);
		toggle_div("next", false);
		toggle_div("info", false);
	}
	
	return true;	
}

function submit_form(){
	$("areas").value = selected_areas.toJSON();
	$("search_form").submit(); 
}

// Makes sure the max > min and warns if not
function check_price_range(no_ajax){
	
	var do_ajax = no_ajax || true;
	
	if($('min_price')){
		minimum = $("min_price").options[$("min_price").selectedIndex].value;
		maximum = $("max_price").options[$("max_price").selectedIndex].value;
	}
	else{
		minimum = $("min_price_lettings").options[$("min_price_lettings").selectedIndex].value;
		maximum = $("max_price_lettings").options[$("max_price_lettings").selectedIndex].value;
	}
	
	//alert(minimum + ' ' + maximum);
	if(minimum>0 && maximum>0 && (parseInt(minimum)>parseInt(maximum))){
		$("errors").update("The minimum amount cannot be more than the maximum amount");
		toggle_div("errors", true);
		toggle_div("search", false);
	}
	else{
		toggle_div("errors", false);
		toggle_div("search", true);
		if(do_ajax) get_results_preview();
	}
	return true;
}


function get_results_preview_1(){
		
	new Ajax.Request('/search/ajaxpreview', {
		
		method:'post',
		
		parameters: {
			areas: selected_areas.toJSON(),
			search_type: parseInt($("search_type").value),
			reduced: parseInt($("reduced").value),
			flexible: parseInt($("flexible").value),
			location_keyword: current_location_keyword
		},
		
		onLoading: function(){
			$("total_1").update('<img src="/images/loading.gif" alt="Content is loading" />');
		},

		onSuccess: function(transport){
			$("total_1").update(transport.responseText ? transport.responseText : "0");
			refresh_div('total_1');
			appear_if_not_visible("next");
			appear_if_not_visible("info");
		},
		
		onFailure: function(transport){ handle_error(request, "/"); }
	
	});

}




function get_results_preview(){
	//alert(selected_areas);
	
	//alert(current_location_keyword);
	
	if($('min_price') || $('max_price')){
		var min_price_val = $("min_price").options[$("min_price").selectedIndex].value;
		var max_price_val = $("max_price").options[$("max_price").selectedIndex].value;
		var min_price_lettings_val = 0;
		var max_price_lettings_val = 0;
	}
	else {
		var min_price_val = 0;
		var max_price_val = 0;	
		var min_price_lettings_val = $("min_price_lettings").options[$("min_price_lettings").selectedIndex].value;
		var max_price_lettings_val = $("max_price_lettings").options[$("max_price_lettings").selectedIndex].value;
	}
	
	
	new Ajax.Request('/search/ajaxpreview', {
		
		method:'post',
		
		parameters: {
			areas: selected_areas.toJSON(), 
			bedrooms: $("bedrooms").options[$("bedrooms").selectedIndex].value,
			min_price: min_price_val,
			max_price: max_price_val,
			min_price_lettings: min_price_lettings_val,
			max_price_lettings: max_price_lettings_val,
			property_type_id: $("property_type_id").options[$("property_type_id").selectedIndex].value,
			search_type: parseInt($("search_type").value),
			reduced: parseInt($("reduced").value),
			flexible: parseInt($("flexible").value),
			keywords: current_keywords, // $F() cannot be used in IE for some reason */
			location_keyword: current_location_keyword
		},
		
		onLoading: function(){
			$("total_2").update('<img src="/images/loading.gif" alt="Content is loading" />');
		},

		onSuccess: function(transport){
			$("total_2").update(transport.responseText ? transport.responseText : "0");
			refresh_div('total_2');
		},
		
		onFailure: function(transport){ handle_error(request, "/"); }
	
	});

}



// counter
var i = 0;

// create object
imageObj = new Image();

// set image list
images = new Array();
images[0]="/images/layout/"+theme+"/search/uttoxeter_on.png";
images[1]="/images/layout/"+theme+"/search/ashbourne_on.png";
images[2]="/images/layout/"+theme+"/search/stafford_on.png";
images[3]="/images/layout/"+theme+"/search/lichfield_on.png";
images[4]="/images/layout/"+theme+"/search/burton_upon_trent_on.png";
images[5]="/images/layout/"+theme+"/search/ashby_de_la_zouch_on.png";
images[6]="/images/layout/"+theme+"/search/stafford_on.png";
images[7]="/images/layout/"+theme+"/search/lichfield_on.png";
images[8]="/images/layout/"+theme+"/search/burton_upon_trent_on.png";
images[9]="/images/layout/"+theme+"/search/ashby_de_la_zouch_on.png";
images[10]="/images/layout/"+theme+"/search/west_bridgford_on.png";
images[11]="/images/layout/"+theme+"/search/east_leake_on.png";
images[12]="/images/layout/"+theme+"/search/loughborough_on.png";


/*	This bit has been written to trigger a get_results_preview() following an update to the 
	keywords field. There is a ready-made class (Form.Element.DelayedObserver) in controls.js but it
	only worked in FF, so I had to write this workaround. Also, $().value, $().getValue() or indeed $F() doesn't seem to work in IE for text fields
	so I had to set a global variable (current_keywords) to access the value (which is sent via onkeyup=monitor_keywords(this.value)).
	Grr indeed.
*/
function check_keywords(){
	if(last_keywords == current_keywords) return false;	
	// Keywords must have changed...
	if((current_keywords.length)<=2) return false; // 2 letters is not enough :(
	last_keywords = current_keywords;
	get_results_preview(); 
	return true;
}

function monitor_keywords(keywords){
	current_keywords = keywords;
	if(timer) clearTimeout(timer);
	timer = setTimeout(check_keywords, 750);
	return true;
}

function check_location_keyword(){
	
	if(last_location_keyword == current_location_keyword) return false;	
	
	// Keywords must have changed...
	if((current_location_keyword.length)<=2){
		toggle_div("total_1", false);
		toggle_div("next", false);
		toggle_div("info", false);
		return false; // 2 letters is not enough :(
	}
	
	last_location_keyword = current_location_keyword;
	
	get_results_preview_1();
	
	return true;
}


function monitor_location_keyword(keywords){

	current_location_keyword = keywords;
	
	if(timer2) clearTimeout(timer2);
	timer2 = setTimeout(check_location_keyword, 750);
	return true;
}


