
function addbookmark(lingua){
     bookmarkurl="http://www.toscanatour.eu";
     
     if(lingua=='ita') {

          bookmarktitle="toscanatour.eu - Il portale della toscana";
     } else if(lingua=='eng') {

          bookmarktitle="toscanatour.eu - Travel in Tuscany";
     }
     

     if(document.all) {
          window.external.AddFavorite(bookmarkurl, bookmarktitle);
     } else if(window.sidebar) {
          window.sidebar.addPanel(bookmarktitle, bookmarkurl, "");
     }


}



function ric_lingua(lingua) {
	mio_link = document.location.pathname;
	 passa = document.location.search;
	 parametri="";
	 if(passa.indexOf("lang=ita")==true) {

		a = passa.split("lang=ita");
		parametri = a[0]+a[1];
	 }
	 if(passa.indexOf("lang=eng")==true) {

		a = passa.split("lang=eng");
		parametri = a[0]+a[1];
	 }
	 if(passa.indexOf("lang")!=true) {
		parametri=passa;
	 }

	 if(parametri!="") {
		 lun = parametri.length;
		 stringa="&";
		 for(i=0; i<lun; i++){

			 if(i>0) {
				 stringa=stringa+parametri.charAt(i);
			 }
		 }
	 } else {
		 stringa="";
	 }
	mio_link = mio_link+"?lang="+lingua+stringa;
	document.location.replace(mio_link);
}


function adatta_personal() {

	if(document.getElementById('middle_pers').style.height=="") {
		centro = document.getElementById('middle_pers').offsetHeight;
	} else {
		centro = document.getElementById('middle_pers').style.height;
	}
	if(document.getElementById('right_pers').style.height=="") {
		destra=document.getElementById('right_pers').offsetHeight;
	} else {
		destra=document.getElementById('right_pers').style.height;
	}

	if(document.getElementById('left_pers').style.height=="") {
		sinistra=document.getElementById('left_pers').offsetHeight;
	} else {
		sinistra=document.getElementById('left_pers').style.height;
	}


	//alert('centro='+centro);

	centro = parseInt(centro);
	//alert("centro=" + centro);
	destra = parseInt(destra);
	//alert("destra="+destra);
	sinistra = parseInt(sinistra);
	//alert("sinistra="+sinistra);


	if(centro>destra && centro>sinistra) {

	    //alert('il centro vince');
		document.getElementById('left_pers').style.height=centro+'px';
		document.getElementById('right_pers').style.height=centro+'px';
		destra = document.getElementById('right_pers').style.height;
		//alert(destra);

	}

	if(destra>centro && destra>sinistra) {
		document.getElementById('left_pers').style.height=destra+'px';
		document.getElementById('middle_pers').style.height=destra+'px';
	}

	if(sinistra>centro && sinistra>destra) {
		document.getElementById('right_pers').style.height=sinistra+'px';
		document.getElementById('middle_pers').style.height=sinistra+'px';
	}

}


function adatta_stringa(a) {
	a=a.replace("à ", "a'");
	a=a.replace("è", "e'");
	a=a.replace("é", "e'");
	a=a.replace("ì", "i'");
	a=a.replace("ò", "o'");
	a=a.replace("ù", "u'");
	a=a.replace("\"", "'");
	return a;
}


function acce_citta(n) {


	if(n==1) {
	    document.getElementById('cella1').style.backgroundColor="#D5F3F4";
	    document.getElementById('cella2').style.backgroundColor="";
	    document.getElementById('cella3').style.backgroundColor="";
	    document.getElementById('cella4').style.backgroundColor="";

	//scatola.innerHTML=tab_descrizione;


	}

	if(n==2) {
	    document.getElementById('cella1').style.backgroundColor="";
	    document.getElementById('cella2').style.backgroundColor="#D5F3F4";
	    document.getElementById('cella3').style.backgroundColor="";
	    document.getElementById('cella4').style.backgroundColor="";

	//scatola.innerHTML=tab_storia;


	}

	if(n==3) {
	    document.getElementById('cella1').style.backgroundColor="";
	    document.getElementById('cella2').style.backgroundColor="";
	    document.getElementById('cella3').style.backgroundColor="#D5F3F4";
	    document.getElementById('cella4').style.backgroundColor="";
	//scatola.innerHTML=tab_monumenti;
	}

	if(n==4) {
	    document.getElementById('cella1').style.backgroundColor="";
	    document.getElementById('cella2').style.backgroundColor="";
	    document.getElementById('cella3').style.backgroundColor="";
	    document.getElementById('cella4').style.backgroundColor="#D5F3F4";
	//scatola.innerHTML=tab_eventi;
	}


}

