<!--
/**
 * Javascript function that will do an inline creation of the appropriate HTML
 * and javascript structures necessary to have a popup calendar.
 *
 * Example Usage:
 *
 * <input type=text name=SubmissionDate>
 * <script language='JavaScript'>addPopupCalendar("SubmissionDate")</script>
 *
 * @param sEditName the name of the Edit
 */

function addPopupCalendar(sEditName, contextPath)
{
	// Create IMG with link to <edit>CalObj.show() method and a <edit>CalSpan SPAN
	document.write("<SPAN CLASS='PopupCalendarSpan' ID='" + sEditName + "CalSpan'></SPAN>");
	document.write("<A ID='" + sEditName + "CalImage' style='display: inline' HREF='javascript:" + sEditName + "CalObj.show()'>");
	document.write("<IMG BORDER=0 SRC='"+contextPath+"/images/PopupCalendar.gif'></A>\n");

	// Create the <edit>CalObj data object

	eval(sEditName + "CalObj = new PopupCalendar(document.getElementById('" + sEditName + "'), document.getElementById('" + sEditName + "CalSpan'),contextPath)");
}

/**
 * Javascript function that will hide the popup calendar that is associated
 * to the specified field name.
 *
 * @param sEditName the name of the Edit.
 */
function hidePopupCalendar(sEditName)
{
	var calImage = document.getElementById(sEditName + "CalImage");
	calImage.style.display = "none";
}

/**
 * Javascript function that will show the popup calendar that is associated
 * to the specified field name.
 *
 * @param sEditName the name of the Edit.
 */
function showPopupCalendar(sEditName)
{
	var calImage = document.getElementById(sEditName + "CalImage");
	calImage.style.display = "inline";
}

function addPopupCalendarMultiForm(sEditName, formNumber)
{
	// @todo - Make this conditional on browser type

	// Create IMG with link to <edit>CalObj.show() method and a <edit>CalSpan SPAN
	document.write("<SPAN CLASS='PopupCalendarSpan' ID='" + sEditName + "CalSpan'></SPAN>");
	document.write("<A HREF='javascript:" + sEditName + "CalObj.show()'>");
	document.write("<IMG BORDER=0 SRC='images/PopupCalendar.gif'></A>\n");

	// Create the <edit>CalObj data object
	eval(sEditName + "CalObj = new PopupCalendar(document.forms[" + formNumber + "]." + sEditName + ", document.getElementById('" + sEditName + "CalSpan'))");
}

/**
 * Global used to keep track if a popup calendar is open to avoid multiple
 */
var _activePopupCalendar = null;

/**
 * PopupCalendar Object
 * @param oHTMLEdit the HTML edit
 * @param oHTMLDiv the HTML <DIV> object
 */
function PopupCalendar(oHTMLEdit, oHTMLDiv, ContextPath)
{
	// Member Variables
	this.iMonth = 0;
	this.iYear = 0;
	this.oHtmlEdit = oHTMLEdit;
	this.oHtmlDiv = oHTMLDiv;
        this.contextPath = ContextPath;
        
	// Public Methods
	this.show = PopupCalendar_show;
	this.hide = PopupCalendar_hide;
	this.prevMonth = PopupCalendar_prevMonth;
	this.nextMonth = PopupCalendar_nextMonth;
	this.prevYear = PopupCalendar_prevYear;
	this.nextYear = PopupCalendar_nextYear;

	// Internal Methods
	this.redisplay = PopupCalendar_redisplay;
	this.makeGrid = PopupCalendar_makeGrid;
	this.highlightDay = PopupCalendar_highlightDay;
	this.selectDay = PopupCalendar_selectDay;
	this.buildDate = PopupCalendar_buildDate;
	this.getMonthName = PopupCalendar_getMonthName;
	this.getMonthDays = PopupCalendar_getMonthDays;
	this.getFirstWeekday = PopupCalendar_getFirstWeekday;
}

/**
 * Shows the popup calendar
 */
