var http = false;

if(navigator.appName == "Microsoft Internet Explorer"){
	http = new ActiveXObject("Microsoft.XMLHTTP");
}else{
	http = new XMLHttpRequest();
}

function validate(filtro_ricerca,tipo,cambio,ricerca_lang){

    Loading(1);
    
    setTimeout("Search('"+filtro_ricerca+"','"+tipo+"','"+cambio+"','"+ricerca_lang+"')",500);
    
}

function Search(filtro_ricerca,tipo,cambio,ricerca_lang){

    http.abort();
	http.open("GET", "search_object.php?ricerca_lang="+ricerca_lang+"&ajax_call_tipo="+tipo+"&filtro_ricerca=" + filtro_ricerca, true);
	http.onreadystatechange=function(){
		if(http.readyState == 4){
			/* Prima di ricaricare controllo la presenza dell'oggetto */
			if(document.getElementById(cambio)){
				document.getElementById(cambio).innerHTML = http.responseText;
				if(cambio == 'span_tipo' && tipo == 'cat'){
					cambio = 'span_subtipo';
					ForceValidate('','tipo',cambio);
				}else{
					Loading(0);            
				}
			}else{
				Loading(0); 
			}
          
		}
	}
	http.send(null);
	
}

function Loading(Load){

    if(Load == 1){
        document.getElementById('loader').innerHTML = 'Loading...';
    }else{
        document.getElementById('loader').innerHTML = '';
    }
    
}

function ForceValidate(filtro_ricerca,tipo,cambio,ricerca_lang){
    Search(filtro_ricerca,tipo,cambio,ricerca_lang);
}

function SwitchPrezzo(Modo){
	/* svuoto le option */
	var Obj = document.forms['frmRicerca'].filtro_prezzo;
	for(var x=1; x<Obj.length; x++){
		for(p=Obj.length; p>0; p--){
			Obj.options[1] = null;
		}
	}	
	/* popolo le option */
	switch (Modo){
	case "0": 
         Obj.options[1] = new Option("fino a 150mila €", "0|150000");
         Obj.options[2] = new Option("da 150mila a 200mila €", "150000|200000");
         Obj.options[3] = new Option("da 200mila a 250mila €", "200000|250000");
         Obj.options[4] = new Option("da 250mila a 300mila €", "250000|300000");		
         Obj.options[5] = new Option("oltre i 300mila €", "300000|9999999999");			
	break;
	case "1": 
         Obj.options[1] = new Option("fino a 400 €", "0|400");
         Obj.options[2] = new Option("da 400 a 600 €", "400|600");
         Obj.options[3] = new Option("da 600 a 1000 €", "600|1000");
         Obj.options[4] = new Option("da 1000 a 1500 €", "1000|1500");		
         Obj.options[5] = new Option("oltre i 1500 €", "1500|9999999999");			
	break;
	
	default: break;
	}
}

function AzzeraParametri(){
	document.forms['frmRicerca'].contratto[0].checked = true;
	document.forms['frmRicerca'].contratto[1].checked = false;
	document.forms['frmRicerca'].cat.selectedIndex = 0;
	document.forms['frmRicerca'].tipo.selectedIndex = 0;
	document.forms['frmRicerca'].comune.selectedIndex = 0;
	SwitchPrezzo('0');
}