function apri_foto(tipo, codice) {
	a = "foto.php?"+tipo+"="+codice;
	window.open(a, 'fin_foto', 'width=720, height=550, top=10, left=10, resizable=no, menubar=no, toolbar=no, location=no, scrollbars=no, 	status=no');
}

function reg_utente() {
	mio_form = document.registrazione;
	errori=0;
	sbagli = new Array();


	// Nome
	nome = mio_form.nome.value;
	if(nome != "") {
		lun_nome = mio_form.nome.value.length;
		count = 0;
		for(i=0; i<lun_nome; i++) {
			lettera = nome.charAt(i);
			if(!isNaN(lettera)) { if(lettera != " ") { count++; } }
		}
		if(count > 0) {

		sbagli[1] = "Nome";
		errori++;
		}
	}
	else { alert("Attenzione: il campo nome e' obbligatorio !");
	errori++;

	}

	//Cognome
	cognome = mio_form.cognome.value;
	if(cognome != "") {
		lun_cognome = mio_form.cognome.value.length;
		count = 0;
		for(i=0; i<lun_cognome; i++) {
			lettera = cognome.charAt(i);
			if(!isNaN(lettera)) { if(lettera != " ") { count++; } }
		}
		if(count > 0) {

			sbagli[2] = "Cognome";
			errori++;
		}
	} else {
		alert("Attenzione: il campo cognome e' obbligatorio !");
        errori++;

	}

	//Provincia
	if(mio_form.provincia.value==0) {
		alert("Attenzione selezionare una provincia");
		errori++;
	}

	//CittÃ 
	citta = mio_form.citta.value;
	if(citta != "") {
		lun_citta = mio_form.citta.value.length;
		count = 0;
		for(i=0; i<lun_citta; i++) {
			lettera = citta.charAt(i);
			if(!isNaN(lettera) && lettera != " ") { count++; }
		}
		if(count > 0) {

			sbagli[3] = "Citta' ";
			errori = errori + 1;
		}

	}
	else { alert("Attenzione: il campo citta'  e' obbligatorio !");
	errori = errori + 1;

	}

	//Via
	if(mio_form.via.value=="") {
		alert("Indicare la Via");
		errori++;
	}

	//Cap
	cap = mio_form.cap.value;
	if(cap != "") {
		lun_cap = mio_form.cap.value.length;
		count = 0;
		for(i=0; i<lun_cap; i++) {
			lettera = cap.charAt(i);
			if(isNaN(lettera)) { count++; }
		}
		if(count > 0 || lun_cap != 5) {
			errori = errori + 1;

			sbagli[4] = "Cap";

		}
	} else {
		alert("Indicare il CAP");
		errori++;
	}

	//Telefono
	telefono = mio_form.tel.value;
	if(telefono != "") {
		lun_telefono = telefono.length;
		count = 0;
		for(i=0; i<lun_telefono; i++) {
			lettera = telefono.charAt(i);
			if(isNaN(lettera)) { count++; }
		}
		if(count > 0) {

			sbagli[5] = "Numero telefonico";
			errori = errori + 1;
		}
	}

	//Fax
	telefono = mio_form.fax.value;
	if(telefono != "") {
		lun_telefono = telefono.length;
		count = 0;
		for(i=0; i<lun_telefono; i++) {
			lettera = telefono.charAt(i);
			if(isNaN(lettera)) { count++; }
		}
		if(count > 0) {

			sbagli[6] = "Numero telefonico";
			errori = errori + 1;
		}
	}

	//Cellulare
	telefono = mio_form.cel.value;
	if(telefono != "") {
		lun_telefono = telefono.length;
		count = 0;
		for(i=0; i<lun_telefono; i++) {
			lettera = telefono.charAt(i);
			if(isNaN(lettera)) { count++; }
		}
		if(count > 0) {

			sbagli[7] = "Numero telefonico";
			errori = errori + 1;
		}
	}

	//Email
	mail = mio_form.mail.value;
	if(mail != "") {
		lun_mail = mail.length;
		chiocciole = 0;
		punti = 0;
		for(i=0; i<lun_mail; i++) {
			lettera = mail.charAt(i);
			if(lettera == "@") { chiocciole = chiocciole + 1; }
			if(lettera == ".") { punti = punti + 1;  }
		}

		if(punti < 1 || chiocciole != 1) {
			errori = errori + 1;

			sbagli[8] = "Email";
		}

	} else {
		errori++;   alert("Attenzione, il campo mail e' obbligatorio !");

	}

	if(mio_form.password.value == "") {
		alert("Il campo password e' obbligatorio");
		errori++;
	}

	if(mio_form.password.value != mio_form.rip_pass.value) {
		alert("Il campo password e ripeti password \n non corrispondono");
		errori++;
	}

	if(mio_form.assenso.checked==false) {
		errori++;
		alert('Prestare consenso al trattamento dei dati personali');
	}

	if(errori > 0) {
		if(sbagli.length != null && sbagli.length != 0) {
			stringa = "Ci sono errori nei seguenti campi: ";

			var numero_sbagli = sbagli.length;

			for(i=1; i<9;i++) {
				if(sbagli[i]) {
					if(i != (numero_sbagli-1)) {
					stringa = stringa + sbagli[i] + ", ";
					}
					else {
						stringa = stringa + sbagli[i] + ".";
					}
				}

			}
			alert(stringa);
		}
	}

	else {
		new Ajax.Request('/ajax/contr_email.php', {
						 method: 'get',
						 parameters: {mail: mio_form.mail.value },
						 onComplete: function(transport){
							 resp = transport.responseText || "no response text";
							 if(resp==1) {
								alert("Esiste gia'  un utente registrato con questa casella email. \n\r Non e' possibile effettuare un'altra registrazione con la stessa email");
							 }
							 if(resp==0) {
								invia_form('reg');
							 }

						 }
					});
	}

}

