
var caricaDove;
var storia;
var caricaCosa;
var caricaDoveTemp;
var caricaCosaTemp;
var caricaDove1;
var caricaCosa1;
var inputName="";
var LocPartenza;
var LocArrivo;
var Lingua;

function caricaMappa(pagina,dove,Arrivo)
{
	LocArrivo=Arrivo;
	if (!pagina=="")
	{

		caricaDove=dove;	
		caricaCosa=pagina;
		if (window.XMLHttpRequest) {
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = ricaricaMappa;
			ricerca_pro.open("GET", pagina,true);
			ricerca_pro.send(null);
			} 
		else if (window.ActiveXObject) {
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
				ricerca_pro.onreadystatechange = ricaricaMappa;
				ricerca_pro.open("GET", pagina,true);
				ricerca_pro.send(null);
			}
	}
	}
}



function ricaricaMappa() {
	var strRes;
	var arrValori;
	if (ricerca_pro.readyState == 4 && ricerca_pro.status==200) {
		
		strRes=ricerca_pro.responseText;
		document.getElementById(caricaDove).innerHTML=strRes;
//alert(LocPartenza);
//alert(LocArrivo);

			initialize();
			setDirections(LocPartenza,LocArrivo,Lingua);

	}
}
var caricaID="";

function ultimiAnnunci(pagina,dove)
{
	if (!pagina=="")
	{
		caricaID=dove;	
		if (window.XMLHttpRequest) {
			ricerca_pro2 = new XMLHttpRequest();
			ricerca_pro2.onreadystatechange = ultimiAnnunciRicarica;
			ricerca_pro2.open("GET", pagina,true);
			ricerca_pro2.send(null);
			} 
		else if (window.ActiveXObject) {
			ricerca_pro2 = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro2) {
				ricerca_pro2.onreadystatechange = ultimiAnnunciRicarica;
				ricerca_pro2.open("GET", pagina,true);
				ricerca_pro2.send(null);
			}
	}
	}
}



function ultimiAnnunciRicarica() {
	
	if (ricerca_pro2.readyState == 4 && ricerca_pro2.status==200) {
		document.getElementById(caricaID).innerHTML=ricerca_pro2.responseText;

	}
}




function ricarica(pagina,dove,Partenza,Arrivo,lingua)
{
	Lingua=lingua;
	LocArrivo=Arrivo;
	LocPartenza=Partenza;
	if (!pagina=="")
	{
		document.getElementById(dove).style.display="";
		caricaDove=dove;	
		caricaCosa=pagina;
		if (window.XMLHttpRequest) {
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = ricaricaPagina;
			ricerca_pro.open("GET", pagina,true);
			ricerca_pro.send(null);
			} 
		else if (window.ActiveXObject) {
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
				ricerca_pro.onreadystatechange = ricaricaPagina;
				ricerca_pro.open("GET", pagina,true);
				ricerca_pro.send(null);
			}
	}
	}
}



function ricaricaPagina() {
	var tappeIntermedie;
	var strRes;
	var arrValori;

	if (ricerca_pro.readyState == 4 && ricerca_pro.status==200) {
		strRes=ricerca_pro.responseText;
		document.getElementById(caricaDove).innerHTML=strRes;
		if (document.getElementById("Annunciopersonale").value=="true")
		{
				Sortable.create("lista",{onUpdate: function(){ricarica("tappeintermedie.asp?azione=OrdinatappaIntermedia&"+ Sortable.serialize("lista") +"&idAnnuncio="+document.getElementById("idAnnuncio").value +"&cittaArrivo="+ document.getElementById("cittaArrivo").value ,"list",document.getElementById("cittaPartenza").value ,document.getElementById("cittaArrivo").value, "Italian")}});
		}
			
			tappeIntermedie=document.getElementById("tappeIntermedie").value;
			//alert(tappeIntermedie);
			caricaMappa("Creamappa.asp","mappa",tappeIntermedie);

	}
}



function carica(pagina,dove,ricorda)
{
	//caricaDoveTemp;
	//caricaCosaTemp;
	storia=ricorda;

	if (!pagina=="")
	{
		caricaDove=dove;	
		caricaCosa=pagina;

		if (window.XMLHttpRequest) {
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = caricaPagina;
			ricerca_pro.open("GET", pagina,true);
			ricerca_pro.send(null);
			} 
			else if (window.ActiveXObject) {
				ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
				ricerca_pro.onreadystatechange = caricaPagina;
				ricerca_pro.open("GET", pagina,true);
				ricerca_pro.send(null);
			}
		}
	}
}

