function check(){
	var uscita = '';
	var tabella = document.prenotazioni;
	var camere = false;
	//va controllato la data odierna

	if (!tabella.arrivo.value || !check_date(tabella.arrivo)) uscita += "Data di arrivo errata\n";
	if (!tabella.partenza.value || !check_date(tabella.partenza) )uscita += "Data di partenza errata\n";
	//controllino sulla data di arrivo congrua con quella di partenza
	if(!CompareDates(tabella.arrivo,tabella.partenza)) uscita+="La data di partenza deve essere successiva a quella di arrivo\n";
	if(!CompareDates(tabella.dataodierna,tabella.arrivo)) uscita+="La data di arrivo deve essere successiva alla data odierna\n";
	if (tabella.singole.value) {
		if (isNaN(tabella.singole.value)|| tabella.singole.value<0) {
			uscita += "Numero singole errato\n";
		}	else if(tabella.singole.value>0){
			tabella.singole.value = Math.floor(tabella.singole.value);
			camere = true;
		}
	}
	if (tabella.doppie.value) {
		if(isNaN(tabella.triple.value) || tabella.doppie.value<0) {
			uscita += "Numero doppie errato\n";
		}	else if(tabella.doppie.value>0){
			tabella.doppie.value = Math.floor(tabella.doppie.value);
			camere = true;
		}
	}
	if (tabella.triple.value) {
		if (isNaN(tabella.triple.value)|| tabella.triple.value<0) {
			uscita += "Numero triple errato\n";
		}	else if(tabella.triple.value>0){
			tabella.triple.value = Math.floor(tabella.triple.value);
			camere = true;
		}
	}
	if (tabella.quadruple.value) {
		if (isNaN(tabella.quadruple.value)|| tabella.quadruple.value<0) {
			uscita += "Numero quadruple errato\n";
		}	else if(tabella.quadruple.value>0){
			tabella.quadruple.value = Math.floor(tabella.quadruple.value);
			camere = true;
		}
	}
	if(camere==false) uscita+="Selezionare almeno una tipologia\n";
	if (uscita) {
		alert(uscita);
	}else{
		tabella.comando.value = 'ricerca';
		tabella.submit();
	}
}
function check_camere(){
	//somma i posti alle variabili e nel caso fa apparire il form basso
	var datogliere=7;
	var ele=document.prenotazioni2.elements.length-datogliere;
	var tmp_singole=0;
	var tmp_doppie=0;
	var tmp_triple=0;
	var tmp_quadruple=0;
	var costo=0;
	for (i = 1; i < ele; i+=2) {
		var posti=document.prenotazioni2.elements[i].value;
		if (document.prenotazioni2.elements[i].checked) {
			if (posti == 1)	tmp_singole++;
			if (posti == 2)	tmp_doppie++;
			if (posti == 3)	tmp_triple++;
			if (posti == 4)	tmp_quadruple++;
			costo+=(1*document.prenotazioni2.elements[i+1].value);

		}
	}
	if (tmp_singole == n_singole) {
		$('#id_singole').css({
			color: "green"
		});
	} else {
		$('#id_singole').css({
			color: "red"
		});
	}

	if (tmp_doppie == n_doppie) {
		$('#id_doppie').css({
			color: "green"
		});
	} else {
		$('#id_doppie').css({
			color: "red"
		});
	}


	if (tmp_triple == n_triple) {
		$('#id_triple').css({
			color: "green"
		});
	} else {
		$('#id_triple').css({
			color: "red"
		});
	}
	if (tmp_quadruple == n_quadruple) {
		$('#id_quadruple').css({
			color: "green"
		});
	} else {
		$('#id_quadruple').css({
			color: "red"
		});
	}


	if (tmp_singole == n_singole && tmp_doppie == n_doppie && tmp_triple == n_triple && tmp_quadruple == n_quadruple) {
		$('#costo_cam').html('Costo totale camere: &euro; '+costo);
		document.prenotazioni2.costo.value=costo;
		$('#campo_nascosto1').hide();
		$('#campo_nascosto2').show();
	} else {
		$('#campo_nascosto2').hide();
		$('#campo_nascosto1').show();
	}
}
function invia_preno(){
	if (confirm("Inviare la richiesta di prenotazione ?")) {
		var cam = $("input[@type=checkbox]").serialize();

		$.post("invia_preno.php", {
			lista_camere: cam,
			nominativo: document.prenotazioni2.nominativo.value,
			email: document.prenotazioni2.email.value,
			recapiti: document.prenotazioni2.recapiti.value,
			costo: document.prenotazioni2.costo.value,
			arrivo: document.prenotazioni2.arrivo.value,
			partenza: document.prenotazioni2.partenza.value
		}, function(data){
			var msg='';
			if(data=='OK')msg='La sua prenotazione è stata inoltrata con successo';
			if(data=='ERR_EMAIL')msg="Attenzione, l'email è errata o mancante";
			if(data=='ERR_NOMINATIVO')msg='Attenzione, manca il nominativo';
			if(data=='ERR_RECAPITI')msg='Attenzione, mancano i recapiti';
			if(data=='ERR_SYS')msg='Errore di sistema, si prega di riprovare più tardi';
			alert(msg);
		});
	}
}
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
	  return true;
   }
   /* Error-message if err != 0 */
   else {
      return false;
   }
}
function CompareDates(d1,d2){ //arrivo, partenza
   var str1  = d1.value;
   var str2  = d2.value;
   var dt1   = parseInt(str1.substring(0,2),10);
   var mon1  = parseInt(str1.substring(3,5),10);
   var yr1   = parseInt(str1.substring(6,10),10);
   var dt2   = parseInt(str2.substring(0,2),10);
   var mon2  = parseInt(str2.substring(3,5),10);
   var yr2   = parseInt(str2.substring(6,10),10);
   var date1 = new Date(yr1, mon1, dt1);
   var date2 = new Date(yr2, mon2, dt2);
   if(date2 <= date1)   {
      return false;
   }   else   {

      return true;

   }

}