function PopupCalendar_show()
{
	var sDate;

	// Hide any previously show calendars
	if (_activePopupCalendar != null)
		_activePopupCalendar.hide();

	// Attempt to parse out default Month and year
	sDate = String(this.oHtmlEdit.value);
	if ((sDate != "") && (sDate.length == 10) && (sDate.indexOf('-') == 4))
	{
		this.iYear = parseInt(sDate.substring(0, 4), 10);
		this.iMonth = parseInt(sDate.substring(5, 7), 10);
	}
	else
	{
		var dDate = new Date();

		this.iYear = dDate.getFullYear();
		this.iMonth = dDate.getMonth() + 1;
	}

	this.redisplay();
}

/**
 * Hides the popup calendar
 */
function PopupCalendar_hide()
{
	// Make invisibile and reset global
	this.oHtmlDiv.style.visibility = "hidden";
	//_activePopupCalendar = null;
}

/**
 * Redisplays the popup calendar
 */
function PopupCalendar_redisplay()
{
	var sTable;

	sTable = "<TABLE CLASS=PopupCalendar CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH=100%>\n";
// Year and month separate
//	sTable += "<TR CLASS=PopupCalendarHeading><TD CLASS=PopupCalendarButton OnClick='_activePopupCalendar.prevYear()'>&lt&lt</TD><TD COLSPAN=5><B>" + this.iYear + "</B></TD><TD CLASS=PopupCalendarButton OnClick='_activePopupCalendar.nextYear()'>&gt&gt</TD></TR>\n";
//	sTable += "<TR CLASS=PopupCalendarHeading><TD CLASS=PopupCalendarButton OnClick='_activePopupCalendar.prevMonth()'>&lt&lt</TD><TD COLSPAN=5><B>" + this.getMonthName() + "</B></TD><TD CLASS=PopupCalendarButton OnClick='_activePopupCalendar.nextMonth()'>&gt&gt</TD></TR>\n";
// Month and year together

	sTable += "<TR CLASS=PopupCalendarHeading><TD CLASS=PopupCalendarButton OnClick='_activePopupCalendar.prevMonth()'><IMAGE SRC='"+this.contextPath+"/images/PopupCalendarLeft.gif' BORDER='0' ALT='&lt&lt'></TD><TD COLSPAN=5><B>" + this.getMonthName() + " " + this.iYear + "</B></TD><TD CLASS=PopupCalendarButton OnClick='_activePopupCalendar.nextMonth()'><IMG SRC='"+this.contextPath+"/images/PopupCalendarRight.gif' BORDER='0' ALT='&gt&gt'></TD></TR>\n";
	sTable += "<TR CLASS=PopupCalendarWeekdays><TD>S</TD><TD>M</TD><TD>T</TD><TD>W</TD><TD>T</TD><TD>F</TD><TD>S</TD></TR>\n";
	sTable += this.makeGrid(this.getFirstWeekday(), this.getMonthDays());
	sTable += "<TR CLASS=PopupCalendarDay><TD COLSPAN=7 OnClick='_activePopupCalendar.hide()'>close</TD></TR>\n";
	sTable += "</TABLE>\n";

	// Copy generated HTML into DIV/SPAN
	this.oHtmlDiv.innerHTML = sTable;

	// Make visible and set global
	this.oHtmlDiv.style.visibility = "inherit";
	shieldPopup(this.oHtmlDiv);
	_activePopupCalendar = this;
}

/**
 *Skips the popup calendar ahead one year
 */
function PopupCalendar_nextYear()
{
	this.iYear++;
	this.redisplay();
}

/**
 *Skips the popup calendar back one year
 */
function PopupCalendar_prevYear()
{
	this.iYear--;
	this.redisplay();
}

/**
 *Skips the popup calendar ahead one month
 */
function PopupCalendar_nextMonth()
{
	this.iMonth++;
	if (this.iMonth > 12)
	{
		this.iMonth = 1;
		this.iYear++;
	}
	this.redisplay();
}

/**
 *Skips the popup calendar back one month
 */
