function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
obj.visibility=v; }
}

function altezza_camere(a, h) 
{
	var e=document.getElementById(a);
	var e=document.getElementById(a);
	e.style.height = h;
}	

function mostracamere(valore)
{
	if(valore==1)
	{
		MM_showHideLayers('camera2','','hide','camera3','','hide','camera4','','hide');
		altezza_camere('contenitore_camere','20px');
	}
	if(valore==2)
	{
		MM_showHideLayers('camera2','','show','camera3','','hide','camera4','','hide');
		altezza_camere('contenitore_camere','40px');
	}
	if(valore==3)
	{
		MM_showHideLayers('camera2','','show','camera3','','show','camera4','','hide');
		altezza_camere('contenitore_camere','60px');
	}
	if(valore==4)
	{
		MM_showHideLayers('camera2','','show','camera3','','show','camera4','','show');
		altezza_camere('contenitore_camere','80px');
	}
}


function checkDate(dal, al) {
/* @param data: DD/MM/YYYY */

	if ((!dal) || (!al))
	{
		alert('Periodo di soggiorno non compilato correttamente');
		return false;
	}

	var data1 = dal.split('/');
	var data2 = al.split('/');

	var giorno1 = data1[0];
	var giorno2 = data2[0]; 
	
	var mese1 = data1[1];
	var mese2 = data2[1];
	
	var anno1 = data1[2];
	var anno2 = data2[2];

	//trasformo le date nel formato aaaammgg
	data1str = anno1+mese1+giorno1;
	data2str = anno2+mese2+giorno2;
	//controllo se la seconda data è successiva alla prima
    if (data2str-data1str<=0) 
	{
		alert('Il periodo di soggiorno inserito non è valido.\nDevi inserire almeno una notte');
		return false;
	}
	return true;
}

function aggiorna_numero_bambini(index,sel) 
{
	camera=document.getElementById('allestimento_camera'+index).options[document.getElementById('allestimento_camera'+index).selectedIndex].value;

	while (document.getElementById('bambini_camera'+index).length > 1)
		document.getElementById('bambini_camera'+index).options[document.getElementById('bambini_camera'+index).options.length - 1] = null;

	var posti = camera.split("_"); 
	
	for (i=0; i < posti[1]; i++)
	{
		document.getElementById('bambini_camera'+index).options[i] = new Option (i,i);
		if (i== sel)
			document.getElementById('bambini_camera'+index).options[i].selected = true;
	}
}	
	
function submitform5()
{
 	if (document.getElementById('privacy_accetta').checked == false)
 	{
		alert('Devi leggere ed accettare l\'informativa sulla privacy prima di continuare');
		return;			
 	}
	if (document.getElementById('policy_accetta').checked == false)
 	{
		alert('Devi leggere ed accettare il regolamento della policy prima di continuare');
		return;			
 	}
	document.conferma.submit();
}

function submitform4()
{
	for (var i=0,objs=document.altri_partecipanti.elements;i<objs.length;i++)
		if(objs[i].value=="")
		{
			alert('Devi compilare tutti i campi');
			objs[i].focus();
			return;			
		}

	document.altri_partecipanti.submit();
}

