/* Controls state dropdowns */
var provinces = [3, 6, 23, 33, 38, 41, 42, 43, 46, 49, 50, 52, 64];

$(document).ready(function() {
    
    // Shipping form adjustments: Add the text 'optional' to the last 3 fields for address forms.
    $('#id_suite').parent().append('<span class="optional-text">(optional)</span>');
    $('#id_floor').parent().append('<span class="optional-text">(optional)</span>');
    $('#id_special_instructions').parent().append('<span class="optional-text">(optional - e.g. ATTN: Mike Smith)</span>');
    $('#id_gift').after('<span class="optional-text">&nbsp;&nbsp;If this is a gift, we will omit the dollar amounts on the invoice.</span>');

    $('select[id*=country]').each(function() {
        update_state(this, true);    
        $(this).change(function() {
            update_state(this);
        });
    });
});

function update_state(obj, initial) {
    my_name = obj.name;
    my_state = my_name.replace('country', 'state');
    index = $(obj).attr('selectedIndex');
    state_field = $('select[name='+my_state+']');
    
    if (initial) {
        state_field.children('option').each(function() {
            if (jQuery.inArray(Number(this.value), provinces) != -1) {
                $(this).addClass('CAN');
            }
            else {
                $(this).addClass('USA');
            }
        });
    }

    if (index == 1 || index == 2) {
        state_field.attr('disabled', '');
        if (index == 1){
            // Show only provinces.
            state_field.children('option.CAN').show();
            state_field.children('option.USA').hide();
        }
        else {
            state_field.children('option.CAN').hide();
            state_field.children('option.USA').show();
        }
    }
    else {
        state_field.attr('disabled', 'disabled');
        state_field.attr('selectedIndex', 0);
    }
}
