function initDataPeriodo(periodo){
	$("#filtraperiodo ul ul").hide();
	var find = periodo+".html";
	$.each($("#filtraperiodo ul ul a"),function(index,value){
		var check = $(value).attr("href").split("/");
		if(find==check[check.length-1]) {
			$(value).parent().addClass("current").parent().show().parent().addClass("mese_open");
		}
	});
	$.each($("#filtraperiodo ul ul"),function(){
		var count = $(this).find("li").length;
		var height = ((Math.ceil((count-1)/8))*20)+20;
		$(this).css("height",height+"px");
	});
}
function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}
function setClass(element,value) {
	$(element).removeClass();
	$(element).addClass(value);
}
function showMailForm() {
	$("#mailSuccess").hide();
	$("#commentForm").fadeIn("slow");
}
function sendMail(form) {
	var name = trim(form.name.value);
	var email = trim(form.email.value);
	var comment = trim(form.comment.value);
	var agenzia = trim(form.agenzia.value);
	var error = "";
	if(name=="") error = error+"Inserisci il tuo NOME\n";
	if(email=="") error = error+"Inserisci la tua E-MAIL\n";
	if(comment=="") error = error+"Inserisci un COMMENTO\n";
	if(agenzia=="0") error = error+"Seleziona l'AGENZIA\n";
	if (error=="") {
		$(form.submit).hide();
		$.ajax({
			type: "POST",
			url: "/mail.html",
			data: dataSent = "cname="+name+"&cemail="+email+"&ccomment="+comment+"&cagenzia="+agenzia,
			success: function(msg){
				if(msg=="1") {
					$("#commentForm").hide();
					$("#mailSuccess").fadeIn("slow");
					form.name.value = "";
					form.email.value = "";
					form.comment.value = "";
					$(form.submit).show();
					t = setTimeout("showMailForm("+form.value+")", 7000);
				} else {
					alert("Attenzione si è verificato un errore, riprova.");
				}
			}
		});
	} else alert("Attenzione:\n\n"+error);
	return false;
}
function showPrenotaForm() {
	$("#prenotaSuccess").hide();
	$("#formPrenota").fadeIn("slow");
}
function sendPrenota(form) {
	var periodo = trim(form.periodo.value);
	var name = trim(form.name.value);
	var email = trim(form.email.value);
	var comment = trim(form.comment.value);
	var offerta = trim(form.offerta.value);
	var agenzia = trim(form.agenzia.value);
	var error = "";
	if(periodo=="0") error = error+"Seleziona il periodo\n";
	if(name=="") error = error+"Inserisci il tuo NOME\n";
	if(email=="") error = error+"Inserisci la tua E-MAIL\n";
	if(agenzia=="0") error = error+"Seleziona l'AGENZIA\n";
	if (error=="") {
		$(form.submit).hide();
		$.ajax({
			type: "POST",
			url: "/info_offerta.html",
			data: "cname="+name+"&cperiodo="+periodo+"&cemail="+email+"&ccomment="+comment+"&cofferta="+offerta+"&cagenzia="+agenzia,
			success: function(msg){
				if(msg=="1") {
					$("#formPrenota").hide();
					$("#prenotaSuccess").fadeIn("slow");
					form.name.value = "";
					form.email.value = "";
					form.comment.value = "";
					$(form.submit).show();
					t = setTimeout("showPrenotaForm()", 7000);
				} else {
					alert("Attenzione si è verificato un errore, riprova.");
				}
			}
		});
	} else alert("Attenzione:\n\n"+error);
	return false;
}
/* NEWSLETTER */
function showNewsletter() {
	$("select").hide();
	$("body").append("<div id='no-click' onclick='closeNewsletter();'></div>");
	$("#no-click").show();
	var newsletter = "<div id='newsletter'>";
	newsletter+="<h2>Iscrizione alla newsletter</h2>";
	newsletter+="<a href='#' onclick='closeNewsletter();' style='position: absolute; right: 10px; top: 10px;'><img src='http://www.decioviaggi.com/images/closelabel.gif'/></a>";
	newsletter+="<div id='newsletterIscrizione'>";
	newsletter+="<p>Testo descrittivo Testo descrittivo Testo descrittivo Testo descrittivo Testo descrittivo Testo descrittivo </p>";
	newsletter+="<label>Indirizzo email:</label>";
	newsletter+="<input id='indirizzoNewsletter' type='text' value=''><button name='submit' type='button' onclick='inviaNewsletter();'>Invia</button>";
	newsletter+="</div>";
	$("body").append(newsletter);
	
	var scrollTop = $(window).scrollTop();
	var winHeight = $(window).height();
	var newsletterHeight = $("#newsletter").height();
	var topPosition = ((parseInt(winHeight)-parseInt(newsletterHeight))/2)+parseInt(scrollTop)-60;
	$("#newsletter").css("top",topPosition);
	
	var winWidth = $(window).width();
	var newsletterWidth = $("#newsletter").width();
	var leftPosition = Math.floor((winWidth-newsletterWidth)/2);
	$("#newsletter").css("left",leftPosition);
	
	$("#newsletter").show();
}
function closeNewsletter() {
	$("#no-click").remove();
	$("#newsletter").remove();
	$("select").show();
}
function inviaNewsletter() {
	var mail = $("#indirizzoNewsletter").val();
	$.ajax({
		type: "POST",
		url: "/newsletter.html",
		//url: "newsletter.php",
		data: "mode=iscrizione&mail="+mail,
		success: function(msg){
			switch (msg) {
				case "1":
					$("#newsletter").append("<div id='newsletterIscrizioneOk'><p>Hai completato il primo passo!</p><p>E' stato inviato al tuo indirizzo email: "+mail+"<br>il codice di attivazione necessario a completare l'iscrizione.</p></div>");
					$("#newsletterIscrizione").fadeOut(500, function () {
						$("#newsletterIscrizioneOk").fadeIn(500);
					});

					break;
				case "-1":
					alert("Attenzione la mail inserita non è valida.");
					break;
				default:
					alert("Attenzione si è verificato un errore, riprova.");
					break;
			}
		}
	});
}
/* END NEWSLETTER */
