(function(n){n.addressLookup=function(t){function c(n,t){var i=function(){};this.name=n;this.setup=t.setup||i;this.cleanup=t.cleanup||i;this.execute=t.execute||i;this.eventLabel=t.eventLabel||"unknown"}var a={sourceField:null,lookupUrl:null,lookupQueryFieldName:null,lookupWait:null,lookupFailed:null,dropDownItemField:null,dropDownContainer:null,mapFields:{},findButton:null,findText:"Find my address",retryButton:null,retryText:"Search again",selectButton:null,selectText:"Select",NoAddressFoundText:"No address found",validator:function(n){var t=$.trim(n)!="";return{isValid:t,message:t?null:"No postal code was entered."}},validatorField:null},i={},o,u;$.extend(i,a,t);i.dropDownPanelClosedEventName="dropDownPanelClosed";o=new function(){var n=this;this.getSearchPattern=function(){return $(i.sourceField).val()};this.validateSearchField=function(){var t=i.validator(n.getSearchPattern());return $(i.validatorField).attr("value",t.isValid?"":t.message),t};this.setAddressFields=function(n){var t,r;for(t in n)r=i.mapFields[t],$(r).val(n[t])};this.dropdownpanel=function(n){function o(i){var r=$(n).offset();r.top+=$(n).height()+5;r.width=$(n).width();r.height="200px";$(t).css({width:r.width,"max-heigth":r.height,top:i=="absolute"?r.top:"",left:i=="absolute"?r.left:"",position:i||"absolute","margin-bottom":"5px"})}function s(n){var t,r;if(i.dropDownItemField)return n[i.dropDownItemField];t=[];for(r in n)t.push(n[r]);return t.join(" ")}var t=document.createElement("div");$("body").append(t);$(t).css({"z-index":1,"background-color":"#ffffff",border:"1px solid #ccc",height:"200px",display:"none","overflow-y":"auto"});var r=null,e=i.dropDownContainer,u=!0,f={show:function(n,h){var c,l,a;o(e?"relative":"absolute");e&&$(i.dropDownContainer).append(t).show();n&&$(t).empty();c="data-index";for(l in n)a=document.createElement("div"),$(a).append(s(n[l])).css({padding:"2px"}).click(function(){$(t).find("div").each(function(){$(this).css({"background-color":""})});$(this).css({"background-color":"#eee"});r=$(this).attr(c);h!=null&&typeof h=="function"&&h.call(f,r)}).attr(c,l),$(t).append(a);$(t).css({display:"block"});u=!1},hide:function(){u||($(t).css({display:"none"}).trigger(i.dropDownPanelClosedEventName),$(i.dropDownContainer).hide(),u=!0)},getSelectedId:function(){return r}};return $("body").click(function(n){var r=$(n.target);r.is($(t))||r.is($(i.selectButton))||$(t).has(r).length>=1||f.hide()}),f}};u={fail:"$_fail",success:"$_success"};var r=new function(n){function t(n){return typeof n=="string"?n:n.name}var f=this,i={},r={};this.register=function(n,r,u){n.constructor===c&&(i[t(n.name)]={item:n,next:r,fail:u,triggers:[]})};this.trigger=function(e,o,s){var h=i[t(e)],c=function(){var e=o,c=s,i=h;return i.cleanup=function(){for(var n in i.triggers)i.triggers[n].off();i.item.cleanup()},{on:function(){n(document).on(c,e,function(){var o=[],s=function(){var r=new function(){var t=[],i=this;this.enqueue=function(r){return r==null?i:(typeof r.promise=="function"?t.push(r):t.push(n.Deferred().done(function(){o.push(typeof r=="function"?r():r)})),i)};this.waitAll=function(){t=t.reverse();var i=function(n){n!=null&&(n.done(function(){i(t.pop())}),n.resolve&&n.resolve())};return i(t.pop()),n.when.apply(n,t)}},t={enqueue:function(n){return r.enqueue(n),t}},u=i.item.execute(t,n(e),c);return u!==t&&t.enqueue(u),r.waitAll()};s().done(function(){var l,e,s,c;i.cleanup();l=n(o).last()[0];e=l==u.success;f.start(t(e?h.next:h.fail));e&&(s=i.item.eventLabel,c=r[s],c!=null&&c.call({},s))})})},off:function(){n(document).off(c,e)}}}();h.triggers.push(c)};this.start=function(n){var r=i[t(n)],u;r.item.setup();for(u in r.triggers)r.triggers[u].on()};this.on=function(n,t){r[n]=t}}(n),f=new function(n){var t=[],r={},u=o.dropdownpanel(i.sourceField);this.lookup=function(r){var u={},f;return u[i.lookupQueryFieldName]=r,f={url:i.lookupUrl,data:u,cache:!0},n(i.lookupWait).show(),n.ajax(f).done(function(n){t=n}).fail(function(){alert("The address lookup request failed. Please try again later.")}).always(function(){n(i.lookupWait).hide()})};this.show=function(n){u.show(t,n)};this.shut=function(){u.hide()};this.getSelectedAddress=function(){return r=t[u.getSelectedId()],r=r||{}};this.hasAddress=function(){return t&&t.length>0}}(n),l={find:"find",select:"select",retry:"retry"},e=new c(l.find,{setup:function(){$(i.findButton).attr("value",i.findText);$(i.lookupWait).hide()},cleanup:function(){var n=$(i.lookupFailed);f.hasAddress()?(n.text(""),n.hasClass("field-validation-error")&&n.removeClass("field-validation-error")):(n.text(i.NoAddressFoundText),n.hasClass("field-validation-error")||n.addClass("field-validation-error"))},execute:function(n){var t=o.validateSearchField();return t.isValid?n.enqueue(f.lookup(o.getSearchPattern())).enqueue(function(){return f.hasAddress()?u.success:u.fail}):u.fail}}),s=new c(l.select,{setup:function(){$(i.selectButton).attr("value",i.selectText).hide();f.show(function(n){n&&$(i.selectButton).show()})},cleanup:function(){f.shut()},execute:function(n,t){if(!t.is(i.selectButton))return u.fail;var r=f.getSelectedAddress();return r?(o.setAddressFields(r),u.success):u.fail},eventLabel:"selected"}),h=new c(l.retry,{setup:function(){$(i.retryButton).attr("value",i.retryText).show()},cleanup:function(){return e.cleanup()},execute:function(n){return e.execute(n)}});return r.register(e,s,e),r.register(s,h,h),r.register(h,s,h),r.trigger(e,i.findButton,"click"),r.trigger(s,i.selectButton,"click"),r.trigger(s,i.dropDownContainer,i.dropDownPanelClosedEventName),r.trigger(h,i.retryButton,"click"),r.start(e),{on:function(n){for(var t in n)r.on(t,n[t])}}}})($)