var
	objects = new Array()
	
function dateFromDateTime(date)
{
	return(new Date(date.getFullYear(), date.getMonth(), date.getDate()));
}

function daysInMonth(intMonth, intYear)
{
	switch(intMonth + 1)
	{
		case 1: case 3: case 5: case 7: case 8: case 10: case 12: return(31);
		case 4: case 6: case 9: case 11: return(30);
		case 2: return((((intYear - 96) % 4) == 0) ? 29 : 28);
		default: return(-1);
	}
}

function Calendar(name, weekDayLength, colWidth)
{
	this.name = name;
	objects[name] = this;

	this.today = dateFromDateTime(new Date());
	
	this.colWidth = colWidth;
	this.weekDayLength = weekDayLength;
	
	this.show = show;
	this.hide = hide;
	this.nextMonth = nextMonth;
	this.prevMonth = prevMonth;

	this.monthNames = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];
	this.weekDays = ["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sab"];

	function nextMonth()
	{
		this.show(new Date(this.displayDate.getFullYear(), this.displayDate.getMonth() + 1, this.displayDate.getDate()));
	}

	function prevMonth()
	{
		this.show(new Date(this.displayDate.getFullYear(), this.displayDate.getMonth() - 1, this.displayDate.getDate()));
	}

	function hide()
	{
		document.all["calendar"].style.top = -1000;
	}
		
	function show(displayDate, currentDate, top, left)
	{
		this.displayDate = dateFromDateTime(displayDate);
		if(currentDate != null){this.currentDate = dateFromDateTime(currentDate)}
		
		var
			year = this.displayDate.getFullYear(), month = this.displayDate.getMonth(),
			firstWeekDay = (new Date(year, month, 1).getDay()) - 1,
			monthSize = daysInMonth(month, year),
			HTML = "<TABLE ALIGN=\"center\" BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"0\" width=\"140\" bordercolor=\"#7A7A7A\"><TR CLASS=\"headerCenter\" bgcolor=\"#7A7A7A\"><TH style=\"border-bottom-color : #FFFFFF;\"><A CLASS=\"month\" HREF=\"javascript:objects['" + this.name + "'].prevMonth()\">&laquo;</A></TH><TH COLSPAN=\"5\" style=\"color:#FFFFFF; font-family:arial; font-size:11px; border-bottom-color : #FFFFFF;\">" + this.monthNames[month] + " / " + year + "</TH><TH style=\"border-bottom-color : #FFFFFF;\"><A CLASS=\"month\" HREF=\"javascript:objects['" + this.name + "'].nextMonth()\">&raquo;</A></TH></TR><TR CLASS=\"headerCenter\">";


		if ((firstWeekDay + monthSize + 1) <= 35 ) {
			j = 35
		} else {
			j = 42
		}
		
		for(var i = 0; i < this.weekDays.length; i++)			
		{
			HTML += "<TD WIDTH=\"" + this.colWidth + "\" class=\"weekday\" style=\"color:#FFFFFf;\" bgcolor=\"#7A7A7A\" align=\"center\">" + this.weekDays[i].substr(0, 3) + "</TD>";
		}
		HTML += "</TR><TR CLASS=\"normal\">";
		for(var i = 0; i < j; i++)
		{
			if(i % 7 == 0 && i){HTML += "</TR><TR CLASS=\"" + (i % 2 == 0 ? "day" : "day") + "\">";}
			
			if(i > firstWeekDay && (i - 1) < monthSize + firstWeekDay)
			{
				var
					iDate = new Date(year, month, i - firstWeekDay),
					isToday = (iDate - this.today) == 0,
					isCurrent = (iDate - this.currentDate) == 0,
					day = (i < (10 + firstWeekDay) ? "0" : "") + (i - firstWeekDay);
				
				HTML += "<TD" + (isToday ? " bgcolor=\"silver\"" : "") + " bgcolor=\"#F3F3F3\" align=\"center\">";
				HTML += "<A CLASS=\"" + (isCurrent ? "day" : "day") + "\" HREF=\"javascript:abreAtivCalendario('" + day + "/" + (month + 1) + "/" + year + "')\">" + day + "</A>";
				HTML += "</TD>";
			}
			else
			{
				HTML += "<TD><img src=\"/portal/img/dot.gif\" width=\"1\" height=\"1\" alt=\"\" border=\"0\"></TD>";
			}
		}
		HTML += "</TR></TABLE>";
		with(document.all["calendar"])
		{
			innerHTML = HTML;
			if(top != null){style.top = top;}
			if(left != null){style.left = left;}
		}
	}
}