// JavaScript Document

function check_formReservation(formobj)
{
	if (formobj.Cognome.value == ""){alert("Il campo 'Cognome' è obbligatorio.");return false;}
	if (formobj.Nome.value == ""){alert("Il campo 'Nome' è obbligatorio.");return false;}
	if (formobj.Indirizzo.value == ""){alert("Il campo 'Indirizzo' è obbligatorio.");return false;}
	if (formobj.Cap.value == ""){alert("Il campo 'CAP' è obbligatorio.");return false;}
	if (formobj.Comune.value == ""){alert("Il campo 'Comune' è obbligatorio.");return false;}
	if (formobj.Provincia.value == ""){alert("Il campo 'Provincia' è obbligatorio.");return false;}
	
	//Email
	if(formobj.Email.value != "")
	{
		if (formobj.Email.value.indexOf('@', 0) == -1)
		{
			alert("Indirizzo e-mail non valido!");
			return false;
		}
	}
	else
	{
		alert("Il campo 'E-mail' è obbligatorio.");
		return false;
	}
	
	//Telefono
	if(formobj.Telefono.value.length < 5)
	{
		alert("Il campo 'Telefono' è obbligatorio.");
		return false;
	}
	else
	{
		for(i=0; i < formobj.Telefono.value.length; i++)
		{
			var codiceA = formobj.Telefono.value.charCodeAt(i);
		
			if (formobj.Telefono.value.charAt(i)!=" " && codiceA < 48 || codiceA > 57)
			{
				alert("Numero di 'Telefono' non valido.");
				return false;
			}
		}
	}//end Check Telefono

	//Valida la data
	if(validateForm(formobj))
	{
		var dataCalendar =formobj.Data.value;
		var gg = dataCalendar.substr (0, 2);
		var mm = dataCalendar.substr (3, 2);
		var aa = dataCalendar.substr (6, 4);
		var ore =formobj.HH.value;
		var minuti =formobj.MM.value;
		
		//2007,11,20,01,22
		var dataServer = formobj.ServerData.value;
		var as = dataServer.substr (0, 4);
		var ms = dataServer.substr (5, 2);
		var gs = dataServer.substr (8, 2);
		var hs = dataServer.substr (11, 2);
		var mins = dataServer.substr (14, 2);

		var d1 = new Date(aa, (mm-1), gg, ore, minuti);  // Data JCalendar
		var d2 = new Date(as, (ms-1), gs, hs, mins); // data del Server
		
		if(d1<d2){alert("Attenzione, la data di prenotazione non può riferisi al passato.");return false;}
	}
	else {return false};
	
	if (formobj.HH.value == "-"){alert("Il campo 'Ora' è obbligatorio.");return false;}
	if (formobj.Persone.value == ""){alert("Il campo 'Numero Persone' è obbligatorio.");return false;}
	
	if (formobj.Privacy.checked == false){
	alert("Non sono state accettate le condizioni generali del servizio e le politiche sulla privacy");return false;}
	
	//Controlla l'estensione dell'immagine
	//return check_ImageExtension(formobj);
	
	return true;
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=2007;
var maxYear=2020;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Il formato della data deve essere : gg-mm-aaaa")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Inserire una valida data del giorno.")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Inserire una valida data del mese.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Inserire una valida data dell'anno compreso tra "+minYear+" e "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Inserire una valida data.")
		return false
	}
return true
}

function validateForm(formobj)
{
	var dt = formobj.Data;
	if (isDate(dt.value)==false)
	{
		dt.focus()
		return false
	}
    return true
 }


<!--
function check_ImageExtension(formobj)
{
	
	//var fileField = document.getElementById( 'logoFile' );
	var valore = formobj.logoFile.value;
	
	if(valore != "")
	{
		var valsub = valore.substring(valore.indexOf("."));
		if(valsub != ".jpg" && valsub != ".JPG")
		{
			alert("La foto deve avere estensione .jpg"); 
			return false;
		}
	}


	cacheON();
	document.getElementById('Submit').disabled=true;//Disabilita il bottone Invia
	//document.getElementById('res').disabled=true;//Disabilita il bottone Reset
	return true;
}


function calc_Acconto(n_pers, acconto)
{
	
	var tot = (n_pers.value * acconto);
	/*
	var bool = false;
	
	if(selected != -1 && selected != 0)
	{
		bool = true;
	}
	
	//Disattiva o Attiva tutti gli object
	for (i = 0; i < attributo.length; i++)
	{
		document.getElementById(attributo[i]).disabled=bool;
		document.getElementById(attributo[i]).selectedIndex=0;
	}
	
	//Disattiva o Attiva il button Visualizza
	document.getElementById("Visualizza").disabled= !bool;
	*/
	document.getElementById("tot_accontoDiv").innerHTML = "€ " +tot +".00";
}
