var weekend = [0,6];
var weekendColor = "#e0e0e0";
var fontface = "Tahoma";
var fontsize = 1;
var nextMM, nextYYYY;
var dias_mes;

var gNow = new Date();

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) 
{
	if ((p_month == null) && (p_year == null))	return;
	
	if (p_month == null) 
	{
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else 
	{
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
}

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 evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are 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 1-D 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) 
	{
		// B A C K W A R D
		if (p_Month == 0) 
		{
			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) 
	{
		// F O R W A R D
		if (p_Month == 11) 
		{
			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;
}

// 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 = "";
	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	nextMM = nextMMYYYY[0];
	nextYYYY = nextMMYYYY[1];
	var vHeader_Code = "";
	var vData_Code = "";
	
	// Begin Table Drawing code here..
	vCode = vCode + "<TABLE class=normal cellspacing=0 cellpadding=0 style='color:black' border=0>";
	vCode = vCode + "<TR height=20px><TD ALIGN=CENTER class='CTitulo'>";
	vCode = vCode + this.gMonthName + " " + this.gYear + "</TD>";
	
	vCode = vCode + "</TR><TR><TD class='CalendarCell' valign=top width='150px'>";
	vCode = vCode + "<TABLE cellspacing=0 cellpadding=0 width=100%>";
	vCode = vCode + "<TR><TD class='HeaderLine'><TABLE cellspacing=1 class='CalendarHeader'>";
	vHeader_Code = this.cal_header();
	vCode = vCode + vHeader_Code;
	vCode = vCode + "</TABLE></TD></TR>"; 
	vCode = vCode + "<TR><TD><TABLE cellspacing=1 class='CalendarTable'>";
	vData_Code = this.cal_data(0);		
	vCode = vCode + vData_Code;
	vCode = vCode + "</TABLE></TD></TR></TABLE></TD></TR></TABLE>"; 
		
	this.wwrite(vCode);
}

Calendar.prototype.wwrite = function(wtext) 
{
	document.writeln(wtext);
}

Calendar.prototype.wwriteA = function(wtext) 
{
	document.write(wtext);
}

Calendar.prototype.cal_header = function() 
{
	var vCode = "";
	
	vCode = vCode + "<TR>";
	vCode = vCode + "<TD align=center WIDTH='14%' class='DayCell'>d</TD>";
	vCode = vCode + "<TD align=center WIDTH='14%' class='DayCell'>s</TD>";
	vCode = vCode + "<TD align=center WIDTH='14%' class='DayCell'>t</TD>";
	vCode = vCode + "<TD align=center WIDTH='14%' class='DayCell'>q</TD>";
	vCode = vCode + "<TD align=center WIDTH='14%' class='DayCell'>q</TD>";
	vCode = vCode + "<TD align=center WIDTH='14%' class='DayCell'>s</TD>";
	vCode = vCode + "<TD align=center WIDTH='16%' class='DayCell'>s</TD>";
	vCode = vCode + "</TR>";
	
	return vCode;
}

Calendar.prototype.format_day = function(temp, vDay) 
{
	var code_aux, w;
	code_aux = "";
	var dia, data;
	
	if(temp == 1 )
	{
		
		for(w=0; w < dias_mes.length; w++)
		{
			dia = dias_mes[w].split(",");
			if (dia[0] == 2)
			{
				if (vDay == dia[1] ) 
				{
					data = (this.gMonth + 1) + "/" + dia[1] + "/" + this.gYear;
					code_aux = "<TD WIDTH='14%' align=center>" + "<A HREF=\"javascript:self.location.href='default.asp?flag=13&data=" + data + "';\" class='LinkDay'>" + vDay + "</A>" + "</TD>";
				}
			}
		}
	}
	else
	{
		for(w=0; w < dias_mes.length; w++)
		{
			dia = dias_mes[w].split(",");
			if (dia[0] == 1)
			{
				if (vDay == dia[1] )
				{
					data = (this.gMonth + 1) + "/" + dia[1] + "/" + this.gYear;
					code_aux = "<TD WIDTH='14%' align=center>" + "<A HREF=\"javascript:self.location.href='default.asp?flag=13&data=" + data + "';\" class='LinkDay'>" + vDay + "</A>" + "</TD>";
				}
			}
		}
	}
	
	if(code_aux == "")
	{
		code_aux = "<TD WIDTH='14%' align=center>" +  vDay + "</TD>";
	}
	return code_aux;
}

Calendar.prototype.cal_data = function(temp) 
{
	
	if(temp == 1)
	{	
		this.gMonth = nextMM;
		this.gyear = nextYYYY;
	
		Calendar.get_month = Calendar_get_month;
		Calendar.get_daysofmonth = Calendar_get_daysofmonth;
		Calendar.calc_month_year = Calendar_calc_month_year;
	}
	else
		temp = 0
		
	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 = "";

	/*
	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 align=center WIDTH='14%'>&nbsp</TD>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) 
	{
		vCode = vCode + this.format_day(temp,vDay);
		
		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++) 
		{
			vCode = vCode + this.format_day(temp,vDay);	
			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++) 
	{
		vCode = vCode + "<TD WIDTH='14%'" +	">&nbsp</TD>";
	}
	return vCode;
}

function show_calendar() 
{
	if (arguments[0] == null)
		p_month = new String(gNow.getMonth());
	else
		p_month = arguments[0];
	
	if (arguments[1] == "" || arguments[1] == null)
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[1];
	
	if (arguments[2] == "" || arguments[2] == null)
		dias_mes = "";
	else
		dias_mes = arguments[2].split(";");

	gCal = new Calendar(p_month, p_year);	
	gCal.show();
}

