/*
 * Return the number of days in a given month/year combo
 */
function dayCountForMonth(month, year) {
  // JS bases months off of a zero index
  realMonth = month - 1;
  // obtained from http://snippets.dzone.com/posts/show/2099
  return (32 - new Date(year, realMonth, 32).getDate());
}

/*
 * Dynamically update the day selection in the start/end search dates
 */
function updateDays(element) {
  // get parent section
  var section = $(element).parent().parent().parent();
  // get day selection object
  var daySelectElement = $(section).find('div.day select');
  // get selected month, day, and year
  var month = $(section).find('div.month select').val();
  var year = $(section).find('div.year select').val();
  var day = $(daySelectElement).val();
  // adjust day selection for shorter months
  var daysInMonth = dayCountForMonth(month, year);
  if (day > daysInMonth) {
    day = daysInMonth;
  }
  // empty the day options and refill for new selection
  daySelectElement.empty();
  var i;
  for (i = 1; i <= daysInMonth; i++) {
    var newOption = '<option value="' + i + '">' + i + '</option>';
    daySelectElement.append(newOption);
  }
  // redo the day selection
  daySelectElement.val(day);
}