isIE = navigator.appName.indexOf("Explorer") >= 0;

function Calendar(psID, piDay, piMonth, piYear) {
	this.browseTo = function(piMonth, piYear) {
		if (this.piMinMonth > piMonth - 1 && this.piMinYear >= piYear) {
			piNewMonth = this.piMinMonth;
			piNewYear = this.piMinYear;
		} else if (this.piMaxMonth < piMonth - 1 && this.piMaxYear <= piYear) {
			piNewMonth = this.piMaxMonth + 1;
			piNewYear = this.piMaxYear;
		} else {
			piNewMonth = piMonth;
			piNewYear = piYear;
		}
	
		this.piDay = 1;
		if (piNewMonth>12) {
			this.piMonth = 0;
			this.piYear = piNewYear + 1;
		} else if (piNewMonth<1) {
			this.piMonth = 11;
			this.piYear = piNewYear - 1;
		} else {
			this.piMonth = piNewMonth - 1;
			this.piYear = piNewYear;
		}

		this.write();
	};
	this.setMinDate = function(piDay, piMonth, piYear) {
		this.piMinDay = piDay;
		this.piMinMonth = piMonth - 1;
		this.piMinYear = piYear;
		this.write();
	}
	this.setMaxDate = function(piDay, piMonth, piYear) {
		this.piMaxDay = piDay;
		this.piMaxMonth = piMonth - 1;
		this.piMaxYear = piYear;
		this.write();
	}
	this.setToday = function(piDay, piMonth, piYear) {
		this.piNowDay = piDay;
		this.piNowMonth = piMonth - 1;
		this.piNowYear = piYear;	
	}
	this.selectDate = function(piMonth, piYear, piDay) {
		this.piSelectedDay = piDay;
		this.piSelectedMonth = piMonth;
		this.piSelectedYear = piYear;
		this.piDay = piDay;
		this.piMonth = piMonth;
		this.piYear = piYear;
		this.write();
	};
	this.clickDate = function(piMonth, piYear, piDay) {
		this.selectDate(piMonth, piYear, piDay);
		calendarAction(piDay, piMonth+1, piYear);
	}
	this.write = function() {
		//html inicial
		sStart = "<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
		
		//html final
		sEnd = "</table>";

		//string do mes
		sMonth = "";
		sMonth += "<tr><td><table width=\"184\" align=\"center\" cellpadding=\"0\"  cellspacing=\"0\" border=\"0\"><tr>";
		sMonth += "<td width=\"4\" align=\"center\">";
		sMonth += "<td width=\"4\" align=\"center\">";
//		LINK PARA DIMINUIR MES
		if ((this.piYear == this.piMinYear && this.piMonth > this.piMinMonth) || (this.piYear > this.piMinYear)) {
			sMonth += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1-1) + ", " + (this.piYear) + "))\"><img src=\"_img/seta_esquerda.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else if (!this.piMinMonth && !this.piMinYear) {
			sMonth += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1-1) + ", " + (this.piYear) + "))\"><img src=\"_img/seta_esquerda.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else {
			sYear += "<img src=\"_img/seta_esquerda2.gif\" width=\"4\" height=\"9\" border=\"0\">";
		}
		sMonth += "</td>";
		sMonth += "<td width=\"111\" align=\"center\" class=\"calendartitle\"><b>" + this.monthLabels[this.piMonth] + "</b></td>";
		sMonth += "<td width=\"4\" align=\"center\">";
//		LINK PARA AUMENTAR MES
		if ((this.piYear == this.piMaxYear && this.piMonth < this.piMaxMonth) || (this.piYear < this.piMaxYear)) {
			sMonth += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1+1) + ", " + (this.piYear) + "))\"><img src=\"_img/seta_direita.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else if (!this.piMaxMonth && !this.piMaxYear) {
			sMonth += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1+1) + ", " + (this.piYear) + "))\"><img src=\"_img/seta_direita.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else {
			sMonth += "<img src=\"_img/seta_direita2.gif\" width=\"4\" height=\"9\" border=\"0\">";

		}
		sMonth += "</td>";
		
		//string do ano
		sYear = "";
		sYear += "<td width=\"20\"> </td>";
		sYear += "<td>";
//		LINK PARA DIMINUIR ANO
		if (this.piYear > this.piMinYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1) + ", " + (this.piYear-1) + "))\"><img src=\"_img/seta_esquerda.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else if (!this.piMinYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1) + ", " + (this.piYear-1) + "))\"><img src=\"_img/seta_esquerda.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else {
			sYear += "<img src=\"_img/seta_esquerda2.gif\" width=\"4\" height=\"9\" border=\"0\">";
		}
		sYear += "</td>";
		sYear += "<td width=\"50\" align=\"center\" class=\"calendartitle\"><b>" + this.piYear + "</b></td>";
		sYear += "<td width=\"4\" align=\"center\">";
