function ricarica_prezzi(Select)
{
	var zona = Select.form.zone.value;
	window.location.href="CaricaPrezzi.php?id=" + Select.value + "&zona=" + zona;
}

function ricarica_strutture_prezzi(Select)
{
	window.location.href="CaricaPrezzi.php?zona=" + Select.value;
}

function PrenotaPrimaDinamic(Select,Array,nomeLivello)
{
	if (Array[Select.value]==0)
	 	nascondiLivello(nomeLivello);
	else
		mostraLivello(nomeLivello);
}

function StrutturaDinamic(Checkbox,Checkbox2,nomeLivello,nomeLivello2)
{
	if (Checkbox.checked)
	{
		mostraLivello(nomeLivello);
		Checkbox2.checked=false;
		nascondiLivello(nomeLivello2);
	}
	else
	{
		nascondiLivello(nomeLivello);
		Checkbox1.checked=true;
		mostraLivello(nomeLivello2);
	}
}

function nascondiLivello(nomeLivello) {

	if (document.layers){ 
    	document.layers[nomeLivello].visibility="hide";
  	} else if (document.getElementById) { 
    	document.getElementById(nomeLivello).style.visibility="hidden";
 }
}

function mostraLivello(nomeLivello) {
 
	if (document.layers){
   		document.layers[nomeLivello].visibility="show";
 	} else if (document.getElementById) {
   		document.getElementById(nomeLivello).style.visibility="visible";
 }
}

function CheckinsertColonna(form){
    var booReturn = true;
    var strMessage = "";
    
    if(form.idstruttura.value == "-1"){
        booReturn = false;
        strMessage = "Attenzione,selezionare prima una struttura";
    }
    
    if(form.ContenutoColonna.value == "") {
        booReturn = false;
        strMessage = "contenuto dati colonna non valido";
    }
    
    if(form.VariazionePrezzo.checked)
    {
    	if ((form.id_variazione.value =="-1") || (form.Variazione.value=""))
    	{
    		booReturn = false;
        	strMessage = "Variazione prezzo non valida";
    	}
    }

     if(booReturn == false){
        alert (strMessage);
    }
    return booReturn;
}

function CheckInsertVariazione(form){
	var booReturn = true;
    var strMessage = "inserire una descrizione valida";
     
    if(form.Descrizione.value == ""){
        booReturn = false;
    }
     if(booReturn == false){
        alert (strMessage);
    }
    return booReturn;
}

function abilitaPrenotaPrima(form)
{
	var ischeck = form.PrenotaPrimaCheck.checked;

		form.PrenotaPrima.disabled = ! ischeck;
		form.Anticipo.disabled = ! ischeck;
}

function abilitaVariazionePrezzo(form)
{
	var ischeck = form.VariazionePrezzo.checked;

		form.id_variazione.disabled = ! ischeck;
		form.TestoVariazione.disabled = ! ischeck;
}

function CheckDatiDisponibilita(form) {

   	var datA = FormatDate(form.dateFrom.value);
   	var datB = FormatDate(form.dateTo.value);
   	var SelectCat = document.getElementById('cat');
	var stato ="";
	var booReturn = true;
	
	if (SelectCat.value==0)
	{
		strMessage = "Selezionare una categoria vallida";
        booReturn = false;
	}
	
	if (form.statopers.disabled==false)
		stato = form.statopers.value;
	else
		if (form.stati.selectedIndex>0)
			stato =  form.stati.value;
	if (stato.length ==0)
	{
		strMessage = "Inserire uno stato valido";
        booReturn = false;
	}

	if (datA.toString() =="Invalid Date" || datB.toString()=="Invalid Date")
	{
		strMessage = "Le date inserite non sono valide " + form.dateFrom.value + " - " + form.dateTo.value;
        booReturn = false;
	}

   if (datA > datB) {
        strMessage = "Le date inserite non sono valide " + form.dateFrom.value + " - " + form.dateTo.value;
        booReturn = false;
   }
   if(booReturn == false){
        alert (strMessage);
   }
   return booReturn;
}

function ScegliTipoStato(form,index)
{
	var flagtipo = index==0 ? true : false;
		form.stati.disabled = ! flagtipo;
		form.statopers.disabled = flagtipo;
}	

