isIE = navigator.appName.indexOf("Explorer") >= 0;

function Calendar(psID, pField) {
	//by pedromoraes@gmail.com. use it freely, I can't do anything about it, anyway.

	ldToday = new Date();
	this.browseTo = function(piMonth, piYear, piDay) {
		this.browseDate = new Date(piYear, piMonth, 1);
		this.write();
	};
	this.selectDate = function(piMonth, piYear, piDay) {
		this.selectedDate = new Date(isIE ? piYear : piYear < 1000 ? piYear + 1900 : piYear, piMonth, piDay);
		this.browseDate = this.selectedDate;
		this.write();
		//this.outputField.value = this.formatDate(this.selectedDate);
		this.outputField[0].value = this.formatDate(this.selectedDate).split("/")[0];
		this.outputField[1].value = this.formatDate(this.selectedDate).split("/")[1];
		this.outputField[2].value = this.formatDate(this.selectedDate).split("/")[2];
		this.hide();
	};
	this.formatDate = function(pdDate) {
		liYear = pdDate.getYear();
		if (!isIE) liYear += 1900;
		lsDay = pdDate.getDate() < 10 ? ("0" + pdDate.getDate()) : pdDate.getDate();
		lsMonth = pdDate.getMonth() < 9 ? ("0" + (pdDate.getMonth() + 1)) : (pdDate.getMonth() + 1);
		return(lsDay + "/" + lsMonth + "/" + liYear);
	};
	this.display = function(piTop, piLeft) {
		Div = document.getElementById ? document.getElementById("Calendar_" + this.id) : document.all("Calendar_" + this.id);
		Div.style.display = "block";
	};
	this.hide = function() {
		Div = document.getElementById ? document.getElementById("Calendar_" + this.id) : document.all("Calendar_" + this.id);
		Div.style.display = "none";
	};
	this.write = function() {
		lsHTML = "";
		lsHTML += "<table border=0 width=\"84\" bgcolor=\"white\" class=\"Calendar\" cellpadding=0 cellspacing=0>";
		//Month / Year row
		lsHTML += "<tr><td colspan=\"4\">";
		lsHTML += "<table border=0 cellpadding=0 cellspacing=0 width=\"84\"><tr>";
		lsHTML += "<td align=left class=fieldLabel>&nbsp;<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.browseDate.getMonth() - 1) + ", " + (isIE ? this.browseDate.getYear() : this.browseDate.getYear() + 1900) + "))\" class=\"CalendarNavArrow\">&lt;&lt;</a></td>";
		lsHTML += "<td align=center class=fieldLabel>" + this.monthLabels[this.browseDate.getMonth()] + "</td>";
		lsHTML += "<td align=right class=fieldLabel><a href=\"javascript:void(" + this.id + ".browseTo(" + (this.browseDate.getMonth() + 1) + ", " + (isIE ? this.browseDate.getYear() : this.browseDate.getYear() + 1900) + "))\" class=\"CalendarNavArrow\">&gt;&gt;</a>&nbsp;</td>";
		lsHTML += "</table></td><td colspan=\"3\">";
		lsHTML += "<table border=0 cellpadding=0 cellspacing=0 width=\"62\"><tr>";
		lsHTML += "<td align=left class=fieldLabel>&nbsp;<a href=\"javascript:void(" + this.id + ".browseTo(" + this.browseDate.getMonth() + ", " + ((isIE ? this.browseDate.getYear() : this.browseDate.getYear() + 1900) - 1) + "))\" class=\"CalendarNavArrow\">&lt;&lt;</a></td>";
		lsHTML += "<td align=center class=fieldLabel>" + (this.browseDate.getYear() + (isIE ? 0 : 1900)) + "</td>";
		lsHTML += "<td align=right class=fieldLabel><a href=\"javascript:void(" + this.id + ".browseTo(" + this.browseDate.getMonth() + ", " + ((isIE ? this.browseDate.getYear() : this.browseDate.getYear() + 1900) + 1) + "))\" class=\"CalendarNavArrow\">&gt;&gt;</a>&nbsp;</td>";
		lsHTML += "</table></td></tr>";
		//Days row
		lsHTML += "<tr>";
		for (liDay = 0; liDay < 7; liDay ++) lsHTML += "<td class=\"CalendarHeaderCell\">" + this.dayLabels[liDay] + "</td>";
		lsHTML += "</tr>";
		liDate = 1;
		for (liRow = 0; liRow < 6; liRow ++) {
			lsHTML += "<tr>";
			for (liDay = 0; liDay < 7; liDay ++) {
				ldTestDate = new Date(isIE ? this.browseDate.getYear() : this.browseDate.getYear() + 1900, this.browseDate.getMonth(), liDate);
				if (ldTestDate.getDate() == liDate && ldTestDate.getDay() == liDay) {
					if (this.selectedDate.getYear() == ldTestDate.getYear() && this.selectedDate.getMonth() == ldTestDate.getMonth() && this.selectedDate.getDate() == ldTestDate.getDate()) {
						lsClass = "CalendarDayCellSelDay";
					} else if (ldToday.getYear() == ldTestDate.getYear() && ldToday.getMonth() == ldTestDate.getMonth() && ldToday.getDate() == ldTestDate.getDate()) {
						lsClass = "CalendarDayCellCurDay";
					} else {
						lsClass = "CalendarDayCell";
					}
					lsHTML += "<td onclick=\"" + this.id + ".selectDate(" + ldTestDate.getMonth() + ", " + ldTestDate.getYear() + ", " + ldTestDate.getDate() + ")\" style=\"cursor: pointer\" class=\"" + lsClass + "\">" + liDate + "</td>";
					liDate ++;
				} else if (liRow == 5 && liDay == 6) {
					lsHTML += "<td class=fieldLabel align=right><a href=\"javascript:void(" + this.id + ".hide())\" style='text-decoration:none'>X</a>&nbsp;</td>";
				} else {
					lsHTML += "<td class=CalendarDayCell>&nbsp;</td>";
				}
			}
			lsHTML += "</tr>";
		}
		lsHTML += "</table>";
		Div = document.getElementById ? document.getElementById("Calendar_" + this.id) : document.all("Calendar_" + this.id);
		Div.innerHTML = lsHTML;
	};
	this.id = psID;
	document.write("<div id=\"Calendar_" + this.id + "\" style=\"position: absolute; display: none;\"></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.minYear = ldToday.getYear() + 1900;
	this.maxYear = this.minYear + 5;
	this.outputField = pField;
	//alert(this.outputField[0]);
	if (this.outputField[0].value != "") {
		//laDtParts = this.outputField.value.split("/");
		laDtParts = new Array(this.outputField[0].value, this.outputField[1].value, this.outputField[2].value);
		this.selectDate(Number(laDtParts[1]) - 1, Number(laDtParts[2]), Number(laDtParts[0]));
	} else {
		this.selectDate(ldToday.getMonth(), ldToday.getYear(), ldToday.getDate());
	}
}
