// Class Calendar
// Description: Generates a dynamic calendar given a month and year.
// Usage:
//   To setup you need to define three stylesheet elements: .caldefault, .calday, .caltoday
//   Then create a Calendar object, e.g.,
//     var cal = new Calendar(date, month, year, action);
//   Where:
//     date - day of the month, e.g., 31 for March 31
//     month - number representing the month, e.g., 3 for March
//     year - e.g., 2001
//     action - the name of the function you want called if the user clicks on a day
//	This function will be passed with the arguments date, month, year
//	 When you want to display the calendary, simply call the Show method, e.g.,
//	   cal.Show();

// Class Methods
				
function Calendar_GetString()
{
	var firstDate = new Date(this.year,this.month,1);
	var firstDay = firstDate.getDay();
	
	var calStr = "<TABLE BORDER=1 COLS=7 width=200>";
	calStr += "<TR bgcolor=steelblue>";
	calStr += "<TD COLSPAN=7 ALIGN=center class=tvb1><SPAN class=caldefault>"+Calendar.monthName[this.month].toUpperCase()+" "+this.year+"</SPAN></TD>";
	calStr += "</TR>";
	calStr += "<TR>";

	for (var i=0;i<Calendar.dayName.length;i++) 
		calStr += "<TD ALIGN=center class=tvbot><SPAN class=caldefault>"+Calendar.dayName[i]+"</SPAN></TD>";
	calStr += "</TR>";

	var dayCount = 1;
	calStr += "<TR>";
	for (var i=0;i<firstDay;i++) 
		calStr += "<TD> </TD>";

	var monthArg = this.month + 1;
	for (var i=0;i<this.monthDays[this.month];i++)
	{
		var styleStr = "calday";
		if (dayCount==this.date)
			styleStr = "tvbot1";
			
//	calStr += '<TD ALIGN="center" class=tvbot><A class="'+styleStr+'" HREF="javascript:void('+this.action+
//	'('+dayCount+','+monthArg+','+this.year+'))">'+'<SPAN class="'+styleStr+'">'+ dayCount+'</SPAN></A></FONT></TD>';
	calStr += '<TD ALIGN="center" class=tvbot>'+'<SPAN class="'+styleStr+'">'+ dayCount+'</SPAN></FONT></TD>';

		dayCount++;
		if ((i+firstDay+1)%7==0&&(dayCount<this.monthDays[this.month]+1)) 
			calStr += "</TR><TR bgcolor=black>";
	}

	var totCells = firstDay+this.monthDays[this.month];
	for (var i=0;i<(totCells>28?(totCells>35?42:35):28)-totCells;i++) 
		calStr += "<TD> </TD>"
	calStr += "</TR>";
	calStr += "</TABLE>";
	return calStr;
}

function Calendar_Show()
{
	var calStr = this.GetString();
	document.write(calStr);
}

function Calendar(date,month,year,action)
{
	// Properties
	this.date = date;
	this.month = month;
	this.year = year;
	this.action = action;
	this.monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	// Leap year test
	if ((this.year%4==0||this.year%100==0)&&(this.year%400==0)) 
		this.monthDays[1] = 29; 
	else 
		this.monthDays[1] = 28;
		
	// Methods
	this.Show = Calendar_Show;
	this.GetString = Calendar_GetString;
}
// Static Class Properties
Calendar.dayName = new Array("Sun","Mon","Tue","Wed","Thr","Fri","Sat");
Calendar.monthName = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

// Class CalendarSet
// Description: Keeps track of multiple calendars and has methods for populating popup fields.

function CalendarSet_Show()
{	
	var border = '<td bgcolor="' + this.borderColor +'"><img border="0" src="' + gImageDir + 'spacer.gif" width="1" height="1"></td>';
	document.write('<table border="0" cellpadding="0" cellspacing="0"><tr>');

	// Show the top border
	for (var i=0;i<this.calendars.length+2;i++)
	{
		document.write(border);
	}
	document.write('</tr><tr>');

	document.write(border);
	for (var i=0;i<this.calendars.length;i++)
	{
		document.write('<td valign=top>');
		this.calendars[i].Show();
		document.write('</td>');
	}
	document.write(border);
	document.write('</tr><tr>');
	
	// Show the bottom border
	for (var i=0;i<this.calendars.length+2;i++)
	{
		document.write(border);
	}
	document.write('</tr></table>');
}


function CalendarSet_SetMonthOptions(selectField)
{
	var selected = true;
	selectField.options.length = 0; // Clear the popup
	
	for (var i=0;i<this.calendars.length;i++)
	{
		var anOption = new Option(Calendar.monthName[this.calendars[i].month]+" "+this.calendars[i].year,(this.calendars[i].month+1)+","+this.calendars[i].year,selected,selected);
		selectField.options[i] = anOption;
	
		selected = false;
	}
	selectField.options[0].selected = true;
}

function CalendarSet_SetDateOptions(monthSelectField,dateSelectField)
{
	var calObject = this.calendars[monthSelectField.selectedIndex];
	var daysInMonth = calObject.monthDays[calObject.month];
	
	if (dateSelectField.options.length==0)
	{
		var anOption = new Option("Date",0,true,true);
		dateSelectField.options[0] = anOption;	
	}
	
	if (dateSelectField.options.length > daysInMonth+1)
	{
		// Remove last entries
		var excess = dateSelectField.options.length - daysInMonth - 1;
		for (var i=0;i<excess;i++)
		{
			dateSelectField.options[dateSelectField.length-1] = null;
		}
	}
	else if (dateSelectField.options.length < daysInMonth+1)
	{
		// Add entries
		var deficit = daysInMonth - dateSelectField.options.length + 1;
		for (var i=0;i<deficit;i++)
		{
			var anOption = new Option(dateSelectField.options.length,dateSelectField.options.length,false,false);
			dateSelectField.options[dateSelectField.options.length] = anOption;
		}
	}
	
	// Set the default day to current
	var now = new Date();
	dateSelectField.options[now.getDate()].selected = true;
}

function CalendarSet(startDate,months,action)
{
	// Properties
	this.borderColor = "aqua";
	this.calendars = new Array();

	var now = new Date();
	var month=startDate.getMonth();
	var year = startDate.getFullYear();
	
	for (var i=0;i<months;i++)
	{
		var dayToHilite = 0;
		if ((now.getFullYear()==year)&&(now.getMonth()==month))
			dayToHilite = now.getDate();
		this.calendars[i] = new Calendar(dayToHilite,month,year,action);
		month++;
		if (month>=12)
		{
			month = 0;
			year++;
		}
	}

	// Methods
	this.Show = CalendarSet_Show;
	this.SetMonthOptions = CalendarSet_SetMonthOptions;
	this.SetDateOptions = CalendarSet_SetDateOptions;
}

