// JavaScript Document
<!--
var INVALID_CALENDAR_DAY = 00; // Valid day (1-31).
var NO_NAV_MONTH = -1;
var MONTHS_CALENDAR = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var DAYS_CALENDAR = new Array("Su", "Mo", "Tu", "We", "Th", "Fr", "Sa");

function CalendarDisplay()
{
	this.name = "";
	this.createYear = createYearCD;
	this.createMonth = createMonthCD;
	this.createMonthInternal = createMonthInternalCD;
	this.formatter;
	this.monthValidated = "";
	this.dayValidated = "";
	this.yearValidated = "";
	this.errorThrown = false;
	
	// settings and methods for links
  this.argvalue = ""; // This is the field name
	this.linkIsOn = false;
	this.linkDayHandlerName = ""; // This is calling setLinkOn
	this.setLinkOn = setLinkOnCD;
	this.setLinkOff = setLinkOffCD;
	
	// settings and methods for open window
	this.openIsOn = false;
	this.setOpenOn = setOpenOnCD;
	this.setOpenOff = setOpenOffCD;
	this.autoCloseIsOn = false;
	this.setAutoCloseOn = setAutoCloseOnCD;
	this.setAutoCloseOff = setAutoCloseOffCD;
	this.windowWidth = 200; // 190
	this.windowHeight = 250; // 220
	this.windowWidthYear = 690; 
	this.windowHeightYear = 600;
	
	// navigation settings and methods
	this.navigateMonth = navigateMonthCD;
	this.navigateYear = navigateYearCD;
	this.navigationIsOn = false;
	this.setNavigationOn = setNavigationOnCD;
	this.setNavigationOff = setNavigationOffCD;
}

/*
 * Create and return the HTML for all the months in a calendar year,
 * given the month, day, and year.
 * Month is zero based, which means January is 0, February is 1, etc.
 *
 * The year should be valid, but month and day are not required,
 * so they can be set to any value out of range.
 * It's recommended to use "" or INVALID_CALENDAR_DAY.
 * If a valid month and day are passed in, then that day will be highlighted.
 *
 * If setOpenOn() has been called, then the year is also opened in a separate window.
 *
 * Example (for February 11, 2004):
 * var calendarEx = new CalendarDisplay();
 * document.writeln(calendarEx.createYear(1, 11, 2004));
 */
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 : INVALID_CALENDAR_DAY;
			this.createMonthInternal(incr, dd, cdYear);
			this.formatter.endColumn();
			incr++;		}//end for c<4 loop
		this.formatter.endRow();
	}//end for r < 3 loop
	this.formatter.concludeCalendarYear();
	if (this.openIsOn)	{
		// setup navigation
		var prefixFormatter = new CalendarFormatter();
		if (this.navigationIsOn && !this.errorThrown){
			var aYear = this.yearValidated;
			prefixFormatter.addNavigationYear(aYear, this.name);}
		var settings = "scrollbars=yes,menubar=yes,resizable=yes,width=" + this.windowWidthYear + ",height=" + this.windowHeightYear;
		var calendarWindowYear = window.open("","CalendarWindowYear", settings);
		calendarWindowYear.document.open();
    calendarWindowYear.document.writeln("<HTML><HEAD><TITLE>Calendar<"+"/TITLE><"+"/HEAD>");
		calendarWindowYear.document.writeln("<BODY onLoad='self.focus()'>");
		calendarWindowYear.document.writeln(prefixFormatter.results);	
		calendarWindowYear.document.writeln(this.formatter.results);
		calendarWindowYear.document.writeln("<"+"/BODY>"+"<"+"/HTML>");
		calendarWindowYear.document.close();
	}//end if this.openIsOn
	return this.formatter.results;
}//end createYearCD

/*
 * Create a month and return the HTML for a month,
 * given a month, day, and year.
 * Month is zero based, which means January is 0, February is 1, etc.
 *
 * The year and month should be valid, but day is not required,
 * so it can be set to any value out of range.
 * It's recommended to use "" or INVALID_CALENDAR_DAY.
 * If a day is passed in, then that day will be highlighted.
 *
 * If setOpenOn() has been called, then the month is also opened in a separate window.
 *
 * Example (for February 11, 2004):
 * var calendarEx = new CalendarDisplay();
 * document.writeln(calendarEx.createMonth(1, 11, 2004));
 */
