var core     = "components/",
    server   = "php", 
		pathname = window.location.pathname;

$(document).ready(function() {
  
    $('form').submit(function() {
          
          if(!isValidEmailAddress( $("#email").val() )){
            alert("Campo Email non compilato o non corretto.");
            return false;
          }  
          if( ($("#Messaggio").val() == '') || ($("#Messaggio").val() == null) ){ 
            alert("Campo Messaggio non compilato o non corretto.");
            return false;
          }  
          
          $("#ajForm").css( "display", "none" );
          $("#ajBox").css( "display", "block" );
         
          $.ajax({
               type: "POST",
               data: $(this).serialize() + "&mode=txData&p=" + pathname + "&sid=" + Math.round(Math.random() * 10000000),
               url: core + "ajxRichiesta" + "." + server,
               cache: false,
               success: function(html){
                    $("#ajBox").css( "display", "none" );
                    $("#ajMessage").css( "display", "block" );
                    $("#ajMessage").empty().append(html);
               }
          });
          return false;
    });

		$("#email").keyup(function(event) {
          if ( $("#email").val() != '' ){
             if ( isValidEmailAddress( $("#email").val() ) ){
                  $("#validEmail").css( "background-image", "url('template/pix/validYes.png')" );
             } else {
                  $("#validEmail").css( "background-image", "url('template/pix/validno.png')" );
				       }
          } else {
                  $("#validEmail").css( "background-image", "none" );
 	        }
		});

  
});

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);
}
