$(document).ready (function () {
    // Add a listener to the 'country' select box
    var country_select = $("#trial_country");
    var state_select = $("#trial_state");

    country_select.change (function () {
        // Get the list of states...
        state_select.html ("<option value='0' selected='selected'>Loading...</option>");
        disableStates ();

        // Now, lets load the states for this selected country...
        loadStates (country_select.val ());
    });
});

function disableStates () {
    $("#trial_state").attr ("disabled", true);
} // disableStates ()

function enableStates () {
    $("#trial_state").attr ("disabled", false);
} // enableStates ()

function loadStates (id) {
    // Load the data...
    $.getJSON ("recovery/freetrial/getstates/id/" + id, function (data) {
        if (data.success == true) {
            // Set up the states in the select area
            var state_select = $("#trial_state");

            var states = "<option value=''>Please select</option>";

            for (var i = 0; i < data.regions.length; i++) {
                var r = data.regions [i];

                states = states + "<option value='" + r.code + "'>" + r.name + "</option>";
            } // for ()

            state_select.html (states);

            enableStates ();
        } // if ()
        else {
            alert (data.message);
        } // else
    });
} // loadStates ()
