/*
 * address.js
 */

CAYMANCHEM.address = {
    /*
     * Invoke from state select's change handler to adjust country select referenced in data-countryregionid attribute
     *
     * (Caller responsible for revalidation and any wizard height adjustment)
     */
    adjustCountryRegionId: function(input){
        //  Currently selected state
        var state = $(input).val();

        //  Country for currently selected state
        var countryRegionId = $(input).find(':selected').attr('data-countryregionid')

        //  jQuery selector for country select element
        var countrySelect = '#' + $(input).attr("data-countryregionid");

        //  Conditional logging
        if( typeof(console) === 'object') console.log('changing ' + countrySelect + ' to ' + countryRegionId + ' based on state ' + state);

        //  Adjust
        $(countrySelect + " option[value='" + countryRegionId + "']").attr('selected','selected');
    },

    /*
     * Invoke from country select's change handler to adjust state select referenced in data-state attribute
     *
     * (Caller responsible for revalidation and any wizard height adjustment)
     */
    adjustState: function(input){
        //  Currently selected country
        var countryRegionId = $(input).val();

        //  id for associated state select
        var stateSelectId = $(input).attr("data-state");

        //  jQuery selector for select element
        var stateSelector = '#' + stateSelectId;

        //  jQuery selector for select element and all associated elements (only label for now)
        var stateSelectors = stateSelector + ',label[for="' + stateSelectId + '"]';

        if( countryRegionId === 'US' || countryRegionId == 'CA' ) {
            //  Require states or provinces for this country

            //  Show and enable associated elements
            $(stateSelectors).show().removeAttr('disabled');

            //  Retrieve country associated with currently selected state or province
            var selectedCountryRegionId = $(stateSelector + ' option:selected').attr('data-countryregionid');

            //  Reset to "Pick a state" if selected state or province not appropriate
            if( selectedCountryRegionId !== countryRegionId ) {
                $(stateSelector + " option").eq(0).attr('selected','selected');
            }

        } else {
            //  Forbid states or provinces for this country

            //  Hide and disable associated elements
            $(stateSelectors).hide().attr('disabled','disabled');
        }
    },

    doctor: function(options){
        $.ajax({
            url: options.url,
            context: options.context,
            data: options.data,
            dataType: 'json',
            success: options.success,
            error: function(xmlHttpRequest,textStatus,errorThrown){
                alert(textStatus);
            },
            complete: function(xmlHttpRequest,textStatus){
            }
        })
    },

    dummy: true
}



