$(document).ready(function() {
	$(".close").click(function() {
		//alert("chiudi");
		$(".close").removeClass("contactclose");
		$("#obscure").hide();
		$("#show_container").hide();
		$("#show_form").empty();
		$("#show_bottom").empty();	//svuoto nel caso di immagine sondaggio
	});
	
	$(".close_another_form").click(function() {
		//nascondo il pulsande di chiusura e mostro l'altro
		$("#prendi_appuntamento_close_button").hide();
		$(".close").show();
		$("div").remove("#show_another_form");		
	});
	
	/*
	controllo campi mail form banner 
	*/
	$("#prendi_app_nome").focus(function() {
		$(this).removeClass("nok");
	});
	$("#prendi_app_nome").blur(function() {
		if($(this).val()==""){
			$(this).removeClass("ok");
			$(this).addClass("nok");
		} else {
			$(this).addClass("ok");
		}
	});
	
	$("#prendi_app_cognome").focus(function() {
		$(this).removeClass("nok");
	});
	$("#prendi_app_cognome").blur(function() {
		if($(this).val()==""){
			$(this).removeClass("ok");
			$(this).addClass("nok");
		} else {
			$(this).addClass("ok");
		}
	});
	
	/*
		Campo obbligatorio per essere obbligatorio, ma NON per ricevere info
	*/
	$("#prendi_app_telefono").focus(function() {
		$(this).removeClass("nok");
	});
	$("#prendi_app_telefono").blur(function() {
		if(isNaN($(this).val())){ 				//se non è un numero
			$(this).removeClass("ok");
			$(this).addClass("nok");
		} else if($(this).val() == "") {		//se è vuoto
			$(this).removeClass("ok");
			if($(this).hasClass("required")) {
				$(this).addClass("nok");
			} else {
				$(this).removeClass("nok");
			}
		} else {
			if($(this).val().length > 6) {		//se è un numero
				$(this).addClass("ok");
			} else {
				$(this).addClass("nok");
			}
		}
	});
	
	$("#prendi_app_email").focus(function() {
		$(this).removeClass("nok");
	});	
	$("#prendi_app_email").blur(function() {
		if(!isValidEmail($(this).val())) {
			$(this).removeClass("ok");
			$(this).addClass("nok");
		} else {
			$(this).addClass("ok");
		}
	});
});

function showForm(form,size,from) { //size introdotto dopo CR per gestire form piccole | from introdotto dopo CR per gestire proveniente da PV
	/* se da un sondaggio voglio andare al trova filiale prima svuoto il tutto e levo l'immagine*/
	$("#show_form").empty();
	$(".survey_conto").remove();
	
	obscure(size);
	/* Se form è un numero allora si riferisce al dem code e gestisce la mail form commerciale */
	if(!isNaN(form)) {
		$("#show_form").append("<iframe class='showform' frameborder='0' scrolling='no' src='/cc/jsp/mail-form-commerciali.jsp?dc="+form+"'></iframe>");
	} else {	
		switch(form) {
				case "calcolaiban":
					$("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/cc/jsp/calcola-iban.jsp\"></iframe>");
				break;
				
				case "trovafiliali":
					$("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/cc/jsp/dove-trovarci-cerca.jsp?f=carige\"></iframe>");
					iframe(400);
				break;
				
				case "questionario-conti":
					$("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/privati/conti-correnti/questionario.html\"></iframe>");
				break;
	
				case "contattaci":
					moveCloseButton();					
					$("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/cc/jsp/mail-form-contattaci.jsp?f=carige \"></iframe>");
					iframe(770);
				break;

	            case "video-sicurezza":
	                $("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/info/sicurezza/video-sicurezza-online.html?f=carige\"></iframe>");
	            break;
				
				case "video-dammilcinque":
	                $("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/privati/finanziamenti/video-dammilcinque.html?f=carige\"></iframe>");
	            break;
				
				case "video-pensione":
	                $("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/privati/polizze-vita-fondi-pensione/video-fondo-pensione.html?f=carige\"></iframe>");
	            break;
				
				case "video-tassazione":
					  $("#show_form").append("<iframe class=\"showform\" frameborder=\"0\" scrolling=\"no\" src=\"/privati/fondi-gestioni-patrimoniali/video-tassazione-fondi.html?f=carige\"></iframe>");
				break;
		}	
	}
	scroll2Top("#header");
}

