!function(a,b){"use strict";function c(a,d){return"string"!=typeof a||""===b.trim(a)?void d(a,[]):c.cache.hasOwnProperty(a)?void d(a,c.cache[a]):(d(a,[]),void b.ajax({cache:!0,data:{countryCode:a,activeLocale:CCM_ACTIVE_LOCALE},dataType:"json",method:"GET",url:CCM_DISPATCHER_FILENAME+"/ccm/system/country-stateprovince-link/get_stateprovinces"}).fail(function(b,d,e){window.console&&window.console.error&&window.console.error(b.responseJSON||e),c.cache[a]=[]}).success(function(b){c.cache[a]=b instanceof Array?b:[]}).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){var c=this;c.$country=a,c.$stateprovince=b,c.replacer=new d(c.$stateprovince),c.$stateprovinceSelect=c.replacer.$select,c.$country.on("change",function(){c.countryChanged()}).trigger("change"),c.$stateprovinceSelect.on("change",function(){c.$stateprovince.val(c.$stateprovinceSelect.val()).trigger("change")})}if(!a.ccmCountryStateprovinceLink){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(){var a=this;c(a.$country.val(),function(c,d){if(a.$country.val()===c){a.$stateprovinceSelect.empty();var e=d.length;if(0===e)a.replacer.setEnabled(!1);else{var f=b.trim(a.$stateprovince.val());a.$stateprovinceSelect.append(b('<option value="" selected="selected" />').text(""));for(var g,h=0;h<e;h++)g=b("<option />").val(d[h][0]).text(d[h][1]),d[h][0]===f&&g.attr("selected","selected"),a.$stateprovinceSelect.append(g);a.replacer.setEnabled(!0)}}})}},e.withCountryField=function(a){var c=a.closest("form");0===c.length&&(c=b(document.body));var d=[];switch(c.find('input[data-countryfield="'+a.attr("id")+'"]').each(function(){d.push(new e(a,b(this)))}),d.length){case 0:return null;case 1:return d[0];default:return d}},a.ccmCountryStateprovinceLink=e}}(this,jQuery);