function calcMaximo()
{
	document.getElementById('maximo').value = '';
}

$(function(){
//Document ready

//if($("#province").val() == '9') alert ('epaaaaa');
//$("#elsresultats").hide();

//$("#elsresultats").hide();
$("#elboto").show();

	$('div.ajax div.text input').val(''); //aquí podariem el valor inicial.
	
	//bind change events
	$("form#search :checkbox, form#search :radio").click(showValues);
    $("form#search select").change(showValues);
    
	$('input.button').hover(
		function(){
			$(this).addClass('hover');
		},//over
		function(){
			$(this).removeClass('hover');
		}//out
	);
	
});


function showValues() {
	
	$("#elsresultats").fadeIn("slow");
	$("#elboto").fadeIn("slow");

    if ($(this).attr('id') == 'provincia')
	 { //Loading cities     
	 	if((document.getElementById('provincia').value == 9) || (document.getElementById('provincia').value == 26) || (document.getElementById('provincia').value == 18) || (document.getElementById('provincia').value == 44) || document.getElementById('provincia').value == 0)
		{
			$('#comarca').attr('disabled', false);
			$.ajax(
			{
				url: comarca_script_url,
				data: "id_provincia="+document.getElementById('provincia').value,
				success: function(html)
				{
			    	$('#comarca').html(html);
			    	$('#comarca').attr('disabled', false);
				}
			});
		}
		else
		{
			document.getElementById('comarca').options.length = 1;    		
   			$('#comarca option:first').text('Seleccionar Comarca');
			$('#comarca').attr('disabled', true);
		}
		$('#ciudad').attr('disabled', true);
    	if ($(this).val() > 0)
		{			
			document.getElementById('ciudad').options.length = 1;    		
    		$('#ciudad option:first').text('Loading...');
    		$.ajax(
			{
				url: city_script_url,
				data: "provincia_id="+$(this).val(),
				success: function(html)
				{
			    	$('#ciudad').html(html);
			    	$('#ciudad').attr('disabled', false);
				}
			});    			
    	} 
		else 
		{    		
    		document.getElementById('ciudad').options.length = 1;    		
    		$('#ciudad option:first').text('Seleccionar Provincia');
    	}
    }
	
	if ($(this).attr('id') == 'comarca')
	 { //Loading cities     
		if((document.getElementById('provincia').value == 9) || (document.getElementById('provincia').value == 26) || (document.getElementById('provincia').value == 18) || (document.getElementById('provincia').value == 44))
			$('#provincia').attr('disabled', false);	
		else
			$('#provincia').attr('disabled', true);
		if(document.getElementById('comarca').value == 0)
		{
			if(document.getElementById('provincia').value == 0)
				$('#provincia').attr('disabled', false);
		}
    	$('#ciudad').attr('disabled', true);
    	if ($(this).val() > 0)
		{			
			document.getElementById('ciudad').options.length = 1;    		
    		$('#ciudad option:first').text('Loading...');
    		$.ajax(
			{
				url: city_script_url,
				data: "comarca_id="+$(this).val(),
				success: function(html)
				{
			    	$('#ciudad').html(html);
			    	$('#ciudad').attr('disabled', false);
				}
			});    			
    	} 
		else 
		{
			if(document.getElementById('provincia').value != 0)
			{
				$.ajax(
				{
					url: city_script_url,
					data: "provincia_id="+document.getElementById('provincia').value,
					success: function(html)
					{
				    	$('#ciudad').html(html);
				    	$('#ciudad').attr('disabled', false);
					}
				});
			}
			else
			{
	    		document.getElementById('ciudad').options.length = 1;    		
    			$('#ciudad option:first').text('Seleccionar Provincia');
			}
    	}
    }
	if ($(this).attr('id') == 'ciudad')
	 { 
		$('#zona').attr('disabled', true);
    	if ($(this).val())
		{
			document.getElementById('zona').options.length = 1;    		
			if((document.getElementById('ciudad').value == 'Barcelona') || (document.getElementById('ciudad').value == 'Madrid'))
			{
	    		$('#zona option:first').text('Loading...');
    			$.ajax(
				{
					url: zona_script_url,
					data: "ciudad_id="+$(this).val(),
					success: function(html)
					{
				    	$('#zona').html(html);
				    	$('#zona').attr('disabled', false);
					}
				});    			
			}
    	} 
		else 
		{    		
    		document.getElementById('zona').options.length = 1;    		
    		$('#ciudad option:first').text('Seleccionar Barri o Zona');
    	}
    }
     if ($(this).attr('id') == 'operacion')
	 { 
    	if ($(this).val())
		{
	    	$('#maximo option:first').text('Loading...');
    		$.ajax(
			{
				url: maximo_script_url,
				data: "lang=ct&operacion="+$(this).val(),
				success: function(html)
				{
			    	$('#maximo').html(html);
					sucesso = true;
				}
			});    			
    	} 
    }
      
      var request_str = $("form#search").serialize();
	  var dir = "buscador_"+idioma+".php?delahome=1&"+request_str;
      $('div.ajax div.text a').attr('href', "buscador_"+idioma+".php?delahome=1&"+request_str); //set params to results link
      $.ajax({
		  url: search_script_url,
		  data: request_str,
		  success: function(response){
		    $('div.ajax div.text input').val(response);
		}
});
}