function calculateForm(form) {
  setNumberOfDays(form); 
  var baseRate = parseFloat(getSelectValue(form.programLevel));
  var daysPerWeek = parseInt(getSelectValue(form.daysPerWeek));
  var buyoutDays = parseInt(form.buyoutDays.selectedIndex);
  var buyoutHours = parseInt(form.buyoutHours.selectedIndex);
  var earlyAM = parseInt(form.earlyAM.selectedIndex);
  var extendedDay = parseInt(getSelectValue(form.extendedDay));

  var buyoutRate = 45;

  var baseTuition = baseRate * daysPerWeek;

  var buyoutTotal = (buyoutDays * buyoutRate) + ((buyoutHours > 0) ? 90:0);

  var extendedFees = 8.5 * 4 * (earlyAM + (extendedDay * 2));
  var tuitionTotal = baseTuition + buyoutTotal + extendedFees;

  form.baseTuition.value = baseTuition.toFixed(2);
  form.buyoutTotal.value = buyoutTotal.toFixed(2);
  form.extendedFees.value = extendedFees.toFixed(2);
  form.tuitionTotal.value = tuitionTotal.toFixed(2);
  form.participationTotal.value = getParticipationPerMonth(form) - buyoutDays + " days";
}

function getSelectValue(select) {
  return select[select.selectedIndex].value;
}

function getParticipationPerMonth(form) {
  return Math.round(getDaysPerWeek(form) * 4 / (form.programLevel.selectedIndex ? 4 : 3));
}

function getDaysPerWeek(form) {
  return parseInt(getSelectValue(form.daysPerWeek));
}

function setNumberOfDays(form) {
  var days = getDaysPerWeek(form);
  var participationDaysPerMonth = getParticipationPerMonth(form);
  buildSelectValues(form.earlyAM, days);
  buildSelectValues(form.extendedDay, days);
  buildSelectValues(form.buyoutDays, participationDaysPerMonth);
}

function buildSelectValues(select, num) {
  var selectIndex = select.selectedIndex;
  selectIndex = selectIndex <= num ? selectIndex : num;
  select.innerHTML = '';
  for(var i = 0; i <= num; i++) {
    var e = document.createElement('option');
    select.appendChild(e);
    e.value = i;
    e.text = i==0 ? 'None' : i;
  }
  select.selectedIndex = selectIndex;
}

function disableOptions(select, numToDisable) {
  for(var i = select.length - numToDisable; i < select.length; i++) {
    select.options[i].disabled = true;
    select.options[i].className = "disabled";
  }
  var maxSelect = select.length - numToDisable - 1;
  if(select.selectedIndex > maxSelect)
    select.selectedIndex = maxSelect;
}