/*****************************************************************/
// SMALLCALENDAR (CARLOS A. CERIANI)
// ------------------
//
//	REQUIRES:
//		datefunctions.js
//
/*****************************************************************/

/***************************************************************/
/* Functions                                                   */
/***************************************************************/
function CSmallCalendar_MoveToMonth(iMonth, iYear)
{
	oCSmallCalendarCalendar.Month = iMonth;
	oCSmallCalendarCalendar.Year = iYear;
	oCSmallCalendarCalendar.Draw(oCSmallCalendarCalendar.sContainerId);
}

/***************************************************************/
/* Class CSmallCalendarConfig                                  */
/***************************************************************/
function CSmallCalendarConfig(sLanguage)
{
	this.DateFormat = ((sLanguage.toLowerCase() == "english") ? "{mm}-{dd}-{yyyy}" : "{dd}/{mm}/{yyyy}"); //english or español
	this.MondayFirstDay = ((sLanguage.toLowerCase() == "english") ? 0 : 1);
	
	/*this.sTableCalendarStyle = "ComponentCalendarSmallCalendar";
	this.sTrTitleStyle = "ComponentCalendarSmallCalendarTitle";
	this.sTrDayNameStyle = "ComponentCalendarSmallCalendarDayName";
	this.sTrButtonsStyle = "ComponentCalendarSmallCalendarButtons";
	this.sTdDayStyle = "ComponentCalendarSmallCalendarDay";
	this.sTdSelectedDayStyle = "ComponentCalendarSmallCalendarSelectedDay";
	this.sTdEmptyDayStyle = "ComponentCalendarSmallCalendarEmptyDay";*/
}

/***************************************************************/
/* Class CSmallCalendarSelectedDay                             */
/***************************************************************/
function CSmallCalendarSelectedDay(iDay, iMonth, iYear, sTitle)
{
	//Attributes
	this.Day = iDay;
	this.Month = iMonth;
	this.Year = iYear;
	this.Title = (sTitle ? sTitle : "");
	
	//Methods
	this.IsEqual = CSmallCalendarSelectedDayIsEqual;
}
function CSmallCalendarSelectedDayIsEqual(iDay, iMonth, iYear)
{
	return ((this.Day == iDay) && (this.Month == iMonth) && (this.Year == iYear));
}

