// JavaScript Document

// chiede conferma relativamente alla cancellazione di un oggetto
function fn_confirm(){
	if (confirm("Procedere con la cancellazione?")){
		return true;
	}else{
		return false;
	};
}
// chiede conferma della ricezione della caparra
function fn_confermaCaparra(){
	if (confirm("Procedere con la conferma di questa prenotazione (la caparra è stata ricevuta)?")){
		return true;
	}else{
		return false;
	};
}
// chiede conferma della ricezione del saldo
function fn_confermaSaldo(){
	if (confirm("Procedere con la conferma di questa prenotazione (il saldo è stato ricevuto)?")){
		return true;
	}else{
		return false;
	};
}
// chiede conferma del pagamento del saldo all'arrivo
function fn_confermaSaldoArrivo(){
	if (confirm("Procedere con la conferma di questa prenotazione (il pagamento del saldo verrà effettuato all'arrivo)?")){
		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 prezzo
function is_prezzo(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 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;		
}

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
}

// mi aspetto due date nel formato gg/mm/aaaa
function fn_confronta_data(data1, data2){
	// trasformo le date nel formato aaaammgg (es. 20081103)
	data1str = data1.substr(6)+data1.substr(3, 2)+data1.substr(0, 2);
	data2str = data2.substr(6)+data2.substr(3, 2)+data2.substr(0, 2);
	
	// controllo che la seconda data sia strettamente successiva alla prima
	// (quindi se sono uguali non va bene)
	if (data2str-data1str<=0)		
		return false;
	else
		return true;
}
function fn_check_disponibilita(){
	if (document.form.data_da.value==''){
		alert("Il campo 'DATA DA' è obbligatorio.");
		return false;
	}
	if (document.form.data_a.value==''){
		alert("Il campo 'DATA A' è obbligatorio.");
		return false;
	}

	if (!is_date(document.form.data_da.value)){
		return false;
	}
	if (!is_date(document.form.data_a.value)){
		return false;
	}
	if (!fn_confronta_data(document.form.data_da.value, document.form.data_a.value)){
		alert("La data di arrivo deve essere precedente alla data di partenza.");
		return false;
	}
	if (document.form.nr_persone.value==''){
		alert("Il campo 'NUMERO DI PERSONE' è obbligatorio.");
		return false;
	}
	if (!is_numero(document.form.nr_persone.value)){
		alert("Il campo 'NUMERO DI PERSONE' deve essere numerico.");
		return false;
	}
	if ( (document.form.nr_persone.value<=0)||(document.form.nr_persone.value>=8) ){
		alert("Il campo 'NUMERO DI PERSONE' deve essere compreso fra 1 e 7, per ogni altra eventuale richiesta si prega di contattare direttamente il residence attraverso il form 'CONTATTI'.");
		return false;
	}
		
	if ((document.form.cmbAppartamento.value==-1)||(document.form.cmbAppartamento.value=='')){
		alert("Il campo 'APPARTAMENTO' è obbligatorio.");
		return false;
	}
	
	return true;
}

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;
}