function submitform3()
{
	if (document.dati_contraente.cognome.value == "")
	{
		alert('Devi compilare il campo COGNOME');
		document.dati_contraente.cognome.focus();
		return;
	}	
	if (document.dati_contraente.nome.value == "")
	{
		alert('Devi compilare il campo NOME');
		document.dati_contraente.nome.focus();
		return;
	}	
	if ((document.dati_contraente.prov_nascita.value == "0") ||
		(document.dati_contraente.com_nascita.value == ""))
	{
		alert('Devi scegliere la PROVINCIA ed il COMUNE di nascita');
		return;
	}	
	if (document.dati_contraente.data_nascita.value == "")
	{
		alert('Devi compilare il campo DATA DI NASCITA');
		document.dati_contraente.data_nascita.focus();
		return;
	}	
	if (document.dati_contraente.indirizzo.value == "")
	{
		alert('Devi compilare il campo INDIRIZZO');
		document.dati_contraente.indirizzo.focus();
		return;
	}	
	if ((document.dati_contraente.prov_residenza.value == "0") ||
		(document.dati_contraente.com_residenza.value == ""))
	{
		alert('Devi scegliere la PROVINCIA ed il COMUNE di residenza');
		return;
	}	
	if (document.dati_contraente.telefono.value == "")
	{
		alert('Devi specificare un RECAPITO TELEFONICO');
		document.dati_contraente.telefono.focus();
		return;
	}
	else
	{
		var checkOK = "0123456789";
  		var checkStr = document.dati_contraente.telefono.value;
  		for (i = 0; i < checkStr.length; i++)
		{
    		ch = checkStr.charAt(i);
    		for (j = 0; j < checkOK.length; j++)
      			if (ch == checkOK.charAt(j))
        			break;
    		if (j == checkOK.length)
		    {
				alert('RECAPITO TELEFONICO non valido. Inserire solo caratteri numeri (Es.: 3331234567)');
				document.dati_contraente.telefono.focus();
				return;
    		}
  		}
	}	
	if (document.dati_contraente.mail.value == "")
	{
		alert('Devi specificare un INDIRIZZO EMAIL');
		document.dati_contraente.mail.focus();
		return;
	}

	email_inserita=document.dati_contraente.mail.value;
	formacorretta = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if(!formacorretta.test(email_inserita))
	{
		alert('Hai specificato un INDIRIZZO EMAIL non valido');
		document.dati_contraente.mail.focus();
		return;
	}

		
	if (document.dati_contraente.cod_fiscale.value == "")
	{
		alert('Devi compilare il campo CODICE FISCALE');
		document.dati_contraente.cod_fiscale.focus();
		return;
	}	
	else
	{
		str= document.dati_contraente.cod_fiscale.value;
		if (str.length != 16)
		{
			alert('CODICE FISCALE non valido');
			document.dati_contraente.cod_fiscale.focus();
			return;
		}
	}	

	if (document.dati_contraente.id_camera_contraente.value == "0")
	{
		alert('Scegli in quale camera soggiorna il contraente');
		document.dati_contraente.id_camera_contraente.focus();
		return;
	}	

	document.dati_contraente.submit();
}

function submitform2()
{
 	if (checkMatrixForm(document.scelta_camere))
  		document.scelta_camere.submit();
}

function submitform1()
{
	var dal = document.prenotazione.data_dal.value;
	var al = document.prenotazione.data_al.value;

	if (checkDate(dal,al))
  		document.prenotazione.submit();
}

function submitformback()
{
	document.getElementById('step').value -=2;
	document.forms[0].submit();
}

// da qui iniziano quelli fatti bene :)

function submit_mese()
{
	document.mese.submit();
}

function submit_prenotazione(azione)
{
	document.getElementById('azione').value = azione;
	document.prenotazione.submit();
}
function submit_contraente()
{
	document.contraente.submit();
}

function aggiorna_bambini_admin(posti_max, adulti, sel) 
{
	while (document.getElementById('num_bambini').length > 1)
		document.getElementById('num_bambini').options[document.getElementById('num_bambini').options.length - 1] = null;
	document.getElementById('num_bambini').options[0] = new Option (0,0);

	for (i=1; i<=(posti_max-adulti); i++)
	{
		document.getElementById('num_bambini').options[i] = new Option (i,i);
		if (i== sel)
			document.getElementById('num_bambini').options[i].selected = true;
	}
		
}	
	
function submit_modifica_indisp(index)
{
	var dal = document.getElementById('data_dal_'+index).value;
	var al = document.getElementById('data_al_'+index).value;

	if (checkDate(dal,al))
  		document.getElementById('indisp_'+index).submit();
}

function submit_blocca()
{
	var dal = document.preventivo.data_dal.value;
	var al = document.preventivo.data_al.value;

	if (checkDate(dal,al))
	{
  		document.preventivo.blocca.value = "1";
  		document.preventivo.submit();
	}
}

function submit_prenota()
{
	var dal = document.preventivo.data_dal.value;
	var al = document.preventivo.data_al.value;

	if ((document.preventivo.num_adulti.value + document.preventivo.num_bambini.value) == 0)
	{
		alert('Non hai specificato il numero di adulti e/o bambini');
		return;
	}	

	if (checkDate(dal,al))
	{
  		document.preventivo.prenota.value = "1";
  		document.preventivo.submit();
	}
}

