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();
        dataStart.setDate(sDayVal);
	dataStart.setMonth(sMonthVal-1);
	dataStart.setYear(sYearVal);
	
	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();
        dataEnd.setDate(sDayVal);
	dataEnd.setMonth(sMonthVal-1);
	dataEnd.setYear(sYearVal);
	if (dataStart<=dataEnd) {
		document.modulo.nights.value=diffDays(dataStart, dataEnd);
	}	
	else {
	 	document.modulo.nights.value=2;
	 	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,12,00,00);

		dataEnd = addDays (dataStart, Number(document.modulo.nights.value));
//		dataEnd.setDate(dataStart.getDate() + Number(document.modulo.nights.value));
		document.modulo.gf.options[dataEnd.getDate()-1].selected = true;
		document.modulo.mf.options[""+dataEnd.getMonth()].selected = true;
		document.modulo.af.options[""+(dataEnd.getYear()-dataStart.getYear())].selected = true;
	}
		else
		{
			alert ("Invalid Night number");
			document.modulo.nights.value="";
			
		}
	return true;
}
	