function getSelectionId(text, li) {
	li.getAttribute('provincia');
	li.getAttribute('comune');
	//alert (text.value);
}

function PartenzaAutostop(text, li) {
	Ricerca1.idComunePartenzaAutostop.value=li.getAttribute('comune');
	//Ricerca1.ComunePartenzaAutostop.value="";
	Ricerca1.idProvinciaPartenzaAutostop.value=li.getAttribute('provincia');
	if (Ricerca1.idRegionePartenzaAutostop)
	{
		Ricerca1.idRegionePartenzaAutostop.value=li.getAttribute('regione');
		Ricerca1.idNazionePartenzaAutostop.value=li.getAttribute('Nazione');
	}
}
function ArrivoAutostop(text, li) {
	Ricerca1.idComuneArrivoAutostop.value=li.getAttribute('comune');
	//Ricerca1.ComuneArrivoAutostop.value="";
	Ricerca1.idProvinciaArrivoAutostop.value=li.getAttribute('provincia');
	if (Ricerca1.idRegioneArrivoAutostop)
	{
		Ricerca1.idRegioneArrivoAutostop.value=li.getAttribute('regione');
		Ricerca1.idNazioneArrivoAutostop.value=li.getAttribute('Nazione');
	}
}
function PartenzaPendolari(text, li) {
	Ricerca.idComunePartenzaPendolari.value=li.getAttribute('comune');
	//Ricerca.ComunePartenzaPendolari.value="";
	Ricerca.idProvinciaPartenzaPendolari.value=li.getAttribute('provincia');
	if (Ricerca.idRegionePartenzaPendolari)
	{
		Ricerca.idRegionePartenzaPendolari.value=li.getAttribute('regione');
		Ricerca.idNazionePartenzaPendolari.value=li.getAttribute('Nazione');
	}

}
function ArrivoPendolari(text, li) {
	Ricerca.idComuneArrivoPendolari.value=li.getAttribute('comune');
	//Ricerca.ComuneArrivoPendolari.value="";
	Ricerca.idProvinciaArrivoPendolari.value=li.getAttribute('provincia');
	if (Ricerca.idRegioneArrivoPendolari)
	{
		Ricerca.idRegioneArrivoPendolari.value=li.getAttribute('regione');
		Ricerca.idNazioneArrivoPendolari.value=li.getAttribute('Nazione');
	}
}
function aggiornaTappa(text, li) {
	Ricerca3.idComuneTappa.value=li.getAttribute('comune');
	Ricerca3.idProvinciaTappa.value=li.getAttribute('provincia');
}


function closePopUp()
{	
	document.getElementById("messaggio").style.display="none";
	document.getElementById("sfondoOpaco").style.display="none";
	//	alert (caricaDove1)
	//alert (caricaCosa1)
	if (!caricaDove1=="")
	{
		caricaDove=caricaDove1;
		caricaCosa=caricaCosa1;

		//alert(caricaDove);
		//alert(caricaCosa);
		if (window.XMLHttpRequest) {
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = caricaPagina;
			ricerca_pro.open("GET", caricaCosa,true);
			ricerca_pro.send(null);
			} 
			else if (window.ActiveXObject) {
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
			ricerca_pro.onreadystatechange = caricaPagina;
			ricerca_pro.open("GET", caricaCosa,true);
			ricerca_pro.send(null);
						}
			}

	}	
}

 


//FUNZIONE PER caricare la pagina nell'oggetto con id=caricaDove
var topDistance="";