function showSurvey(survey) {
	scroll2Top("#header");
	
	var xmlhttp;
	if (window.XMLHttpRequest)
  	{
  		xmlhttp=new XMLHttpRequest();
  	} else {
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xmlhttp.onreadystatechange=function()
  	{
  		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
		    document.getElementById("show_form").innerHTML=xmlhttp.responseText;
			
	   	}
  	}
	
	xmlhttp.open("GET","/includes/survey_"+survey+".shtm",true);
	xmlhttp.send();
	
	obscure();
	$("#show_middle").css("height" , "400");
	$("#show_bottom").append('<div class="survey_'+survey+'"></div>');
}

function showAnotherForm(agenzia,brand) {

	//mostro un altro iframe, per ora è il caso singolo di prendi appuntamento proveniente da mappa
	
	$(".close").hide(); //nascondo il pulsante di chiusura e mostro l'altro
	$("#prendi_appuntamento_close_button").show(); //questo pulsante di chiusura ha un comportamento diverso
	
	$("#show_middle").prepend('<div id="show_another_form"></div>'); //aggiungo all'inizio un altro contenitore per mettere l'iframe

	$("#show_another_form").append("<iframe class='showform' frameborder='0' scrolling='no' src='/cc/jsp/prendi-appuntamento.jsp?agenzia="+agenzia+"&brand="+brand+"' style='z-index:100; background:#ffffff'></iframe>"); //all'interno del div creato inserisco l'iframe	
}

function closeAnotherForm() {
	$("#prendi_appuntamento_close_button").hide();
		$(".close").show();
		$("div").remove("#show_another_form");		
}

function obscure(size) {				//size aggiunto dopo CR per gestire finestre modali più piccole
	var height = $(document).height();	//controllare se la finestra modale è più alta della finestra stessa
	$("#obscure").css("height", height);
	$("#obscure").show();
	$("#show_container").show();
	
	if(size == "small") {				//se size è definito e vale small mostro la finestra modale più piccola
		$("#show").hide();
		$("#show_form").remove();		//evito di creare doppioni
		$('#show_small_middle').append('<div id="show_form"></div>');							
		$("#show_small").show();		
	} else {							//altrimenti visualizzo quella classica
		$("#show_small").hide();	
		//$("#show_form").remove();		//evito di creare doppioni				
		$('#show_middle').append('<div id="show_form"></div>');		
		$("#show").show();
	}
}

function iframe(height) {
	$("iframe").css("height" , height);
	$("#show_middle").css("height" , height);
	$("#show_small_middle").css("height" , height);
}

function moveCloseButton() {
	$(".close").addClass("contactclose");
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function submitMailForm(what) {
	//what può essere info o contatto
	var submitForm = true;
	
	// nome
	if($("#prendi_app_nome").val()==""){
		$("#prendi_app_nome").removeClass("ok");
		$("#prendi_app_nome").addClass("nok");
		submitForm = false;
	}
	
	// cognome
	if($("#prendi_app_cognome").val()==""){
		$("#prendi_app_cognome").removeClass("ok");
		$("#prendi_app_cognome").addClass("nok");
		submitForm = false;
	}
	
	// telefono
	if(isNaN($("#prendi_app_telefono").val())) {
		$("#prendi_app_telefono").removeClass("ok");
		$("#prendi_app_telefono").addClass("nok");
		submitForm = false;
	}	
	
	// email	
	if(!isValidEmail($("#prendi_app_email").val())) {
		$("#prendi_app_email").removeClass("ok");
		$("#prendi_app_email").addClass("nok");
		submitForm = false;
	}
	
	if(!submitForm) {
		return false;
	}
}

//SCRIPT DI GESTIONE CLIENTE BANCA MAIL FORM BANNER
function SetBancaDiContatto() {
        var banca = document.getElementById("select_cliente_banca").value.split('|');
        document.getElementById("banca_di_contatto").value = banca[1];
		document.getElementById("cliente_banca").value = banca[0];
		//alert(banca[0]+banca[1]);
}
