// JavaScript Document
//functions
/*function jcarousel_initCallback(carousel) {
	
	var numItem = carousel.size();
	//alert( numItem );
	for (var i=1; i<= numItem; i+=2){
		var page = $('<a href="#" id="bullet-'+i+'"><b>'+i+'</b></a>');
		page.appendTo(".cc_scroller_bullets");
	}
	///prima selezione
	jQuery('.cc_scroller_bullets a').first().addClass("selected");
	///bind selezionatori
	jQuery('.cc_scroller_bullets a').bind('click', function() {
		jQuery('.cc_scroller_bullets a').removeClass("selected");
		jQuery(this).addClass("selected");
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).find("b").text()));
        return false;
    });
	
};
function jcarousel_stepCallback(carousel) {
	jQuery('.cc_scroller_bullets a').removeClass("selected");
	jQuery('#bullet-').addClass("selected");
}*/
//on ready
function isValidEmailAddress(emailAddress) {
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
				return pattern.test(emailAddress);
			}
function close_ok()
{
	$(".alert_ok:visible").slideUp("slow");
}
function form_ok()
{
	$(".alert_ok").fadeIn("slow");
	//t=setTimeout("close_ok()",2000);
}

function systemerror(titolo,testo){
	$(".alert_systemerror h2").text(titolo);
	$(".alert_systemerror p").text(testo);
	$(".alert_systemerror").slideDown("fast");
}
function completamentoCheck(){
	
	var errors = false;
	
			/////////////campi testo
			$('#completamento .mandatory[type="text"]').each(function()
			{				
				
				if( $(this).val()==0 |! $(this).val())
				{
					//alert($(this).attr("id"));
  					$(this).parents(".form-item").addClass("error");
					$(this).addClass("error");
					errors=true;
				}
				
				if($(this).attr("id")=="email" &! isValidEmailAddress($(this).val()) )
				{
					$(this).parents(".form-item").addClass("error");
					$(this).addClass("error");
					errors=true;
				}		
				
				if($(this).attr("id")=="confermaemail" && $("#email").val()!= $(this).val())
				{
					alert ("conferma mail error");
					$(this).parents(".form-item").addClass("error");
					$(this).addClass("error");
					errors=true;
				}		
				
			});
			
			/////////////campi check
			$('#completamento .mandatory[type="checkbox"]').each(function()
			{				
				if( $(this).is(":checked")){
					
				}else{
					//alert($(this).attr("id"));
  					$(this).parents(".form-item").addClass("error");
					$(this).addClass("error");
					errors=true;
				}
			});
			
			/////////////campi regalo
			$("#regalo .mandatory").each(function()
			{	
				if($("#friend_email").val()!=""){
					
					
					if( $(this).val()==0 |! $(this).val())
					{
					$(this).parents(".form-item").addClass("error");
					$(this).addClass("error");
						errors=true;
					}
					
				}
			});
			
			if( $("#friend_email").val()!="" &! isValidEmailAddress( $("#friend_email").val() ) ){
				
					$("#friend_email").parents(".form-item").addClass("error");
					$("#friend_email").addClass("error");
					errors=true;
			}

			
			////conferma mail
			$("#confermaemail").each(function()
			{
				if(  $(this).val()==0 |! $(this).val())
				{
  				$(this).parents(".form-item").addClass("error");
				$(this).addClass("error");
					errors=true;
				}
			});
			
			//alert ( $("#codfisc_result").text()  );
			
			if( $("#codfisc_result").text()!="OK" ){
								$("#codfisc_result").parents(".form-item").addClass("error");
								$("#codfisc_result").addClass("error");
								errors=true;
							}
			
			
			
			if(errors)
			{
				//alert("compl errori");
				 var alert_text="<h2>Purtroppo ci sono degli errori nella compilazione</h2>";
				 alert_text+="<p>Devi inserire tutti i dati prima di inviare oppure controllare che siano corretti.</p>";
				 if($("#codfisc_result").text()=="DB" || $("#codfisc_result").text()=="CC" ){
					alert_text+='<p><em>Gent.mo Dottore, il suo codice fiscale risulta gi&aacute; attivo nel nostro sistema. Riprovi a compilare il campo correttamente. Se il problema persiste invii mail a: <a href="mailto:support@consulcesi.it">support@consulcesi.it</a> . Siamo a Sua disposizione. Grazie.</em></p>';
				 }
				  if($("#codfisc_result").text()=="KO" ){
					alert_text+='<p><em>Gent.mo Dottore, il suo codice fiscale risulta errato. Riprovi a compilare il campo correttamente. Se il problema persiste invii mail a: <a href="mailto:support@consulcesi.it">support@consulcesi.it</a>. Siamo a Sua disposizione. Grazie.</em></p>';
				 }
				 $(".alert_error div").html(alert_text);
				$(".alert_error").slideDown("slow");
				return false;
			}
			else
			{
					//alert("compl ok");
				/////////////OK
				$('#completamento').submit();
                return true;
			}
}
$(document).ready(function()
{
	inner_ready();

	////////controllo forms
	$('input').each(
			function()
			{
				$(this).data("default", $(this).val());
			}
	);
	///////autoclear
	$('#risposta .mandatory, #login .mandatory').focus(
			function()
			{
				$(this).parents(".form-item").removeClass("error");
				$(this).removeClass("error");
				$(".alert_error").slideUp("slow");
				
			}
	);
	$('#risposta input[type="text"]').focus(
			function()
			{
				//alert("ecco");
				if( $(this).val() == $(this).data("default") )
				{
					$(this).val("");
				}
			}
	);
	$('#risposta input[type="password"]').focus(
			function()
			{
				//alert("ecco");
				if( $(this).val() == $(this).data("default") )
				{
					$(this).val("");
				}
			}
	);
	
	
	
	
	$('#risposta input[type="text"]').focusout(
			function()
			{
				//alert("ecco");
				if( $(this).val() == "" && $(this).parent().attr("class")!=".form-item-small"  )
				{
					$(this).val($(this).data("default"));
				}
			}
	);
	$('#risposta input[type="password"]').focusout(
			function()
			{
				//alert("ecco");
				if( $(this).val() == "" )
				{
					$(this).val($(this).data("default"));
				}
			}
	);
	
	
	
	$('#areariservata input[type="text"]').focus(
			function()
			{
				//alert("ecco");
				if( $(this).val() == $(this).data("default") )
				{
					$(this).val("");
				}
			}
	);
	$('#areariservata input[type="password"]').focus(
			function()
			{
				//alert("ecco");
				if( $(this).val() == $(this).data("default") )
				{
					$(this).val("");
				}
			}
	);
	
	
	$('#areariservata input[type="text"]').focusout(
			function()
			{
				//alert("ecco");
				if( $(this).val() == ""  )
				{
					$(this).val($(this).data("default"));
				}
			}
	);
	$('#areariservata input[type="password"]').focusout(
			function()
			{
				//alert("ecco");
				if( $(this).val() == "" )
				{
					$(this).val($(this).data("default"));
				}
			}
	);
	
	$('#codice-invito').focus(
			function()
			{
				//alert("ecco");
				if( $(this).val() == $(this).data("default") )
				{
					$(this).val("");
				}
			}
	);
	
	
	
	$('#codice-invito').focusout(
			function()
			{
				//alert("ecco");
				if( $(this).val() == ""  )
				{
					$(this).val($(this).data("default"));
				}
			}
	);
	
	//////
	$("#areariservata input").focus(function(){
			$(".login_risposta").slideUp("slow");								 
	});
	$("#areariservata").submit(
		function()
		{
			var response;
			jQuery.ajax( { url: '/area-riservata/check.php?'+($(this).serialize()), async: false, success: function(data){response=data} } );

			if(response=='ok')
			  return true;
			else
			{
				$(this).find(".login_risposta").slideDown("slow");
				return false;
			}
		});
	$(".login_risposta").hover(function(){},function(){
													 $(this).slideUp("slow");
													 });
	//////
	$('#form').submit(
		function()
		{
			var errors= false;
			$("#form .mandatory").each(function()
			{				
				if( $(this).val() == $(this).data("default") || $(this).val()==0 |! $(this).val())
				{
  				$(this).addClass("error");
					errors=true;
				}
				if($(this).attr("id")=="indirizzoemail" &! isValidEmailAddress($(this).val()) )
				{
					$(this).addClass("error");
					errors=true;
				}
			});
			
			if(errors)
			{
				//alert("errori");
				$(this).find(".alert_error").fadeIn("slow");
				return false;
			}
			else
			{
				/////////////OK
				/// function da richiamare al reaload???
				
				var dati=$(this).serialize();
				var dataString = '/loger.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				
				$('#risposta').load(dataString,
									function(){
										location.href = "http://www.consulcesi.it/richiesta-rimborsi";
									}
								
				).hide();
				
				//form_ok();
				return false;
			}
		}
	);
	
	
	//////
	$('#servizi').submit(
						 
		function()
		{
			//alert("SERVIZI");
			var errors= false;
			$("#servizi .mandatory").each(function()
			{				
				if( $(this).val() == $(this).data("default") || $(this).val()==0 |! $(this).val())
				{
  				$(this).addClass("error");
					errors=true;
				}
				if($(this).attr("id")=="indirizzoemail" &! isValidEmailAddress($(this).val()) )
				{
					$(this).addClass("error");
					errors=true;
				}
			});
			
			if(errors)
			{
				//alert("errori");
				$(this).find(".alert_error").fadeIn("slow");
				return false;
			}
			else
			{
				/////////////OK
				/// function da richiamare al reaload???
				
				var dati=$(this).serialize();
				var dataString = '/logers.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				
				$('#risposta').load(
							dataString,
							function(){
								location.href = "http://www.consulcesi.it/richiesta-servizi";
							}
				).hide();

				//form_ok();
				
				return false;
			}
		}
	);
	
	$('#quesito').submit(
		function()
		{
			var errors= false;
			$(".mandatory").each(function()
			{				
				if( $(this).val()==0 |! $(this).val())
				{
  				$(this).addClass("error");
					errors=true;
				}
				
			});
			
			if(errors)
			{
				//alert("errori");
				$(this).find(".alert_error").slideDown("slow");
				return false;
			}
			else
			{
				/////////////OK
				/// function da richiamare al reaload???
				
				var dati=$(this).serialize();
				var dataString = '/quesito.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				$('#risposta').load(dataString).hide();

				form_ok();
				return false;
			}
		}
	);

	$('#upgrade').submit(
		function()
		{
			var errors= false;
			$(".mandatory").each(function()
			{				
				if( $(this).val()==0 |! $(this).val())
				{
  				$(this).addClass("error");
					errors=true;
				}
				
			});
			
			if(errors)
			{
				//alert("errori");
				$(this).find(".alert_error").slideDown("slow");
				return false;
			}
			else
			{
				/////////////OK
				/// function da richiamare al reaload???
				
				var dati=$(this).serialize();
				var dataString = '/upgrade.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				$('#risposta').load(dataString).hide();

				form_ok();
				return false;
			}
		}
	);

	$('#associati').submit(
		function()
		{
			var errors= false;
			$(".mandatory").each(function()
			{				
				if( $(this).val()==0 |! $(this).val())
				{
  				$(this).addClass("error");
					errors=true;
				}
				
			});
			
			if(errors)
			{
				//alert("errori");
				$(this).find(".alert_error").slideDown("slow");
				return false;
			}
			else
			{
				/////////////OK
				/// function da richiamare al reaload???
				
				var dati=$(this).serialize();
				var dataString = '/associati.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				$('#risposta').load(dataString).hide();

				form_ok();
				return false;
			}
		}
	);
	///////////////////////////////FORM NATALE///////////////////////////////
	$('#regalo input[type="text"]').keyup( function() {
				//alert("ecco");
			var relId = $(this).attr("id")+"_2";
			$("#"+relId).val($(this).val());						   
	});
	
	$("#regalo #friend_invia").click( function() {
											 //alert("ecco");
											if($(this).is(":checked")){
												$("#friend_invia_2").val("true");
											}else{
												$("#friend_invia_2").val("false");
											}	
												
									});
	
//	$('#completamento').submit(
//		function()
//		{
//			var errors= false;
//			var dataString = '/cf.php?cf='+ $("#codfisc").val();
//
//					$("#codfisc_result").load (dataString, function() {
//							//alert(	dataString+">"+$(this).val());					
//							/*if($(this).val()!="OK"){
//								$(this).parents(".form-item").addClass("error");
//								$(this).addClass("error");
//								errors=true;
//							}*/
//							//return false;
//							return completamentoCheck();
//					});
//		}
//	);
	
	$('#completamento .mandatory[type="text"]').each(function()									  
			{				
			
				$(this).parents(".form-item").removeClass("error");
					$(this).removeClass("error");
			});
	$('#completamento input[type="submit"]').click(
		function(event)
		{
			event.preventDefault();
			var errors= false;
			var dataString = '/cf.php?cf='+ $("#codfisc").val();

					$("#codfisc_result").load (dataString, function() {
							
							return completamentoCheck();
					});
		}
	);
	
	
	
	
	$('#profilo').submit(
		function()
		{
			//alert("profilo");
			var errors = false;
			$('#profilo .mandatory').each(function()
			{				

				//alert(">"+$(this).attr("id"));
				
				if( !$(this).val() || $(this).val()=="" || $(this).val()==0)
				{
					$(this).addClass("error");
					errors=true;
				}
				
				
				if($(this).attr("id")=="indirizzoemail" &! isValidEmailAddress($(this).val()) )
				{
					//alert($(this).attr("id"));
					$(this).addClass("error");
					errors=true;
				}
					
				
			});
			
			if(errors)
			{
				//alert("errori profilo");
				$('#profilo .alert_error').slideDown("slow");
				return false;
			}
			else
			{
				var dati=$(this).serialize();
				var dataString = '/profilo-x.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				$('#risposta').load(dataString).hide();

				form_ok();
				return false;
			}		
			
		}
	);
	
	
	
	$('#profilo2').submit(
		function()
		{
			//alert("profilo2");
			var errors = false;
			$('#profilo2 .mandatory').each(function()
			{				
				
				//alert("2>"+$(this).attr("id"));
				
				if($(this).attr("id")=="conferma-password" && $("#password").val()!= $(this).val())
				{
					
					$(this).addClass("error");
					errors=true;
				}	
				
					
				
			});
			
			if(errors)
			{
				//alert("errori profilo2");
				$('#profilo2 .alert_error').slideDown("slow");
				return false;
			}
			else
			{
				/////////////OK
               // return true;
			   
			    var dati=$(this).serialize();
				var dataString = '/profilo-x.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				$('#risposta').load(dataString).hide();

				form_ok();
				return false;
				
			}		
			
		}
	);
	
	
	
	$('#sondaggio').submit(
		function()
		{
			$('.sondaggio .alert_error:visible').slideUp("slow");
			var errors = false;
			var risposte = $('#sondaggio .sondaggio_item').size();
			var risposteArray = $(this).serializeArray();
			
			//alert("sondaggio "+risposte);
			if(risposteArray.length){
				/*for ( var i=0; i<risposteArray.length ; i++){
					 console.log( risposteArray[i]['value'] );
				}*/
				if(risposteArray.length<risposte) {
					//console.log( 'risposte mancanti' );
					errors = true;
				}
				
				
			}else {
				//console.log( 'nessuna risposta' );
				errors = true;
				
			}
			
			
			
			if(errors)
			{
				//alert("errori profilo2");
				$('.sondaggio .alert_error').slideDown("slow");
				return false;
			}
			else
			{
				/////////////OK
               // return true;
			  
			    var dati=$(this).serialize();
				//alert (dati);
				var dataString = '/sondaggio_send.php?'+ dati;
				$('#risposta').load(dataString,
									function(){
										location.href = "http://www.consulcesi.it/sondaggio-grazie";
									}
								
				).hide();
				return false;
				
			}		
			
		}
	);
	
	
	/***FB forms***/
	$('#formfb').submit(
		function()
		{
			var errors= false;
			$("#form .mandatory").each(function()
			{				
				if(  $(this).val()==0 |! $(this).val())
				{
  				$(this).addClass("error");
					errors=true;
				}
				if($(this).attr("id")=="indirizzoemail" &! isValidEmailAddress($(this).val()) )
				{
					$(this).addClass("error");
					errors=true;
				}
			});
			
			if(errors)
			{
				//alert("errori");
				$(this).find(".alert_error").fadeIn("slow");
				return false;
			}
			else
			{
				/////////////OK
				/// function da richiamare al reaload???
				
				var dati=$(this).serialize();
				var dataString = '/logerfb.php?'+ dati;
				$('.alert_ok').html('<div><h2>Grazie</h2><p>La tua richiesta &egrave; stata inviata con successo!</p></div>');
				
				$('#risposta').load(dataString,
									function(){
										location.href = "http://www.consulcesi.it/speciale/fb/grazie.html";
									}
								
				).hide();
				
				//form_ok();
				return false;
			}
		}
	);
	
	$('#login').submit(
		function()
		{
			//alert("profilo");
			var errors = false;
			$('#login .mandatory').each(function()
			{				

				//alert(">"+$(this).attr("id"));
				
				if( !$(this).val() || $(this).val()=="" || $(this).val()==0)
				{
					$(this).addClass("error");
					errors=true;
				}
				
			});
			
			if(errors)
			{
				//alert("errori profilo");
				$('#login .alert_error').slideDown("slow");
				return false;
			}
			else
			{
				
				var response;
				jQuery.ajax( { url: '/area-riservata/check.php?'+($(this).serialize()), async: false, success: function(data){response=data} } );
	
				if(response=='ok')
				  return true;
				else
				{
					$('#login .alert_error').slideDown("slow");
					return false;
				}
				
				
				//$('#risposta').load(dataString).hide();

				//form_ok();
				
			}		
			
		}
	);
	
	////// Ride the carousel...

   /* jQuery(".cc_quote_slider").jcarousel({
        scroll: 1,
        initCallback: jcarousel_initCallback,
		animationStepCallback: jcarousel_stepCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
		wrap: 'circular',
        buttonNextHTML: null,
        buttonPrevHTML: null,
		auto: 2,
    });*/
	$('.cc_quote_slider').slides();
	$('.cc_news_slider').slides({
								play: 4000,
									hoverPause: true,
									pause: 100
								});
	
	///overlay ultime newss
	$(".overlay").slideDown("slow", function(){ $("#close").fadeIn("fast"); });
	$(".menu a").hover(
					   function(){ $(this).find(".submenu").slideDown("fast"); }, 
					   function(){ $(this).find(".submenu").slideUp("fast"); }
					   );
	
	$("#close").click(function(event){
			event.preventDefault();
			$("#close").fadeOut("fast", function(){$(".overlay").slideUp("slow"); });
	});
	
	///////////////CONTROLLO MENU
	
	$(".cc_toolbar li").hover( 
							  function(){
								  	$(this).find(".cc_menu_overlayer").slideDown("fast");
								  },
							  function(){
								  	$(this).find(".cc_menu_overlayer").slideUp("fast");
								  }
							  );
	///////////////CONTROLLO sottoMENU
	
	//$(".cc_toolbar").children(".cc_overlayer_articoli").not(":first").css("display","none");
	//$(".cc_overlayer_articoli:first-child").css("display","block");
	$(".cc_overlayer_lista a").hover( 
							  function(){
								    //var actualSelection = $(this).parent(".cc_overlayer_lista").find(".selected").attr("id").split("-");
								    //$("#item-"+actualSelection[1]).removeClass("selected");
									$(this).parent(".cc_overlayer_lista").find(".selected").removeClass("selected");
									$(this).addClass("selected");
								    $(".cc_overlayer_articoli:visible").hide();
								  	var theId = $(this).attr("id").split("-");
									//alert(theId[1]);
									$("#submenu-"+theId[1]).show();
								  }/*,
							  function(){
								    //var actualSelection = $(this).parent(".cc_overlayer_lista").find(".selected").attr("id").split("-");
								  	var theId = $(this).attr("id").split("-");
									$("#submenu-"+theId[1]).hide();
									$(this).parents().parents().find(".cc_overlayer_lista a:first").addClass("selected");
									$(this).parents().parents().find(".cc_overlayer_articoli:first").show();
									//$("#submenu-"+actualSelection[1]).show();
									//$("#item-"+actualSelection[1]).addClass("selected");
								  }*/
							  );
	
	
	$("#fad-table .info").hover(function(){
								  	 $(this).parent().find(".hilite").slideDown("fast");
								   },
							function(){
									$(this).parent().find(".hilite").slideUp("fast");
								   });
	$.datepicker.setDefaults($.datepicker.regional['it']);
	$( ".date-input" ).datepicker();
	
	
	//systemerror("titolo","testo");
	
});

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}
