var myRequest = null;	
var myRequest2 = null;
var myRequest3 = null;
var myRequest4 = null;

function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  try {
	xmlhttp = new XMLHttpRequest();
  } catch(e) {
  try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
 }
  xmlhttp.onreadystatechange = handler;
  return xmlhttp;
}
	
function myHandlerInfo() {	
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		document.getElementById("richiesta_info").innerHTML = myRequest.responseText;
		document.getElementById("frm_info").reset();
	}
}

function myHandlerSegnal() {	
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		document.getElementById("richiesta_segnal").innerHTML = myRequest.responseText;
		document.getElementById("frm_segnal").reset();
	}
}

function invia_info(idazienda) {
	var richiesta = 'info';
	var to = '';
	
	var nome = document.getElementById('nome_info').value;
	var telefono = document.getElementById('telefono_info').value;
	var posta = document.getElementById('posta_info').value;
	var privacy = document.getElementById('privacy_info').value;
	
	var idofferta = null ;
	if ( document.getElementById('offerta_info') ) 
		idofferta = document.getElementById('offerta_info').value;
	
	var tipo = null ;
	if ( document.getElementById('tipo_info') ) 
		tipo = document.getElementById('tipo_info').value;
	
	var note = document.getElementById('note_info').value;
	
	myRequest = CreateXmlHttpReq(myHandlerInfo);
	myRequest.open("GET","/infoaziende/html/dformed.php?nome="+nome+"&telefono="+telefono+"&posta="+posta+"&idofferta="+idofferta+
						"&privacy="+privacy+"&note="+note+"&to="+to+"&richiesta="+richiesta+"&idazienda="+idazienda+"&tipo="+tipo);
	
	myRequest.send(null);
}

function invia_segnal(idazienda) {	
	var richiesta = 'segnalazione';
	var to = '';
	
	var nome = document.getElementById('nome_segnal').value;
	var telefono = document.getElementById('telefono_segnal').value;
	var posta = document.getElementById('posta_segnal').value;
	var privacy = document.getElementById('privacy_segnal').value;
	var note = document.getElementById('note_segnal').value;
	myRequest = CreateXmlHttpReq(myHandlerSegnal);
	myRequest.open("GET","/infoaziende/html/dformed.php?nome="+nome+"&telefono="+telefono+"&posta="+posta+
						"&privacy="+privacy+"&note="+note+"&to="+to+"&richiesta="+richiesta+"&idazienda="+idazienda);
	myRequest.send(null);	
}


function avvisoprivacy() { 	   	
   	var finestra = window.open("","Privacy","height=230,width=450");
   	
    finestra.document.write("<h2>Informativa sulla Privacy.</h2>");
    finestra.document.write("La informiamo che i dati da lei inseriti saranno trattati al solo fine di fornirle le informazioni richieste. I suoi dati saranno trattati con mezzi informatici nel rispetto dei principi stabiliti dal codice della Privacy (D. Legisl. 196/2003). Per ulteriori informazioni sulle modalitā del trattamento, e per esercitare gli altri diritti a  lei riconosciuti dall'articolo 7 del Codice della Privacy, potraā rivolgersi al Titolare dell'agenzia immobiliare.");
	finestra.document.close();
}



	
	
function myHandler() {
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		document.getElementById("regioni_SP").innerHTML = myRequest.responseText;
	}
}
function myHandler_prov() {
	if (myRequest2.readyState == 4 && myRequest2.status == 200) {
		document.getElementById("province_SP").innerHTML = myRequest2.responseText;
	}
}
function myHandler_com() {
	if (myRequest3.readyState == 4 && myRequest3.status == 200) {
		document.getElementById("comuni_SP").innerHTML = myRequest3.responseText;
	}
}
function myHandler_fraz() {
	if (myRequest4.readyState == 4 && myRequest4.status == 200) {			
		document.getElementById("frazioni_SP").innerHTML = myRequest4.responseText;
	}
}
		
function populate_regioni(pag) {
	var r = Math.random();
	myRequest = CreateXmlHttpReq(myHandler);
	myRequest.open("GET",pag+"?rand="+escape(r));		
	myRequest.send(null);
}
		
function populate_province() {	
	myRequest2 = CreateXmlHttpReq(myHandler_prov);
	if (document.getElementById('cod_regione')) {
		var i = document.getElementById('cod_regione').selectedIndex;
		var cod_regione = document.getElementById('cod_regione').options[i].value;
	}
	else var cod_regione = "";		
	myRequest2.open("GET","province.php?cod_regione="+cod_regione);
	myRequest2.send(null);
	document.getElementById('cod_regione_h').value = cod_regione;		
}
	
function populate_comuni() {
	myRequest3 = CreateXmlHttpReq(myHandler_com);	
	var i = document.getElementById('cod_provincia').selectedIndex;	
	myRequest3.open("GET","comuni.php?cod_provincia="+document.getElementById('cod_provincia').options[i].value);
	myRequest3.send(null);
	document.getElementById('cod_provincia_h').value = document.getElementById('cod_provincia').options[i].value;
}
	
