function initPassengers() {
  numAdults = document.getElementById("numAdults");
  numChildren = document.getElementById("numChildren");
  numInfants = document.getElementById("numInfants");

  hasAdults = (numAdults && numAdults.value != 0 ? true : false);
  hasChildren = (numChildren && numChildren.value != 0 ? true : false);
  hasInfants = (numInfants && numInfants.value != 0 ? true : false);
}

// update passenger numbers with pre-selected values
function updatePassengerForm() {
  initPassengers();
  if (numAdults) numAdults.value = numAdultsSelected;
  if (numChildren) numChildren.value = numChildrenSelected;
  if (numInfants) numInfants.value = numInfantsSelected;
  if (numAdultsSelected + numChildrenSelected + numInfantsSelected == 0) {
    numAdults.value = 1;
  }  
}


// dynamic passenger dropdowns
function updatePassengerNumbers() {
  initPassengers();
  updateChildNumbers();
}

/**
 updateChildNumbers() - 
 ensure: 
  numInfants = numAdults 
  numChildren + numAdults <= 7
  max 1 child per adult if without infant
 */
function updateChildNumbers() {
  
  if (numAdults.value != 0) {
    if (numAdults.value == 7) {
  	  removeOptions(numChildren);
  	}
    // ensure max numInfants = numAdults
    if (numAdults.value != numInfants.length - 1) {
      if (numInfants.length - 1 > numAdults.value) {
        for (var i = numInfants.length; i > numAdults.value; i--) {
          numInfants.options[i] = null;
        }
      } else {
        for (var i = numInfants.length; i <= numAdults.value; i++) {
          createOption('numInfants', i, i, i);
        }
      }
    }
    
    var maxChildren = 7 - numAdults.value;
    if (numChildren.length - 1 < maxChildren) {
      for (var i = numChildren.length; i <= maxChildren; i++) {
        createOption('numChildren', i, i, i);
      }
    } else {
      for (var i = numChildren.length; i > maxChildren; i--) {
        numChildren.options[i] = null;
      }
    }
    
  } else {
    removeOptions(numChildren);
    removeOptions(numInfants);
  }
}

// remove all options but the first one
function removeOptions(el) {
  if (el) {
    var numOptions = el.options.length;
    for (var i = numOptions; i > 0; i--) {
      el.options[i] = null;
    }
  }
}
