var weekend = [0,6];
var weekendColor = "#A4BAD0";
var fontface = "Verdana, Arial";
var fontsize = 1;
var gMessage = "";
var gForm = "";
gCampo = "";
xPos = "";
yPos = "";
idDiv = "";
var gNow = new Date();
var gArgDate;
var isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
var isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

Calendar.Months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho",
"Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];

// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar( p_month, p_year, p_format) 
{
	if ((p_month == null) && (p_year == null))	return;
	else 
	{
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
	this.gBGColor = "white";
	this.gFGColor = "black";
	this.gTextColor = "black";
	this.gHeaderColor = "black";
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;

function Calendar_get_month(monthNo) 
{
	return Calendar.Months[monthNo];
}

function Calendar_get_daysofmonth(monthNo, p_year) 
{
	/* 
	Check for leap year ..
	1.Years divisible by four and 400 are leap years, except for...
	2.Years divisible by 100 are not leap years 
	*/
	if ((p_year % 4) == 0) 
	{
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];
	
		return Calendar.lDOMonth[monthNo];
	} 
	else	return Calendar.DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) 
{
	/* 
	Will return an array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) 
	{
		if (p_Month == 0) 		// B A C K W A R D
		{
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else 
		{
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} 
	else if (incr == 1) 
	{
		if (p_Month == 11) 		// F O R W A R D
		{
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else 
		{
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} 
	
	return ret_arr;
}

function formataMes(mes){
	return (mes.toString().length < 2) ? "0" + mes : mes;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

Calendar.prototype.show = function() 
{
	var vCode = "";
	
	// Show navigation buttons
	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = parseInt(prevMMYYYY[0]) + 1;
	var prevYYYY = parseInt(prevMMYYYY[1]);

	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = parseInt(nextMMYYYY[0]) + 1;
	var nextYYYY = parseInt(nextMMYYYY[1]);
	
	//inicio da tabela do calendario
	this.wwrite("<body  alink=txVd9Az vlink=txVd9Az link=txVd9Az>");
	this.wwrite("<TABLE BORDER=1 BGCOLOR=#CED7E1 bordercolordark=#0A2046 cellpadding=0 cellspacing=0>");
	this.wwrite("<TR><TD bgcolor=#CED7E1 ALIGN=center colspan=7>");
	this.wwrite("<FONT FACE='" + fontface + "' SIZE=2><B>" + this.gMonthName + " " + this.gYear + "</B></FONT>");
	this.wwrite("</TD></TR><TD bgcolor=#CED7E1 ALIGN=center>");

	gDay = (gArgDate.getDate().toString().length < 2) ? "0" + gArgDate.getDate() : gArgDate.getDate();
	gMonth1 = ((parseInt(this.gMonth) + 1).toString().length < 2)? "0" + (parseInt(this.gMonth) + 1) : (parseInt(this.gMonth) + 1);
	gMonth2 = ((parseInt(gNow.getMonth()) + 1).toString().length < 2)? "0" + (parseInt(gNow.getMonth()) + 1) : (parseInt(gNow.getMonth()) + 1)	

//Ano anterior
	this.wwrite("<FONT size=1 face='" + fontface + "'><A class=txVd9Az HREF=\"" +
		"javascript:dropdownOff(" + "'" + gDay + "/" + gMonth1 + 
		"/" + (parseInt(this.gYear)-1) + "'" + "," + gCampo + ",'" + idDiv + 
		"');dropdownOn(0, " + gForm + "," + gCampo + "," + xPos + "," + yPos + ",'" + idDiv + 
		"');" +	"\">&nbsp;<<&nbsp;<\/A></font></TD><TD bgcolor=#CED7E1 ALIGN=center>");

//Mês anterior
	this.wwrite("<FONT size=1 face='" + fontface + "'><A class=txVd9Az HREF=\"" +
		"javascript:dropdownOff(" + "'" + gDay + "/" + formataMes(prevMM) + "/" + prevYYYY + "'" +
		"," + gCampo + ",'" + idDiv + "');dropdownOn(0, " + gForm + "," + gCampo + "," + xPos + "," + yPos + ",'" + idDiv + "');" +
		"\">&nbsp;<&nbsp;<\/A></font></TD><TD bgcolor=#CED7E1 colspan=3 ALIGN=center>");

//Mês atual
	this.wwrite("<FONT size=1 face='" + fontface + "'><A class=txVd9Az HREF=\"" +
		"javascript:dropdownOff(" + "'" + gDay + "/" + gMonth2 + 
		"/" + parseInt(gNow.getFullYear()) + "'" +	"," + gCampo + ",'" + idDiv + 
		"');dropdownOn(0, " + gForm + "," + gCampo + "," + xPos + "," + yPos + ",'" + idDiv + 
		"');" +	"\">Mês atual<\/A></font></TD><TD bgcolor=#CED7E1 ALIGN=center>");

//Próximo mês
	this.wwrite("<FONT size=1 face='" + fontface + "'><A class=txVd9Az HREF=\"" +
		"javascript:dropdownOff(" + 
		"'" + gDay + "/" + formataMes(nextMM) + "/" + nextYYYY + "'" +
		"," + gCampo + ",'" + idDiv + "');dropdownOn(0, " + gForm + "," + gCampo + "," + xPos + "," + yPos + ",'" + idDiv + "');" +
		"\">&nbsp;>&nbsp;</a><\/A></font></TD><TD bgcolor=#CED7E1 ALIGN=center>");

//Próximo ano
	this.wwrite("<FONT size=1 face='" + fontface + "'><A class=txVd9Az HREF=\"" +
		"javascript:dropdownOff(" + "'" + gDay + "/" + gMonth1 + 
		"/" + (parseInt(this.gYear)+1) + "'" + "," + gCampo + ",'" + idDiv + 
		"');dropdownOn(0, " + gForm + "," + gCampo + "," + xPos + "," + yPos + ",'" + idDiv + 
		"');" +	"\" >&nbsp;>>&nbsp;</a><\/A></font></TD></TR>");

	// Get the complete calendar code for the month..
	var vHeader_Code = "";
	var vData_Code = "";
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	
	vCode = vCode + "</TABLE>";
	this.wwrite(vCode);

	return gMessage;
}

Calendar.prototype.wwrite = function(wtext) {
	gMessage = gMessage + wtext ;
}

Calendar.prototype.cal_header = function() {
	var vCode = "";
	
	vCode = vCode + "<tr>";
	vCode = vCode + "<td height=16 bgcolor='#A4BAD0' class='txVd9AzN'>&nbsp;Dom&nbsp;</td>";
	vCode = vCode + "<td height=16 bgcolor='#CED7E1' class='txVd9AzN'>&nbsp;Seg&nbsp;</td>";
	vCode = vCode + "<td height=16 bgcolor='#CED7E1' class='txVd9AzN'>&nbsp;Ter&nbsp;</td>";
	vCode = vCode + "<td height=16 bgcolor='#CED7E1' class='txVd9AzN'>&nbsp;Qua&nbsp;</td>";
	vCode = vCode + "<td height=16 bgcolor='#CED7E1' class='txVd9AzN'>&nbsp;Qui&nbsp;</td>";
	vCode = vCode + "<td height=16 bgcolor='#CED7E1' class='txVd9AzN'>&nbsp;Sex&nbsp;</td>";
	vCode = vCode + "<td height=16 bgcolor='#A4BAD0' class='txVd9AzN'>&nbsp;Sab&nbsp;</td>";
	vCode = vCode + "</tr>";
	
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";
	var vBackColor = "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/

	vCode = vCode + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD height=16 class=txVd9Az " + this.write_weekend_string(i) + " ALIGN=center>&nbsp;</TD>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		if( vDay == gNow.getDate() && this.gMonth == gNow.getMonth() && this.gYear == gNow.getFullYear())
				vBackColor = " BGCOLOR=6699cc ";
	    else 
			if( vDay == gArgDate.getDate() && this.gMonth == gArgDate.getMonth() && this.gYear == gArgDate.getFullYear())
					vBackColor = " BGCOLOR=FFFFFF ";
			else vBackColor = "";
		vCode = vCode + "<TD height=16 " + vBackColor + this.write_weekend_string(j) + " ALIGN=center><FONT SIZE='1' FACE='" + fontface + "'>" + 
			"<A class=txVd9Az " + 
					" HREF=\"javascript:dropdownOff('" +	this.format_data(vDay) + "'," + gCampo + ", '" + idDiv + "');\">" + vDay
			"</A>" + 
			"</FONT></TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=0; j<7; j++) {
		if( vDay == gNow.getDate() && this.gMonth == gNow.getMonth() && this.gYear == gNow.getFullYear())
				vBackColor = " BGCOLOR=6699cc ";
	    else 
			if( vDay == gArgDate.getDate() && this.gMonth == gArgDate.getMonth() && this.gYear == gArgDate.getFullYear())
					vBackColor = " BGCOLOR=FFFFFF ";
			else vBackColor = "";
			vCode = vCode + "<TD height=16 " + vBackColor + this.write_weekend_string(j) + " ALIGN=center><FONT SIZE='1' FACE='" + fontface + "'>" + 
				"<A class=txVd9Az " + 
					"HREF=\"javascript:dropdownOff('" +	this.format_data(vDay) + "'," + gCampo + ",'" + idDiv + "');\">" + vDay
				"</A>" + 
				"</FONT></TD>";
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++) {
		if (this.gYearly)
			vCode = vCode + "<TD height=16 " + this.write_weekend_string(j+m) + 
			" ALIGN=center><FONT SIZE='1' FACE='" + fontface + "' COLOR='gray'> </FONT></TD>";
		else
			vCode = vCode + "<TD height=16 " + this.write_weekend_string(j+m) + 
			" ALIGN=center><FONT SIZE='1' FACE='" + fontface + "' COLOR='gray'>" + m + "</FONT></TD>";
	}
	
	return vCode;
}

Calendar.prototype.format_day = function(vday) {
	if (vday == gNow.getDate() && this.gMonth == gNow.getMonth() && this.gYear == gNow.getFullYear())
		return ("<FONT COLOR=\"lightgreen\"><B>" + vday + "</B></FONT>");
	else if (vday == gArgDate.getDate() && this.gMonth == gArgDate.getMonth() && this.gYear == gArgDate.getFullYear())
		return ("<FONT COLOR=\"red\"><B>" + vday + "</B></FONT>");
	else return (vday);
}

Calendar.prototype.write_weekend_string = function(vday) {
	var i;
	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i] || vday == weekend[i + 1])
			return (" BGCOLOR=\"" + weekendColor + "\"");
	}
	return "";
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vY4 = new String(this.gYear);
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (this.gFormat) {
		case "DD\/MM\/YYYY" :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
			break;
		default :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
	}
	return vData;
}

function Build( p_month, p_year, p_format) {
	gMessage = "";
	gCal = new Calendar( p_month, p_year, p_format);

	// Customize your Calendar here..
	gCal.gBGColor="blue";
	gCal.gLinkColor="blue";
	gCal.gTextColor="blue";
	gCal.gHeaderColor="blue";

	// Choose appropriate show function
	return (gCal.show());
}

function show_calendar() {
	/* 
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
	*/
	gForm = "document." + arguments[1].name;
	gCampo = gForm + "." + arguments[2].name;
	xPos = arguments[3];
	yPos = arguments[4];
	idDiv = arguments[5];
	
	if( arguments[0] == "" || arguments[0] == null )
	{
		p_month = new String(gNow.getMonth());
		p_year = new String(gNow.getFullYear());
		gArgDate = gNow;
	}
	else if( arguments[0].length >= 8 )
	{
//Convertendo para formato UTC MM/DD/YYYY		
		dateTemp = arguments[0].substr(3,3) + arguments[0].substr(0,3) + arguments[0].substr(6,4);
		gArgDate = new Date(dateTemp);
		if( isNaN(gArgDate))
		{
			alert("Entre com a data no formato 'DD/MM/YYYY'");
			return;
		}
		p_month = new String(gArgDate.getMonth());
		p_year =  new String(gArgDate.getFullYear());
	}
	else
	{
		alert("Entre com a data no formato 'DD/MM/YYYY'");
		return;
	}
	return Build( p_month, p_year, "DD/MM/YYYY");
}

