/* create <option value="$optionValue">$optionText</option> */

function createOption(selectId, optionIndex, optionValue, optionText, sClassName) 
{            
    var source = $("#"+selectId+"");
    var options = $("option", source);
        
    var sOptionsElements = "";
    for (var i=0; i<= optionIndex; i++)
    {
        sOptionsElements += '<option value="'+i+'">'+i+'</option>';
    } 
	    
	$('#'+sClassName+'').html("");
    $('#'+sClassName+'').append(sOptionsElements).resetSS();
}

function deleteOption(selectId, optionIndex, optionValue, optionText, sClassName) 
{    
    var source = document.getElementById(selectId);
    //alert(optionIndex)
    source.options[optionIndex] = null;   
    
    $('#'+sClassName+'').resetSS();    
}

// update passenger numbers with pre-selected values
function updatePassengerForm() {
    
  var numAdults = document.getElementById("ddlAdults");
  var numChildren = document.getElementById("ddlChildren");
  var numInfants = document.getElementById("ddlInfants");
  if (numAdults) numAdults.value = numAdultsSelected;
  if (numChildren) numChildren.value = numChildrenSelected;
  if (numInfants) numInfants.value = numInfantsSelected;
}



// dynamic passenger dropdowns
function updatePassengerNumbers() {

  var numAdults = document.getElementById("ddlAdults");
  var numChildren = document.getElementById("ddlChildren");
  var numInfants = document.getElementById("ddlInfants");

  updateChildNumbers();
}

/**
 updateChildNumbers() - 
 ensure: 
  numChildren + numAdults <= 9
 */
function updateChildNumbers() {
  var numAdults = document.getElementById("ddlAdults");
  var numChildren = document.getElementById("ddlChildren");
  var numInfants = document.getElementById("ddlInfants");
  
  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--) 
                {
                    //alert("infants: " + i);
                    deleteOption('ddlInfants', i, i, i, "ddlInfants");
                    //numInfants.options[i] = null;
                }
            } 
            else 
            {
               
                for (var i = numInfants.length; i <= maxInfants; i++) 
                {
                    createOption('ddlInfants', i, i, i, "ddlInfants");
                }
            }
        }
        
        // ensure max 9 passengers
        var maxChildren = maxPassengerCount - numAdults.value; 
        
        if (numChildren.length - 1 < maxChildren) 
        {
            for (var i = numChildren.length; i <= maxChildren; i++) 
            {
                createOption('ddlChildren', i, i, i, "ddlChildren");
            }
        } 
        else 
        {
            for (var i = numChildren.length; i > maxChildren; i--) 
            {
                //alert("kids1: " + i);
                deleteOption('ddlChildren', i, i, i, "ddlChildren");
                //numChildren.options[i] = null;
            }
        }
    }
    else 
    {
        //alert("remove");
        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;
    }
  }
}

// disableAdults( true | false )  -- disables / enables adult/children/infant dropdown
function disableAdults(disable) {
  var numAdults = document.getElementById("ddlAdults");
  var numChildren = document.getElementById("ddlChildren");
  var numInfants = document.getElementById("ddlInfants");
}

// 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);
}