function populate_frazioni() {		
	myRequest4 = CreateXmlHttpReq(myHandler_fraz);	
	var i = document.getElementById('cod_comune').selectedIndex;					
	myRequest4.open("GET","frazioni.php?cod_comune="+document.getElementById('cod_comune').options[i].value);
	myRequest4.send(null);		
	document.getElementById('cod_comune_h').value = document.getElementById('cod_comune').options[i].value;
}

function clearSelect(tipo) {
	switch(tipo){
		case 'regione':			   
				document.getElementById('cod_comune').disabled = true;
				document.getElementById('cod_comune_h').value = "";
				document.getElementById("comuni_SP").innerHTML = "<select id=\"cod_comune\" name=\"cod_comune\" disabled></select>";
				
				document.getElementById('cod_frazione').disabled = true;
				document.getElementById('cod_frazione_h').value = "";
				document.getElementById("frazioni_SP").innerHTML = "<select id=\"cod_frazione\" name=\"cod_frazione\" disabled></select>";
			break;
		case 'provincia':
				document.getElementById('cod_frazione').disabled = true;
				document.getElementById('cod_frazione_h').value = "";
				document.getElementById("frazioni_SP").innerHTML = "<select id=\"cod_frazione\" name=\"cod_frazione\" disabled></select>";
			break;
		default: break;
	}
}


function salva_comuni() {	
	var i = document.getElementById('cod_comune').selectedIndex;
	document.getElementById('cod_comune_h').value = document.getElementById('cod_comune').options[i].value;
}
	
function salva_frazioni() {	
	var i = document.getElementById('cod_frazione').selectedIndex;
	document.getElementById('cod_frazione_h').value = document.getElementById('cod_frazione').options[i].value;
}

function selFasciaPrezzo(fascia,extfrm){
	var stroption="<select name='prezzo' style='width:150px'>";
	if (fascia == "affitto"){
		stroption += "<option value=\"0\" selected > - Qualsiasi - </option>";
		stroption += "<option value=\"6\">da 0 a 500</option>";
		stroption += "<option value=\"7\">da 500 a 800</option>";
		stroption += "<option value=\"8\">da 800 a 1000</option>";
		stroption += "<option value=\"9\">da 1000 a 2000</option>";
		stroption += "<option value=\"10\">oltre 2000</option>";
	}
	else {
		stroption += "<option value=\"0\" selected > - Qualsiasi - </option>";
		stroption += "<option value=\"1\">da 0 a 100000</option>";
		stroption += "<option value=\"2\">da 100000 a 200000</option>";
		stroption += "<option value=\"3\">da 200000 a 300000</option>";
		stroption += "<option value=\"4\">da 300000 a 500000</option>";
		stroption += "<option value=\"5\">oltre 500000</option>";
	}
	stroption += "</select>";
	var obj = document.getElementById('idprezzo'+extfrm);
	obj.innerHTML = stroption;
}
	
function checkSecurityCode(security_code, lang){
	var inserted_number = document.getElementById("security_code").value;
	var msg = "Impossibile inviare la richiesta!\nIl codice di sicurezza inserito non č corretto."
	if(lang != null && lang == "eng") msg = "Unable to send request!Uncorrect security code!";
		 
	if(security_code != inserted_number ) {
		alert(msg);											
		return false;	
	}
	else return true;
	
}
	
function cambia_prezzi(tipo, lang) {
	if(lang != null && lang == "eng"){
		var opzioni_vendite = new Array({text:" - Any - ", value:'0'}, {text:"from 0 to 100000", value:'1'},{text:"from 100000 to 200000",value:'2'},{text:"from 200000 to 300000",value:'3'},{text:"from 300000 to 500000",value:'4'},{text:"up 500000",value:'5'});
		var opzioni_affitti = new Array({text:" - Any - ", value:'0'},{text:"from 0 to 500", value:'1'},{text:"from 500 to 800",value:'2'},{text:"from 800 to 1000",value:'3'},{text:"from 1000 to 2000",value:'4'},{text:"up 2000",value:'5'});
	}
	else {
		var opzioni_vendite = new Array({text:" - Qualsiasi - ", value:'0'}, {text:"da 0 a 100000", value:'1'},{text:"da 100000 a 200000",value:'2'},{text:"da 200000 a 300000",value:'3'},{text:"da 300000 a 500000",value:'4'},{text:"oltre 500000",value:'5'});
		var opzioni_affitti = new Array({text:" - Qualsiasi - ", value:'0'}, {text:"da 0 a 500", value:'1'},{text:"da 500 a 800",value:'2'},{text:"da 800 a 1000",value:'3'},{text:"da 1000 a 2000",value:'4'},{text:"oltre 2000",value:'5'});
	}
	switch(tipo){
		case '1':			   
			var opt = document.ricerca_immobili.prezzo.options;
			for(var i=0;i<opzioni_vendite.length;i++){
				opt[i].value = opzioni_vendite[i]['value'];
				opt[i].text = opzioni_vendite[i]['text'];
			}				
			break;
		case '2':	
			var opt = document.ricerca_immobili.prezzo.options;
			for(var i=0;i<opzioni_affitti.length;i++){
				opt[i].value = opzioni_affitti[i]['value'];
				opt[i].text = opzioni_affitti[i]['text'];
			}				
			 break;
		default: break;
	}
}
