/*
 *  Calendar.js - Javascript code for displaying a calendar.
 *  
 *  $Id: calendar.js,v 1.1.1.1 2004/05/21 21:31:21 thim Exp $
 */

//Browser detection code
var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;
//Browser detection code ends here

var monthNames = new Array(
    'January', 'February', 'March', 
    'April',   'May',      'June', 
    'July',    'August',   'September', 
    'October', 'November', 'December'
    );

var monthDigits = new Array(
    '01', '02', '03', 
    '04', '05', '06', 
    '07', '08', '09', 
    '10', '11', '12'
    );  

var daysPerMonth = new Array(  
    /* Jan */ 31,     /* Feb */ 28, /* Mar */ 31,     /* Apr */ 30, 
    /* May */ 31,     /* Jun */ 30, /* Jul */ 31,     /* Aug */ 31, 
    /* Sep */ 30,     /* Oct */ 31, /* Nov */ 30,     /* Dec */ 31 
    );

var todayObject= new Date();
var fieldYear = todayObject.getYear();
var fieldMonth = todayObject.getMonth(); 
var fieldDate = todayObject.getDate();
if (N6)
{
    fieldYear = fieldYear + 1900;
}
        
// years to be from the current year to current year + 7.
var minYear = fieldYear;		// inclusive
var maxYear = fieldYear + 7;	// exclusive


var calendarFrameId, calendarFormId, dayFieldIdPrefix, fieldId;

/*
 *  Initialize.
 *
 *  calendarFormId is the calendar form id.
 */
function init(_calendarFrameId, _calendarFormId, _dayFieldIdPrefix, _fieldId)
{
	calendarFrameId = _calendarFrameId;
	calendarFormId = _calendarFormId;
	dayFieldIdPrefix = _dayFieldIdPrefix;
	fieldId = _fieldId;
	
	var yearOptions = document.getElementById(calendarFormId).year.options;
	var n = 0;
	for (var x = minYear; x < maxYear; x ++,n++)
	{
		yearOptions[n] = new Option();
		yearOptions[n].text = yearOptions[n].value = x;
	}
	
	var monthOptions = document.getElementById(calendarFormId).month.options;
	for (x = 0; x < 12; x ++)
	{
		monthOptions[x] = new Option();
		monthOptions[x].text = monthNames[x];
		monthOptions[x].value = x;
	}
	
    var field = parent.document.getElementById(fieldId).value;
    var m = parseFloat(field.substring(0, 2))-1;
    var d = parseFloat(field.substring(3, 5));
    var y = parseFloat(field.substring(6));
    if (0<=m&&m<=11&&1<=d&&d<=31&&minYear<=y&&y<maxYear)
    {
		fieldYear = y;
		fieldMonth = m;
		fieldDate = d;
    }

	// Populating the current year value for the Year input field for the first time.
	document.getElementById(calendarFormId).year.value = fieldYear;
	document.getElementById(calendarFormId).month.selectedIndex = fieldMonth;
	displayCalendar();
}


/*
 *  This function populates the table with the dates for a particular month 
 *  for a given year.
 *
 *  dayFieldIdPrefix is the prefix associated with the day field.
 *  year is year in yyyy format.
 *  month is month in numerical format viz 0=january, 1=february, etc.
 */
function displayCalendar()
{
	var year = document.getElementById(calendarFormId).year.value;
	var month = document.getElementById(calendarFormId).month.selectedIndex;

	var thisDate = (fieldMonth == month && fieldYear == year);
	
    if (year%4 == 0 && (year%100 != 0 || year%400 == 0))
    {
        daysPerMonth[1]=29;
    }

    // Get the first day of the week for this month.
    var oDateNow = new Date();  
    var oDate = new Date(monthNames[month] + 1 + "," + year);
    var dayOfWeek = oDate.getDay();

    // Input values in calendar fields (i.e., the <td> fields).
    var count=0;
    var end = daysPerMonth[month]+(dayOfWeek);
    
    // Reset calendar fields to blank.
    for (var s=1;s<=42;s++)
    {
        document.getElementById(dayFieldIdPrefix+s).childNodes[0].innerHTML="";
    }

    // Fill the calendar fields starting with the first day of the week
    // for this month.
    for (s=(dayOfWeek+1); s<=end; s++)
    {
        count=count+1;
        document.getElementById(dayFieldIdPrefix+s).childNodes[0].innerHTML=(!thisDate || count != fieldDate)? count : "<font color='red'>"+count+"</font>";
        document.getElementById(dayFieldIdPrefix+s).childNodes[0].id=count;
    }
}



/*
 *  This function sets the specified date field
 *  to the specified year, month, and day.
 *
 *  calendarFrameId is the calendar frame id.
 *  fieldId is the date field id.
 *  year is year in yyyy format.
 *  month is month in numerical format viz 0=january, 1=february, etc.
 */
function setDateField(year, month, day)
{
    // Check to see if we need to set the field to today's date.
    if (year == 1)
    {
        // Display today's date in "mm/dd/yyyy" format.
        year = todayObject.getYear();
        month = todayObject.getMonth();
        day = todayObject.getDate();
        if (N6)
        {
            year = year+1900;
        }
    }
	else
	{
		year = document.getElementById(calendarFormId).year.value;
		month = document.getElementById(calendarFormId).month.selectedIndex;
	}

    // Display the date in "mm/dd/yyyy" format.
    var day1 = day;
    if (day1 <=9) day1 = "0" + day1;
    parent.document.getElementById(fieldId).value = monthDigits[month] + "/" + day1 + "/" + year; 
	fieldYear = year;
	fieldMonth = month;
	fieldDate = day;
    displayCalendar();

    // Hide the calendar frame.
    parent.document.getElementById(calendarFrameId).style.display='none'
}


/*
 *  This function displays the previous month for the specified calendar form.
 *
 *  calendarFormId is the id of the calendar form being updated.
 *  dayFieldIdPrefix is the prefix associated with the day field.
 */
function displayPreviousMonth()
{ 
	var year = document.getElementById(calendarFormId).year.value;
	var month = document.getElementById(calendarFormId).month.selectedIndex;
    var month = month - 1;
    if (month==-1)
    {
        var y = parseFloat(year)-1;
        if (y < minYear)
        {
        	return;
        }
        month=11;
        document.getElementById(calendarFormId).year.value=y;
    }
    document.getElementById(calendarFormId).month[month].selected = "1";
    displayCalendar();
}


/*
 *  This function displays the next month for the specified calendar form.
 *
 *  calendarFormId is the id of the calendar form being updated.
 *  dayFieldIdPrefix is the prefix associated with the day field.
 */
function displayNextMonth()
{ 
	var year = document.getElementById(calendarFormId).year.value;
	var month = document.getElementById(calendarFormId).month.selectedIndex;
    month= month + 1;
    if (month==12)
    {
        var y = parseFloat(year)+1;
        if (y >= maxYear)
        {
        	return;
        }
        month=0;
        document.getElementById(calendarFormId).year.value=y;
    }
    document.getElementById(calendarFormId).month[month].selected = "1";
    displayCalendar();
}
