/* 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 numChildren2 = document.getElementById("ddlChildren2");
  var numInfants = document.getElementById("ddlInfants");
  if (numAdults) numAdults.value = numAdultsSelected;
  if (numChildren) numChildren.value = numChildrenSelected;
  if (numChildren2) numChildren2.value = numChildren2Selected;
  if (numInfants) numInfants.value = numInfantsSelected;
}
// dynamic passenger dropdowns
function updatePassengerNumbers(el) {

  var numAdults = document.getElementById("ddlAdults");
  var numChildren = document.getElementById("ddlChildren");
  var numChildren2 = document.getElementById("ddlChildren2");
  var numInfants = document.getElementById("ddlInfants");
  updateChildNumbers(el);
}

/**
 updateChildNumbers() - 
 ensure: 
  numChildren + numAdults <= 9
 */
function updateChildNumbers(el) {
  var numAdults = document.getElementById("ddlAdults");
  var numChildren = document.getElementById("ddlChildren");
  var numChildren2 = document.getElementById("ddlChildren2");
  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--) 
                {
                    deleteOption('ddlInfants', i, i, i, "ddlInfants");
                }
            } 
            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 (el==null || el==numAdults) 
        {
            AdjustChildren(numChildren, maxChildren, 'ddlChildren');    
            AdjustChildren(numChildren2, maxChildren, 'ddlChildren2');    
        } 
        else if (el == numChildren) 
        {
        	var restChildren = maxChildren - numChildren.value;	
		    AdjustChildren(numChildren2, restChildren, 'ddlChildren2');
        }	
        else if (el == numChildren2) 
        {
    	    var restChildren = maxChildren - numChildren2.value;	
    	    AdjustChildren(numChildren, restChildren, 'ddlChildren');    
        }        
    }       
    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) {
  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);
}

function AdjustChildren(el, restChildren, elname)
{		 
   if (el.length -1 < restChildren)
    {
        for (var i = el.length; i <= restChildren; i++) 
        {
            createOption(elname, i, i, i, elname);
        }
    }
    else
    {
        for (var i = el.length; i >restChildren; i--) 
        {
            deleteOption(elname, i, i, i, elname);                
        }   
    }
}
