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 myHandlerDisp() {		
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		document.getElementById("richiesta_disp").innerHTML = myRequest.responseText;
		document.getElementById("frm_disp").reset();
		//alert("Rischiesta inviata correttamente");
	}
}
	
function myHandlerInfo() {	
	if (myRequest.readyState == 4 && myRequest.status == 200) {
		document.getElementById("richiesta_info").innerHTML = myRequest.responseText;
		document.getElementById("frm_info").reset();
		//alert("Rischiesta inviata correttamente");
	}
}

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

	
function invia_disp(idazienda,type) {
	var richiesta;
	var to = '';
	
	switch (type) {
		case 'alloggio' :
			richiesta = 'disp_alloggio';
			var ga = document.getElementById('ga').options[document.getElementById('ga').selectedIndex].value;
			var ma = document.getElementById('ma').options[document.getElementById('ma').selectedIndex].value;
			var aa = document.getElementById('aa').options[document.getElementById('aa').selectedIndex].value;
			var gp = document.getElementById('gp').options[document.getElementById('gp').selectedIndex].value;
			var mp = document.getElementById('mp').options[document.getElementById('mp').selectedIndex].value;
			var ap = document.getElementById('ap').options[document.getElementById('ap').selectedIndex].value;
			var adulti = document.getElementById('adulti').value;
			var bambini = document.getElementById('bambini').value;
			var singole = document.getElementById('singole').value;
			var doppie = document.getElementById('doppie').value;
			var triple = document.getElementById('triple').value;
			var quadruple = document.getElementById('quadruple').value;
			var trattamento = document.getElementById('trattamento').options[document.getElementById('trattamento').selectedIndex].value;
			var nome = document.getElementById('nome').value;
			var telefono = document.getElementById('telefono').value;
			var posta = document.getElementById('posta').value;
			var privacy = document.getElementById('privacy').value;
			var note = document.getElementById('note').value;
			myRequest = CreateXmlHttpReq(myHandlerDisp);
			myRequest.open("GET","/infoaziende/html/dformed.php?ga="+ga+"&ma="+ma+"&aa="+aa+"&gp="+gp+"&mp="+mp+"&ap="+ap+
							"&adulti="+adulti+"&bambini="+bambini+"&singole="+singole+"&doppie="+doppie+"&triple="+triple+"&quadruple="+quadruple+
							"&trattamento="+trattamento+"&nome="+nome+"&telefono="+telefono+"&posta="+posta+
							"&privacy="+privacy+"&note="+note+"&to="+to+"&richiesta="+richiesta+"&idazienda="+idazienda);
			myRequest.send(null);
			break
			
		case 'locale':
			richiesta = 'disp_locale';
			var ga = document.getElementById('ga').options[document.getElementById('ga').selectedIndex].value;
			var ma = document.getElementById('ma').options[document.getElementById('ma').selectedIndex].value;
			var aa = document.getElementById('aa').options[document.getElementById('aa').selectedIndex].value;
			var orario = document.getElementById('orario').value;
			var posti = document.getElementById('posti').value;
			var nome = document.getElementById('nome').value;
			var telefono = document.getElementById('telefono').value;
			var posta = document.getElementById('posta').value;
			var privacy = document.getElementById('privacy').value;
			var note = document.getElementById('note').value;
			myRequest = CreateXmlHttpReq(myHandlerDisp);
			myRequest.open("GET","/infoaziende/html/dformed.php?ga="+ga+"&ma="+ma+"&aa="+aa+"&orario="+orario+"&posti="+posti+
							"&nome="+nome+"&telefono="+telefono+"&posta="+posta+
							"&privacy="+privacy+"&note="+note+"&to="+to+"&richiesta="+richiesta+"&idazienda="+idazienda);
			myRequest.send(null);
			break
		
		case 'stbalneare' :
			richiesta = 'disp_stbalneare';
			var ga = document.getElementById('ga').options[document.getElementById('ga').selectedIndex].value;
			var ma = document.getElementById('ma').options[document.getElementById('ma').selectedIndex].value;
			var aa = document.getElementById('aa').options[document.getElementById('aa').selectedIndex].value;
			var gp = document.getElementById('gp').options[document.getElementById('gp').selectedIndex].value;
			var mp = document.getElementById('mp').options[document.getElementById('mp').selectedIndex].value;
			var ap = document.getElementById('ap').options[document.getElementById('ap').selectedIndex].value;
			var adulti = document.getElementById('adulti').value;
			var bambini = document.getElementById('bambini').value;
			var ombrelloni = document.getElementById('ombrelloni').value;
			var tende = document.getElementById('tende').value;
			var sdraio = document.getElementById('sdraio').value;
			var lettini = document.getElementById('lettini').value;
			var nome = document.getElementById('nome').value;
			var telefono = document.getElementById('telefono').value;
			var posta = document.getElementById('posta').value;
			var privacy = document.getElementById('privacy').value;
			var note = document.getElementById('note').value;
			myRequest = CreateXmlHttpReq(myHandlerDisp);
			myRequest.open("GET","/infoaziende/html/dformed.php?ga="+ga+"&ma="+ma+"&aa="+aa+"&gp="+gp+"&mp="+mp+"&ap="+ap+
							"&adulti="+adulti+"&bambini="+bambini+"&ombrelloni="+ombrelloni+"&tende="+tende+"&sdraio="+sdraio+"&lettini="+lettini+
							"&nome="+nome+"&telefono="+telefono+"&posta="+posta+
							"&privacy="+privacy+"&note="+note+"&to="+to+"&richiesta="+richiesta+"&idazienda="+idazienda);
			myRequest.send(null);
			break
		
		default:
			return false;
	}
}
	
	
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 ed eventualmente. 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.");
	finestra.document.close();
}



	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 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 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;			
		if(security_code != inserted_number ){
			if (lang == null)
			     alert("Impossibile inviare la richiesta!\nIl codice di sicurezza inserito non č corretto.");	
			else if (lang == 'eng')	 
			     alert("Unable to send request!\nUncorrect security code");
	     	return false;	
		}
		else
		    return true;
		
	}
	
	
	








