$(document).ready(function(){
		$("#prenota").dialog({ autoOpen: false ,bgiframe: true, hide: 'drop',modal: true,show: 'drop',width: 500});
		$("#dataPrenotazione").datepicker({dateFormat: 'dd/mm/yy',dayNamesMin: ['Do', 'Lu', 'Ma', 'Me', 'Gi', 'Ve', 'Sa'],monthNames: ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'], dayNames: ['Domenica', 'Lunedi', 'Martedi', 'Mercoledi', 'Giovedi', 'Venerdi', 'Sabato'] , minDate:'0d'});
	});
function openPrenota(){
	$("#prenota").dialog('open');
}
function savePrenota(){
	check();
}

function send()
{
	 $.ajax({
	   type: "POST",
	   url: "savePrenota.php",
	   data: $('#prenotaForm').serialize(),
	   success: function(msg){
		 alert( "Grazie per la prenotazione. (Mail sent)");
	   },
	   error:function(o, textStatus, errorThrown){
	     alert( "ko: " + o.responseText );
		 alert( "ko: " + textStatus );
		 alert( "ko: " + errorThrown );
	   }
	 });
}

function validate_email(field)
{
with (field)
  {
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {return false;}
  else {return true;}
  }
}
   
function check()
{
	var nome = document.prenotaForm.nome.value;
	var cognome = document.prenotaForm.cognome.value;
	var telefono = document.prenotaForm.telefono.value;
	var email = document.prenotaForm.email.value;
	var persone = document.prenotaForm.persone.value;
	var dataPrenotazione = document.prenotaForm.dataPrenotazione.value;
	var ora = document.prenotaForm.ora.value;
  
  if ((nome == "") || (nome == "undefined")) {
	   alert("Il campo Nome è obbligatorio.");
	   document.prenotaForm.nome.focus();
	   return false;
	}
	else if ((cognome == "") || (cognome == "undefined")) {
	   alert("Il campo Cognome è obbligatorio.");
	   document.prenotaForm.cognome.focus();
	   return false;
	}
	else if ( (telefono == "" || telefono == "undefined") &&   
            (email == "" || email == "undefined")
          ) {
	   alert("Un campo tra telefono o email è necessario.");
	   document.prenotaForm.telefono.focus();
	   return false;
	}
	else if ((persone == "") || (persone == "undefined")) {
	   alert("Il campo Persone è obbligatorio.");
	   document.prenotaForm.persone.focus();
	   return false;
	}
	else if ((dataPrenotazione == "") || (dataPrenotazione == "undefined")) {
	   alert("Il campo Data di Prenotazione è obbligatorio.");
	   document.prenotaForm.dataPrenotazione.focus();
	   return false;
	}
	else if ((ora == "") || (ora == "undefined")) {
	   alert("Il campo Ora è obbligatorio.");
	   document.prenotaForm.ora.focus();
	   return false;
	}
	else if( validate_email(document.prenotaForm.email)==false 
      && (telefono == "" || telefono == "undefined")) {
	    alert("Email non valida.Not a valid e-mail address")
      document.prenotaForm.email.focus();
      return false;
  }
  else
    send();

}
