// Palazzo Leopoldo EN - JavaScript Document
// +++++++++++++++++++++++++++++++++++++++++

// Controllo formale validità indirizzo E-Mail
function check_email(indirizzo){
  if (!(indirizzo.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/))){
    alert("ATTENTION! You have dialed an E-mail address in a not valid format.");
  	return false;
  } else {
  	return true;
  }
}
// Controllo inserimento E-Mail per Newsletter in box colonna dx
function check_newsletter(){
  var rispondo=false;
  var errore="";
  if (document.getElementById('formHpNewsletter').email.value == ""){
  	errore=errore+"- E-Mail\n";	
  }		
	if (errore){
		alert("ATTENTION! No E-mail address has been dialed.");
		rispondo=false;
	} else {
		rispondo=check_email(document.getElementById('formHpNewsletter').email.value);
	}
	return rispondo;
}
// Controllo iscrizione Newsletter
function checkdatins(){
	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(){
	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;
}
// Controlli box prenotazioni Blastness
function setDate()
{
	var gg = document.getElementById("id_gg");	
	var mm = document.getElementById("id_mm");	
	var aa = document.getElementById("id_aa");	
	var oggi = new Date();
	
	if(gg && mm && aa)
	{
		gg.options[(oggi.getDate()-1)].selected = true;
		mm.options[(oggi.getMonth())].selected = true;
		for(i=0; i<10; i++)
		{
			if(aa.options[i])
			{
				if(aa.options[i].value == oggi.getFullYear()) aa.options[i].selected = true;
			}
		}
	}	
}
function frmcheck()
{
	frm = document.getElementById("myform");
	if (!(frm)) return false;
	
	var oggi = new Date();

	if (parseInt(frm.aa.value) < oggi.getFullYear())
	{
		alert ("ATTENTION: the date is in the past. Please select a date and try again.");
		frm.aa.focus();
		return false;
	}
	if (parseInt(frm.aa.value) == oggi.getFullYear())
	{
		if (parseInt(frm.mm.value) < (parseInt(oggi.getMonth())+1))
		{
			alert ("ATTENTION: the date is in the past. Please select a date and try again.");
			frm.mm.focus();
			return false;
		}
		if (parseInt(frm.mm.value) == (parseInt(oggi.getMonth())+1))
		{
			if (parseInt(frm.gg.value) < parseInt(oggi.getDate()))
			{
				alert ("ATTENTION: the date is in the past. Please select a date and try again.");
				frm.gg.focus();
				return false;
			}
		}
	}
	frm.submit();
}
// 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 dates of your stay are not correctly indicated!")
		return false;
	}
	year = matchArray[1]; // parse date into variables
	month = matchArray[3];
	day = matchArray[4];	
	if (month < 1 || month > 12){ // check month range
		alert("Months should be among 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31){
		alert("Days should be among 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 richiesta disponibilità
function checkbooking(){
  var doc=document.getElementById("disponibilita");
  var data_arrivo = doc.anno_arrivo.value+'-'+doc.mese_arrivo.value+'-'+doc.giorno_arrivo.value;
  var data_partenza = doc.anno_partenza.value+'-'+doc.mese_partenza.value+'-'+doc.giorno_partenza.value;
  var data = new Date();
  if ((data.getDate())<=9){
    var giorno = '0'+data.getDate();
  } else{
    var giorno = data.getDate();  
  }
  if ((data.getMonth())<=8){
    var oggi = data.getFullYear()+'-0'+(parseInt((data.getMonth())+1))+'-'+giorno;
  } else{
    var oggi = data.getFullYear()+'-'+(parseInt((data.getMonth())+1))+'-'+giorno;  
  }
  var errore="";
	var rispondo=false;
	if ((!isValidDate(data_arrivo)) || (!isValidDate(data_partenza))){
		rispondo=false;
	} else if ((data_arrivo)>=(data_partenza)){
		alert("ATTENTION: departure date should be subsequent of arrival date!");
		rispondo=false;
	} else if (data_arrivo<oggi){
    alert("ATTENTION: the selected arrival date is in the past!");
		rispondo=false;
	} else {
  	if (doc.cognome.value == ""){
  		errore=errore+"- Surname\n";	
  	}
  	if (doc.nome.value == ""){
  		errore=errore+"- Name\n";
  	}
  	if (doc.email.value == ""){
  		errore=errore+"- E-Mail\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('disponibilita').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 invio Feedback
function check_feedback(){
	var doc=document.getElementById('feedback');
	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.commento.value == ""){
		errore=errore+"- Comment\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('feedback').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;
}