// update passenger numbers with pre-selected values
function updatePassengerForm() {
  var numAdults = document.getElementById("numAdults");
  var numChildren = document.getElementById("numChildren");
  var numInfants = document.getElementById("numInfants");
  var numChildren2 = document.getElementById("numChildren2");
  
  
  if (numAdults) numAdults.value = numAdultsSelected;
  if (numChildren) numChildren.value = numChildrenSelected;
  if (numInfants) numInfants.value = numInfantsSelected;
  if (numChildren2) numChildren2.value = numChildren2Selected
}


// dynamic passenger dropdowns
function updatePassengerNumbers(el) {
	
  var numAdults = document.getElementById("numAdults");
  var numChildren = document.getElementById("numChildren");
  var numInfants = document.getElementById("numInfants");
  var numChildren2 = document.getElementById("numChildren2");

  updateChildNumbers(el);
}

/**
 updateChildNumbers() - 
 ensure: 
  numChildren + numAdults <= 9
 */
function updateChildNumbers(el) {
	
  var numAdults = document.getElementById("numAdults");
  var numChildren = document.getElementById("numChildren");
  var numInfants = document.getElementById("numInfants");
  var numChildren2 = document.getElementById("numChildren2");
  
  
  var selectedChildren = numChildren.value;  
  if (numAdults.value > 0) {
    // ensure max numInfants = numAdults
   
    if (numAdults.value != numInfants.length - 1) {
	    var maxInfants = (numAdults.value > maxInfantCount ? maxInfantCount : numAdults.value);
      if (numInfants.length > numAdults.value) {
        for (var i = numInfants.length; i > maxInfants; i--) {
          numInfants.options[i] = null;
        }
      } else {
        for (var i = numInfants.length; i <= maxInfants; i++) {
          createOption('numInfants', i, i, i);
        }
      }
    }
    
    // ensure max 9 passengers
    var maxChildren = maxPassengerCount - numAdults.value;
    
    if (el==null || el==numAdults) 
    {
    	removeOptions(numChildren);
    	removeOptions(numChildren2);
    	if (numChildren2.length - 1 < maxChildren) {
    		for (var i=numChildren2.length; i <= maxChildren; i++) {
    			createOption('numChildren2', i, i, i);
    		}
    	}	

    	if (numChildren.length - 1 < maxChildren) {
    		for (var i=numChildren.length; i <= maxChildren; i++) {
    			createOption('numChildren', i, i, i);
    		}
    	}    
    } else if (el == numChildren) 
    {
    	
    	var temp = numChildren2.value ;    		
    		
		removeOptions(numChildren2);
		for (var i=1; i <= (maxChildren - numChildren.value); i++) {
			createOption('numChildren2', i, i, i);
		}
		numChildren2.value = temp;
    	 
    	
    }	else if (el == numChildren2) 
    {
    	
    	var temp = numChildren.value ;    		
    		    	

		removeOptions(numChildren);
		for (var i=1; i <= (maxChildren - numChildren2.value ); i++) {
			createOption('numChildren', i, i, i);
		}

    	numChildren.value = temp;

    }	
    
  } else {	  
	  removeOptions(numChildren);
	  removeOptions(numChildren2);	    
	  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;
    }
  }
}

// disableAdults( true | false )  -- disables / enables adult/children/infant dropdown
function disableAdults(disable) {
	alert("hi");
  disableFormElement("numAdults",disable);
  disableFormElement("numChildren",disable);
  disableFormElement("numInfants",disable);
}

// disableSeniors( true | false ) -- disables / enables senior dropdown
function disableSeniors(disable) {
  disableFormElement("numSeniors",disable);
}

// disableYouths( true | false ) -- disables / enables youth dropdown
function disableYouths(disable) {
  disableFormElement("numYouths",disable);
}

