/******************************
OO-based DropdownDatePicker
By: Glenn G. Vergara
glenngv@yahoo.com
Philippines

Please make this intact
if you want to use this script.
Thanks!
******************************/

function DropdownDatePicker(month, day, year, hasLeadingZero){ var me = this; this.month = month; this.day = day; this.year = year; if (hasLeadingZero==null || typeof hasLeadingZero=="undefined") hasLeadingZero=false; this.hasLeadingZero = hasLeadingZero; this.setWeekend = false; this.populateDate = function(){ var dSelYear = me.year.options[me.year.selectedIndex].value; var dSelMonth = me.month.options[me.month.selectedIndex].value; var timeA = new Date(dSelYear,dSelMonth,1); var timeDifference = timeA - 86400000; var timeB = new Date(timeDifference); var daysInMonth = timeB.getDate(); prevDaysInMonth = me.day.options.length; if (daysInMonth < prevDaysInMonth){ if (me.day.selectedIndex>=daysInMonth-1){ me.day.selectedIndex = daysInMonth-1;}
me.day.options.length = daysInMonth;}
else if (daysInMonth > prevDaysInMonth){ var sDay; for (var i = prevDaysInMonth; i < (daysInMonth); i++) { sDay = ((i+1)<10 && me.hasLeadingZero)?"0"+(i+1):i+1; me.day.options[i] = new Option(sDay,sDay);}
}
if (me.setWeekend){ me.setWeekendStyleOn(me.callbackOn);}
}
this.setDisplayMonths = function(mode, isUpperCase, arrMonth){ switch (mode){ case 1:
for (var i=0;i<12;i++){ me.month.options[i] = new Option(((i+1)<10 && me.hasLeadingZero)?"0"+(i+1):i+1, i+1);}
break; case 2:
var arrMonth = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); for (var i=0;i<arrMonth.length;i++){ me.month.options[i] = new Option((isUpperCase)?arrMonth[i].toUpperCase():arrMonth[i],i+1);}
break; case 3:
var arrMonth = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); for (var i=0;i<arrMonth.length;i++){ me.month.options[i] = new Option((isUpperCase)?arrMonth[i].toUpperCase():arrMonth[i],i+1);}
break; case 4:
if (arrMonth==null || typeof arrMonth=="undefined"){ var arrMonth = new Array("January","February","March","April","May","June","July","August","September","October","November","December");}
for (var i=0;i<arrMonth.length;i++){ me.month.options[i] = new Option((isUpperCase)?arrMonth[i].toUpperCase():arrMonth[i],i+1);}
break; default:
for (var i=0;i<12;i++){ me.month.options[i] = new Option(((i+1)<10 && me.hasLeadingZero)?"0"+(i+1):i+1, i+1);}
}
}
this.setDisplayYears = function(fromYear, toYear){ me.year.length=0; for (var i=fromYear;i<=toYear;i++){ me.year.options[i-fromYear] = new Option(i,i);}
}
this.setDefaultDate = function(defaultDate){ if (defaultDate==null || typeof defaultDate=="undefined" || isNaN(defaultDate)){ defaultDate = new Date();}
me.month.options[defaultDate.getMonth()].selected = true; me.day.options[defaultDate.getDate()-1].selected = true; for (var i=0;i<me.year.options.length;i++){ if (me.year.options[i].value==defaultDate.getFullYear()){ me.year.selectedIndex = i; break;}
}
me.month.onchange();}
this.displayDay = function(arrDay){ if (arrDay==null || typeof arrDay=="undefined"){ var arrDay = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");}
me.arrDay = arrDay; me.month.onmouseover = function(){ var selectedDate = new Date(me.year.options[me.year.selectedIndex].value, this.selectedIndex, me.day.options[me.day.selectedIndex].value); this.title = me.arrDay[selectedDate.getDay()];}
for (var i=0;i<me.month.options.length;i++){ me.month.options[i].onmouseover = function(){ var selectedDate = new Date(me.year.options[me.year.selectedIndex].value, this.value-1, me.day.options[me.day.selectedIndex].value); this.title = me.arrDay[selectedDate.getDay()];}
}
me.day.onmouseover = function(){ var selectedDate = new Date(me.year.options[me.year.selectedIndex].value, me.month.selectedIndex, this.options[this.selectedIndex].value); this.title = me.arrDay[selectedDate.getDay()];}
for (var i=0;i<me.day.options.length;i++){ me.day.options[i].onmouseover = function(){ var selectedDate = new Date(me.year.options[me.year.selectedIndex].value, me.month.selectedIndex, this.value); this.title = me.arrDay[selectedDate.getDay()];}
}
me.year.onmouseover = function(){ var selectedDate = new Date(me.year.options[me.year.selectedIndex].value, this.selectedIndex, me.day.options[me.day.selectedIndex].value); this.title = me.arrDay[selectedDate.getDay()];}
for (var i=0;i<me.year.options.length;i++){ me.year.options[i].onmouseover = function(){ var selectedDate = new Date(this.value, me.month.selectedIndex, me.day.options[me.day.selectedIndex].value); this.title = me.arrDay[selectedDate.getDay()];}
}
}
this.setWeekendStyleOn = function(callbackFunc){ me.callbackOn = callbackFunc; me.setWeekend = true; for (var i=0;i<me.day.options.length;i++){ var date = new Date(me.year.options[me.year.selectedIndex].value, me.month.selectedIndex, i+1); var day = date.getDay(); if (day==0 || day==6){ me.callbackOn(me.day.options[i]);}
else{ me.callbackOff(me.day.options[i]);}
}
}
this.setWeekendStyleOff = function(callbackFunc){ me.callbackOff = callbackFunc;}
this.compareDate = function(otherDropdownDatePicker){ if (otherDropdownDatePicker instanceof DropdownDatePicker){ var thisDate = new Date(me.year.options[me.year.selectedIndex].value, me.month.options[me.month.selectedIndex].value, me.day.options[me.day.selectedIndex].value); var otherDate = new Date(otherDropdownDatePicker.year.options[otherDropdownDatePicker.year.selectedIndex].value, otherDropdownDatePicker.month.options[otherDropdownDatePicker.month.selectedIndex].value, otherDropdownDatePicker.day.options[otherDropdownDatePicker.day.selectedIndex].value); return Date.parse(thisDate) - Date.parse(otherDate);}
return 0;}
var monthChange = this.month.onchange; if (monthChange){ this.month.onchange = function(){ me.populateDate(); monthChange();}
}
else{ this.month.onchange = this.populateDate;};this.month.onchange(); var yearChange = this.year.onchange; if (yearChange){ this.year.onchange = function(){ me.populateDate(); yearChange();}
}
else{ this.year.onchange = this.populateDate;}
}