function PopupCalendar_prevMonth()
{
	this.iMonth--;
	if (this.iMonth <= 0)
	{
		this.iMonth = 12;
		this.iYear--;
	}
	this.redisplay();
}

/**
 *Writes the grid of the popup calendar
 *@param iStartDay - the day to start on
 *@param iNumDays - the number of days in the month
 *@return the month in grid format
 */
function PopupCalendar_makeGrid(iStartDay, iNumDays)
{
	var sGrid;
	var i;
	var iNum = 1;

	sGrid = "<TR>\n";
	for (i = 0; i < 45; i++)
	{
		if ((i >= iStartDay) && (iNum <= iNumDays))
		{
			if ((i % 7 == 0) && (i > 0))
				sGrid += "</TR>\n<TR>\n";
			sGrid += "<TD CLASS=PopupCalendarDay";

			// Add Selected to PopupCalendarDay if currentvalue
			if ((this.oHtmlEdit != null) && (this.buildDate(this.iYear, this.iMonth, iNum) == this.oHtmlEdit.value))
				sGrid += "Selected";

			sGrid += " OnClick='_activePopupCalendar.selectDay(" + iNum + ")' OnMouseOver='_activePopupCalendar.highlightDay(this, true)' OnMouseOut='_activePopupCalendar.highlightDay(this, false)'>" + iNum + "</TD>";

			iNum++;
		}
		else if (iNum < iNumDays)
			sGrid += "<TD></TD>\n";
	}
	sGrid += "</TR>\n";

	return sGrid;
}

/**
 *Builds a date from a Year,Month,Day tuple
 *@param iYear - the year
 *@param iMonth - the month
 *@param iDay - the Day
 *return a string representation of the Month/Day/Year
 */
function PopupCalendar_buildDate(iYear, iMonth, iDay)
{
	var sValue="";

	if (iMonth < 10)
		sValue += '0';
	sValue += iMonth + '/';
	if (iDay < 10)
		sValue += '0';
	sValue += iDay + '/';
	sValue += iYear;

	return sValue;
}

/**
 *Highlight the present day
 *@param oObj - the object to highlight
 *@param bOn - TRUE highlights - FALSE removes highlight
 */
function PopupCalendar_highlightDay(oObj, bOn)
{
	if (bOn)
	{
		oObj.style.backgroundColor = "#b0c4de";
	}
	else
	{
		oObj.style.backgroundColor = "white";
	}
}

/**
 *Set the day of the present month
 *@param iDay the day to select in the month
 */
function PopupCalendar_selectDay(iDay)
{
	this.oHtmlEdit.focus();
	this.oHtmlEdit.value = this.buildDate(this.iYear, this.iMonth, iDay);
	this.hide();
}

/**
 *Get the name of the present month
 *@return the name of the present month
 */
function PopupCalendar_getMonthName()
{
	switch (this.iMonth)
	{
		case 1: return "January";
		case 2: return "February";
		case 3: return "March";
		case 4: return "April";
		case 5: return "May";
		case 6: return "June";
		case 7: return "July";
		case 8: return "August";
		case 9: return "September";
		case 10: return "October";
		case 11: return "November";
		case 12: return "December";
	}
}
/**
 *Get the number of days in the present month
 *@return the number of days in the present month
 */
function PopupCalendar_getMonthDays()
{
	switch (this.iMonth)
	{
		case 1:	case 3:	case 5:	case 7:	case 8:	case 10: case 12:
			return 31;

		case 4:	case 6:	case 9:	case 11:
			return 30;

		case 2:
			// Leap Year Rules
			if ((this.iYear % 4) == 0)
			{
				if (((this.iYear % 100) == 0) && ((this.iYear % 400) != 0))
					return 28;
				else
					return 29;
			}
			else
				return 28;

		default:
			return 0;
	}
}

/**
 *Get the first weekday of the month
 *@return the first weekday of the month
 */
function PopupCalendar_getFirstWeekday()
{
	var dDate = new Date(this.iYear, this.iMonth - 1, 1);
	return dDate.getDay();
}

// -->