function muda_mes(m,y)
{
	var mn=['Janeiro','Fevereiro','Mar&ccedil;o','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
	
	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st
	
	var todaydate=new Date() //DD added
	hoje = todaydate.getFullYear() + "" + todaydate.getMonth() + "" + todaydate.getDate();

	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;

	if (m==0) {m = 12; y -= 1; }
	if (m==13) {m = 1; y += 1; }
	var allTDs = document.getElementsByTagName('TD');
	//limpa o mês inteiro
	for(i=0;i<42;i++)
		allTDs.item(i+8).innerHTML = "&nbsp;";
	//o primeiro dia é índice 08
	//verifica o dia da semana do primeiro dia do mês
	var data1 = new Date(y,m-1,1);
	document.frm_busca.dt_form.value = "-" + m + "-" + y;
	var d1 = data1.getDay();
	var inicio = 10 + d1;
	allTDs.item(0).innerHTML = '<img src="imagens/prev.gif" style="cursor:pointer" onclick="muda_mes(' + (m-1) + ',' + y +');" alt="" />';
	allTDs.item(1).innerHTML = mn[m-1] + ' - ' + y;
	allTDs.item(2).innerHTML = '<img src="imagens/next.gif" style="cursor:pointer" onclick="muda_mes('+ (m+1) + ',' + y +');" alt="" />';	
	for (i=inicio; i<inicio+dim[m-1]; i++)
	{
		dia = (i - inicio + 1);
		if ((y + "" + (m-1) + "" + dia) == hoje) //DD added
			x = '<span id="today">' + dia + '</span>'; //DD added
		else
			x = dia;
		allTDs.item(i).innerHTML = x;
		v_x = x + "-" + m + "-" + y;
	}
}

function buildCal(m, y, cM, cH, cDW, cD, brdr)
{
	var mn=['Janeiro','Fevereiro','Mar&ccedil;o','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
	
	var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
	oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st
	
	var todaydate=new Date(); //DD added
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0; //DD added
	
	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	
	document.frm_busca.dt_form.value = "-" + m + "-" + y;
	var t='<div class="'+cM+'"><table id="calend" class="'+cM+'" cols="7" cellpadding="2" border="'+brdr+'" cellspacing="0"><tr align="center" class="'+cH+'">';
	t+='<td width="8px"><img src="imagens/prev.gif" style="cursor:pointer" onclick="muda_mes(' + (m-1) + ',' + y +');" alt="" /></td><td colspan="5" align="center">'+mn[m-1]+' - '+y+'</td><td width="8px"><img src="imagens/next.gif" style="cursor:pointer" onclick="muda_mes('+ (m+1) + ',' + y +');" alt="" /></td></tr><tr align="center">';
	for(s=0;s<7;s++)
		t+='<td class="'+cDW+'">'+"DSTQQSS".substr(s,1)+'</td>';
	t+='</tr><tr align="center">';
	for(i=1;i<=42;i++)
	{
		var x=((i-oD.od>=0)&&(i-oD.od<dim[m-1]))? i-oD.od+1 : '&nbsp;';
		if(x=='&nbsp;') v_x=""; else v_x = x + "-" + m + "-" + y;
		if (x==scanfortoday) //DD added
			x='<span id="today">'+x+'</span>'; //DD added
		t+='<td class="'+cD+'" style=\'cursor:pointer\' onmouseover="this.style.backgroundColor=\'#CCC\';" onmouseout="this.style.backgroundColor=\'#EEF\';" onclick="clica_dia(this.innerHTML);">'+x+'</td>';
		if(((i)%7==0)&&(i<36))
			t+='</tr><tr align="center">';
	}
	return t+='</tr></table></div>';
}

function clica_dia(x)
{
	if (x =='&nbsp;') 
		document.frm_busca.dt_ref.value=''; 
	else 
		if(x.substring(1,5)!="SPAN") 
			document.frm_busca.dt_ref.value = x + document.frm_busca.dt_form.value; 
		else
			if (x.length == 24)
				document.frm_busca.dt_ref.value = x.substring(15,17) + document.frm_busca.dt_form.value;
			else
				document.frm_busca.dt_ref.value = x.substring(15,16) + document.frm_busca.dt_form.value;
}

