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

function diffDays(date1, date2) {
	diff = Math.round((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);

	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=2;
	 	setEndNight();
	}
	return true;
}

function setEndNight() {
	if (!isNaN(Number (document.modulo.nights.value)) ) {
		var dataEnd =  new Date();
		year_ini = dataEnd.getYear();
		
		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);
//		alert(dataStart);
		
		dataEnd = addDays(dataStart, Number(document.modulo.nights.value));
//		dataEnd.setDate(dataStart.getDate() + Number(document.modulo.nights.value));

//		alert(dataEnd);
		
		document.modulo.gf.options[""+dataEnd.getDate()].selected = true;
		document.modulo.mf.options[""+dataEnd.getMonth()].selected = true;
		document.modulo.af.options[""+(dataEnd.getYear()-year_ini)].selected = true;

	} else	{
		document.modulo.nights.value="";
		alert ("Invalid Night number");
	}
	return true;
}

