/*
 *  file:       datepicker.js
 *  author:     Ned Collyer
 *  created:    27/11/2001 2:21PM
 *  tested browsers: netscape 4.7, netscape 6, ie4, ie5, ie5.5, ie6
 */
/*
 * g_arrDatePickers stores any date picker objects that we create in an array for later use.
 */
g_arrDatePickers = new Array();

/*
 * checkDate(in_datePickerName)
 * this function runs a sanity check on the last changed date.
 * it matches the last changed date select with a date object in the g_arrDatePickers array
 * and then checks the day month and year fields for sanity.
 */
function checkDate(in_datePickerName)
{
    // for each date picker in the array of date pickers
    for (var i = 0; i < g_arrDatePickers.length; i++)
    {
        // if we find a match in the array
        if (g_arrDatePickers[i].dateName == in_datePickerName)
        {
            // then sanitize the matched date object and break the loop.
            g_arrDatePickers[i].sanitize();
            break;
        }
    }
}

/*
 * inlineDatePicker(in_formName, in_dateName, in_defaultDate)
 * in_formName = (string) the name of the form the date picker is a member of.
 * in_dateName = (string) the name of the date fields.
 * The select boxes will be named:
 *  in_DateName + Day
 *  in_DateName + Month
 *  in_DateName + Year
 * in_defaultDate = (string) the date we wish to start on
 * options are :
 *  "blank" for no default date selected, base date rendered from clients current date.
 *  "today" for default date being today, base date rendered from clients current date.
 *  "1979/07/27" (yyyy/mm/dd) for default date being in_defaultDate, base date rendered from in_defaultDate.
   in_extraOptions = extra options to be appended within both span and select tags
 */
