function addDays(date, dayAdd) {
	millisec = (dayAdd-1) * 86400000;
	newTime = date.getTime() + millisec;
	return new Date(newTime);
}

function diffDays(date1, date2) {
	diff = Math.floor((date2.getTime() - date1.getTime())/86400000  );
	return diff;
}
		
function setNumberNight() {
	var sDayVal  = Number(document.modulo.gi[document.modulo.gi.selectedIndex].value);
	var sYearVal = Number(document.modulo.ai[document.modulo.ai.selectedIndex].value);
	var sMonthVal= Number(document.modulo.mi[document.modulo.mi.selectedIndex].value);
	var dataStart =  new Date(sYearVal,sMonthVal-1,sDayVal);
//alert(dataStart);
	
	var sDayVal  = Number(document.modulo.gf[document.modulo.gf.selectedIndex].value);
	var sYearVal = Number(document.modulo.af[document.modulo.af.selectedIndex].value);
	var sMonthVal= Number(document.modulo.mf[document.modulo.mf.selectedIndex].value);
	var dataEnd =  new Date(sYearVal,sMonthVal-1,sDayVal);

	if (dataStart<dataEnd) {
		document.modulo.nights.value=diffDays(dataStart, dataEnd);
	} else {
      document.modulo.nights.value=1;
	 	setEndNight();
	}
	return true;
}

function setEndNight() {
	if (!isNaN(Number (document.modulo.nights.value)) ){
		var dataEnd =  new Date();
		var sDayVal  = Number(document.modulo.gi[document.modulo.gi.selectedIndex].value);
		var sYearVal = Number(document.modulo.ai[document.modulo.ai.selectedIndex].value);
		var sMonthVal= Number(document.modulo.mi[document.modulo.mi.selectedIndex].value);
		var dataStart =  new Date(sYearVal,sMonthVal-1,sDayVal);
	    var sYearini = 2007;
	    
		dataEnd = addDays (dataStart, Number(document.modulo.nights.value));
		document.modulo.gf.options[""+dataEnd.getDate()].selected = true;
		document.modulo.mf.options[""+dataEnd.getMonth()].selected = true;
		//alert(dataEnd.getFullYear()-dataStart.getFullYear());
		//alert(dataStart.getFullYear());
		//alert(dataEnd.getFullYear());
		document.modulo.af.options[""+(dataEnd.getFullYear()-sYearini)].selected = true;
	
	} else {
			document.modulo.nights.value="";
			alert ("Invalid Night number");
		}
	return true;
}
	

