function DatePickerClient(id, dayDropDownId, monthDropDownId, calendarPopupId, childDatePickerId, onClientChange, minimumChildIncrement) { /** -- PROPERTIES -- **/ this.Id = id; this.DayDropDownId = dayDropDownId; this.MonthDropDownId = monthDropDownId; this.CalendarPopupId = calendarPopupId; this.ChildDatePickerId = childDatePickerId; this.OnClientChange = onClientChange; this.MinimumChildIncrement = minimumChildIncrement; /** -- METHODS -- **/ if (typeof DatePickerClient._initialised == "undefined") { // SetDate (return: Void) DatePickerClient.prototype.SetDate = function(dNewDate) { this.SetDropDownDate(dNewDate); this.SetCalendarDate(dNewDate); } // GetDropDownDate (return: Date) DatePickerClient.prototype.GetDropDownDate = function() { var oDayDropDown = document.getElementById(this.DayDropDownId); var oMonthDropDown = document.getElementById(this.MonthDropDownId); var aMonthYear = oMonthDropDown.options[oMonthDropDown.selectedIndex].value.split("|"); var iSelectedDay = parseInt(oDayDropDown.options[oDayDropDown.selectedIndex].value); var iSelectedMonth = parseInt(aMonthYear[0]); var iSelectedYear = parseInt(aMonthYear[1]); var aDaysInMonth = getDaysInMonth(iSelectedYear); if (iSelectedDay > aDaysInMonth[iSelectedMonth]) { // Raise status msg? // Change selected date to last day of selected Month iSelectedDay = aDaysInMonth[iSelectedMonth]; oDayDropDown.selectedIndex = aDaysInMonth[iSelectedMonth] - 1; } return new Date(iSelectedYear, iSelectedMonth, iSelectedDay, 0, 0, 0); } // GetCalendarDate (return: Date) DatePickerClient.prototype.GetCalendarDate = function() { return CalendarPopup_FindCalendar(this.CalendarPopupId).GetDate(); } // SetDropDownDate (return: Void) DatePickerClient.prototype.SetDropDownDate = function(dNewDate) { var oDayDropDown = document.getElementById(this.DayDropDownId); var oMonthDropDown = document.getElementById(this.MonthDropDownId); var dCurrentDate = new Date(); var iNewDay = dNewDate.getDate(); var iNewMonth = dNewDate.getMonth(); var newSelectedIndex = 0; if (dNewDate.getMonth() != dCurrentDate.getMonth() || dNewDate.getFullYear() != dCurrentDate.getFullYear()) { //Need to select next years current month. var aIndex = new Array(); var iIndex = 0; // Calculate Index Pos for each month // Based on the assumption: // - The current Month will always be in pos 0 // - The current Month + 1 Year will always be in pos 12 // Calc positions for months remaining this year for (var i = dCurrentDate.getMonth(); i < 12; i++) { aIndex[i] = iIndex; iIndex++; } // Calc positions for applicable months next year for (var i = 0; i < 12 - (12 - dCurrentDate.getMonth()) + 1; i++) { aIndex[i] = iIndex; iIndex++; } newSelectedIndex = aIndex[dNewDate.getMonth()]; } oDayDropDown.selectedIndex = iNewDay - 1; oMonthDropDown.selectedIndex = newSelectedIndex; } // SetCalendarDate (return: Void) DatePickerClient.prototype.SetCalendarDate = function(dNewDate) { CalendarPopup_FindCalendar(this.CalendarPopupId).SelectDate(dNewDate); } // UpdateChildDatePicker (return: Void) DatePickerClient.prototype.UpdateChildDatePicker = function(dNewDate) { if (this.ChildDatePickerId.length > 0) // check null etc { oChildDatePicker = findDatePickerClient(this.ChildDatePickerId); if (oChildDatePicker.GetDropDownDate() < dNewDate) { var iNewDay = dNewDate.getDate(); var iNewMonth = dNewDate.getMonth(); var iNewYear = dNewDate.getFullYear(); var aDaysInMonth = getDaysInMonth(iNewYear); var iChildDay = iNewDay + parseInt(this.MinimumChildIncrement); var iChildMonth = iNewMonth; if (iChildDay > aDaysInMonth[iChildMonth]) { iChildMonth++; iChildDay = iChildDay - aDaysInMonth[iNewMonth]; } var dChildDate = new Date(iNewYear, iChildMonth, iChildDay); oChildDatePicker.SetDate(dChildDate); } } } // CallClientFunction (return: Void) DatePickerClient.prototype.CallClientFunction = function() { if (this.OnClientChange.length > 0) { eval(this.OnClientChange + "();"); } } /** -- EVENTS -- **/ // DropDownDateChanged (return: Void) DatePickerClient.prototype.DropDownDateChanged = function() { var _dDropDownDate = this.GetDropDownDate(); var _dCalendarDate = this.GetCalendarDate(); if (_dDropDownDate.toUTCString() !== _dCalendarDate.toUTCString()) { this.SetCalendarDate(_dDropDownDate); this.UpdateChildDatePicker(_dDropDownDate); this.CallClientFunction(); } } // CalendarPopupDateChanged (return: Void) DatePickerClient.prototype.CalendarPopupDateChanged = function() { var _dDropDownDate = this.GetDropDownDate(); var _dCalendarDate = this.GetCalendarDate(); if (_dCalendarDate.toUTCString() !== _dDropDownDate.toUTCString()) { this.SetDropDownDate(_dCalendarDate); this.UpdateChildDatePicker(_dCalendarDate); this.CallClientFunction(); } } } /** -- INITILISATION FLAG -- **/ DatePickerClient._initialised = true; } /** -- STAND ALONE METHODS -- **/ function findDatePickerClient(id) { for (var i = 0; i < aMyTestArray.length; i++) { if (aMyTestArray[i].Id == id) { return aMyTestArray[i]; } } } function getDaysInMonth(iYear) { var _aDaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // check for leap year if ( ( (iYear % 4 == 0) && (iYear % 100 != 0) ) || (iYear % 400 == 0) ) { _aDaysInMonth[1] = "29"; } return _aDaysInMonth; } /** -- EVENT HANDLERS -- **/ function onDropDownValueChange(id) { findDatePickerClient(id).DropDownDateChanged(); } function onCalendarPopupValueChanged(id) { findDatePickerClient(id).CalendarPopupDateChanged(); }