function caricaPagina() {
	var strRes;
	var arrValori;
//	if (ricerca_pro.readyState == 1) {
//		document.getElementById(caricaDove).innerHTML="Loading...";
//	}

	if (ricerca_pro.readyState == 4) {
		strRes=ricerca_pro.responseText;
		if (document.getElementById(caricaDove).style.display=="none")
		{	topDistance=topDistance-1000;
			document.getElementById(caricaDove).style.display="block";
		}
		
		if (caricaDove=="carica")
		{	

			document.getElementById("anteprimaAnnuncio").innerHTML="";		
		}
		
		if (caricaDove=="messaggio")
		{	

			document.getElementById("sfondoOpaco").style.display="block";
			
			strRes="<div class='chiudiPopUp'><SPAN style='cursor:pointer;' onclick='closePopUp()'>Chiudi X </SPAN></div><div style='padding:10px;'>"+strRes+"</div>";
		}
			document.getElementById(caricaDove).innerHTML=strRes;
	//	tb_init('a.thickbox, area.thickbox, input.thickbox');

		Nifty("div.tabellaRicerca","middle");
		Nifty("div#tabellaRicerca","middle same-height");
		Nifty("div.tabellaSinistra","middle");
		Nifty("div.tabellaDestra","middle");
		Nifty("div#tabellaSinistra","middle same-height");
		Nifty("div#tabellaDestra","middle same-height");
		
		Nifty("div.sottotitolo","bottom same-height");

		if (document.getElementById("PartenzaAutostop"))
		{	
		//	new Ajax.Autocompleter('PartenzaAutostop','autocomplete_ArrivoPendolari2','cercaComuni.asp', {afterUpdateElement : PartenzaAutostop});
		}
		if (document.getElementById("ArrivoAutostop"))
		{	
			//new Ajax.Autocompleter('ArrivoAutostop','autocomplete_ArrivoPendolari2','cercaComuni.asp', {afterUpdateElement : ArrivoAutostop});

		}
		if (document.getElementById("PartenzaPendolari"))
		{	
		//	new Ajax.Autocompleter('PartenzaPendolari','autocomplete_ArrivoPendolari','cercaComuni.asp', {afterUpdateElement : PartenzaPendolari});
		}
		if (document.getElementById("ArrivoPendolari"))
		{	
		//	new Ajax.Autocompleter('ArrivoPendolari','autocomplete_ArrivoPendolari','cercaComuni.asp', {afterUpdateElement : ArrivoPendolari});
		}
		if (document.getElementById("Tappa"))
		{	
		//	new Ajax.Autocompleter('Tappa','idTappa','cercaComuni.asp', {afterUpdateElement : aggiornaTappa});
		}

		var caricaPag=caricaCosa.split("?");
		if ((caricaPag[0]=="mappa.asp") || (caricaPag[0]=="annunciUtenti.asp") || (caricaPag[0]=="annunciPersonali.asp") || (caricaPag[0]=="account.asp"))
		{
			caricaDove1=caricaDove;
			caricaCosa1=caricaCosa;

		}

		if ((caricaPag[0]=="mappa.asp") || (caricaPag[0]=="visualizzaAnnuncio.asp") || (caricaPag[0]=="AnnunciUtenti.asp") || (caricaPag[0]=="AnnunciPersonali.asp") || (caricaPag[0]=="AreaPersonale.asp"))
		{
			ultimiAnnunci("creaCookies.asp?idAnnuncio="+ document.getElementById("idAnnuncio").value ,"UltimiAnnunci");
	
			//alert(document.getElementById("anteprimaAnnuncio").offsetTop);
			//alert(document.getElementById('Pagina').style.height);
			ricarica("tappeintermedie.asp?idUtente="+ document.getElementById("idUtente").value +"&idAnnuncio="+ document.getElementById("idAnnuncio").value +"&cittaArrivo="+document.getElementById("cittaArrivo").value,"list",document.getElementById("cittaPartenza").value, document.getElementById("cittaArrivo").value, 'Italian')
			
		 
			if (document.getElementById("anteprimaAnnuncio"))
			{
				document.body.scrollTop=document.getElementById("anteprimaAnnuncio").offsetTop+100;

			}

		}
		if (caricaPag[0]=="caricaPage.asp")
		{
			var nodo=document.getElementsByTagName("Placemark");
				var testo="";
				if (nodo.length>0)
				{
					
					testo=testo+"<ul style=\"margin-left:30px;\">";
					for (i=0;i< nodo.length; i++)
						{
							var address=nodo[i].getElementsByTagName("address")[0];
							var coord=nodo[i].getElementsByTagName("coordinates")[0];
							var LocalityName=nodo[i].getElementsByTagName("LocalityName")[0];
							var CountryName=nodo[i].getElementsByTagName("CountryName")[0];
							testo=testo+"<li>"+address.innerHTML+" - cittą: "+LocalityName.innerHTML +" - stato: "+CountryName.innerHTML +" - coord: "+coord.innerHTML +"</li>";
						}
					testo=testo+"</ul>";

				}
				document.getElementById(caricaDove).innerHTML=testo;

		}

	}

}
var c_value;
function valoreForm()
{
c_value = ""
var c_name = "";
form=document.myFormlistaContatti.amico;
for (var i=0; i < form.length; i++)
   {
   if (form[i].checked)
      {
	   if (c_value=="")
	   {
			  c_value = form[i].value ;
			  c_name = form[i].getAttribute("nome");
	   }
	   else{
			  c_value =  c_value +  "," + form[i].value;
			  c_name = c_name + ", " +form[i].getAttribute("nome")  ;
		}
	  }
   }
}

	function scegli(stato)
	{
		if (stato=="20")
		{
			document.getElementById("regione").style.display = "";
			document.getElementById("provincia").style.display = "";
			document.getElementById("citta").style.display = "";
			document.getElementById("cittaEstera").style.display = "none";
		}
		else
		{
			document.getElementById("regione").style.display = "none";
			document.getElementById("provincia").style.display = "none";
			document.getElementById("citta").style.display = "none";
			document.getElementById("cittaEstera").style.display = "";
		
		}
	}
	
	
