// Simple Calender Creator
// By Brian Gosselin of http://scriptasylum.com

// Usage:  buildCal(month, year, main_css_classname, header_css_classname, Sat-Sun_css_classname, days_css_classname, border_thickness, starting_day, date_onclick_handler)
//         Pass the result to the document.write() method. ex: document.write( buildCal(<parameters>) );

// Release History:
// V1.0 - Initial Release
// V1.1 - Added revisions made by DynamicDrive.com (changes denoted by the letters "DD").
// V1.2 - Added ability to specify the starting day of the week (Sun, Mon, Tue, etc)
// V1.3 - Added ability to execute a function when a date is clicked. Fixed a minor visual bug in Mozilla.

function buildCal(m, y, brdr, sDay, action){
	var mn=['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'];
	var dim=[31,0,31,30,31,30,31,31,30,31,30,31];
	if(sDay<1 || sDay>7) sDay=1;
  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 tmpod=oD.od-sDay;
	if(tmpod<0)tmpod=7+tmpod;
	oD.od=tmpod+1;
  var wkstr="DSTQQSS".substr(sDay-1,"DSTQQSS".length)+"DSTQQSS".substr(0,sDay-1);
  var todaydate=new Date() //DD
	var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD
	dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;
	var t='<div class="calMAIN"><table class="calMAIN" cellpadding="0" border="'+brdr+'" cellspacing="0">';
	t+='<tr align="center"><td colspan="7" align="center" class="calMONTH">'+mn[m-1]+' - '+y+'</td></tr>';
	t+='<tr align="center">';
	for(s=0;s<7;s++) t+='<td class="calDW">'+wkstr.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]) )? '<div style="width:100%" '+((action!='')? 'onmouseup="'+action+'('+m+','+(i-oD.od+1)+','+y+')':(i-oD.od+1))+'">'+(i-oD.od+1)+'</div>' : '&nbsp;';
		if (i-oD.od+1==scanfortoday)x='<span id="today" class="calDAYS">'+x+'</span>' //DD
		t+='<td width="14.28%" class="calDAYS">'+x+'</td>';
		if(((i)%7==0)&&(i<36))t+='</tr><tr align="center">';
	}
	t+='</tr></table></div>';
	return t;
}