function submit_preventivo()
{
	var dal = document.preventivo.data_dal.value;
	var al = document.preventivo.data_al.value;

	if ((document.preventivo.num_adulti.value + document.preventivo.num_bambini.value) == 0)
	{
		alert('Non hai specificato il numero di adulti e/o bambini');
		return;
	}	

	if (checkDate(dal,al))
	{
  		document.preventivo.prev.value = "1";
  		document.preventivo.submit();
	}
}

function submit_pagamento()
{
		var checkOK = "0123456789,";
  		var checkStr = document.pagamento.importo_acconto.value;
  		for (i = 0; i < checkStr.length; i++)
		{
    		ch = checkStr.charAt(i);
    		for (j = 0; j < checkOK.length; j++)
      			if (ch == checkOK.charAt(j))
        			break;
    		if (j == checkOK.length)
		    {
				alert('Importo pagamento non valido. Inserire solo numeri (Es.: 3331234567)');
				document.pagamento.importo_acconto.focus();
				return;
    		}
  		}
		document.pagamento.submit();
}
function submit_saldo()
{
	if (document.saldo.data_dal.value == "")
	{
		alert('Devi compilare il campo DATA DI PAGAMENTO');
		document.saldo.data_dal.focus();
		return;
	}
	
	document.saldo.submit();
}

function print_fattura()
{
	var a = window.open('','','width=700,height=900');
    a.document.open("text/html");
    a.document.write('<html><head><link rel="stylesheet" href="css/style.css" />');
    a.document.write('<link rel="stylesheet" href="css/text.css" /></head><body>');
    a.document.write(document.getElementById('area_stampa_fattura').innerHTML);
    a.document.write('</body></html>');
    a.document.close();
    a.print();

	document.fattura.submit();
}

function controllo_fattura()
{
	if (document.fattura.id_contraente.value == "0")
	{
		alert('Devi inserire i dati del contraente');
		document.saldo.data_dal.focus();
		return;
	}

	document.fattura.submit();

}	

function replace_comma(campo)
{
	campo = campo.replace(/\,/g, ".");
	var valore = parseFloat(campo);
	return valore;
}

function aggiorna_totale_fattura()
{
	totale = 0;
	if (document.getElementById('tipo_prodotti').value == "0")
    {
    	if (document.getElementById('totale_soggiorno').value != "")
    		totale += document.getElementById('totale_soggiorno').value / 100;
    	if (document.getElementById('sconti').value != "")
    	{
    		v = replace_comma(document.getElementById('sconti').value);
    		totale -= v;
    		v = v.toFixed(2);
    		document.getElementById('sconti').value = v.replace(".",",");
    	}
    	if (document.getElementById('supplementi').value != "")
    	{
    		v = replace_comma(document.getElementById('supplementi').value);
    		totale += v;
    		v = v.toFixed(2);
    		document.getElementById('supplementi').value = v.replace(".",",");
    	}
    	if (document.getElementById('varie').value != "")
    	{
    		v = replace_comma(document.getElementById('varie').value);
    		totale += v;
    		v = v.toFixed(2);
    		document.getElementById('varie').value = v.replace(".",",");
    	}
    }
    else
    {
     	for (i = 0; i < 10; i++)
        {
     		totale += replace_comma(document.getElementById('totale_costo_prodotto' + i).value);
        	if (document.getElementById('sconto_'+i).value != "")
        	{
        		v = replace_comma(document.getElementById('sconto_'+i).value);
        		totale -= v;
        		v = v.toFixed(2);
        		document.getElementById('sconto_'+i).value = v.replace(".",",");
        	}
         }
    }
    			 
	totale = totale.toFixed(2);
	document.getElementById('totale_fattura').value = totale.replace(".",",");
}
function totale_prodotto(i)
{
	totale = document.getElementById('qty_'+i).value * document.getElementById('prezzo_'+i).value;
	totale /= 100;
	totale = totale.toFixed(2);
	document.getElementById('totale_costo_prodotto'+i).value = totale.replace(".",",");

	//	aggiorna_totale_ordine();
	//aggiorna_totale_fattura()
	if (document.getElementById('provenienza').value == "fattura")
		aggiorna_totale_fattura();
	if (document.getElementById('provenienza').value == "ordine")
		aggiorna_totale_ordine();
}

