﻿function loadComboBoxes()
{
    
    var selectBrandByManufacturerCountryElement = document.getElementById("selectBrandByManufacturerCountry");
    var selectCountryElement = document.getElementById("selectCountry");
    var selectBrandElement = document.getElementById("selectBrand");
    
    var indexElements = document.getElementById('menuListTable').getElementsByTagName('*');
    var detailsElements = document.getElementById('detailsList').getElementsByTagName('*');

    var i;
    var elementId;
    var stringIndex;
    var country, option;

    if (selectCountryElement)
    {
        // Clearing (fix)  previous options
        selectCountryElement.innerHTML = "";
        
        // running on index elements
        for (i = 0;  i < indexElements.length;  i++)
        {
            elementId = indexElements[i].id;
            
            if (    (elementId != null)  &&
                    (elementId.indexOf("Retailers") == -1) &&
                    (elementId.indexOf("Food-Service") == -1)
                )
            {     
                // checking if current element represents a country 
                stringIndex = elementId.indexOf(" index");
                
                if (stringIndex != -1)
                {
                    country = elementId.substr(0, stringIndex);
                    option = document.createElement('option');
                    option.text = country;
                    option.value = country + " index";
                    
                    try
                    {
                        selectCountryElement.add(option, null);      // standards compliant; doesn't work in IE
                    }
                    catch(ex)
                    {
                       selectCountryElement.add(option);    // IE only
                    }
                }
            }
        }
    }
    
    if (selectBrandElement)
    {
        // Clearing (fix)  previous options
        selectBrandElement.innerHTML = "";
        
        // running on details elements
        for (i = 0;  i < detailsElements.length;  i++)
        {
            elementId = detailsElements[i].id;
            var elementClassName = detailsElements[i].className;
        
            // checking if current element represents a brand  
            if ( (elementClassName != null)  &&  (elementClassName == "brandDetailsBox") )
            {
                option = document.createElement('option');
                option.text = elementId;
                option.value = elementId;
                
                try
                {
                    selectBrandElement.add(option, null);      // standards compliant; doesn't work in IE
                }
                catch(ex)
                {
                   selectBrandElement.add(option);    // IE only
                }
                continue;  
            }
        }
    }
    
    // Adding initial options to countries box
    var option0 = document.createElement('option');
    option0.text = "List of countries...";
    option0.value = "";
    var option1 = document.createElement('option');
    option1.text = "--------------------------";
    option1.value = "";
    var option2 = document.createElement('option');
    option2.text = "USA";
    option2.value = "USA index";
    var option3 = document.createElement('option');
    option3.text = "UK";
    option3.value = "UK index";
    var option4 = document.createElement('option');
    option4.text = "Canada";
    option4.value = "Canada index";
    var option5 = document.createElement('option');
    option5.text = "China";
    option5.value = "China index";
    var option6 = document.createElement('option');
    option6.text = "India";
    option6.value = "India index";
    var option7 = document.createElement('option');
    option7.text = "--------------------------";
    option7.value = "";
    
    try
    {
        selectCountryElement.add (option7, selectCountryElement.options[0]);
        selectCountryElement.add (option6, selectCountryElement.options[0]);
        selectCountryElement.add (option5, selectCountryElement.options[0]);
        selectCountryElement.add (option4, selectCountryElement.options[0]);
        selectCountryElement.add (option3, selectCountryElement.options[0]);
        selectCountryElement.add (option2, selectCountryElement.options[0]);
        selectCountryElement.add (option1, selectCountryElement.options[0]);
        selectCountryElement.add (option0, selectCountryElement.options[0]);
        
    }
    catch(ex)   // IE
    {
        selectCountryElement.add (option7, 0);
        selectCountryElement.add (option6, 0);
        selectCountryElement.add (option5, 0);
        selectCountryElement.add (option4, 0);
        selectCountryElement.add (option3, 0);
        selectCountryElement.add (option2, 0);
        selectCountryElement.add (option1, 0);
        selectCountryElement.add (option0, 0);
    }
    selectCountryElement.selectedIndex = 0;
   // In case the user chose a "states country" before the page was fully loaded:
    document.getElementById("selectState").disabled = true;
    
    
    
    // Sorting and adding initial options to brands box
    sortSelectBox(selectBrandElement);
    
    option0 = document.createElement('option');
    option0.text = "List of brands...";
    option0.value = "";
    option1 = document.createElement('option');
    option1.text = "----------------------";
    option1.value = "";
    try
    {
        selectBrandElement.add (option1, selectBrandElement.options[0]);
        selectBrandElement.add (option0, selectBrandElement.options[0]);
    }
    catch(ex)   // IE
    {
        selectBrandElement.add (option1, 0);
        selectBrandElement.add (option0, 0);
    }
    selectBrandElement.selectedIndex = 0;
}
