<!--

//
// Originally written by Keith Jenci
// www.mredkj.com
//

function CalendarDisplay()
{
	this.ONE_LETTER = 1; // this is the first number for setDayFormat
	this.TWO_LETTER = 2;
	this.THREE_LETTER = 3;
	this.FULL_LETTER = 4; // this is the last number for setDayFormat
	
	this.dayFormat = this.ONE_LETTER;

	this.setDayFormat = setDayFormatCD;
	this.createYear = createYearCD;
	this.createMonth = createMonthCD;
	this.createMonthInternal = createMonthInternalCD;
	
	this.formatter;
}

function setDayFormatCD(dayF)
{
	this.dayFormat = (dayF < this.ONE_LETTER || dayF > this.FULL_LETTER) ? this.ONE_LETTER : dayF;
}

function createYearCD(cdMonth, cdDay, cdYear)
{
	this.formatter = new CalendarFormatter(); 

	var r;
	var c;
	var incr = 0;
	
	this.formatter.setupCalendarYear();
	
	for (r=0; r<3; r++)
	{
		this.formatter.startRow();
		
		for (c=0; c<4; c++)
		{
			this.formatter.startColumn();
			var dd = (cdMonth == incr) ? cdDay : this.formatter.NO_DAY;
			this.createMonthInternal(incr, dd, cdYear);
			this.formatter.endColumn();
			incr++;
		}
		
		this.formatter.endRow();
	}
	
	this.formatter.concludeCalendarYear();

	return this.formatter.results;
}

function createMonthCD(cdMonth, cdDay, cdYear)
{
	this.formatter = new CalendarFormatter();
	return this.createMonthInternal(cdMonth, cdDay, cdYear);
}

function createMonthInternalCD(cdMonth, cdDay, cdYear)
{
	this.formatter.dayChosen = cdDay;
	
	if (isNaN(cdMonth) || cdMonth < 0 || cdMonth >= this.formatter.MONTHS.length)
	{
		return "Not a valid month.";
	}

	var startingDay = 1;
	var aDate = new Date(cdYear, cdMonth, startingDay);
		
	var DAY_ROW = 0;
	var SPACE_BEFORE = 1;
	var DAYS_BETWEEN = 2;
	var SPACE_AFTER = 3;
	var phase = DAY_ROW;

	this.formatter.setupCalendarMonth(cdMonth, cdYear);

	var rows;
	for (rows = 0; rows < 7; rows++)
	{
		this.formatter.startRow();
	
		var cols;
		for (cols = 0; cols < 7; cols++)
		{
			if (phase == DAY_ROW)
			{
				this.formatter.addFormattedDayName(this, cols);
				if (cols >= 6) phase = SPACE_BEFORE;
			}
			else if (phase == SPACE_BEFORE)
			{
				var tempDay = aDate.getDay();
				if (cols == tempDay)
				{
					this.formatter.addFormattedDay(1);
					aDate.setDate(2);
					phase = DAYS_BETWEEN;
				}
				else
				{
					this.formatter.addFormattedDayBlank();
				}
			}
			else if (phase == DAYS_BETWEEN)
			{
				// After using setDate, calling getMonth will
				// verify we're still in the current month
				if (aDate.getMonth() == cdMonth)
				{
					var dayOfMonth = aDate.getDate();
					this.formatter.addFormattedDay(dayOfMonth);
					aDate.setDate(dayOfMonth + 1);
				}
				else
				{
					this.formatter.addFormattedDayBlank();
					phase = SPACE_AFTER;
				}
			}
			else if (phase == SPACE_AFTER)
			{
				this.formatter.addFormattedDayBlank();
			}
		} // end for cols
		
		this.formatter.endRow();
		
	} // end for rows
	
	this.formatter.concludeCalendarMonth();
	
	return this.formatter.results;

}

function CalendarFormatter()
{
	this.results = "";
	
	this.NO_DAY = 99;
	this.dayChosen = this.NO_DAY;
	this.MONTHS = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	this.DAYS_1 = new Array("S", "M", "T", "W", "T", "F", "S");
	this.DAYS_2 = new Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");
	this.DAYS_3 = new Array("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT");
	this.DAYS_FULL = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

	this.setupCalendarYear = setupCalendarYearCF;
	this.setupCalendarMonth = setupCalendarMonthCF;
	this.concludeCalendarYear = concludeCalendarYearCF;
	this.concludeCalendarMonth = concludeCalendarMonthCF;
	this.addFormattedDay = addFormattedDayCF;
	this.addFormattedNonDay = addFormattedNonDayCF;
	this.addFormattedDayName = addFormattedDayNameCF;
	this.addFormattedDayBlank = addFormattedDayBlankCF;
	this.startRow = startRowCF;
	this.endRow = endRowCF;
	this.startColumn = startColumnCF;
	this.endColumn = endColumnCF;
}

function setupCalendarYearCF(month, year)
{
	this.results = this.results + '<table border="1" cellpadding="5" cellspacing="0">';
}

function setupCalendarMonthCF(month, year)
{
	this.results = this.results + '<table><tr><td colspan="7" align="center">' + this.MONTHS[month] + ', ' + year + '</td></tr>';
}

function concludeCalendarYearCF()
{
	this.results = this.results + "</table>";
}

function concludeCalendarMonthCF()
{
	this.results = this.results + "</table>";
}

function addFormattedDayCF(num)
{
	if (num == this.dayChosen)
	{
		this.results = this.results + '<td align="center" bgcolor="yellow"><b>' + num + '</b></td>';
	}
	else
	{
		this.results = this.results + '<td align="center">' + num + '</td>';
	}
}

function addFormattedNonDayCF(contents)
{
	this.results = this.results + '<td align="center">' + contents + '</td>';
}

function addFormattedDayNameCF(cal, num)
{
	if (cal.dayFormat == cal.ONE_LETTER)
	{
		this.addFormattedNonDay(this.DAYS_1[num]);
	}
	else if (cal.dayFormat == cal.TWO_LETTER)
	{
		this.addFormattedNonDay(this.DAYS_2[num]);
	}
	else if (cal.dayFormat == cal.THREE_LETTER)
	{
		this.addFormattedNonDay(this.DAYS_3[num]);
	}
	else if (cal.dayFormat == cal.FULL_LETTER)
	{
		this.addFormattedNonDay(this.DAYS_FULL[num]);
	}
	else
	{
		this.addFormattedNonDay("#ERR");
	}
}

function addFormattedDayBlankCF()
{
	this.addFormattedNonDay("&nbsp;");
}

function startRowCF()
{
	this.results = this.results + "<tr>";
}

function endRowCF()
{
	this.results = this.results + "</tr>";
}

function startColumnCF()
{
	this.results = this.results + "<td>";
}

function endColumnCF()
{
	this.results = this.results + "</td>";
}

//-->