function invia_form(mio_form) {
	switch(mio_form) {
		case "reg":
			document.registrazione.submit();
			break;

	}
}

function citta_provincia(prov) {
	mio_link=document.location.pathname;

	mio_link = mio_link+"?provincia="+prov;
	document.location.replace(mio_link);

}

function login() {
	mio_form=document.log_utente;
	if(mio_form.log_email.value=="" || mio_form.log_password.value=="") {
		alert('Riempire entrambi i campi');
	} else {
		mio_form.submit();
	}

}


function contatta_inserz(tipo, cod) {
	apro="/contatta_inserzionista.php?tipo="+tipo+"&codice="+cod;
	
	window.open(apro, 'inserzionista', 'width=400, height=300, top=10, left=10, resizable=yes, menubar=no, toolbar=no, location=no, scrollbars=yes, status=yes');
}

function foto_pers(id, foto, cat) {
	pag="/foto_pers.php?id="+id+"&foto="+foto+"&tipo="+cat;	
	
	window.open(pag, 'fin_foto', 'width=720, height=550, top=10, left=10, resizable=no, menubar=no, toolbar=no, location=no, scrollbars=no, status=no');
}

function verifica_disponibilita(lang) {
	a=document.verifica_disp;
	
	if(a.etichetta.value=="" || a.disp_entrata.value=="" || a.disp_uscita.value=="") {
		if(lang=="ita") {
			alert("I campi sono tutti obbligatori");	
		}
		if(lang=="eng") {
			alert("All the fields are obligatory");	
		}
	} else {
	
		par="albergo="+a.albergo.value+"&stanza="+a.etichetta.value+"&entrata="+a.disp_entrata.value+"&uscita="+a.disp_uscita.value;
		//alert(par);
		new Ajax.Request('/ajax/verifica_disponibilita.php', {
				 method: 'post',
				 parameters: par,
				 onComplete: function(transport){
					 resp = transport.responseText || "no response text";
					 campo="cont_result";
					 
					 if(resp=="impossibile") {
						 if(lang=="ita") {
							alert("Attenzione ! la data di entrata deve essere antecedente a quella di uscita"); 
						 }
						 if(lang=="eng") {
							alert("Attention! date of entrance has be previous than that of going out"); 
						 }
					 }
					 
					 if(resp=="libero") {
						 
						 dati="etichetta="+a.etichetta.value+"&albergo="+a.albergo.value+"&entrata="+a.disp_entrata.value+"&uscita="+a.disp_uscita.value+"&lingua="+lang+"&tariffa="+a.tariffa.value;
						 request=new Ajax.Request("/ajax/risposta_disponibilita.php", { 
							method: "post",
							parameters: dati,
							onComplete: function (transport) { 
								scrivi = transport.responseText || "no response text";
								document.getElementById(campo).innerHTML=scrivi;
								num=a.tot_stanze.value;
								//alert(num);
								if(num>3) {
									suppl_alt=40*num;	
								} else {
									suppl_alt=1;	
								}
								suppl_alt=suppl_alt+1100;
								//alert(suppl_alt);
								document.getElementById('middle_pers').style.height=suppl_alt+'px';
								//alert(document.getElementById('middle_pers').style.height);
								adatta_personal(); 
								adatta_layout(); 
							}	
			
						  });
					 } 
					 if(resp=="occupato") {
						 if(lang=="ita") {
							alert("La stanza non e' disponibile per il periodo indicato.\n\rProva un altro periodo.");
						 }
						 if(lang=="eng") {
							alert("The room is not free in the indicated period.\n\rTry to change the period.");  
						 }
						 document.getElementById(campo).innerHTML="&nbsp;";
					 }
					 
				 }
			});
	}
	//adatta_personal();
	
}

