// JavaScript Document
//Inizio funzioni calcolo giorni
function calcolaGiorni(el,f){
	setCampoModificato()
	if(document.getElementById("Giorni") && document.getElementById("Notti")){
		if(el.value==""){
			document.getElementById("Giorni").value = ""
			document.getElementById("Notti").value = ""
			return true
		}
	}
	switch(el.name){
	case "Giorni":
		if(isNaN(el.value)){
			alert("Il campo Giorni deve essere numerico")
			el.focus()
			return true
		}else{
			if(document.getElementById("DataDal").value!=""){
				myDate = document.getElementById("DataDal").value
				myV=myDate.split("/")
				var newData = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				newData = newData.valueOf() + (parseFloat(el.value))
				//document.getElementById("DataAl").value = newData.getDay() + "/" + (parseFloat(newData.getMonth())+1) + "/" + newData.getFullYear()
			}else if(document.getElementById("DataAl").value!=""){
				myDate = document.getElementById("DataAl").value
				myV=myDate.split("/")
				var newData = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				newData = newData.valueOf() - (parseFloat(el.value) * 86400000)
				//document.getElementById("DataDal").value = newData.getDay() + "/" + (parseFloat(newData.getMonth())+1) + "/" + newData.getFullYear()
			}
		}
		break;
	case "Notti":
		if(isNaN(el.value)){
			alert("Il campo Notti deve essere numerico")
			el.focus()
			return true
		}else{
			if(document.getElementById("DataDal").value!=""){
				myDate = document.getElementById("DataDal").value
				myV=myDate.split("/")
				var newData = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				newData = newData.valueOf() + ((parseFloat(el.value)-1) * 86400000)
				//document.getElementById("DataAl").value = newData.getDay() + "/" + (parseFloat(newData.getMonth())+1) + "/" + newData.getFullYear()
			}else if(document.getElementById("DataAl").value!=""){
				myDate = document.getElementById("DataAl").value
				myV=myDate.split("/")
				var newData = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				newData = newData.valueOf() - ((parseFloat(el.value)-1) * 86400000)
				//document.getElementById("DataDal").value = newData.getDay() + "/" + (parseFloat(newData.getMonth())+1) + "/" + newData.getFullYear()
			}
		}
		break;
	case "DataDal":
		if(!isDate(el.value)){
			//alert("Il campo Data Dal non è una data valida")
			//el.focus()
			//return true
		}else{
			if(document.getElementById("DataAl").value!=""){
				/*
				myDate = document.getElementById("DataDal").value
				myV=myDate.split("/")
				if(myV[2].length==2){myV[2] = "20" + myV[2]}
				var myDataDal = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				myDate = document.getElementById("DataAl").value
				myV=myDate.split("/")
				if(myV[2].length==2){myV[2] = "20" + myV[2]}
				var myDataAl = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				gg = Math.floor((myDataAl.getTime()-myDataDal.getTime()) / 86400000)
				*/				
				gg = calcolaGiorniFraDate(document.getElementById("DataDal").value,document.getElementById("DataAl").value)
				document.getElementById("Giorni").value = gg + 1
				document.getElementById("Notti").value = gg
			}
		}
		break;
	case "DataAl":
		if(!isDate(el.value)){
			//alert("Il campo Data Al non è una data valida")
			//el.focus()
			//return true
		}else{
			if(document.getElementById("DataDal").value!=""){
				/*
				myDate = document.getElementById("DataDal").value
				myV=myDate.split("/")
				if(myV[2].length==2){myV[2] = "20" + myV[2]}
				var myDataDal = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				myDate = document.getElementById("DataAl").value
				myV=myDate.split("/")
				if(myV[2].length==2){myV[2] = "20" + myV[2]}
				var myDataAl = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
				gg = Math.floor((myDataAl.getTime()-myDataDal.getTime()) / 86400000)
				*/
				gg = calcolaGiorniFraDate(document.getElementById("DataDal").value,document.getElementById("DataAl").value)
				document.getElementById("Giorni").value = gg + 1
				document.getElementById("Notti").value = gg
			}
		}
		break;
	}
}



function calcolaGiorniFraDate(data_1,data_2) {
	
	tmp_1 = calcolaJulianDate(data_1)
	tmp_2 = calcolaJulianDate(data_2)
	
	return (Math.floor(tmp_2)-Math.floor(tmp_1))
}


function calcolaJulianDate(data){
	greg=1582
	
	tmp = data.split("/")
	
	// parse input
	yy = parseInt(tmp[2],10)
	mm = parseInt(tmp[1],10)
	dd = parseInt(tmp[0],10)
	hh = parseInt("0",10)
	
	if (yy>greg) {
		ly=(!(yy%4)-!(yy%100)+!(yy%400))
	} else {
		ly=!(yy%4)
	}

	// Gregorian changes
	yys = yy + mm/100 + dd/10000
	
	// Let the calculating begin
	if (mm<3) {
		mm += 12
		yy -= 1
	}

	// Gregorian correction
	if (
		(
			greg==1582 && yys >= 1582.1015
		) || (
			greg==1752 && yys >= 1752.0914
		)
	) {
		gc=2-Math.floor(0.01*yy)+Math.floor(0.0025*yy)
	} else {
		gc=0
	}

	jd=Math.floor(365.25*yy)+Math.floor(30.6001*(mm+1))+dd+1720994.5+gc+(hh/24)

	wd=Math.floor(jd+0.5-(7*Math.floor((jd+0.5)/7)))

	if (!isNaN(jd)) {
		return Math.floor(jd*100)/100
	}else{
		return 0
	}
	
}

