/**
 * state_codes javascript to handle the ajax stuff.
 *
 * 
 * 
 * 
 * 
 * @license     LGPL
 *
 * @package     SiMech
 * @subpackage  Javascript
 *
/**/

// This is only used from... country_codes?
    function update_states(prefix) {
        var country = get_element(prefix+'country');
        var state   = get_element(prefix+'state');
        var other   = get_element(prefix+'other_state');
    // Get the current value
        var cur_value = other.value.trim().toUpperCase();
        if (!cur_value)
            cur_value = state.options[state.selectedIndex].value.trim().toUpperCase();
        if (!cur_value)
            cur_value = '';
    // Get the states for the selected country
        var country_code = country.options[country.selectedIndex].value;
        var ajax_response = new Ajax.Request(
            '/ajax/state_codes.php',
            {
                  parameters: "country="+country_code,
                      method: "get",
                asynchronous: false
            });
        state.options.length = 0;
        var state_list = ajax_response.transport.responseText.split("\n");

        var found = false;
        if (state_list.length > 1) {
            for ( key in state_list) {
            // We need to make sure that the type is a string, and that it's longer then a null string
                if (typeof state_list[key] == 'string' && state_list[key].length > 0) {
                    var state_option = state_list[key].split("-");
                    if (state_option[0].trim().toUpperCase() == cur_value) {
                        state.options[state.options.length] = new Option(state_option[1], state_option[0], true, true);
                        found = true;
                    }
                    else {
                        state.options[state.options.length] = new Option(state_option[1], state_option[0]);
                    }
                }
            }
            state.options[state.options.length] = new Option("-", "");
            state.options[state.options.length-1].disabled = true;
        }
    // Make sure the "other" option and its accompanying text input box get set properly
        if (found || !cur_value) {
            state.options[state.options.length] = new Option("Other:", "");
            other.value = '';
        }
        else {
            state.options[state.options.length] = new Option("Other:", "", true, true);
            other.value = cur_value;
        }
    }