function ctl_prenotazione(entrata, uscita, lingua) {
	errori=0;
	obblighi=0;
	a=document.richiesta_prenotazione;
	reg=new RegExp("^[a-zA-Z ]");
	reg_mail=new RegExp("^([a-zA-Z0-9\._-])+@([a-zA-Z0-9])+\.([a-zA-Z0-9])+$");
	if(a.nome.value=="") {
		errori++;
		obblighi++;
	} else {
		res=reg.test(a.nome.value);
		if(!res) {
			errori++;
			if(lingua=="ita") {
				alert("Ci sono errori nel campo NOME");
			}
			if(lingua=="eng") {
				alert("There are errors in the field NAME");	
			}
		}
	}
	if(a.cognome.value=="") {
		errori++;
		obblighi++;
	} else {
		res=reg.test(a.cognome.value);
		if(!res) {
			errori++;
			if(lingua=="ita") {
				alert("Ci sono errori nel campo COGNOME");
			}
			if(lingua=="eng") {
				alert("There are errors in the field SECOND NAME");	
			}
		}
	}
	
	if(a.nazione.value=="") {
		errori++;
		obblighi++;
	}
	if(a.email.value=="") {
		errori++;
		obblighi++;
	} else {
		res=reg_mail.test(a.email.value);
		if(!res) {
			errori++;
			if(lingua=="ita") {
				alert("La casella mail e' sbagliata");
			}
			if(lingua=="eng") {
				alert("Email is wrong");	
			}
		}
	}
	
	if(errori>0) {
		if(obblighi>0) {
			if(lingua=="ita") {
				alert("I campi contrassegnati da asterisco sono obbligatori");
			}
			if(lingua=="eng") {
				alert("Some obligatory field is not empty");	
			}
		}
			
	} else {
		par="albergo="+a.struttura.value+"&etichetta="+a.etichetta.value+"&nome="+a.nome.value+"&cognome="+a.cognome.value+"&nazione="+a.nazione.value+"&citta="+a.citta.value+"&telefono="+a.telefono.value+"&casella_posta="+a.email.value+"&entrata="+a.entrata.value+"&uscita="+a.uscita.value+"&messaggio="+a.messaggio.value+"&nome_eti="+a.nome_etichetta.value;
		new Ajax.Request('/ajax/mail_prenota.php', {
			 method: 'post',
			 parameters: par,
			 onComplete: function(transport){
					 resp = transport.responseText || "no response text";
					document.getElementById('cont_result').innerHTML=resp;				
				
				 }
			 });
		
	}
	
}

function carica_loading(stato) {
	a=document.getElementById('preloading');
	b=document.getElementById('oscuratore');
	if(stato==1) {
		a.style.visibility='visible';
		b.style.visibility='visible';
	}
	if(stato==0) {
		a.style.visibility='hidden';
		b.style.visibility='hidden';
	}
}

