// JavaScript Document

// chiede conferma relativamente alla cancellazione di un oggetto
function fn_confirm(){
	if (confirm("Procedere con la cancellazione?")){
		return true;
	}else{
		return false;
	}
}

// data una stringa controlla che sia un valido indirizzo email 
function is_email(str) {
	if (window.RegExp) {
    	var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    	var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    	var regnv = new RegExp(nonvalido);
    	var regv = new RegExp(valido);
    	if (!regnv.test(str) && regv.test(str))
      		return true;
    	return false;
	} else {
    	if(str.indexOf("@") >= 0)
      		return true;
    	return false;
  	}
}

// data una stringa controlla che sia un numero
function is_numero(str){
	var nr="1234567890";
	
	// se la stringa è vuota va bene
	if (str=="")
		return true;
		
	for (i=0; i<=(str.length-1); i++)
		if (nr.indexOf(str.charAt(i))==(-1))
			return false;

	return true;		
}

// data una stringa controla che sia una data nel formato gg/mm/aaaa
function is_date(dateStr) {
	//var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
	var datePat = /^(\d{2})(\/)(\d{2})(\/)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		alert("Il formato corretto per la data è gg/mm/aaaa.");
		return false;
	}
	
	// p@rse date into variables
	day 	= matchArray[1];
	month 	= matchArray[3]; 
	year 	= matchArray[5];
	
	if (day < 1 || day > 31) {
		alert("Il giorno deve essere compreso fra 1 e 31.");
		return false;
	}

	if (month < 1 || month > 12) { 
		alert("Il mese deve essere compreso fra 1 e 12..");
		return false;
	}
		
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("30 dì conta Novembre con April Giugno e Settembre, di 28 ce n'è uno tutti gli altri ne han 31!")
		return false;
	}
	
	// check for february 29th
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			alert("Quest'anno non è bisestile.");
			return false;
		}
	}
	return true; // date is valid
}

// controlla se la stringa data in ingresso è nel formato hh:mm
function is_hour(str) {
	var pat = /^(\d{1,2}):(\d{2})?$/;
	var matchArray = str.match(pat); // is the format ok?
	
	if (str=="")
		return true;
		
	if (matchArray == null) {
		alert("Il formato corretto per l'ora è hh:mm.");
		return false;
	}
	
	// parse hour into variables
	hour 	= matchArray[1];
	minute 	= matchArray[2]; 
	
	if (hour < 0  || hour > 23) {
		alert("L'ora deve essere compresa fra 0 e 23.");
		return false;
	}
	if (minute < 0 || minute > 59) {
		alert ("I minuti devono essere compresi fra 0 e 59.");
		return false;
	}
	
	return true;
}
// validazione pagina autenticazione
function fn_check_login(){
	if (document.form.username.value==""){
		alert("Il campo 'USERNAME' è obbligatorio.");
		return false;
	}
	if (document.form.password.value==""){
		alert("Il campo 'PASSWORD' è obbligatorio.");
		return false;
	}

	return true;
}

// validazione form contatti
function fn_check_contatti(){
	if (document.form.cli_nome.value==""){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.form.cli_email.value==""){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (document.form.cli_oggetto.value==""){
		alert("Il campo 'OGGETTO' è obbligatorio.");
		return false;
	}
	if (document.form.cli_note.value==""){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (!document.form.chkPrivacy.checked){
		alert("Il consenso alla 'PRIVACY' è obbligatorio.");
		return false;
	}

	return true;
}

// validazione richiesta informazioni
function fn_check_prenotazione(){
	if (document.form.data_arrivo.value==""){
		alert("Il campo 'DATA ARRIVO' è obbligatorio.");
		return false;
	}
	if (document.form.data_partenza.value==""){
		alert("Il campo 'DATA PARTENZA' è obbligatorio.");
		return false;
	}
	if (!is_date(document.form.data_arrivo.value)||!is_date(document.form.data_partenza.value))
		return false;
	if (document.form.nrCamere.value==""){
		alert("Il campo 'NUMERO DI CAMERE' è obbligatorio.");
		return false;
	}
	if (document.form.nrAdulti.value==""){
		alert("Il campo 'NUMERO DI ADULTI' è obbligatorio.");
		return false;
	}
	if (document.form.nrBambini.value==""){
		alert("Il campo 'NUMERO DI BAMBINI' è obbligatorio.");
		return false;
	}
	if (!is_numero(document.form.nrCamere.value)||!is_numero(document.form.nrAdulti.value)||!is_numero(document.form.nrBambini.value)){
		alert("Il formato dei campi numerici non è corretto.");
		return false;
	}
	if (document.form.cognome.value==""){
		alert("Il campo 'COGNOME' è obbligatorio.");
		return false;
	}
	if ((document.form.tel.value=="")&&(document.form.email.value=="")){
		alert("Specificare almeno un recapito ('TELEFONO' o 'EMAIL')");
		return false;
	}
	if (!is_email(document.form.email.value)){
		alert("Il campo 'EMAIL' non è corretto.)");
		return false;
	}
	/*if (document.form.captcha_usr.value==""){
		alert("Il campo 'SICUREZZA' è obbligatorio.");
		return false;
	}*/
	if (!document.form.chkPrivacy.checked){
		alert("Il consenso alla 'PRIVACY' è obbligatorio.");
		return false;
	}

	return true;
}

// validazione inserimento pacchetto (lato admin)
function fn_check_pack(){
	if ((document.form.titoloIT.value=="")||(document.form.titoloEN.value=="")||(document.form.titoloDE.value=="")||(document.form.titoloES.value=="")||(document.form.titoloFR.value=="")){
		alert("Il campo 'TITOLO' è obbligatorio.");
		return false;
	}
	if ((document.form.testoShortIT.value=="")||(document.form.testoShortEN.value=="")||(document.form.testoShortDE.value=="")||(document.form.testoShortES.value=="")||(document.form.testoShortFR.value=="")){
		alert("Il campo 'ANTEPRIMA' è obbligatorio.");
		return false;
	}
	if ((document.form.testoLongIT.value=="")||(document.form.testoLongEN.value=="")||(document.form.testoLongDE.value=="")||(document.form.testoLongES.value=="")||(document.form.testoLongFR.value=="")){
		alert("Il campo 'TESTO' è obbligatorio.");
		return false;
	}
	
	return true;
}