function createMonthCD(cdMonth, cdDay, cdYear)
{
	// this formatter is used in createMonthInternal
	this.formatter = new CalendarFormatter();
	// createMonthInternal does validation, so call it first
	var	calendarWindowInternal = this.createMonthInternal(cdMonth, cdDay, cdYear);
	if (this.openIsOn)	{
		// setup navigation
		var prefixFormatter = new CalendarFormatter();
		if (this.navigationIsOn && !this.errorThrown)		{
			var aMonth = this.monthValidated;
			var aYear = this.yearValidated;
			prefixFormatter.addNavigation(aMonth, aYear, this.name);}
		// open window
		var settings = "scrollbars=yes,menubar=yes,resizable=yes,width=" + this.windowWidth + ",height=" + this.windowHeight;
		var calendarWindow = window.open("","CalendarWindowMonth", settings);
		calendarWindow.document.open();
		calendarWindow.document.writeln("<"+"HTML>"+"<"+"HEAD>"+"<"+"TITLE>Calendar"+"<"+"/"+"TITLE>"+"<"+"/"+"HEAD>");
		calendarWindow.document.writeln("<BODY onLoad='self.focus()'>");
	  calendarWindow.document.writeln(prefixFormatter.results);		
		calendarWindow.document.writeln(calendarWindowInternal);
		calendarWindow.document.writeln("<"+"/"+"BODY>"+"<"+"/"+"HTML>");
		calendarWindow.document.close();
	}// if this.openIsOn
	return calendarWindowInternal;
}

/*
 * This function is a middle man to call createMonth for the navigation in createMonth.
 *
 * Original purpose: If the navigation buttons are recoded to be href links,
 * then will need this so the return value doesn't attempt to load an invalid page.
 * The href navigation links were slow in Netscape 4.
 */
function navigateMonthCD(cdMonth, cdYear){
	var cdDay = INVALID_CALENDAR_DAY;
	this.createMonth(cdMonth, cdDay, cdYear);
}

function navigateYearCD(cdYear){
	var cdMonth = NO_NAV_MONTH;
	var cdDay = INVALID_CALENDAR_DAY;
	this.createYear(cdMonth, cdDay, cdYear);
}

/*
 * Create a month and return it.
 */
function createMonthInternalCD(cdMonth, cdDay, cdYear){
	if (isNaN(cdDay) || cdDay == "")	{
		cdDay = INVALID_CALENDAR_DAY;	}
	this.dayValidated = cdDay;
	this.formatter.dayChosen = cdDay;

	if (isNaN(cdMonth) || cdMonth < 0 || cdMonth >= MONTHS_CALENDAR.length)	{
		this.errorThrown = true;
		this.monthValidated = 0; 
		return "Not a valid month.";	}	
  this.errorThrown = false;
	this.monthValidated = cdMonth;
	if (isNaN(cdYear) || cdYear == "")	{
		this.errorThrown = true;
		this.yearValidated = 0; 
		return "Not a valid year.";	}
	this.errorThrown = false;
	this.yearValidated = cdYear;
	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(cols);
				if (cols >= 6) phase = SPACE_BEFORE;}//end if phase==DAY_ROW
			else if (phase == SPACE_BEFORE){
				var tempDay = aDate.getDay();
				if (cols == tempDay)	{
					var autoCloseReallyOn = (this.autoCloseIsOn && this.openIsOn);
					this.formatter.addFormattedLinkDay(1, this.linkIsOn, this.linkDayHandlerName, this.openIsOn, autoCloseReallyOn, this.objectName, aDate, this.argvalue);
					aDate.setDate(2);
					phase = DAYS_BETWEEN;	}//end if cols==tempDay
				else{	this.formatter.addFormattedDayBlank();}//end else
			}//end if phase==SPACE_BEFORE
			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();
					var autoCloseReallyOn = (this.autoCloseIsOn && this.openIsOn);
					this.formatter.addFormattedLinkDay(dayOfMonth, this.linkIsOn, this.linkDayHandlerName, this.openIsOn, autoCloseReallyOn, this.objectName, aDate, this.argvalue);
					aDate.setDate(dayOfMonth + 1);}//end if aDate.getMonth==cdMonth
				else{
					this.formatter.addFormattedDayBlank();
					phase = SPACE_AFTER;	}//end else if aDate.getMonth==cdMonth
			}//end if (phase == DAYS_BETWEEN)
			else if (phase == SPACE_AFTER){this.formatter.addFormattedDayBlank();	}
		} // end for cols
		this.formatter.endRow();
	} // end for rows
	this.formatter.concludeCalendarMonth();
	return this.formatter.results;
}

