/// <reference path="jquery-1.2.6.js" />

function EmptySelect(objSelect) {
    while (objSelect.options.length > 0)
        objSelect.options[0] = null;
    // When an option is deleted the option array is 
    // recreated. Hence deleting the first item each time 
    // at least one exists will ensure that all are deleted.
}

function nextDropDown(firstObj, secondObj, strArray, intIndex, selectedValue) {
    var intLoop = 0;
    var strTemp = "";
    var intCheckIndex = 0;
    var found = false;
    intCheckIndex = 0;

    InsertOption(secondObj, 0, "Select a region");
    InsertOption(secondObj, 0, "Any");
    {
        for (intLoop = 0; intLoop < strArray.length; intLoop++) {
            var objOpt;
            if (intIndex == 0) {
                objOpt = InsertOption(secondObj, strArray[intLoop][2], strArray[intLoop][1]);
            }
            else {
                if (strArray[intLoop][intCheckIndex] == intIndex) {
                    objOpt = InsertOption(secondObj, strArray[intLoop][2], strArray[intLoop][1]);
                }
            }

            //reselect if found
            if (objOpt && objOpt.value == selectedValue) {
                found = true;
                objOpt.setAttribute("selected", "selected");
            }
        }
    }

    if (!found)
        secondObj.selectedIndex = 0;
}

function InsertOption(objSelect, varValue, varText) {
    var objOption = new Option(varText, varValue);
    objSelect.options[objSelect.options.length] = objOption;

    return objOption;
}

function starterDropDown(firstSelect, secondSelectID, strArray) {
    var selectedID = firstSelect[firstSelect.selectedIndex].value;


    var secondObj = document.getElementById(secondSelectID);
    var selectedValue = $(secondObj).val();

    EmptySelect(secondObj)
    nextDropDown(firstSelect, secondObj, strArray, selectedID, selectedValue);
}