//		LINK PARA AUMENTAR ANO
		if (this.piYear < this.piMaxYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1) + ", " + (this.piYear+1) + "))\"><img src=\"_img/seta_direita.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else if (!this.piMaxYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1) + ", " + (this.piYear+1) + "))\"><img src=\"_img/seta_direita.gif\" width=\"4\" height=\"9\" border=\"0\"></a>";
		} else {
			sYear += "<img src=\"_img/seta_direita2.gif\" width=\"4\" height=\"9\" border=\"0\">";
		}
		sYear += "</td>";
		sYear += "<tr><td height=\"15\"> </td></tr>";
		sYear += "</tr></table></td></tr>";

		//string dos dias da semana
		sWeekDay = "";
		sWeekDay += "<tr><td valign=top><table width=\"184\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
		sWeekDay += "<tr>";
		for (liDay = 0; liDay < 7; liDay ++) sWeekDay += "<td align=\"center\"><b>" + this.dayLabels[liDay] + "</b></td>";
		sWeekDay += "</tr>";
		sWeekDay += "<tr><td height=\"10\"> </td></tr>";
	
		//string dos dias
		sWeek = "";
		sWeek += "<tr>";
		for (liDay = 0; liDay < 7; liDay ++) sWeek += "<td><div align=\"center\">#" + liDay + "</div></td>";
		sWeek += "</tr>";
		
		//string hoje
		sToday = "";
		sToday += "</table></td></tr>";		
/*		if (this.piNowDay && this.piNowMonth && this.piNowYear) {
			sToday += "<tr><td><table width=\"100%\" cellpadding=\"0\"  cellspacing=\"0\" border=\"0\">";
			sToday += "<tr><td onclick=\"" + this.id + ".clickDate(" + this.piNowMonth + "," + this.piNowYear + "," + this.piNowDay + ")\" style=\"cursor: pointer\" align=\"right\"><img src=\"imagens/imagens/txt_calendario_hj.gif\" width=\"37\" border=\"0\"></td></tr>";
			sToday += "</table></td></tr>";	
		}*/
		
		iCount = 0;
		iDayCount = 1;
		bStarted = false;
		bMonthProblem = false;
		
		sThisMonth = "";
		for (iWeek = 0; iWeek < 6; iWeek++) {
			sThisWeek = sWeek;	
			for (iWeekDay = 0; iWeekDay < 7; iWeekDay++) {
				dTestDate = new Date(this.piYear, this.piMonth, iDayCount);
				if (dTestDate.getDate() == iDayCount && dTestDate.getDay() == iWeekDay || bStarted) {
					if ((dTestDate.getMonth() == this.piMonth || bMonthProblem) && iDayCount <32) {
						iCount++;
						bStarted = true;
						bMonthProblem = false;
						if ((this.piMinDay > iDayCount && this.piMinMonth >= this.piMonth && this.piMinYear >= this.piYear) || (this.piMaxDay < iDayCount && this.piMaxMonth <= this.piMonth && this.piMaxYear <= this.piYear)) {
							sThisWeek = sThisWeek.replace("#" + iWeekDay, "<span class=\"calendardisabled\">" + iDayCount + "</span>");
						} else if (this.piSelectedDay == iDayCount && this.piSelectedMonth == this.piMonth && this.piSelectedYear == this.piYear) {
							sThisWeek = sThisWeek.replace("#" + iWeekDay, "<a class=\"calendarlink\" href=\"javascript:void(" + this.id + ".clickDate(" + this.piMonth + ", " + this.piYear + ", " + iDayCount + "))\"><b><font class=\"calendarselected\">" + iDayCount + "</font></b></a>");
						} else {
							sThisWeek = sThisWeek.replace("#" + iWeekDay, "<a class=\"calendarlink\" href=\"javascript:void(" + this.id + ".clickDate(" + this.piMonth + ", " + this.piYear + ", " + iDayCount + "))\">" + iDayCount + "</a>");
						}
					} else {
						sThisWeek = sThisWeek.replace("#" + iWeekDay, " ");		
					}
					iDayCount++;
				} else {
					sThisWeek = sThisWeek.replace("#" + iWeekDay, " ");
				}
			}
			sThisMonth += sThisWeek;
			
			if (iWeek == 5 && iCount == 0) {
				iWeek = 0;
				iWeekDay = 0;
				bStarted = true;
				bMonthProblem = true;
				sThisMonth = "";
			}
		}
		
		sHTML = sStart + sMonth + sYear + sWeekDay + sThisMonth + sToday + sEnd;
		
		Div = document.getElementById ? document.getElementById("Calendar_" + this.id) : document.all("Calendar_" + this.id);
		Div.innerHTML = sHTML;
	};
	
	this.id = psID;
	document.write("<div id=\"Calendar_" + this.id + "\"></div>");
	
	this.dayLabels = ["D", "S", "T", "Q", "Q", "S", "S"];
	this.monthLabels = ["JANEIRO", "FEVEREIRO", "MAR&Ccedil;O", "ABRIL", "MAIO", "JUNHO", "JULHO", "AGOSTO", "SETEMBRO", "OUTUBRO", "NOVEMBRO", "DEZEMBRO"];
	
	this.piDay = piDay;
	this.piMonth = piMonth - 1;
	this.piYear = piYear;
	
	this.piSelectedDay = piDay;
	this.piSelectedMonth = piMonth - 1;
	this.piSelectedYear = piYear;
	
	this.piMinDay;
	this.piMinMonth;
	this.piMinYear;
	
	this.piMaxDay;
	this.piMaxMonth;
	this.piMaxYear;
	
	this.piNowDay;
	this.piNowMonth;
	this.piNowYear;
}