/**
 * Finds all SELECT lists with the given name, and selects the given option.
 * It is expected that all lists with the given name have identical list items.
 *
 * @var string listName
 *   The DOM name of the lists.
 * @var integer optionValue
 *   The selectedIndex of the desired list option. The first option's index is
 *   always 0.
 * @var boolean isLiteral
 *   If true, then the optionValue is taken as a literal value, and the
 *   selectedIndex is determined by subtracting the list's first option
 *   from the given optionValue. Optional.
 */
function setOption(listName, optionValue, isLiteral) 
{
    var i;
    var items;
    items = document.getElementsByName(listName);
    
    // Normally, optionValue is just the desired selectedIndex.
    // But if it is a literal value, then we must do some more work to find
    // the real selectedIndex.
    if (isLiteral) {
        if (optionValue >= items[0].options[0].value) {
            // Subtract the value of the first list option,
            // to get the index of the desired option.
            optionValue = optionValue - items[0].options[0].value;
            if (optionValue >= items[0].options.length) {
                optionValue = items[0].options.length-1;
            }
        } else {
            // The list options are in some unexpected format, so we will fall
            // back on doing a sequential search of the entire list.
            // Slower, but reliable.
            optionValue = items[0].selectedIndex;
            for (i=0; i<items[0].length; i++) {
                if (items[0].options[i].value == optionValue) {
                    optionValue = i;
                    break;
                }
            }
        }
    }
    
    // Finally, get all our lists to select the chosen item.
    for (i=0; i<items.length; i++) {
        items[i].selectedIndex = optionValue;
    }
}


var today = new Date();

// Default: arrive at dest tomorrow
var arrival = new Date();
arrival.setDate(today.getDate() + 1);

// Default: depart from there a week from today
var departure = new Date();
departure.setDate(arrival.getDate() + 7);

setOption('AD', arrival.getDate()-1); // getDate() begins at 1
setOption('DD', departure.getDate()-1);
setOption('AM', arrival.getMonth()); // getMonth() begins at 0
setOption('DM', departure.getMonth());

setOption('AY', arrival.getFullYear(), true);
setOption('DY', departure.getFullYear(), true);