//INIZIO DELLE MODIFICHE

function caricaSession(sessione,valore)
{
		if (window.XMLHttpRequest) {
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = caricaSessionRichiesta;
			ricerca_pro.open("GET", "creaSessione.asp?sessione="+sessione+"&valore="+valore,true);
			ricerca_pro.send(null);
			} 
		else if (window.ActiveXObject) {
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
				ricerca_pro.onreadystatechange = caricaSessionRichiesta;
			ricerca_pro.open("GET", "creaSessione.asp?sessione="+sessione+"&valore="+valore,true);
				ricerca_pro.send(null);
			}
		}
}
function cancellaSession()
{
		if (window.XMLHttpRequest) {
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = caricaSessionRichiesta;
			ricerca_pro.open("GET", "cancellaSessione.asp",true);
			ricerca_pro.send(null);
			} 
		else if (window.ActiveXObject) {
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
				ricerca_pro.onreadystatechange = caricaSessionRichiesta;
				ricerca_pro.open("GET", "cancellaSessione.asp",true);
				ricerca_pro.send(null);
			}
		}
}



function caricaSessionRichiesta() {
	if (ricerca_pro.readyState == 4) {
//alert(ricerca_pro.responseText)
		

	}
}

var doveID="";
var caricareDove="";
function cercaComuneR(session,valore,dove,tipo,tipoAnnuncio)
{
	//alert(tipo+tipoAnnuncio)
	caricareDove=document.getElementById("Localita"+tipo+"1");
	//caricareDove=document.getElementById("ComunePartenza");
	doveID=dove;
if (valore.length>2)
	{	
		
		if (window.XMLHttpRequest) {
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = cercaComuneRichiesta;
			//ricerca_pro.open("GET", "cercaComune.asp?session="+session+"&valore="+valore+"&tipo="+tipo+"&tipoAnnuncio="+tipoAnnuncio,true);
			ricerca_pro.open("GET", "searchComune.asp?session="+session+"&valore="+valore+"&tipo="+tipo+"&tipoAnnuncio="+tipoAnnuncio,true);
			ricerca_pro.send(null);
			} 
		else if (window.ActiveXObject) {
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro) {
				ricerca_pro.onreadystatechange = cercaComuneRichiesta;
			//ricerca_pro.open("GET", "cercaComune.asp?session="+session+"&valore="+valore+"&tipo="+tipo+"&tipoAnnuncio="+tipoAnnuncio,true);
			ricerca_pro.open("GET", "searchComune.asp?session="+session+"&valore="+valore+"&tipo="+tipo+"&tipoAnnuncio="+tipoAnnuncio,true);
				ricerca_pro.send(null);
			}
		}
	}
}

var valorePlacemark="";

function leggiXML(nodo)
{	

valorePlacemark="";

	x=nodo.childNodes;
	for (i=0;i<x.length;i++)
	{
	if (x[i].innerHTML)
		{
			valorePlacemark=valorePlacemark+"<"+x[i].nodeName+">"+x[i].innerHTML+"</"+x[i].nodeName+">";
		}	
	}
}