function ConfermaCanc(form)
{
	messaggio = window.confirm("Sei sicuro di voler cancellare le colonne selezionate?");
	return messaggio;
}

function controlliPrenotazione(form)
{
	var gg = form.SelPeriodo.value.substring(0,2);
	var mm = form.SelPeriodo.value.substring(3,5);
	var aaaa = form.SelPeriodo.value.substring(6,10);
	var dataSelPeriodo = new Date(aaaa,mm-1,gg);
	var dataSelPeriodoMF = new Date(aaaa,mm-1,gg);
	var dataToday = new Date();

   	var SelectAdulti = document.getElementById('adulti');
   	var SelectBambini = document.getElementById('bambini');
	var eta1 = document.getElementById('eta1');
	var eta2 = document.getElementById('eta2');
	var eta3 = document.getElementById('eta3');
	var eta4 = document.getElementById('eta4');
	var eta5 = document.getElementById('eta5');
  	
	var booReturn = true;
	
	if ((SelectAdulti.selectedIndex==0) && (SelectBambini.selectedIndex==0))
	{
		strMessage = "Numero di adulti/bambini non valido";
        booReturn = false;
	}
	
	if ((SelectBambini.selectedIndex==1) && form.eta1.value=="")
	{
		strMessage = "Devi specificare l'età dei bambini";
        booReturn = false;
	}
	
	if ((SelectBambini.selectedIndex==2) && form.eta2.value=="")
	{
		strMessage = "Devi specificare l'età dei bambini";
        booReturn = false;
	}
	
	if ((SelectBambini.selectedIndex==3) && form.eta3.value=="")
	{
		strMessage = "Devi specificare l'età dei bambini";
        booReturn = false;
	}
	
	if ((SelectBambini.selectedIndex==4) && form.eta4.value=="")
	{
		strMessage = "Devi specificare l'età dei bambini";
        booReturn = false;
	}
	
	if ((SelectBambini.selectedIndex==5) && form.eta5.value=="")
	{
		strMessage = "Devi specificare l'età dei bambini";
        booReturn = false;
	}
	
	if (form.struttura.selectedIndex==0)
   	{
   		strMessage = "Selezionare il tipo di sistemazione";
        booReturn = false;
   	}
   	
   	if (form.zona.selectedIndex==0)
   	{
   		strMessage = "Selezionare la zona";
        booReturn = false;
   	}
	 
   if(dataToday > dataSelPeriodoMF){
		strMessage = "La data di inizio periodo non è valida";
        booReturn = false;
	}
	
   if ((dataToday > dataSelPeriodo)|| (form.SelPeriodo.value=="")) {
        strMessage = "La data di inizio periodo non è valida";
        booReturn = false;
   }
   
   if(booReturn == false){
        alert (strMessage);
   }
   return booReturn;
}

function DettaglioPrenotaz(_id)
{
	var _form = document.getElementById('form3');
	_form.idstruttura.value = _id;
	_form.submit();
}


function calcola_prezzo_prenotazione(Select)
{
	var _form = Select.form;
	var prezzo_selez= parseFloat(Select.value);
	var fattore  = parseInt(_form.fattore.value);
	var adulti = parseInt(_form.adulti.value);
	var bambini = parseInt(_form.bambini.value);
	var giorni = _form.gg.value;
	var totale = 0;
	if (fattore>1)
	{
		totale = prezzo_selez * giorni/7;
	}
	else
	{
		totale = prezzo_selez * giorni * (bambini+adulti);
	}
	_form.totale.value = (parseInt(totale));
}

function elmLoop(theForm){

   for(i=0; i<theForm.elements.length; i++){
   var alertText = ""
   alertText += "Element Type: " + theForm.elements[i].type + "\n"

      if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
      alertText += "Element Value: " + theForm.elements[i].value + "\n"
      }
      else if(theForm.elements[i].type == "checkbox"){
      alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
      }
      else if(theForm.elements[i].type == "select-one"){
      alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
      }
   alert(alertText)
   }

}

function FormatDate(datestring)
{
	var gg = datestring.substring(0,2);
	var mm = datestring.substring(3,5);
	var aaaa = datestring.substring(6,10);
	var resultDate = new Date(aaaa,mm-1,gg);
	return resultDate;
}