/*
 * Set a switch on so days in the calendar will show up as links.
 *
*/
function setLinkOnCD(handlerName,argvalue){
  this.argvalue = argvalue;
	this.linkIsOn = true;
	this.linkDayHandlerName = handlerName;
//  this.linkDayHandlerName = "setField";
}

/*
 * Set a switch off so days in the calendar will not show up as links.
 */
function setLinkOffCD(){
	this.linkIsOn = false;
}

/*
 * Turn navigation on for the month or year calendar.
 *
 * For a month calendar, there will be buttons like << < > >>
 * For a year calendar, there will buttons like << >>
 * which are:
 * << previous year
 * <  previous month
 * >  next month
 * >> next year
 *
 * To include navigation buttons, provide the name of the object that was instantiated.
 *
 * Make sure the calendar object is global by constructing it without a var if it is inside a function.
 * If the object is declared outside a function, then it will be global either way.
 * It needs to be global so the navigation button's onclick event will be able to reference the object.
 */
function setNavigationOnCD(objectName){
	this.navigationIsOn = true;
	this.name = objectName;
	if (this.name == null) this.name = "";
}

/*
 * Turn navigation off.
 */
function setNavigationOffCD(){
	this.navigationIsOn = false;
}

/*
 * Set a switch on so the calendar will open in a new window.
 * It will still return the calendar result.
 */
function setOpenOnCD(){
	this.openIsOn = true;
}

/*
 * Set a switch off so the calendar will not open in a new window.
 * It will just return the calendar result.
 */
function setOpenOffCD(){
	this.openIsOn = false;
}

/*
 * Set that the opened window should be closed when a date is selected.
 */
function setAutoCloseOnCD(){
	this.autoCloseIsOn = true;
}

/*
 * Set that the opened window should not be closed when a date is selected.
 */
function setAutoCloseOffCD(){
	this.autoCloseIsOn = false;
}

/*
 * CalendarDisplay uses this to construct a CalendarFormatter object
 * which it uses to form the HTML for the calendar.
 */
function CalendarFormatter()
{
	this.results = "";
	this.dayChosen = INVALID_CALENDAR_DAY;
	this.setupCalendarYear = setupCalendarYearCF;
	this.setupCalendarMonth = setupCalendarMonthCF;
	this.concludeCalendarYear = concludeCalendarYearCF;
	this.concludeCalendarMonth = concludeCalendarMonthCF;
	this.addFormattedDay = addFormattedDayCF;
	this.addFormattedLinkDay = addFormattedLinkDayCF;
	this.addFormattedNonDay = addFormattedNonDayCF;
	this.addFormattedDayName = addFormattedDayNameCF;
	this.addFormattedDayBlank = addFormattedDayBlankCF;
	this.startRow = startRowCF;
	this.endRow = endRowCF;
	this.startColumn = startColumnCF;
	this.endColumn = endColumnCF;
	
	this.addNavigation = addNavigationCF;
	this.addNavigationYear = addNavigationYearCF;
	this.getPreviousYearLink = getPreviousYearLinkCF;
	this.getPreviousMonthLink = getPreviousMonthLinkCF;
	this.getNextMonthLink = getNextMonthLinkCF;
	this.getNextYearLink = getNextYearLinkCF;
}

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">' + 
  MONTHS_CALENDAR[month] + ', ' + year + '<'+'/'+'td><'+'/'+'tr>';}

function concludeCalendarYearCF(){
	this.results = this.results + "<"+"/table>";}

function concludeCalendarMonthCF(){
	this.results = this.results + "<"+"/table>";}

/*
 * Add a formatted day to the results with no link.
 */
function addFormattedDayCF(num){
	var isLinkOn = false;
	var objectName = ""; // ignored when link is off
	var month; // ignored when link is off
	var day; // ignored when link is off
	var year; // ignored when link is off
	this.addFormattedLinkDay(num, isLinkOn);
}

/*
 * Add a formatted day to the results with a link, if isLinkOn is true.
 * The way the link is constructed, the function specified by handlerName is referenced
 * with the zero based month, day, and full year.
 * Keep in mind the handlerName needs to be referenced globally.
 */
