ActionView date_select calendar logic in javascript

Here's the Gist

document.observe("dom:loaded", function() {
  var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  var dateEl = $('project_expiry_date_3i');
  var monthEl = $('project_expiry_date_2i');
  var yearEl = $('project_expiry_date_1i');
  if (dateEl && monthEl && yearEl) {
    var currentDateIndex = dateEl.selectedIndex;
    function leapYearAdjustedDays() {
      var daysInMonth = DAYS_IN_MONTH[monthEl.selectedIndex];
      // Leap year calc.
      var year = parseInt(yearEl.options[yearEl.selectedIndex].value);
      if (monthEl.selectedIndex == 1 && year % 4 == 0) daysInMonth = 29;
      return daysInMonth;
    }
    function adjustDays() {
      dateEl.options.length = 0;
      for (var idx = 1; idx <= leapYearAdjustedDays(); idx++) { 
        if (currentDateIndex == idx-1)
          dateEl.options[idx-1] = new Option(idx,idx,false,true);
        else       
          dateEl.options[idx-1] = new Option(idx);
      }
    }
    monthEl.observe("change", adjustDays);
    yearEl.observe("change", adjustDays);
  }
})