// JavaScript Document
$(function() {
			   
		$.ui.dialog.defaults.bgiframe = true;		
				
		var nome = $("#nome"),
			cognome = $("#cognome"),
			citta = $("#citta"),
			cap = $("#cap"),
			nazione = $("#nazione"),
			email = $("#email"),
			tel = $("#tel"),
			fax = $("#fax"),
			richiesta = $("#richiesta"),
			arrivo = $("#datepicker"),
			allFields = $([]).add(nome).add(cognome).add(citta).add(cap).add(nazione).add(email).add(tel).add(fax).add(richiesta).add(arrivo),
			tips = $("#validateTips");
		var accetta = document.getElementById("informativa"); 
		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {
			
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("La lunghezza di " + n + " deve essere tra "+min+" e "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

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

		}
		
		function check(){
			
			
			if ( accetta.checked == true ){
				return true;
			}
			else{
					$("#spuntaConsenso").dialog();
					
					return false;
				}
		}
		//AJAX
		var time_variable;
 
		function getXMLObject()  //XML OBJECT
		{
		   var xmlHttp = false;
		   try {
			 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
		   }
		   catch (e) {
			 try {
			   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
			 }
			 catch (e2) {
			   xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
			 }
		   }
		   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
			 xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
		   }
		   return xmlHttp;  // Mandatory Statement returning the ajax object created
		}
 
		var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object
 
		function ajaxFunction() {
		  var getdate = new Date();  //Used to prevent caching during ajax call
		  if(xmlhttp) { 
		  var nome = document.getElementById("nome");
		  var cognome = document.getElementById("cognome");
		  var citta = document.getElementById("citta");
		  var cap = document.getElementById("cap");
		  var nazione = document.getElementById("nazione");
		  var tel = document.getElementById("tel");
		  var fax = document.getElementById("fax");
		  var richiesta = document.getElementById("richiesta");
		  var email = document.getElementById("email");
		  var datepicker = document.getElementById("datepicker");
		  var persone = document.getElementById("persone");
		  var notti = document.getElementById("notti");
		  var stanze = document.getElementById("stanze");
		  var tipo = document.getElementById("tipo");
			var invio = "nome=" + nome.value + "&cognome=" +cognome.value+ "&citta=" +citta.value+ "&cap=" +cap.value+ "&nazione=" +nazione.value+ "&tel=" +tel.value+ "&fax=" +fax.value+ "&richiesta=" +richiesta.value+ "&email=" +email.value+ "&datepicker=" +datepicker.value+ "&persone=" +persone.value+ "&notti=" +notti.value+ "&stanze=" +stanze.value+ "&tipo=" +tipo.value;
			xmlhttp.open("POST","http://www.reginaisabella.it/CMS/prenotazioni.php",true); //calling testing.php using POST method
			xmlhttp.onreadystatechange  = handleServerResponse;
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send(invio); //Posting txtname to PHP File
			
		  }
		}
 
		function handleServerResponse() {
		   if (xmlhttp.readyState == 4) {
			 if(xmlhttp.status == 200) {
				 
			   $("#inviata").dialog();
			 }
			 else {
				$("#nonInviata").dialog();
			 }
		   }
		}
		
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 500,
			width:500,
			modal: true,
			buttons: {
				'Invia': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');
					
					bValid = bValid && checkRegexp(nome,/^[a-zA-Z\-\.]+/i,"Il nome può contenere solo lettere");
					bValid = bValid && checkRegexp(cognome,/^[a-zA-Z\-\.]+/i,"Il cognome può contenere solo lettere");
					bValid = bValid && checkRegexp(citta,/^[a-zA-Z\-\.]+/i,"Il nome può contenere solo lettere");
					bValid = bValid && checkRegexp(cap,/^([0-9])+$/i,"Il campo C.A.P. può contenere solo numeri");
					bValid = bValid && checkRegexp(nazione,/^[a-zA-Z\-\.]+/i,"Il nome può contenere solo lettere");
					bValid = bValid && 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,"Email non corretta. Usare tipo: email@email.com");
					
					
					bValid = bValid && checkRegexp(arrivo,/^\d{1,2}\/\d{1,2}\/\d{4}$/i,"Il campo Arrivo è obbligatorio");
					
					bValid = bValid && checkLength(nome,"Nome",3,16);
					bValid = bValid && checkLength(cognome,"Cognome",3,20);
					bValid = bValid && checkLength(email,"Email",6,80);
					bValid = bValid && checkLength(citta,"Città",4,20);
					bValid = bValid && checkLength(cap,"CAP",3,7);
					bValid = bValid && checkLength(richiesta,"Richiesta",5,999);
				    bValid = bValid && checkLength(arrivo,"Arrivo",5,999);
					
					
					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					
					
					if ((bValid)&&check()){
						$("#invioDatiCorso").dialog();
						ajaxFunction();
						$(this).dialog('close');
						$("#spuntaConsenso").dialog('close');
						$("#invioDatiCorso").dialog('close');
						$("#nonInviata").dialog('close');
						
					}//CHIUDO IF
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		$('#create-user').click(function() {
			$('#dialog').dialog('open');
		})
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
$("#datepicker").datepicker();
	});