/***************************************************************/
/* Class CSmallCalendarCalendar                                */
/***************************************************************/
function CSmallCalendarCalendar(sTitle, iMonth, iYear)
{
	//Attributes
	this.Title = (sTitle ? sTitle : "");
	this.Month = (iMonth ? iMonth : 0);
	this.Year = (iYear ? iYear : 0);
	this.sContainerId = null;
	this.oContainer = null;
	this.SelectedDays = new Array();
	this.sTableCalendarStyle = "ComponentCalendarSmallCalendar";
	this.sTrTitleStyle = "ComponentCalendarSmallCalendarTitle";
	this.sTrDayNameStyle = "ComponentCalendarSmallCalendarDayName";
	this.sTrButtonsStyle = "ComponentCalendarSmallCalendarButtons";
	this.sTdDayStyle = "ComponentCalendarSmallCalendarDay";
	this.sTdSelectedDayStyle = "ComponentCalendarSmallCalendarSelectedDay";
	this.sTdEmptyDayStyle = "ComponentCalendarSmallCalendarEmptyDay";
	this.sLeftImage = "../images/left.gif";
	this.sRightImage = "../images/right.gif";
	this.sTargetUrl = "";
	
	//methods
	this.AddSelectedDay = CSmallCalendarCalendarAddSelectedDay;
	this.DayIsSelected = CSmallCalendarCalendarDayIsSelected;
	this.CheckMonth = CSmallCalendarCalendarCheckMonth;
	this.GetCode = CSmallCalendarCalendarGetCode;
	this.Draw = CSmallCalendarCalendarDraw;
}
function CSmallCalendarCalendarAddSelectedDay(iDay, iMonth, iYear, sTitle)
{
	this.SelectedDays[this.SelectedDays.length] = new CSmallCalendarSelectedDay(iDay, iMonth, iYear, sTitle);
}
function CSmallCalendarCalendarDayIsSelected(iDay, iMonth, iYear)
{
	var iReturn = -1;
	var i = 0;
	while ((i < this.SelectedDays.length) && (iReturn == -1))
	{
		if (this.SelectedDays[i].IsEqual(iDay, iMonth, iYear)) iReturn = i;
		i++;
	}
	return iReturn;
}
function CSmallCalendarCalendarCheckMonth()
{
	if ((this.Month == 0) || (this.Year == 0))
	{
		var dDate = new Date();
		this.Month = dDate.getMonth() + 1;
		this.Year = dDate.getFullYear();
	}
}
function CSmallCalendarCalendarGetCode()
{
	var sCode = "";
	var dDateObject = new DF_Date(oCSmallCalendarConfig.DateFormat);
	var i = 0;
	var j = 0;
	var iWeekDay = 0;
	var iStart = 0;
	var iDays = dDateObject.getdaysinmonth(this.Month, this.Year);
	var dDate = null;
	var iDayIsSelected = -1;
	var sTitle = "";
	
	var iPreviousMonth = this.Month - 1;
	var iPreviousYear = this.Year;
	var iNextMonth = this.Month + 1;
	var iNextYear = this.Year;
	if (iPreviousMonth < 1)
	{
		iPreviousMonth = 12;
		iPreviousYear--;
	}
	if (iNextMonth > 12)
	{
		iNextMonth = 1;
		iNextYear++;
	}
	
	sCode += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"" + this.sTableCalendarStyle + "\">\n";
	if (this.Title != "")
	{
		sCode += "<tr valign=\"middle\" class=\"" + this.sTrTitleStyle + "\">\n";
		sCode += "<td colspan=\"7\">" + this.Title + "</td>\n";
		sCode += "</tr>\n";
	}
	sCode += "<tr valign=\"middle\" class=\"" + this.sTrButtonsStyle + "\">\n";
	sCode += "<td><a href=\"javascript:CSmallCalendar_MoveToMonth(" + iPreviousMonth + "," + iPreviousYear + ");\"><img src=\"" + this.sLeftImage + "\" /></a></td>\n";
	sCode += "<td colspan=\"5\">" + oDF_DateConfig.DF_MonthShortNames[this.Month - 1] + " " + this.Year + "</td>\n";
	sCode += "<td><a href=\"javascript:CSmallCalendar_MoveToMonth(" + iNextMonth + "," + iNextYear + ");\"><img src=\"" + this.sRightImage + "\" /></a></td>\n";
	sCode += "</tr>\n";
	sCode += "<tr valign=\"middle\" class=\"" + this.sTrDayNameStyle + "\">\n";
	for (i=0; i<7; i++)
	{
		j = i;
		if (oCSmallCalendarConfig.MondayFirstDay == 1)
		{
			j = j + 1;
			if (j == 7) j = 0;
		}
		sCode += "<td>" + oDF_DateConfig.DF_DayShortNames[j] + "</td>\n";
	}
	sCode += "</tr>\n";
	
	iWeekDay = new Date(this.Year, this.Month - 1, 1).getDay();
	if (oCSmallCalendarConfig.MondayFirstDay == 1)
	{
		iStart = 1;
		if (iWeekDay == 0) iWeekDay = 7;
	}
	sCode += "<tr valign=\"middle\">\n";
	for (i=iStart; i<iWeekDay; i++)
	{
		sCode += "<td class=\"" + this.sTdEmptyDayStyle + "\">&nbsp;</td>\n";
	}
	for (i=1; i<=iDays; i++)
	{
		if (iWeekDay == (7 + oCSmallCalendarConfig.MondayFirstDay))
		{
			sCode += "</tr>\n";
			sCode += "<tr valign=\"middle\">\n";
			iWeekDay = (0 + oCSmallCalendarConfig.MondayFirstDay);
		}
		iDayIsSelected = this.DayIsSelected(i, this.Month, this.Year);
		if (iDayIsSelected >= 0)
		{
			j = 0;
			sTitle = "";
			while (j < this.SelectedDays.length)
			{
				if (this.SelectedDays[j].IsEqual(i, this.Month, this.Year))
				{
					if (sTitle != "")  sTitle = sTitle + "; ";
					sTitle = sTitle + this.SelectedDays[j].Title;
				}
				j++;
			}
		}
		sCode += "<td class=\"" + ((iDayIsSelected >= 0) ? this.sTdSelectedDayStyle : this.sTdDayStyle) + "\">";
		if (this.sTargetUrl != "") sCode += "<a href=\"" + this.sTargetUrl + "?date=" + dDateObject.builddate(i, this.Month, this.Year) + "\"" + ((iDayIsSelected >= 0) ? " title=\"" + sTitle + "\"" : "") + ">";
		sCode += "" + i;
		if (this.sTargetUrl != "") sCode += "</a>";
		sCode += "</td>\n";
		iWeekDay++;
	}
	for (i=iWeekDay; i<(7 + oCSmallCalendarConfig.MondayFirstDay); i++)
	{
		sCode += "<td class=\"" + this.sTdEmptyDayStyle + "\">&nbsp;</td>\n";
	}
	sCode += "</tr>\n";
	
	sCode += "</table>\n";

	return sCode;
}
function CSmallCalendarCalendarDraw(sContainerId)
{
	this.sContainerId = sContainerId;
	this.oContainer = document.body;
	this.CheckMonth();
	
	if (sContainerId)
	{
		this.oContainer = document.getElementById(sContainerId);
	}
	if (this.oContainer)
	{
		this.oContainer.innerHTML = this.GetCode();
	}
}



var oCSmallCalendarConfig = new CSmallCalendarConfig("español");
var oCSmallCalendarCalendar = new CSmallCalendarCalendar();