function fn_check_contatti(){
	if (document.frmContatti.nome.value==''){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.frmContatti.cognome.value==''){
		alert("Il campo 'COGNOME' è obbligatorio.");
		return false;
	}
	if (document.frmContatti.email.value==''){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (!is_email(document.frmContatti.email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}
	if (document.frmContatti.note.value==''){
		alert("Il campo 'NOTE' è obbligatorio.");
		return false;
	}
	if (!document.frmContatti.chkPrivacy.checked){
		alert("Il consenso alla 'PRIVACY' è obbligatorio.");
		return false;
	}

	return true;
}
/* check prenotazione lato utente */
function fn_check_prenota(){
	if (document.frmPrenota.nome.value==''){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.frmPrenota.cognome.value==''){
		alert("Il campo 'COGNOME' è obbligatorio.");
		return false;
	}
	if (document.frmPrenota.email.value==''){
		alert("Il campo 'EMAIL' è obbligatorio.");
		return false;
	}
	if (!is_email(document.frmPrenota.email.value)){
		alert("Il campo 'EMAIL' non è corretto.");
		return false;
	}

	return true;
}

function fn_check_promozione_fissa(){
	if ((document.form.cmbAppartamento.value==-1)||(document.form.cmbAppartamento.value=="")){
		alert("Il campo 'APPARTAMENTO' è obbligatorio.");
		return false;
	}
	if ((document.form.titoloIT.value=='')||(document.form.titoloEN.value=='')||(document.form.titoloDE.value=='')){
		alert("Il campo 'TITOLO' è obbligatorio in tutte le lingue.");
		return false;
	}
	if ((document.form.testoIT.value=='')||(document.form.testoEN.value=='')||(document.form.testoDE.value=='')){
		alert("Il campo 'TESTO' è obbligatorio in tutte le lingue.");
		return false;
	}

	if ((document.form.data_da.value=='')||(document.form.data_a.value=='')){
		alert("I campi 'DATA DA' e 'DATA A' sono obbligatori.");
		return false;
	}
	if ( (!is_date(document.form.data_da.value))||(!is_date(document.form.data_a.value)) ){
		return false;
	}
	if (!fn_confronta_data(document.form.data_da.value, document.form.data_a.value)){
		alert("La data di arrivo deve essere precedente alla data di partenza.");
		return false;
	}

	if (document.form.prezzo.value==''){
		alert("Il campo 'PREZZO' è obbligatorio.");
		return false;
	}	
	if (!is_prezzo(document.form.prezzo.value)){
		alert("Il campo 'PREZZO' deve essere numerico.");
		return false;
	}
	
	return true;
}

function fn_check_promozione_libera(){
	var trovato = false;
	
	if ((document.form.cmbAppartamento.value==-1)||(document.form.cmbAppartamento.value=="")){
		alert("Il campo 'APPARTAMENTO' è obbligatorio.");
		return false;
	}
	if ((document.form.titoloIT.value=='')||(document.form.titoloEN.value=='')||(document.form.titoloDE.value=='')){
		alert("Il campo 'TITOLO' è obbligatorio in tutte le lingue.");
		return false;
	}
	if ((document.form.testoIT.value=='')||(document.form.testoEN.value=='')||(document.form.testoDE.value=='')){
		alert("Il campo 'TESTO' è obbligatorio in tutte le lingue.");
		return false;
	}

	// promozione per 2 notti (se c'è i dati devono essere corretti)
	if ((document.form.data_da_2notti.value!='')&&(document.form.data_a_2notti.value!='')&&(document.form.prezzo_2notti.value!='')){
		if ( (!is_date(document.form.data_da_2notti.value))||(!is_date(document.form.data_a_2notti.value)) ){
			return false;
		}
		if (!fn_confronta_data(document.form.data_da_2notti.value, document.form.data_a_2notti.value)){
			alert("La data di inizio della promozione deve essere precedente alla data di fine (per 2 notti).");
			return false;
		}
		if (!is_prezzo(document.form.prezzo_2notti.value)){
			alert("Il campo 'PREZZO PROMOZIONALE' (per 2 notti) deve essere numerico.");
			return false;
		}
		trovato = true;
	}
	
	// promozione per 3 notti (se c'è i dati devono essere corretti)
	if ((document.form.data_da_3notti.value!='')&&(document.form.data_a_3notti.value!='')&&(document.form.prezzo_3notti.value!='')){
		if ( (!is_date(document.form.data_da_3notti.value))||(!is_date(document.form.data_a_3notti.value)) ){
			return false;
		}
		if (!fn_confronta_data(document.form.data_da_3notti.value, document.form.data_a_3notti.value)){
			alert("La data di inizio della promozione deve essere precedente alla data di fine (per 3 notti).");
			return false;
		}
		if (!is_prezzo(document.form.prezzo_3notti.value)){
			alert("Il campo 'PREZZO PROMOZIONALE' (per 3 notti) deve essere numerico.");
			return false;
		}
		trovato = true;
	}
	
	// promozione per 7 notti (se c'è i dati devono essere corretti)
	if ((document.form.data_da_7notti.value!='')&&(document.form.data_a_7notti.value!='')&&(document.form.prezzo_7notti.value!='')){
		if ( (!is_date(document.form.data_da_7notti.value))||(!is_date(document.form.data_a_7notti.value)) ){
			return false;
		}
		if (!fn_confronta_data(document.form.data_da_7notti.value, document.form.data_a_7notti.value)){
			alert("La data di inizio della promozione deve essere precedente alla data di fine (per 7 notti).");
			return false;
		}
		if (!is_prezzo(document.form.prezzo_7notti.value)){
			alert("Il campo 'PREZZO PROMOZIONALE' (per 7 notti) deve essere numerico.");
			return false;
		}
		trovato = true;
	}

	if (!trovato){
		alert("E' necessario inserire almeno una promozione: per 2, 3 o 7 notti.");
		return false;
	}
	
	return true;
}

function fn_check_prezzo(){
	if ((document.form.cmbAppartamento.value==-1)||(document.form.cmbAppartamento.value=="")){
		alert("Il campo 'APPARTAMENTO' è obbligatorio.");
		return false;
	}
	if (document.form.data_da.value==''){
		alert("Il campo 'DATA DA' è obbligatorio.");
		return false;
	}
	if (document.form.data_a.value==''){
		alert("Il campo 'DATA A' è obbligatorio.");
		return false;
	}
	if (document.form.prz_settimana.value=''){
		alert("Il campo 'PREZZO SETTIMANALE' è obbligatorio.");
		return false;
	}
	if (!is_date(document.form.data_da.value)){
		return false;
	}
	if (!is_date(document.form.data_a.value)){
		return false;
	}
	if (!is_prezzo(document.form.prz_settimana.value)){
		alert("Il campo 'PREZZO SETTIMANALE' deve essere numerico.");
		return false;
	}
	if (!is_prezzo(document.form.prz_2notti.value)){
		alert("Il campo 'PREZZO PER 2 NOTTI' deve essere numerico.");
		return false;
	}
	if (!is_prezzo(document.form.prz_3notti.value)){
		alert("Il campo 'PREZZO PER 3 NOTTI' deve essere numerico.");
		return false;
	}
	
	return true;
}

function fn_check_periodo(){
	var err = "Tutti i campi sono obbligatori.";
	
	if ((document.form.periodo_libero_da01.value=='') || (document.form.periodo_libero_a01.value=='') ||
		 (document.form.periodo_fisso_da.value=='') || (document.form.periodo_fisso_a.value=='') ||
		 (document.form.periodo_libero_da02.value=='') || (document.form.periodo_libero_a02.value=='')) {
		alert(err);
		return false;
	}

	if (!is_date(document.form.periodo_libero_da01.value) || !is_date(document.form.periodo_libero_a01.value) ||
		 !is_date(document.form.periodo_fisso_da.value) || !is_date(document.form.periodo_fisso_a.value) ||
		 !is_date(document.form.periodo_libero_da02.value) || !is_date(document.form.periodo_libero_a02.value)) {
		return false;
	}

	err = "Le date devono essere in ordine cronologico.";
	if (!fn_confronta_data(document.form.periodo_libero_da01.value, document.form.periodo_libero_a01.value)){
		alert(err);
		return false;
	}
	if (!fn_confronta_data(document.form.periodo_fisso_da.value, document.form.periodo_fisso_a.value)){
		alert(err);
		return false;
	}
	if (!fn_confronta_data(document.form.periodo_libero_da02.value, document.form.periodo_libero_a02.value)){
		alert(err);
		return false;
	}
	
	err = "Non ci devono essere 'buchi' fra gli intervalli.";
	if (document.form.periodo_libero_a01.value != document.form.periodo_fisso_da.value){
		alert(err);
		return false;
	}
	if (document.form.periodo_fisso_a.value != document.form.periodo_libero_da02.value){
		alert(err);
		return false;
	}
		
	return true;
}
/* check prenotazione lato amministratore */
function fn_check_prenotazione(){
	if (document.form.nome.value==''){
		alert("Il campo 'NOME' è obbligatorio.");
		return false;
	}
	if (document.form.cognome.value==''){
		alert("Il campo 'COGNOME' è obbligatorio.");
		return false;
	}
	if (document.form.email.value!=''){
		if (!is_email(document.form.email.value)){
			alert("Il campo 'EMAIL' non è corretto.");
			return false;
		}
	}

	if ((document.form.cmbAppartamento.value==-1)||(document.form.cmbAppartamento.value=="")){
		alert("Il campo 'APPARTAMENTO' è obbligatorio.");
		return false;
	}
	if (document.form.data_da.value==''){
		alert("Il campo 'DATA DA' è obbligatorio.");
		return false;
	}
	if (document.form.data_a.value==''){
		alert("Il campo 'DATA A' è obbligatorio.");
		return false;
	}
	if (!is_date(document.form.data_da.value)){
		return false;
	}
	if (!is_date(document.form.data_a.value)){
		return false;
	}
	if (!fn_confronta_data(document.form.data_da.value, document.form.data_a.value)){
		alert("La data di arrivo deve essere precedente alla data di partenza.");
		return false;
	}
	
	return true;
}

// ---------- ---------- AJAX ---------- ----------
function createRequestObject() {   
    var ro;   
    var browser = navigator.appName;   
    if(browser == "Microsoft Internet Explorer"){   
        ro = new ActiveXObject("Microsoft.XMLHTTP");   
    }else{   
        ro = new XMLHttpRequest();   
    }   
    return ro;   
}   
var http = createRequestObject();   

// ----- PER LA GESTIONE DELLA COMBO DEGLI APP.TI IN BASE AL NUMERO DI PERSONE -----
function fn_sndReqAppartamento(nr_persone){
	if(nr_persone>7){
		alert('Il numero massimo di persone è 5, per ogni altra eventuale richiesta si prega di contattare direttamente il residence attraverso la pagina dei CONTATTI.');
	}else{
		http.open('get', 'ajax_cmbAppartamento.php?nr_persone='+nr_persone);   
		http.onreadystatechange = handleResponseAppartamento;   
		http.send(null);   
	}
}
function handleResponseAppartamento() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('appartamento').innerHTML = http.responseText;   
        }   
    }   
}  
// ----- PER LA GESTIONE DEL NOLEGGIO DELLA CULLA -----
function fn_sndReqNoloCulla(chk, dataDa, dataA){
	val_chk = chk?1:0;

	http.open('get', 'ajax_noloCulla.php?nolo='+val_chk+'&dataDa='+dataDa+'&dataA='+dataA);   
	http.onreadystatechange = handleResponseNoloCulla;   
	http.send(null);   
}
function handleResponseNoloCulla() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('nolo_culla').innerHTML = http.responseText;   
        }   
    }   
}  
// ----- PER LA GESTIONE DELLA COLAZIONE -----
function fn_sndReqColazione(chk, dataDa, dataA, nrPersone){
	val_chk = chk?1:0;
	
	http.open('get', 'ajax_colazione.php?colazione='+val_chk+'&dataDa='+dataDa+'&dataA='+dataA+'&nrPersone='+nrPersone);   
	http.onreadystatechange = handleResponseColazione;   
	http.send(null);   
}
function handleResponseColazione() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('colazione').innerHTML = http.responseText;   
        }   
    }   
}  
// ----- PER LA GESTIONE DELLA MEZZA PENSIONE -----
function fn_sndReqMezzaPensione(chk, dataDa, dataA, nrPersone){
	val_chk = chk?1:0;
	
	http.open('get', 'ajax_mezzaPensione.php?mezza_pensione='+val_chk+'&dataDa='+dataDa+'&dataA='+dataA+'&nrPersone='+nrPersone); 
	http.onreadystatechange = handleResponseMezzaPensione;   
	http.send(null);   
}
function handleResponseMezzaPensione() {   
    if(http.readyState == 4){   
        var response = http.responseText;   
        var update = new Array();   
  
        if(response.indexOf('|' != -1)) {   
            update = response.split('|');   
            document.getElementById('mezza_pensione').innerHTML = http.responseText;   
        }   
    }   
}  