function inlineDatePicker(in_formName, in_dateName, in_defaultDate, in_extraOptions)
{
    // Constructing html elements.
    this.buildDays = buildDays;
    this.buildMonths = buildMonths;
    this.buildYears = buildYears;

    this.formName = in_formName;
    this.dateName = in_dateName

    this.extraOptions = in_extraOptions;

    this.dateSelected = true;

    if (in_defaultDate == "blank")
    {
        // in_defaultDate is blank
        this.defaultDate = new Date();
        this.dateSelected = false;
    }
    else if (in_defaultDate == "today")
    {
        // in_defaultDate is today
        this.defaultDate = new Date();
    }
    else
    {
        // in_defaultDate supplied
        this.defaultDate = new Date(in_defaultDate);
    }

    // renders the html select boxes to the screen
    this.render = render;
    this.renderDays = renderDays;
    this.renderMonths = renderMonths;
    this.renderYears = renderYears;

    // make sure date is a valid date
    this.sanitize = sanitize;

    this.isLeapYear = false;

    this.oDay;
    this.oMonth;
    this.oYear;

    // add this date picker to the array
    g_arrDatePickers[g_arrDatePickers.length] = this;

    // strings defined for month lookup.
    this.arrMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    // strings defined for number of days in each month
    // (lookup from this.arrMonths)
    this.arrMonthDays = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];

    this.displayedDays = 31;
    this.displayedMonths = 12;
    this.displayedYears = 2;

    // this function wraps up the rendering of the select boxes
    function render()
    {
        if (document.forms[this.formName])
        {
        	this.renderDays();
        	this.renderMonths();
        	this.renderYears();
        }
    }

    // this function populates day with the correct number of days.
    function renderDays()
    {
        this.buildDays();
		var newDay;
		var thisDay = this.defaultDate.getDate();

        this.oDay = document.forms[this.formName].elements[this.dateName + "Day"];

        var idxSelectedDay = - 1;
        var blIsSelected;
        var dayVal;

		for (var i = 0; i < this.displayedDays; i++)
		{
		    if (eval(i + 1) == thisDay)
		    {
		        blIsSelected = true;
		        idxSelectedDay = thisDay;
		    }
		    else
		    {
		        blIsSelected = false;
		    }

            if (i < 9)
    			dayVal = "0" + (i + 1);
			else
                dayVal = (i + 1);

            newDay = new Option(i + 1, dayVal, false, blIsSelected);
            this.oDay.options[i + 1] = newDay;


		}

        // make sure the correct date is selected, or index 0 if no selected date
        if (this.dateSelected)
        {
		    this.oDay.selectedIndex = idxSelectedDay;
		}
        else
        {
            this.oDay.selectedIndex = 0;
        }
    }

    // this function populates month with the correct number of months.
    function renderMonths()
    {
        this.buildMonths();
        var newMonth;
        var thisMonth = this.defaultDate.getMonth();

        this.oMonth = document.forms[this.formName].elements[this.dateName + "Month"];

        var idxSelectedMonth = - 1;
        var blIsSelected;
        var monthVal;

        for (var i = 0; i < this.displayedMonths; i++)
        {
            if (i == thisMonth)
            {
                blIsSelected = true;
                idxSelectedMonth = thisMonth;
            }
            else
            {
                blIsSelected = false;
            }

            if (i < 9)
                monthVal = "0" + (i + 1);
            else
                monthVal = (i + 1);

            newMonth = new Option(this.arrMonths[i], monthVal, false, blIsSelected);
            this.oMonth.options[i + 1] = newMonth;
        }


        // make sure the correct date is selected, or index 0 if no selected date
        if (this.dateSelected)
        {
            this.oMonth.selectedIndex = idxSelectedMonth + 1;
        }
        else
        {
            this.oMonth.selectedIndex = 0;
        }
    }

    // this function populates year with correct number of years.
    function renderYears()
    {
        this.buildYears();
        var newYear;
        var thisYear = this.defaultDate.getFullYear();

        this.oYear = document.forms[this.formName].elements[this.dateName + "Year"];

        var idxSelectedYear = - 1;
        var blIsSelected;

        // where we should start
        var startYear = thisYear;
        var endYear = startYear + 10;

        for (var i = 0; i < this.displayedYears; i++)
        {
            if (startYear + i == thisYear)
            {
                blIsSelected = true;
                idxSelectedYear = i;
            }
            else
            {
                blIsSelected = false;
            }
            newYear = new Option(startYear + i, startYear + i, false, blIsSelected);
            this.oYear.options[i + 1] = newYear;
        }

        // make sure the correct date is selected, or index 0 if no selected date
        if (this.dateSelected)
        {
            this.oYear.selectedIndex = idxSelectedYear + 1;
        }
        else
        {
            this.oYear.selectedIndex = 0;
        }

    }

    // builds select object for days
    function buildDays()
    {
        document.write("<span " + this.extraOptions + ">");
        document.write("<select " + this.extraOptions + " name=\"" + this.dateName + "Day\" onChange=\"checkDate('" + this.dateName + "')\">");
        document.write("<option value=\"\"> dd </option>");
        // for netscape put in blank options for buffer size.
        for (var i = 0; i < this.displayedDays; i++)
        {
            document.write("<option></option>");
        }
        document.write("</select>");
        document.write("</span>\n");
    }

    // builds select object for months
    function buildMonths()
    {
        document.write("<span " + this.extraOptions + ">");
        document.write("<select " + this.extraOptions + " name=\"" + this.dateName + "Month\" onChange=\"checkDate('" + this.dateName + "')\">");
        document.write("<option value=\"\"> mmm </option>");
        // for netscape put in blank options for buffer size.
        for (var i = 0; i < this.displayedMonths; i++)
        {
            document.write("<option></option>");
        }
        document.write("</select>");
        document.write("</span>\n");
    }

    // builds select object for years
    function buildYears()
    {
        document.write("<span " + this.extraOptions + ">");
        document.write("<select " + this.extraOptions + " name=\"" + this.dateName + "Year\" onChange=\"checkDate('" + this.dateName + "')\">");
        document.write("<option value=\"\"> yyyy </option>");
        // for netscape put in blank options for buffer size.
        for (var i = 0; i < this.displayedYears; i++)
        {
            document.write("<option></option>");
        }
        document.write("</select>");
        document.write("</span>\n");
    }

    // sanity checks the dates for sanity
    function sanitize()
    {
        var selectedYear = this.oYear.options[this.oYear.selectedIndex].value;
        var selectedMonth = this.oMonth.options[this.oMonth.selectedIndex].value;

        var iDayCount = this.arrMonthDays[eval(selectedMonth - 1)];

        // check leap year
        this.isLeapYear = (selectedYear % 400 != 0 && selectedYear % 4 == 0)

        // is this month feb and leap year?
        if ((eval(selectedMonth - 1) == 1) && (this.isLeapYear))
        {
            // add 1 to day count.
            iDayCount = eval(iDayCount) + 1;
        }

        if (this.oDay.length - 1 > iDayCount)
        {
            // do we need to remove options?
            var blResetSelectedDay = false;
            for (var i = this.oDay.length - 1; i > iDayCount; i--)
            {
                if (this.oDay.selectedIndex == i)
                {
                    blResetSelectedDay = true;
                }
                this.oDay.options[i] = null;
            }

            if (blResetSelectedDay)
            {
                this.oDay.selectedIndex = this.oDay.length - 1;
            }
        }
        else
        {
            // do we need to add options?
            var idxSelectedDay = this.oDay.selectedIndex;
            for (var i = this.oDay.length; i <= iDayCount; i++)
            {
                this.oDay.options[i] = new Option(i, i, false, false);
            }
            this.oDay.selectedIndex = idxSelectedDay;
        }
    }
}