function chiudi_dettagli() {
	document.getElementById('cont_dettagli').style.visibility='hidden';
	document.getElementById('cont_liste').style.visibility='hidden';
	document.getElementById('oscuratore').style.visibility='hidden';
}

function ctl_livello(liv) {
	
	Effect.toggle(liv, 'slide', {duration: 0.5});
	
}

function chiudi_apri_liv(liv1, liv2) {
	
	Effect.toggle(liv1, 'slide', {duration: 0.5});
	setTimeout("ctl_livello('"+liv2+"')", 1000);
	
}

function balloon_apri(liv) {
	Effect.BlindDown(liv, { duration: 0.5 });
	
}
function balloon_chiudi(liv) {
	Effect.BlindUp(liv, { duration: 0.5 });
	
}

function balloon_ctl_div(div){
	if (div=="eventi") {
		if(apertura_eventi==0){
			if (apertura_foto == 1 || apertura_costi == 1) {
				if (apertura_foto == 1) {
				
					balloon_chiudi('b_foto');
					setTimeout("balloon_apri('b_eventi')", 0700);
				}
				
				if (apertura_costi == 1) {
				
					balloon_chiudi('b_costi');
					setTimeout("balloon_apri('b_eventi')", 0700);
				}
				
			} else {
					balloon_apri('b_eventi');
			}
			apertura_eventi = 1;
			apertura_foto = 0;
					
		} else {
			balloon_chiudi('b_eventi');
			apertura_eventi=0;
			apertura_foto=0;
			apertura_costi=0;
		}
	}
	
	if (div == "foto") {
		if (apertura_foto == 0) {
			if (apertura_eventi == 1 || apertura_costi == 1) {
				if (apertura_eventi == 1) {
				
					balloon_chiudi('b_eventi');
					setTimeout("balloon_apri('b_foto')", 0700);
				}
				
				if (apertura_costi == 1) {
				
					balloon_chiudi('b_costi');
					setTimeout("balloon_apri('b_foto')", 0700);
				}
				
			} else {
					balloon_apri('b_foto');
			}
			apertura_eventi = 0;
			apertura_foto = 1;
			apertura_costi=0;
		}
		else {
			balloon_chiudi('b_foto');
			apertura_eventi = 0;
			apertura_foto = 0;
			apertura_costi=0;
		}
	}
	
	if (div == "costi") {
		if (apertura_costi == 0) {
			if (apertura_eventi == 1 || apertura_foto == 1) {
				if (apertura_eventi == 1) {
				
					balloon_chiudi('b_eventi');
					setTimeout("balloon_apri('b_costi')", 0700);
				}				
				if (apertura_foto == 1) {
				
					balloon_chiudi('b_foto');
					setTimeout("balloon_apri('b_costi')", 0700);
				}
				
			} else {
					balloon_apri('b_costi');
			}
			apertura_eventi = 0;
			apertura_foto = 0;
			apertura_costi=1;
		}
		else {
			balloon_chiudi('b_costi');
			apertura_eventi = 0;
			apertura_foto = 0;
			apertura_costi=0;
		}
	}
}

function select_foto(n) {
	
	if(n!=foto_select) {
		
		par="id="+n;
		new Ajax.Updater('foto_dett','/ajax/carica_loading.php', {
			method:'get',
			parameters: par
			
		}
		
		);
		setTimeout("select_foto2('"+n+"')", 1000);
	}
	
}

function select_foto2(n){
	//alert(n);
	par="id="+n;
		new Ajax.Updater('foto_dett','/ajax/sel_foto.php', {
			method:'get',
			parameters: par
			
		}
		
		);
	foto_select=n;
}

function cambia_foto(modo, q) {
	if(modo=='indietro' && foto_select>1) {
		n=foto_select-1;
		select_foto(n);
	}
	
	if(modo=='avanti' && foto_select<q) {
		n=parseInt(foto_select);
		n++;
		select_foto(n);
	}
	
}

function carica_citta(val, dove) {
 	new Ajax.Updater(dove, "/ajax/agg_citta.php", { 
			method: "get",
			parameters: "val="+val,
			onComplete: $(dove).selectedIndex=0
		});

}