function addFormattedLinkDayCF(num, isLinkOn, handlerName, isWindowOpen, autoCloseIsOn, objectName, aDate, argvalue)
{
	var linkStart = "";
	var linkEnd = "";
	if (isLinkOn)	{
		var aYear = aDate.getFullYear();
		var aMonth = aDate.getMonth();
		var aDay = aDate.getDate();
		linkStart = "<a href=\"javascript:"
			+ ((isWindowOpen) ? "window.opener." : "")
			+ handlerName + "("
      + "'" + argvalue + "'" + ","
			+ aMonth + ","
			+ aDay + ","
			+ aYear + ");"
			+ ((autoCloseIsOn) ? "window.close();" : "")
			+ "\">";
		linkEnd = "<"+"/a>";	}//end if isLinkOn
	if (num == this.dayChosen)	{
		this.results = this.results + '<td align="center" bgcolor="yellow"><b>' + linkStart + num + linkEnd + '<'+'/b><'+'/td>';	}
	else	{
		this.results = this.results + '<td align="center">' + linkStart + num + linkEnd + '<'+'/td>';	}
}

function addFormattedNonDayCF(contents){
	this.results = this.results + '<td align="center">' + contents + '<'+'/td>';}

function addFormattedDayNameCF(num){
		this.addFormattedNonDay(DAYS_CALENDAR[num]);	}

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>";}

function addNavigationCF(aMonth, aYear, aName){
	this.results = this.results
		+ "<p><form name=\"navigationCalendar\">"
		+ this.getPreviousYearLink(aMonth, aYear, aName)
		+ "&nbsp;&nbsp;" + this.getPreviousMonthLink(aMonth, aYear, aName)
		+ "&nbsp;&nbsp;" + this.getNextMonthLink(aMonth, aYear, aName)
		+ "&nbsp;&nbsp;" + this.getNextYearLink(aMonth, aYear, aName)
		+ "<"+"/form><"+"/p>";
}

function addNavigationYearCF(aYear, aName){
	var aMonth = NO_NAV_MONTH;
	this.results = this.results
		+ "<p><form name=\"navigationCalendar\">"
		+ this.getPreviousYearLink(aMonth, aYear, aName)
		+ "&nbsp;&nbsp;" + this.getNextYearLink(aMonth, aYear, aName)
		+ "<"+"/form><"+"/p>";
}

function getPreviousYearLinkCF(aMonth, aYear, aName){
	var previousYear = aYear;
	previousYear--;
	var previousYearLink = "<input type=\"button\" value=\"&lt;&lt;\" onclick=\"javascript:window.opener.";
	if (aMonth == NO_NAV_MONTH)	{
		previousYearLink += aName + ".navigateYear(";	}
	else	{
		previousYearLink += aName + ".navigateMonth(" + aMonth + ",";	}
	previousYearLink += previousYear + ")\" />";
	return previousYearLink;
}

function getPreviousMonthLinkCF(aMonth, aYear, aName){
	var previousMonth = aMonth;
	var previousYear = aYear;
	previousMonth--;
	if (previousMonth < 0)	{
		previousMonth = 11;
		previousYear--;	}
	var previousMonthLink = "<input type=\"button\" value=\"&lt;&nbsp;\" onclick=\"javascript:window.opener."
		+ aName + ".navigateMonth("
		+ previousMonth + ","
		+ previousYear + ")\" />";
	return previousMonthLink;
}

function getNextMonthLinkCF(aMonth, aYear, aName)
{
	var nextMonth = aMonth;
	var nextYear = aYear;
	nextMonth++;
	if (nextMonth > 11)	{
		nextMonth = 0;
		nextYear++;	}
	var nextMonthLink = "<input type=\"button\" value=\"&nbsp;&gt;\" onclick=\"javascript:window.opener."
		+ aName + ".navigateMonth("
		+ nextMonth + ","
		+ nextYear + ")\" />";
	return nextMonthLink;
}

function getNextYearLinkCF(aMonth, aYear, aName)
{
	var nextYear = aYear;
	nextYear++;
	var nextYearLink = "<input type=\"button\" value=\"&gt;&gt;\" onclick=\"javascript:window.opener."
	if (aMonth == NO_NAV_MONTH)	{
		nextYearLink += aName + ".navigateYear(";	}
	else	{
		nextYearLink += aName + ".navigateMonth(" + aMonth + ",";	}
	nextYearLink += nextYear + ")\" />";
	return nextYearLink;
}
	
function calendarOpener(arg){
  calendarTry = new CalendarDisplay();
  calendarTry.setNavigationOn("calendarTry");
  calendarTry.setOpenOn();
  calendarTry.setAutoCloseOn();
  calendarTry.setLinkOn("setCalField",arg);
  var time=new Date();
  var lm=time.getMonth();
  var ld=time.getDate();
  var ly=time.getFullYear();
  calendarTry.createMonth(lm, ld, ly);
}

function setCalField(arg, month, day, year){
  document.forms[0][arg].value=(month+1)+"/"+day+"/"+year;
}

//-->