$(document).ready(function() {
	function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				o.removeClass('ui-state-error');
				updateTips("");
				tips.removeClass('ui-state-highlight');
				return true;
			}

		}
		
	function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}
		
	var email = $("#form_mail"),
	nome= $('#form_name'),
	cognome= $('#form_surename'),
	tel= $('#form_tel'),
	azienda= $('#form_azienda'),
	note= $('#form_note'),
	valid=true,
	tips = $(".validateTips");
	
	$('#dialog').dialog({
	    autoOpen: false,
	    width: 550,
	    modal:true,
		draggable:true,
	    buttons: {
	      "Invia": function() {
	        valid = checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"la mail inserita non è nel formato: nome@host.com");
			if (valid){
				var tipologia = $("#select_tipo"),
				lingua = $("#select_lingua"),
				gestione = $("#select_gestione"),
				testi = $("#select_testi"),
				foto = $("#select_foto");
				$.ajax({
							type: "POST",
							url: "index.php?show=home/email&ajax",
							data: 'nome=' + nome.val() + '&cognome=' + cognome.val() + '&email=' + email.val() + '&tel='+tel.val() + '&azienda=' + azienda.val() + '&tipologia=' + tipologia.val() + '&lingua=' + lingua.val() + '&gestione=' + gestione.val() + '&testi=' + testi.val() + '&foto=' + foto.val() + '&note=' + note.val(),
							success: function(msg){
								if (msg == "ACK") 
									updateTips("Invio richiesta avvenuto con successo!");
								else 				
									updateTips("Non hai inserito dati obbligatori!");
								
							}
						});	

			}
	      },
	      "Close": function() {
	        $(this).dialog("close");
	      }
	    }
	    });
	 
	    // Dialog Link
	    $('#link_preventivo').click(function(){
	        $('#dialog').dialog('open');
			
	        return false;
	    });
	
});
