function CaricaMenu(obj) {
	document.all[obj.id].className='sel';
	document.all['a'+obj.id].style.color='#306195';
	document.all['s'+obj.id].style.color='#306195';
	document.all['L'+obj.id].className='nascosta';
	document.all['S'+obj.id].className='visibile';
}

function ApriPopUp(Pgm, NomeWin, Left, Top, Width, Height) {
	window.open(Pgm, NomeWin, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,left="+Left+",top="+Top+",width="+Width+",height="+Height);
}

function Stampa() {
	self.print();
}

function VediFotoPiatti(Foto) {
	ApriPopUp('./FotoPiatti.asp?FF='+Foto, 'FotoPiatti', 0, 0, 400, 400)
}

function CambiaIFrame() {
	//document.all["EventiIFrame"].location.replace("./Immagine_IFrame.asp");
	document.all["EventiIFrame"].src="./Immagine_IFrame.asp";
}

function CambiaCartina(Quale) {
	for (i=1; i<=9; i++) {
		document.all['mappa'+i].className='nascosta';
	}
	document.all['mappa'+Quale].className='visibile'
}
function CambiaCartina1(Quale) {
	for (i=10; i<=19; i++) {
		document.all['mappa'+i].className='nascosta';
	}
	document.all['mappa'+Quale].className='visibile'
}
function CambiaBordi(Obj, Quale) {
	Obj.className='bottoni'+Quale;
}
function EsponiFoto(Quale) {
	for (i=1; i<=4; i++) {
		document.all['foto'+i].className='nascosta';
	}
	document.all['foto'+Quale].className='visibile'
}
function FotoRistorante(Quale) {
	for (i=1; i<=12; i++) {
		document.all['foto'+i].className='nascosta';
	}
	document.all['foto'+Quale].className='visibile'
}
function FotoVineria(Quale) {
	for (i=1; i<=2; i++) {
		document.all['foto'+i].className='nascosta';
	}
	document.all['foto'+Quale].className='visibile'
}
function ControllaGiorno(obj) {
/* controlli formali sulla digitazione del giorno */
	var esito = true;
	var numeri=/^\d+$/;	// uso RegExp per controllare
	var gg = obj.value;

	if (obj.value.length <= 0) {
		return esito;
	}
	if ( gg == 'gg' ) return esito;

	if (!numeri.test(gg)) {
		esito=false;
	} else {
		if ((gg > 31) || (gg < 1)) {
			esito = false;
		}
	}
	if (!esito) {
		alert('Giorno errato');
		try {obj.select(); obj.focus();} catch (e) {}
	} else {
		var temp = obj.value;
		temp=parseInt(temp);
		//obj.gg=temp;
		obj.value+='';
		if (obj.value.length <  2) {
			obj.value = '0'+obj.value;
		}
	}
	return esito;
}

function ControllaMese(obj) {
/* controlli formali sulla digitazione del mese */
	var esito = true;
	var numeri=/^\d+$/;	// uso RegExp per controllare
	var mm = obj.value;


	if (obj.value.length <= 0) {
		return esito;
	}
	if ( mm == 'mm' ) return esito;

	if (!numeri.test(mm)) {
		esito=false;
	} else {
		if ((mm > 12) || (mm < 1)) {
			esito = false;
		}
	}
	if (!esito) {
		alert('Mese errato');
		try {obj.select(); obj.focus();} catch (e) {}
	} else {
		var temp = obj.value;
		temp=parseInt(temp);
		//obj.mm=temp;
		obj.value+='';	
		if (obj.value.length <  2) {
			obj.value = '0'+obj.value;
		}
	}
	return esito;
}

function ControllaAnno(obj) {

/* controlli formali sulla digitazione dell' anno */
	var esito = true;
	var numeri=/^\d+$/;	// uso RegExp per controllare
	var aa = obj.value;
	var yr;

	Today = new Date();
	yr = Today.getFullYear();

	if (obj.value.length <= 0) {
		return esito;
	}
	if (aa == 'aaaa') return esito;

	if (!numeri.test(aa)) {
		esito=false;
	} else {
		if (aa < yr ) {
			esito = false;
		}
	}
	if (!esito) {
		alert('Anno errato');
		try {obj.select(); obj.focus();} catch (e) {}
	} else {
		//obj.aa=obj.value;		
	}
	return esito;
}

function VerificaData(objGG ,objMM, objAAAA) {
	var esito = true;
	if ( (objAAAA.value == 'aaaa') && (objMM.value == 'mm') && (objGG.value == 'gg')) {
		return esito;
	}
	
	var DataI = new Date(objAAAA.value, objMM.value-1, objGG.value, 0, 0, 0, 0);

/* controllo che la data dal  sia una data valida */
	var gg = DataI.getDate();
	var mm = DataI.getMonth();
	var aa = DataI.getFullYear();
	if (gg != objGG.value) esito = false;
	if (mm+1 != objMM.value) esito = false;
	if (aa != objAAAA.value) esito = false;
	if (!esito) {
		try {objGG.focus();} catch (e) {}
		alert('data errata');
	}
	return esito;		
}

function CoerenzaData(objDalGG ,objDalMM, objDalAAAA,objAlGG ,objAlMM, objAlAAAA) {
	var esito=true;

	if ( (objAlAAAA.value == 'aaaa') && (objAlMM.value == 'mm') && (objAlGG.value == 'gg')) {
		return esito;
	}
	var DataI = new Date(objDalAAAA.value, objDalMM.value-1, objDalGG.value, 0, 0, 0, 0);
	var DataF = new Date(objAlAAAA.value, objAlMM.value-1, objAlGG.value, 0, 0, 0, 0);
	if (DataI > DataF) {
		try {objDalGG.focus();} catch (e) {}
		alert("Date non valide");
		esito=false;
	}
	return esito;
}

function VerificaOra(objOra,objMin,objPranzo,objCena) {
	var esito=true;
	if ( (( objOra.value < 17 ) && ( objOra.value > 5 )) && ( objCena.checked ) ){
		alert('La cena è servita dopo le 17');
		return false;
	}
	if ( !(( objOra.value < 17 ) && ( objOra.value > 5 )) && ( objPranzo.checked ) ){
		alert('Il pranzo non è servito dopo le 17');
		return false;
	}
	return esito;
}


function IsMandatory(obj,iniVal) {
	var esito=true;

	if (obj.value.length <= 0) {
		alert (obj.desc + ': Digitazione obbligatoria');
		return false;
	}
	if (obj.value == iniVal ) {
		alert (obj.desc + ': Digitazione obbligatoria');
		return false;
	}

	return esito;		
}

function IsDigit(obj) {
	var esito=true;
	var numeri=/^\d+$/;	// uso RegExp per controllare
	var mm = obj.value;

	if (obj.value.length <= 0) {
		return esito;
	}

	if (!numeri.test(mm)) {
		alert (obj.desc + ': ammessi solo valori numerici');
		esito=false;
	}
	return esito;
}

function IsGreater0(obj) {
	if (obj.value <1) {
		alert (obj.desc + ': digitare valore maggiore di zero');
		return false;
	}
	return true;
}

function IsHour(obj) {
	if (obj.value > 23) {
		alert (obj.desc + ': digitare valore compreso tra 0 e 23');
		return false;
	}
	if (obj.value < 0) {
		alert (obj.desc + ':  digitare valore compreso tra 0 e 23');
		return false;
	}
	return true;
}

function IsMinute(obj) {
	if (obj.value > 59) {
		alert (obj.desc + ':  digitare valore compreso tra 0 e 59');
		return false;
	}
	if (obj.value < 0) {
		alert (obj.desc + ':  digitare valore compreso tra 0 e 59');
		return false;
	}
	return true;
}


function IsEMail(obj) {
	var esito=true;
	var EmailAddr = obj.value;
	var Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

	if (obj.value.length <= 0) {
		return esito;
	}


	if (Filtro.test(EmailAddr))
		return true;
	else
	{
		alert("Controlla l'indirizzo di e-mail inserito");
		esito = false;
	}
	return esito;
}

function ValidaPrenotazione() {
	var esito = true;
	var obj;
	switch (document.all['TYPRE'].value) {
		case 'A' :
				// controllo data
				obj = document.all['ggdal']
				esito = IsMandatory(obj,'gg');
				if (!esito) return false;
				esito = ControllaGiorno(obj);
				if (!esito) return false;

				obj=document.all['mmdal']
				esito = IsMandatory(obj,'mm');
				if (!esito) return false;
				esito = ControllaMese(obj);
				if (!esito) return false;

				obj=document.all['aadal']
				esito = IsMandatory(obj,'aaaa');
				if (!esito) return false;
				esito = ControllaAnno(obj);
				if (!esito) return false;

				esito = VerificaData(document.all['ggdal'],document.all['mmdal'],document.all['aadal']);
				if (!esito) return false;

				obj = document.all['ggal'];
				esito = ControllaGiorno(obj);
				if (!esito) return false;

				obj=document.all['mmal'];
				esito = ControllaMese(obj);
				if (!esito) return false;

				obj=document.all['aaal'];
				esito = ControllaAnno(obj);
				if (!esito) return false;

				esito = VerificaData(document.all['ggal'],document.all['mmal'],document.all['aaal']);
				if (!esito) return false;

				// controllo data dal - al
				esito = CoerenzaData(document.all['ggdal'],document.all['mmdal'],document.all['aadal'],document.all['ggal'],document.all['mmal'],document.all['aaal']);
				if (!esito) return false;
				
				
				// controllo numero persone
				obj=document.all['nper'];
				esito = IsMandatory(obj,'');
				if (!esito) return false;
				esito = IsDigit(obj);
				if (!esito) return false;
				esito = IsGreater0(obj);
				if (!esito) return false;
					
				// controllo email
				obj=document.all['email'];
				esito = IsEMail(obj);
				if (!esito) return false;

				// controllo numero telefonico
				obj=document.all['tele'];
				esito = IsMandatory(obj,'');
				if (!esito) return false;

				break;

		case 'R' :
				// controllo data
				obj = document.all['ggdal'];
				esito = IsMandatory(obj,'gg');
				if (!esito) return false;
				esito = ControllaGiorno(obj);
				if (!esito) return false;

				obj=document.all['mmdal'];
				esito = IsMandatory(obj,'mm');
				if (!esito) return false;
				esito = ControllaMese(obj);
				if (!esito) return false;

				obj=document.all['aadal'];
				esito = IsMandatory(obj,'aaaa');
				if (!esito) return false;
				esito = ControllaAnno(obj);
				if (!esito) return false;

				esito = VerificaData(document.all['ggdal'],document.all['mmdal'],document.all['aadal']);
				if (!esito) return false;

				// controllo numero persone
				obj=document.all['nper'];
				esito = IsMandatory(obj,'');
				if (!esito) return false;
				esito = IsDigit(obj);
				if (!esito) return false;
				esito = IsGreater0(obj);
				if (!esito) return false;

				// controllo ora e cena/pranzo
				obj=document.all['ora'];
				esito = IsMandatory(obj,'hh');
				if (!esito) return false;
				esito = IsDigit(obj);
				if (!esito) return false;
				esito = IsHour(obj);
				if (!esito) return false;

				obj=document.all['min'];
				esito = IsMandatory(obj,'hh');
				if (!esito) return false;
				esito = IsDigit(obj);
				if (!esito) return false;
				esito = IsMinute(obj);
				if (!esito) return false;

				esito = VerificaOra(document.all['ora'],document.all['min'],document.all['pasto'][0],document.all['pasto'][1]);
				if (!esito) return false;
					
				// controllo email
				obj=document.all['email'];
				esito = IsEMail(obj);
				if (!esito) return false;

				// controllo numero telefonico
				obj=document.all['tele'];
				esito = IsMandatory(obj,'');
				if (!esito) return false;
				break;
	}

	document.all['Prenota'].submit();
}