function mod_sequenza() {
	/*
	new Ajax.Request('/ajax/cambia_seq.php', {
		 method: 'get',
		 parameters: {login: a.login_reg.value },
		 onComplete: function(transport){
			 res = transport.responseText || "no response text";
			
		 }
	}); */
	
	n=9;
	r=1+Math.round(n*Math.random());
	alert("Inserimento non corretto.\n\rRiprova.");
	
	document.se_ev.mia_serie.setAttribute('value', r);
	stringa="<img src='/images/sequenze/seq_"+r+".jpg'>";
	document.getElementById('cont_seq').innerHTML=stringa;
}

function invia_evento(lingua) {
	
	obblighi=0;
	errori=0;
	a=document.se_ev;
	if(a.provincia.value=="" || a.citta.value=="" || a.segnala_dal.value=="" || a.segnala_al.value=="" || a.descr_ita=="" || a.descr_eng.value=="" || a.serie_ins.value=="") {
		errori++;
		obblighi++;
	} else {
		if(a.descr_ita.value.length>200 || a.descr_eng.value.length>200) {
			errori++;
			alert("I testi relativi alle descrizioni\n\rdevono contenere al massimo 200 caratteri"); 
		}
		if(a.serie_ins.value.length<6) {
			errori++;
			alert("La sequenza alfanumerica deve contenere 6 caratteri");
		}
		
		
	}
	if(errori==0) {
		par="id="+a.mia_serie.value+"&ins="+a.serie_ins.value;
		new Ajax.Request('/ajax/serie_alfa.php', {
			 method: 'get',
			 parameters: par,
			 onComplete: function(transport){
					 res_serie = transport.responseText || "no response text";
					 if(res_serie==0) {
						 
						 mod_sequenza();
					 } else {
						 
						 a.submit();
					 }
				 }
		});	
	} else {
		if(obblighi>0) {
			if(lingua=="ita") {
				alert("L'unico campo non obbligatorio e' AGGIUNGI MESSAGGIO.");
			} else {
				alert("The only not obbligatory field is ADD A MESSAGE");
			}
		}
		//alert("Ci sono errori");	
	}
	
}

function condividi(sito) {

     if(sito=="facebook") {
          document.location.href="http://www.facebook.com/sharer.php?u=http://www.toscanatour.eu";
     }

     if(sito=="technorati") {
          document.location.href="http://www.technorati.com/faves?add=http://www.toscanatour.eu";
     }

     if(sito=="twitter") {
          document.location.href="http://www.addthis.com/bookmark.php?v=250&winname=addthis&pub=diggita&source=men-250&lng=it&s=twitter&url=http%3A%2F%2Fwww.festivaldeinuoviturismi.org%3Ftitle%3DFestival_dei_nuovi_turismi";
     }

     if(sito=="friendfeed") {
          document.location.href="http://www.friendfeed.com/share?title=Il+viaggio+in+Toscana&link=http://www.toscanatour.eu";
     }

     if(sito=="delicious") {
          document.location.href="http://del.icio.us/post?url=http://www.toscanatour.eu&title=Il+viaggio+in+Toscana";
     }
     
     if(sito=="google") {
          document.location.href="http://www.google.com/bookmarks/mark?op=edit&bkmk=http://www.toscanatour.eu&title=Il+viaggio+in+Toscana";
     }


}

function select_foto(foto, struttura) {
	par="id="+foto+"&struttura="+struttura;

	new Ajax.Updater("foto_dett", "/ajax/sel_foto_pers.php", {
		method: 'get',
		parameters: par,
          onCreate: carica_loading('foto_dett')
		//onComplete:Effect.ScrollTo('sotto_foto', {duration: 0.3})

		});
}

function contatta_pers() {

     a=document.contatta;
     tipo=a.tipo_struttura.value;
     struttura=a.struttura.value;
     testo=a.messaggio.value;
     recapito=a.recapito.value;
     
     par="tipo="+tipo+"&testo="+testo+"&mail_mittente="+recapito+"&codice="+struttura;
     
     new Ajax.Request("/ajax/contatta_inserzionista.php?inviato=1", {
               method:'post',
               parameters: par,
               onComplete: function() {
               
                    alert('Messaggio inviato');
               
               }
     
          });

}

