// FUNZIONI GENERALI
// Controllo validità della data
function isValidDate(dateStr){
	// Checks for the following valid date formats:
	// YYYY-MM-DD
	// Also separates date into year, month, and day variables
	var datePat = /^(\d{4})(-)(\d{1,2})\2(\d{1,2})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null){
		alert("The date is not in the format required!")
		return false;
	}
	year = matchArray[1]; // parse date into variables
	month = matchArray[3];
	day = matchArray[4];	
	if (month < 1 || month > 12){ // check month range
		alert("The months must be between 1 and 12!");
		return false;
	}
	if (day < 1 || day > 31){
		alert("The day must be between 1 and 31!");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31){
		alert("The month of "+month+" has not 31 days!")
		return false
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)){
			alert("February " + year + " has not " + day + " days!");
			return false;
   		}
	}
	return true;  // date is valid
}
// Controllo validità indirizzo e-mail
function check_email(indirizzo){
    if (!(indirizzo.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/))){
		alert("WARNING: you entered an e-mail address of an invalid format!");
		return false;
    } else {
		return true;
	}
}
// Controllo visibilità informazioni
function view_info(tagid){
	var el=document.getElementById(tagid);
	el.style.visibility="visible";
}
function no_info(tagid){
	var el=document.getElementById(tagid);
	el.style.visibility="hidden";
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// HOME PAGE
function checkdati_disponibilita(lastday){
	var doc=document.getElementById("disponibilita");
	var errore="";
	var rispondo=false;
	if (doc.data_arrivo.value == ""){
		errore=errore+"- Date of Arrival\n";
	}
	if (doc.data_partenza.value == ""){
		errore=errore+"- Departure Date\n";	
	}
	if (doc.persone.value == ""){
		errore=errore+"- Number of People\n";	
	}
	/*if (doc.appartamenti.value == ""){
		errore=errore+"- N° Appartamenti\n";	
	}*/
	if (errore){
		alert("WARNING: there are data required who are not included!\n\n"+errore);
		rispondo=false;
	} else {
		if ((!isValidDate(doc.data_arrivo.value)) || (!isValidDate(doc.data_partenza.value))){
			rispondo=false;
		} else if ((doc.data_arrivo.value)>=(doc.data_partenza.value)){
			alert("WARNING: The date of departure must be later than that of arrival!");
			rispondo=false;
		} else if (((doc.data_arrivo.value)>lastday) || ((doc.data_partenza.value)>lastday)){
			alert("WARNING: Check in the page of rates that for the period of residence indicated were established prices of the apartments!");
			rispondo=false;
		} else if (((doc.persone.value)<=0) || ((doc.persone.value)>6)){
			alert("WARNING: Do not have any apartment adequate for the number of persons specified!\nPlease read the notice on side before to repeat the search.");
			rispondo=false;
		} else {
			rispondo=true;
		}
	}
	return rispondo;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SEZIONE PRENOTAZIONI
function checkdati_sel_appartamenti(form,box){
	var Ckbox = false;
	for (j= 0; j < form.elements.length; j++) { 
		if (form.elements[j].name == box) {                 
			if(form.elements[j].checked) {
				Ckbox = true;
			}
		}
	}		
	if (Ckbox){
		return true;			
	} else{
		alert("WARNING: to send a reservation request you have to select at least one apartment!");
		return false;
	}
}
function checkdati_prenotazioni(){
	var doc=document.getElementById('modulo_prenotazioni');
	var errore="";
	var rispondo=false;
	if (doc.cod.value == ""){
		if (doc.nome.value == ""){
			errore=errore+"- Name\n";
		}
		if (doc.cognome.value == ""){
			errore=errore+"- Surname\n";	
		}
		if (doc.email.value == ""){
			errore=errore+"- E-Mail\n";	
		}
		if (doc.indirizzo.value == ""){
			errore=errore+"- Address\n";	
		}
		if (doc.cap.value == ""){
			errore=errore+"- Zip Code\n";	
		}
		if (doc.citta.value == ""){
			errore=errore+"- City\n";	
		}
		if (doc.nazione.value == ""){
			errore=errore+"- Country\n";	
		}
		if (doc.tel.value == ""){
			errore=errore+"- Telephone\n";	
		}
		if (doc.persone_tot.value == ""){
			errore=errore+"- Total Persons\n";	
		}
		if (errore){
			// Mancano dei dati
			alert("WARNING: there are data required who are not included!\n\n"+errore);
			rispondo=false;
		} else {
			// Controllo l'e-mail inserita
			rispondo=check_email(doc.email.value);
			if (rispondo){
				// Verifico l'accettazione della privacy
				if (doc.privacy.checked){
					rispondo=true;
				} else {
					alert("WARNING: To complete the request must sign the information for privacy and accept the processing of personal data.\n");
					rispondo=false;
				}
			}
		}
	} else {
		if (doc.persone_tot.value == ""){
			errore=errore+"- Total Persons\n";	
		}
		if (errore){
			// Mancano dei dati
			alert("WARNING: there are data required who are not included!\n\n"+errore);
			rispondo=false;
		} else {
			rispondo=true;
		}
	}
	return rispondo;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SEZIONE NEWSLETTER
// Controllo iscrizione Newsletter
function checkdatins_newsletter(){
	var doc=document.getElementById('modulons');
	var errore="";
	var rispondo=false;
	if (doc.email.value == ""){
		errore=errore+"- E-Mail\n";	
	}
	if (doc.nome.value == ""){
		errore=errore+"- Name\n";
	}
	if (doc.cognome.value == ""){
		errore=errore+"- Surname\n";	
	}
	if (doc.lingua.value == ""){
		errore=errore+"- Language\n";	
	}
	if (doc.privacy.checked){
		if (errore){
			alert("To complete your request you should provide the following information:\n\n"+errore);
			rispondo=false;
		} else {
			rispondo=check_email(document.getElementById('modulons').email.value);
		}
	} else {
		alert("To complete your request you should accept the privacy report and the treatment of your personal data.\n");
		rispondo=false;
	}
	return rispondo;
}
// Controllo cancellazione Newsletter
function checkdatinsdel_newsletter(){
	var doc=document.getElementById('modulodel');
	var errore="";
	var rispondo=false;
	if (doc.e_mail.value == ""){
		errore=errore+"- E-Mail\n";	
	}
	if (errore){
		alert("To forward your request you should dial an E-mail address.");
		rispondo=false;
	} else {
		rispondo=check_email(document.getElementById('modulodel').e_mail.value);
	}
	return rispondo;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SEZIONE CONTATTI
function checkdati_contatti(){
	var doc=document.getElementById('modulo_contatti');
	var errore="";
	var rispondo=false;
	if (doc.nome.value == ""){
		errore=errore+"- Name\n";
	}
	if (doc.cognome.value == ""){
		errore=errore+"- Surname\n";	
	}
	if (doc.email.value == ""){
		errore=errore+"- E-Mail\n";	
	}
	if (doc.contenuto.value == ""){
		errore=errore+"- Messagge\n";	
	}
	if (doc.cod_antispam.value == ""){
		errore=errore+"- Antispam Code\n";	
	}
	if (errore){
		alert("WARNING: there are data required who are not included!\n\n"+errore);
		rispondo=false;
	} else {
		rispondo=check_email(doc.email.value);
	}
	return rispondo;
}