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.remarcaData = function(piDay, piMonth, piYear) {
    if ((d.getElementById('dtPartida') != 'undefined' || d.getElementById('dtPartida') != null) && (d.getElementById('dtRetorno') != 'undefined' || d.getElementById('dtRetorno') != null)) {
      this.selectDate(piMonth, piYear, piDay);
    }
  };
 
	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 width=\"203\" height=\"176\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"border: #B7BB20 1px solid\">";
		
		//html final
		sEnd = "</table>";

		//string do ano
		sYear = ""; 
		sYear += "<tr><td height=\"23\" class=\"font_branca07_bgCinza\"><table width=\"184\" align=\"center\" cellpadding=\"0\"  cellspacing=\"0\"><tr>";
		sYear += "<td width=\"10\"><div align=\"left\">";
//		LINK PARA DIMINUIR ANO
		if (this.piYear > this.piMinYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1+1) + ", " + (this.piYear-1) + "))\"><img src=\"img_site/seta_branca_left.gif\" width=\"8\" height=\"8\" border=\"0\"></a>";
		} else if (!this.piMinYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1+1) + ", " + (this.piYear-1) + "))\"><img src=\"img_site/seta_branca_left.gif\" width=\"8\" height=\"8\" border=\"0\"></a>";
		}
		sYear += "</div></td>";
		sYear += "<td width=\"161\"><div align=\"center\" class=\"font_branca08_bold\">" + this.piYear + "</div></td>";
		sYear += "<td width=\"11\"><div align=\"right\">";
//		LINK PARA AUMENTAR ANO
		if (this.piYear < this.piMaxYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1+1) + ", " + (this.piYear+1) + "))\"><img src=\"img_site/seta_branca_right.gif\" width=\"8\" height=\"8\" border=\"0\"></a>";
		} else if (!this.piMaxYear) {
			sYear += "<a href=\"javascript:void(" + this.id + ".browseTo(" + (this.piMonth+1+1) + ", " + (this.piYear+1) + "))\"><img src=\"img_site/seta_branca_right.gif\" width=\"8\" height=\"8\" border=\"0\"></a>";
		}
		sYear += "</div></td>";
		sYear += "</tr></table></td></tr>";
		
		//string do mes
		sMonth = "";
		sMonth += "<tr><td height=\"18\" class=\"bg_verm01\"><table width=\"184\" align=\"center\" cellpadding=\"0\"  cellspacing=\"0\"><tr>";
		sMonth += "<td width=\"10\"><div align=\"left\">";
//		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_site/seta_branca_left.gif\" width=\"8\" height=\"8\" 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_site/seta_branca_left.gif\" width=\"8\" height=\"8\" border=\"0\"></a>";
		}
		sMonth += "</div></td>";
		sMonth += "<td width=\"161\" class=\"font_branca08_bold\"><div align=\"center\">" + this.monthLabels[this.piMonth] + "</div></td>";
		sMonth += "<td width=\"11\"><div align=\"right\">";
//		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_site/seta_branca_right.gif\" width=\"8\" height=\"8\" 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_site/seta_branca_right.gif\" width=\"8\" height=\"8\" border=\"0\"></a>";
		}
		sMonth += "</div></td>";
		sMonth += "</tr></table></td></tr>";

		//string dos dias da semana
		sWeekDay = "";
		sWeekDay += "<tr><td class=\"font_branca06_bold_bgCinza\"><table width=\"184\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";
		sWeekDay += "<tr>";
		for (liDay = 0; liDay < 7; liDay ++) sWeekDay += "<td height=\"15\" class=\"font_branca03_bold\"><div align=\"center\">" + this.dayLabels[liDay] + "</div></td>";
		sWeekDay += "</tr>";
	
		//string dos dias
		sWeek = "";
		sWeek += "<tr class=\"font_branca02\">";
		for (liDay = 0; liDay < 7; liDay ++) sWeek += "<td height=\"15\"><div align=\"center\">#" + liDay + "</div></td>";
		sWeek += "</tr>";
		
		//string hoje
		sToday = "";
		if (this.piNowDay && this.piNowYear) {
			sToday += "</table></td></tr>";
			sToday += "<tr><td height=\"20\" class=\"font_branca07_bgCinza\"><table width=\"95%\" height=\"15\" cellpadding=\"0\"  cellspacing=\"0\">";
			sToday += "<tr><td width=\"25%\"><div align=\"center\" style=\"cursor: pointer\">&nbsp;&nbsp;<a href=\"javascript:fechaCalendario();\"><span class=\"font_branca07_bgCinza\">Fechar</span></a></div></td>";
      sToday += "<td width=\"52%\">&nbsp;<td width=\"20%\"><div align=\"center\" style=\"cursor: pointer\"><img src=\"img_site/txt_calendario_hj.gif\" width=\"37\" height=\"10\" border=\"0\" onclick=\"" + this.id + ".clickDate(" + this.piNowMonth + "," + this.piNowYear + "," + this.piNowDay + ")\"></div></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, "<font color=\"#CCCCCC\">" + iDayCount + "</font>");
						} else if (this.piSelectedDay == iDayCount && this.piSelectedMonth == this.piMonth && this.piSelectedYear == this.piYear) {
							sThisWeek = sThisWeek.replace("#" + iWeekDay, "<a href=\"javascript:void(" + this.id + ".clickDate(" + this.piMonth + ", " + this.piYear + ", " + iDayCount + "))\" class=\"font_branca02\"><b><font color=\"#0E3262\" style=\"font-size: 11px\">" + iDayCount + "</font></b></a>");
						} else {
							sThisWeek = sThisWeek.replace("#" + iWeekDay, "<a href=\"javascript:void(" + this.id + ".clickDate(" + this.piMonth + ", " + this.piYear + ", " + iDayCount + "))\" class=\"font_branca02\" style=\"font-size: 11px\">" + 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 + sYear + sMonth + 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 + "\" style=\"position: absolute;\"></div>");
	
	this.dayLabels = [COMPRE_DOM, COMPRE_SEG, COMPRE_TER, COMPRE_QUA, COMPRE_QUI, COMPRE_SEX, COMPRE_SAB];

	this.monthLabels = [tgjan, tgfev, tgmar, tgabr, tgmai, tgjun, tgjul, tgago, tgset, tgout, tgnov, tgdez];
	
	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;
}

function fechaCalendario(){
  document.getElementById('calendario').style.display = "none";
  if (window.desabilitaCalendario) {
     desabilitaCalendario('dtPartida');
  }
}