function aggiorna_totale_ordine()
{
	totale = 0;

 	for (i = 0; i < 10; i++)
 		totale += replace_comma(document.getElementById('totale_costo_prodotto' + i).value);
			 
	totale = totale.toFixed(2);
	document.getElementById('totale_ordine').value = totale.replace(".",",");
}
function submit_ordine()
{
	product_found = 0;
 	for (i = 0; i < 10; i++)
 	{
 		if (document.getElementById('prodotto_' + i).value != "0")
 		{
			product_found = 1;
 			if ((document.getElementById('qty_' + i).value == "") ||
			 	(document.getElementById('qty_' + i).value < 1))
			{
				alert('Devi specificare la quantità per i prodotti selezionati');
				document.getElementById('qty_' + i).focus();
				return;
			}	
				  
 		}
	}

	if (!product_found) // nessun prodotto selezionato
	{
		alert('Devi selezionare almeno un prodotto');
		document.getElementById('prodotto_0').focus();
		return;
	}	



	document.ordine.submit();
}
function submit_ordine_2()
{
	if (document.dati_acquirente.cognome.value == "")
	{
		alert('Devi compilare il campo COGNOME');
		document.dati_acquirente.cognome.focus();
		return;
	}	
	if (document.dati_acquirente.nome.value == "")
	{
		alert('Devi compilare il campo NOME');
		document.dati_acquirente.nome.focus();
		return;
	}	
	if ((document.dati_acquirente.prov_nascita.value == "0") ||
		(document.dati_acquirente.com_nascita.value == ""))
	{
		alert('Devi scegliere la PROVINCIA ed il COMUNE di nascita');
		return;
	}	
	if (document.dati_acquirente.data_nascita.value == "")
	{
		alert('Devi compilare il campo DATA DI NASCITA');
		document.dati_acquirente.data_nascita.focus();
		return;
	}	
	if (document.dati_acquirente.indirizzo.value == "")
	{
		alert('Devi compilare il campo INDIRIZZO');
		document.dati_acquirente.indirizzo.focus();
		return;
	}	
	if ((document.dati_acquirente.prov_residenza.value == "0") ||
		(document.dati_acquirente.com_residenza.value == ""))
	{
		alert('Devi scegliere la PROVINCIA ed il COMUNE di residenza');
		return;
	}	
	if (document.dati_acquirente.telefono.value == "")
	{
		alert('Devi specificare un RECAPITO TELEFONICO');
		document.dati_acquirente.telefono.focus();
		return;
	}
	else
	{
		var checkOK = "0123456789";
  		var checkStr = document.dati_acquirente.telefono.value;
  		for (i = 0; i < checkStr.length; i++)
		{
    		ch = checkStr.charAt(i);
    		for (j = 0; j < checkOK.length; j++)
      			if (ch == checkOK.charAt(j))
        			break;
    		if (j == checkOK.length)
		    {
				alert('RECAPITO TELEFONICO non valido. Inserire solo caratteri numeri (Es.: 3331234567)');
				document.dati_acquirente.telefono.focus();
				return;
    		}
  		}
	}	
	if (document.dati_acquirente.mail.value == "")
	{
		alert('Devi specificare un INDIRIZZO EMAIL');
		document.dati_acquirente.mail.focus();
		return;
	}

	email_inserita=document.dati_acquirente.mail.value;
	formacorretta = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if(!formacorretta.test(email_inserita))
	{
		alert('Hai specificato un INDIRIZZO EMAIL non valido');
		document.dati_acquirente.mail.focus();
		return;
	}

		
	if (document.dati_acquirente.cod_fiscale.value == "")
	{
		alert('Devi compilare il campo CODICE FISCALE');
		document.dati_acquirente.cod_fiscale.focus();
		return;
	}	
	else
	{
		str= document.dati_acquirente.cod_fiscale.value;
		if (str.length != 16)
		{
			alert('CODICE FISCALE non valido');
			document.dati_acquirente.cod_fiscale.focus();
			return;
		}
	}	

	document.dati_acquirente.submit();
}

function submit_ordine_3()
{
 	if (document.getElementById('privacy_accetta').checked == false)
 	{
		alert('Devi leggere ed accettare l\'informativa sulla privacy prima di continuare');
		return;			
 	}
	document.conferma.submit();
}