function cercaComuneRichiesta() {
	if (ricerca_pro.readyState == 2) {
		if (document.getElementById(doveID).style.display=="none")
			document.getElementById(doveID).style.display="";
		{

			var coords = {x: 0, y: 0}; 
			while (caricareDove) { 
				
				coords.x  += caricareDove.offsetLeft;
				coords.y  += caricareDove.offsetTop; 
				caricareDove = caricareDove.offsetParent; 
			}
			
		
			divLeft=coords.x;
			divTop=coords.y+20;
				document.getElementById(doveID).style.top=divTop+"px";
			document.getElementById(doveID).style.left=divLeft+"px";

		}
			document.getElementById(doveID).innerHTML="Caricamento in corso...";
	}
	
	if (ricerca_pro.readyState == 4 && ricerca_pro.status==200) {
			strRes=ricerca_pro.responseText;
			//alert(strRes)
			document.getElementById(doveID).innerHTML=strRes;
			document.getElementById(doveID).focus();
/*alert(strRes)

			var nodo=document.getElementsByTagName("Placemark");
				var testo="";
				
				if (nodo.length>0)
				{
					testo=testo+"<div style=\"width:100%;background: #595959;color:white;\">Select the locality:</div>";
					testo=testo+"<ul style=\"overflow:auto;height:100px;\">";
					for (i=0;i< nodo.length; i++)
						{
							
							var address=nodo[i].getElementsByTagName("address")[0];
							var coord=nodo[i].getElementsByTagName("coordinates")[0];
							var LocalityName=nodo[i].getElementsByTagName("LocalityName")[0];
							var CountryName=nodo[i].getElementsByTagName("CountryName")[0];
							var CountryNameCode=nodo[i].getElementsByTagName("CountryNameCode")[0];
							var Placemark=nodo[i].nodeValue;
							leggiXML(nodo[i]);
							var tipoA=document.getElementById("tipoA").value;
							var tipoAnnuncio=document.getElementById("tipoAnnuncio").value;
							//alert(coord);
							var valori="address="+address.innerHTML+"&coord="+coord.innerHTML+"&LocalityName="+LocalityName.innerHTML+"&CountryName="+CountryName.innerHTML+"&CountryNameCode="+CountryNameCode.innerHTML
							var coordinate=coord.innerHTML;
							testo=testo+"<li onmouseover=\"this.style.color='#6A0000';this.style.background='#FFFF66';\" onmouseout=\"this.style.color='#000000';this.style.background='#FFFFFF';\" onclick=\"controllaComune('"+tipoA+"','"+tipoAnnuncio+"','"+escape(valorePlacemark)+"','"+escape(address.innerHTML)+"','"+escape(LocalityName.innerHTML)+"','"+coordinate+"');\">"+address.innerHTML +"</li>";
						}
					testo=testo+"</ul>";

				}
				document.getElementById(doveID).innerHTML=testo;


			//document.getElementById(doveID).innerHTML=strRes;
			document.getElementById(doveID).focus();
	*/}
}



function controllaComune(tipo,tipoAnnuncio,profondita,AcronimoNazione,NomeNazione,Localita,Indirizzo,CAP,longitudine,latitudine) {

			if (document.getElementById(tipo+"Ok")){
				document.getElementById(tipo+"Ok").value="ok";
			}
			if (document.getElementById("profondita"+tipo)){
				document.getElementById("profondita"+tipo).value=profondita;
			}
			if (document.getElementById("Latitudine"+tipo)){
				document.getElementById("Latitudine"+tipo).value=latitudine;
			}
			if (document.getElementById("Longitudine"+tipo)){
				document.getElementById("Longitudine"+tipo).value=longitudine;
			}
			if (document.getElementById("Localita"+tipo+"1")){
				document.getElementById("Localita"+tipo+"1").value=Localita;
			}	
			if (document.getElementById("Localita"+tipo)){
				document.getElementById("Localita"+tipo).value=Localita;
			}	
			if (document.getElementById("AcronimoNazione"+tipo)){
				document.getElementById("AcronimoNazione"+tipo).value=AcronimoNazione;
			}
			if (document.getElementById("NomeNazione"+tipo)){
				document.getElementById("NomeNazione"+tipo).value=NomeNazione;
			}
			if (document.getElementById("Indirizzo"+tipo)){
				document.getElementById("Indirizzo"+tipo).value=Indirizzo;
			}
			if (document.getElementById("CAP"+tipo)){
				document.getElementById("CAP"+tipo).value=CAP;
			}
			if (document.getElementById(doveID)){
				document.getElementById(doveID).style.display="none";
			}

	inputName="";
}


function InputFocus()
{
	if (inputName!="")
	{	
		document.getElementById(inputName).focus();
	}
	
}


function controllaSessioneUtente()
{
		if (window.XMLHttpRequest) {
			ricerca_pro3 = new XMLHttpRequest();
			ricerca_pro3.onreadystatechange = controllaSessioneUtenteRicarica;
			ricerca_pro3.open("GET", "controllaSessioneUtente.asp",true);
			ricerca_pro3.send(null);
			} 
		else if (window.ActiveXObject) {
			ricerca_pro3 = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro3) {
				ricerca_pro3.onreadystatechange = controllaSessioneUtenteRicarica;
			ricerca_pro3.open("GET", "controllaSessioneUtente.asp",true);
				ricerca_pro3.send(null);
			}
	}
}



function controllaSessioneUtenteRicarica() {
	var strRes;
	var arrValori;
		if (ricerca_pro3.readyState == 4 && ricerca_pro3.status==200) {
		strRes=ricerca_pro3.responseText;
		document.getElementById("controllaSession").innerHTML=strRes;
		if (document.getElementById("sessioneUtente").value=="false")
		{
			window.location.reload();
		}		
		
	}
}