function getFullDate(D,H){
	tmpD = D.split("/")
	tmpH = H.split(":")
	return new Date(tmpD[2], tmpD[1]-1, tmpD[0], tmpH[0], tmpH[1], 0, 0)
}

function getDate(str){
	myV=str.split("/")
	var myDate = new Date()
	if(myV.length==3){
		if(myV[2].length==2){
			myV[2] = "20" + myV[2]
		}
		if(myV[1]=="08" || myV[1]=="8"){myV[1]=8}
		if(myV[1]=="09" || myV[1]=="9"){myV[1]=9}
		myDate = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
	}
	return myDate
}

function isDate(d){
	myV=d.split("/")
	if(myV.length==3){
		if(myV[2].length==2){
			myV[2] = "20" + myV[2]
		}
		if(myV[1]=="08" || myV[1]=="8"){myV[1]=8}
		if(myV[1]=="09" || myV[1]=="9"){myV[1]=9}
		var myDate = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
		if(parseFloat(myDate.getFullYear())!=parseFloat(myV[2])||parseFloat(myDate.getDate())!=parseFloat(myV[0])||parseFloat(myDate.getMonth())!=parseFloat(myV[1])-1){
			return false;
		}else{
			
			return true;
		}
	}else{
		return false;
	}
}

function formatDate(el,d){
	if(d==""){return}	
	var testData = new RegExp(/^\d{6,8}$/)	
	var testData_1 = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{2}$/)	
	var testData_2 = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{4}$/)
	if(!testData.test(d) && (!testData_1.test(d)) && (!testData_2.test(d))){
		alert(Messaggi.DataNonValida)
		el.focus()
		return
	}
	
	if(testData.test(d)){
		if(d.length==6){
			var secolo = ""
			if(parseInt(d.substring(4,6))<30){
				secolo = "20"
			}else{
				secolo = "19"
			}
			d = d.substring(0,2) + "/" + d.substring(2,4) + "/" + secolo + d.substring(4,6)
		}else if(d.length==8){
			d = d.substring(0,2) + "/" + d.substring(2,4) + "/" + d.substring(4,8)
		}
	}
	
	myV=d.split("/")
	if(myV.length==3){
		if(myV[2].length==2){
			if(parseInt(myV[2])<30){
				myV[2] = "20" + myV[2]
			}else{
				myV[2] = "19" + myV[2]
			}
		}
		if(myV[1]=="08"){myV[1]=8}
		if(myV[1]=="09"){myV[1]=9}
		var myDate = new Date(parseFloat(myV[2]),parseFloat(myV[1])-1,parseFloat(myV[0]));
		if(parseFloat(myV[2])>2075 || parseFloat(myV[2])<1900){
			alert(Messaggi.DataFuoriRange)
			el.focus()
			return
		}else if(parseFloat(myDate.getFullYear())==parseFloat(myV[2])&&parseFloat(myDate.getDate())==parseFloat(myV[0])&&parseFloat(myDate.getMonth())==parseFloat(myV[1])-1){
			giorno = parseFloat(myDate.getDate())
			mese = parseFloat(myDate.getMonth()) + 1
			anno = parseFloat(myDate.getFullYear())
			if(giorno<10){giorno = "0" + giorno}
			if(mese<10){mese = "0" + mese}
			el.value = giorno + "/" + mese + "/" + anno
		}else{
			alert(Messaggi.DataNonValida)
			el.focus()
			return
		}
	}
}



function isDigit (c)
{
  return ((c >= 0) && (c <= 9));
}

function isHourHHMM ( c ) {
	d = c.value;
  
 	if (d.length == 0 ) {
	  return true;
	}
	
	if (d == "0" ) {
		c.value = "00:00";
	}
  
	if ( (d.length == 5) &&
		isDigit( d.charAt(0) ) &&
       	isDigit( d.charAt(1) ) &&
			 ( d.charAt(2) == ":") &&
			 isDigit( d.charAt(3) ) &&
			 isDigit( d.charAt(4) ) ) {
			 var ore =  eval(d.charAt(0) +  d.charAt(1));
			 var minuti =  eval(d.charAt(3) +  d.charAt(4));
			 if (ore < 24){
				if (minuti <60){
					return true;
				}
			 }
	}
	
	if (d.length == 2 && isDigit( d.charAt(0) ) && isDigit( d.charAt(1) ) && eval(d)<24){
		c.value = d + ":00";
	 	return true;
	}
	
	if (d.length == 4 && isDigit( d.charAt(0) ) && isDigit( d.charAt(2) ) && isDigit( d.charAt(3) ) && d.charAt(1) == ":"){
		c.value = "0" + d;
		var minuti =  eval(d.charAt(2) +  d.charAt(3));
		if (minuti < 60){
			return true;
		}
	}
	
	if (d.length == 1 && isDigit( d.charAt(0) ) ){
		c.value = "0"+ d + ":00";
		return true;
	}
	alert("Le ore devono essere nel formato hh:mm");
	c.focus();
	return false;
}


//Fine funzioni calcolo giorni
