function days_between(date1, date2) {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;
    
    // Convert both dates to milliseconds
    var date1_ms = date1.getTime();
    var date2_ms = date2.getTime();
    1
    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY);
}
function DateAdd(mo, dy, yr, days){
	dy = parseInt(dy);
	days = parseInt(days);
    Date1 = new Date(yr, mo, (dy+days));

    D=Date1.getDate();
    M=Date1.getMonth()+1;
    Y=Date1.getYear();

    TheUpDate= D + "/" + M + "/" + Y;

    return TheUpDate;
}
function DateSub(mo, dy, yr, days){

	Date1 = new Date(yr, mo, (dy - days));

	D=Date1.getDate();
    M=Date1.getMonth();
    Y=Date1.getYear();
	TheUpDate= D + "/" + M + "/" + Y;

	return TheUpDate;
}

function adjust_dates(fIn, fOut){
	s = fIn.toString();
	e = fOut.toString();
	s = s.split(",");
	e = e.split(",");
	sd = document.getElementById(s[0]);
	sm = document.getElementById(s[1]);
	sy = document.getElementById(s[2]);
	ed = document.getElementById(e[0]);
	em = document.getElementById(e[1]);
	ey = document.getElementById(e[2]);
	
	vsd = parseInt(sd.options[sd.selectedIndex].value);
	vsm = parseInt(sm.options[sm.selectedIndex].value);
	vsy = parseInt(sy.options[sy.selectedIndex].value);
	ved = parseInt(ed.options[ed.selectedIndex].value);
	vem = parseInt(em.options[em.selectedIndex].value);
	vey = parseInt(ey.options[ey.selectedIndex].value);
	
	today = hToday.split("/");
	format = "d/M/y";
	tomorrow = new Date(today[2],(parseInt(today[1])-1),(parseInt(today[0])+1));
	tomorrow = tomorrow.getDate()+"/"+(tomorrow.getMonth()+1)+"/"+tomorrow.getFullYear();
	tomorrow2 = new Date(vsy,(parseInt(vsm)-1),(parseInt(vsd)));
	tomorrow2 = tomorrow2.getDate()+"/"+(tomorrow2.getMonth()+1)+"/"+tomorrow2.getFullYear();
	sv = compareDates(tomorrow,format,vsd+"/"+vsm+"/"+vsy,format);
	ev = compareDates(tomorrow2,format,ved+"/"+vem+"/"+vey,format);
	tomorrow = tomorrow.split("/");
	tomorrow2 = tomorrow2.split("/");
	if(sv==0){
		for(i=0;i<sd.length;i++)if(parseInt(sd.options[i].value)==parseInt(tomorrow[0]))sd.options[i].selected=true;
		for(i=0;i<sm.length;i++)if(parseInt(sm.options[i].value)==parseInt(tomorrow[1]))sm.options[i].selected=true;
		for(i=0;i<sy.length;i++)if(parseInt(sy.options[i].value)==parseInt(tomorrow[2]))sy.options[i].selected=true;
	}
	if(ev==0){
		for(i=0;i<ed.length;i++)if(parseInt(ed.options[i].value)==parseInt(tomorrow2[0]))ed.options[i].selected=true;
		for(i=0;i<em.length;i++)if(parseInt(em.options[i].value)==parseInt(tomorrow2[1]))em.options[i].selected=true;
		for(i=0;i<ey.length;i++)if(parseInt(ey.options[i].value)==parseInt(tomorrow2[2]))ey.options[i].selected=true;
	}
}

// botar reDoCal(obj, month, year) no onclick como ultima instrução
function reDoCal(obj, month, year){

	x = document.getElementById(obj);
	h = document.getElementById(month);
	h = parseInt(h.value);
	for(i=0;i< x.lenght; i++) x.options[i] = null;
	y = getDaysInMonth((h+1),year);
	for(i=1;i<=y; i++){
		x.options[(i-1)] = new Option(i, (i-1), false);
	}
	
}

function changeCalendar(objs, handler){
	s = objs.toString();
	s = s.split(",");
	dia = document.getElementById(s[0]);
	mes = document.getElementById(s[1]);
	ano = document.getElementById(s[2]);
	document.frames[0].gdCtrl = document.getElementById(handler);
	xdia = dia.options[dia.selectedIndex].value;
	xdia = parseInt(xdia-1);
	xmes = mes.options[mes.selectedIndex].value;
	xano = ano.options[ano.selectedIndex].value;
	//					 alert(xano+"-"+xmes+"-"+(xdia+1));
	document.frames[0].fSetDate(xano,xmes,(xdia+1),true, event);
}