/**
 * UICC Country Selection Function
 * 
 * @package All_Packages
 * @subpackage PresentationScripts
 * @copyright UICC 2007
 * @author FC
 */
// United States
var usdb = new Array(
    {   name: 'United States',  states: ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY']}
  , {   name: 'US Territories', states: ['AS', 'FM', 'GU', 'MH', 'MP', 'PR', 'PW', 'VI']}
  , {   name: 'US Military',    states: ['AA', 'AE', 'AP']}
);
// Canada
var cadb = new Array('AB', 'BC', 'MB', 'NB', 'NF', 'NS', 'NT', 'NU', 'ON', 'PE', 'QC', 'SK', 'YT');
var lbl_please_select = "(please select)";
var lbl_none = "(none)";

/**
 * Update the state selector and the postal code field together with the country selector (=chooser)
 *
 * @param Object    chooser                         The country selector
 * @param string    prefix                          The field prefix
 * @param array     countries_without_postal_code   The codes of the countries that do not have postal codes
 */
function updateCountry(chooser, prefix, countries_without_postal_code) {
    var stateChooser        = chooser.form.elements[prefix+"state"];
    var zipField            = chooser.form.elements[prefix+"zip"];
    var stateChooserLabel   = document.getElementById(prefix+"state_label");
    var zipFieldLabel       = document.getElementById(prefix+"zip_label");
    var stateChooserStar    = document.getElementById(prefix+"state_star");
    var zipFieldStar        = document.getElementById(prefix+"zip_star");
    
    // Counter variables
    var i = 0, j = 0, k = 0;
    
    // Empty previous settings: 
    // Javascript re-indexs after each null or removeChild()
    // so we have to count backwards
    for(i = stateChooser.options.length - 1 ; i >= 0 ; i--)
    {
        stateChooser.options[i] = null;
    }
    var optgroups = stateChooser.childNodes;
    for(i = optgroups.length - 1 ; i >= 0 ; i--)
    {
        stateChooser.removeChild(optgroups[i]);
    }
    // zipField.value = '';
    
    // Get chosen value to act as index of statedb hash table
    var choice = chooser.options[chooser.selectedIndex].value;
    if ('USA' == choice) {
        k = 0; // option counter
        stateChooser.options[k++] = new Option(lbl_please_select, "", true, false);
        // This works on Firefox only: stateChooser.appendChild(new Option("(please select)", "", true, false));
        for (i = 0 ; i < usdb.length ; i++) {
            var optG = document.createElement("optgroup");
            optG.label = usdb[i].name;
            stateChooser.appendChild(optG);
            for (j = 0 ; j < usdb[i].states.length ; j++) {
                stateChooser.options[k++] = new Option(usdb[i].states[j], usdb[i].states[j]);
                // This works on Firefox only: optG.appendChild(new Option(usdb2[i][j], usdb2[i][j]));
            }
        }
        stateChooserStar.className = "col1";
        stateChooserLabel.className = "col2_red";
        stateChooser.disabled = false;
    } else if ('CAN' == choice) {
        stateChooser.options[0] = new Option(lbl_please_select, "", true, false);
        for (i = 0 ; i < cadb.length ; i++) {
            stateChooser.options[i + 1] = new Option(cadb[i], cadb[i]);
        }
        stateChooserStar.className = "col1";
        stateChooserLabel.className = "col2_red";
        stateChooser.disabled = false;
    } else {
        stateChooser.options[0] = new Option(lbl_none, 'none', true, false);
        stateChooserStar.className = "col1_invisible";
        stateChooserLabel.className = "col2";
        stateChooser.disabled = true;
    }
    if (inArray(choice, countries_without_postal_code)) {
        zipFieldStar.className = "col1_invisible";
        zipFieldLabel.className = "col2";
        // zipField.disabled = true;
    } else {
        zipFieldStar.className = "col1";
        zipFieldLabel.className = "col2_red";
        // zipField.disabled = false;
    }
}

