!function(a,b){"use strict";function c(a,d){return"string"!=typeof a||""===b.trim(a)?void d(a,{statesProvices:{},addressUsedFields:[]}):c.cache.hasOwnProperty(a)?void d(a,c.cache[a]):(d(a,{statesProvices:{},addressUsedFields:[]}),void b.ajax({cache:!0,data:{countryCode:a,activeLocale:CCM_ACTIVE_LOCALE},dataType:"json",method:"GET",url:CCM_DISPATCHER_FILENAME+"/ccm/system/country-data-link/all"}).fail(function(b,d,e){window.console&&window.console.error&&window.console.error(b.responseJSON||e),c.cache[a]={statesProvices:{},addressUsedFields:[]}}).success(function(b){var d={};b.statesProvices instanceof Object&&(d=b.statesProvices);var e=[];b.addressUsedFields instanceof Array&&(e=b.addressUsedFields),c.cache[a]={statesProvices:d,addressUsedFields:e}}).always(function(){d(a,c.cache[a])}))}function d(a){var c=this;c.enabled=!1,c.$text=a,c.$select=b("<select />"),f?c.mutationObserver=new window.MutationObserver(function(a){c.updateSelectAttributes(),c.$text.hide(),c.$select.show()}):c.mutationObserver=null,c.originalFocus=c.$text[0].focus,c.$text[0].focus=function(){c.enabled?c.$select.focus():c.originalFocus.apply(c.$text[0])}}function e(a,b,c){var e=this;e.$country=a,e.$stateprovinceWrapper=b,b.is("input")?e.$stateprovince=b:e.$stateprovince=b.find("input:first"),e.config=c,e.replacer=new d(e.$stateprovince),e.$stateprovinceSelect=e.replacer.$select,e.$country.on("change",function(){e.countryChanged()}),e.$stateprovinceSelect.on("change",function(){e.$stateprovince.val(e.$stateprovinceSelect.val()).trigger("change")}),e.countryChanged(!0)}if(!a.ccmCountryDataLink){var f=!!(window.MutationObserver&&window.MutationObserver.prototype&&window.MutationObserver.prototype.observe);c.cache={},d.prototype={updateSelectAttributes:function(){var a=this;b.each(["class","style","required"],function(b,c){var d=a.$text.attr(c);"string"==typeof d&&a.$select.attr(c,d)})},setEnabled:function(a){var b=this;a=!!a,a!==b.enabled&&(a?(b.updateSelectAttributes(),b.$text.before(b.$select),b.$text.hide(),b.enabled=!0,null!==b.mutationObserver&&setTimeout(function(){b.enabled===!0&&(b.mutationObserver.disconnect(),b.mutationObserver.observe(b.$text[0],{attributes:!0}))},0)):(null!==b.mutationObserver&&b.mutationObserver.disconnect(),b.enabled=!1,b.$select.detach(),b.$text.show()))}},e.prototype={countryChanged:function(a){var d=this;c(d.$country.val(),function(c,e){if(d.$country.val()===c){d.$stateprovinceSelect.empty(),!a&&d.config.clearStateProvinceOnChange&&d.$stateprovince.val(""),d.config.hideUnusedStateProvinceField&&(e.addressUsedFields.indexOf("state_province")>-1?d.$stateprovinceWrapper.show():d.$stateprovinceWrapper.hide());var f=Object.keys(e.statesProvices).length;if(0===f)d.replacer.setEnabled(!1);else{var g=b.trim(d.$stateprovince.val());d.$stateprovinceSelect.append(b('<option value="" selected="selected" />').text("")),b.each(e.statesProvices,function(a,c){var e=b("<option />").val(a).text(c);a===g&&e.attr("selected","selected"),d.$stateprovinceSelect.append(e)}),d.replacer.setEnabled(!0)}d.$country.trigger("country-data",[e])}})}},e.withCountryField=function(a,c){c=b.extend({hideUnusedStateProvinceField:!1,clearStateProvinceOnChange:!1},c);var d=a.closest("form");0===d.length&&(d=b(document.body));var f=[];switch(d.find('[data-countryfield="'+a.attr("id")+'"]').each(function(){f.push(new e(a,b(this),c))}),f.length){case 0:return null;case 1:return f[0];default:return f}},a.ccmCountryDataLink=e}}(this,jQuery);