var Adoxio = Adoxio || {}; Adoxio.CanadaPost = Adoxio.CanadaPost || { CanadaPostUrls:{ AutoCompleteUrl: "//ws1.postescanada-canadapost.ca/AddressComplete/Interactive/Find/v2.10/json3ex.ws", AddressRetrieveDetailsUrl: "//ws1.postescanada-canadapost.ca/AddressComplete/Interactive/Retrieve/v2.10/json3.ws", CanadaPostLiquidTemplateUrl: window.location.origin + "/canadapostsettings/", }, CanadaPostSettings:{ Key: "PH44-GF93-HC71-BD66", Location: "", LocationAccuracy: "", Country: "Canada", LanguagePreference: "" }, FieldMapping:{ Id:null, DomesticId:null, Language:null, LanguageAlternatives:null, Department:null, Company:null, SubBuilding:null, BuildingNumber:null, BuildingName:null, SecondaryStreet:null, Street:null, Block:null, Neighbourhood:null, District:null, City:"city", Line1:"street-address", Line2:"street-address2", Line3:null, Line4:null, Line5:null, AdminAreaName:null, AdminAreaCode:null, Province:null, ProvinceName:"state", ProvinceCode:null, PostalCode:"postcode", CountryName:"country", CountryIso2:null, CountryIso3:null, CountryIsoNumber:null, SortingNumber1:null, SortingNumber2:null, Barcode:null, POBoxNumber:null, Label:null, Type:null, DataLevel:null, }, ConvertFieldtoCanadaPostLookup: function(name, placeholdertext, Settings, fieldMappings){ placeholdertext = placeholdertext != "" ? placeholdertext : "Address Lookup"; // create new select2 field $('').insertBefore($("#" + name)); $('#' + name).hide(); $("#" + name + "_canadapostlookup").select2({ allowClear: true, placeholder: placeholdertext, ajax: { url: Adoxio.CanadaPost.CanadaPostUrls.AutoCompleteUrl, dataType: 'json', delay: 250, data: function (term) { return { Key: Settings.Key, SearchTerm: term, Location: Settings.Location, LocationAccuracy: Settings.LocationAccuracy, Country: Settings.Country, LanguagePreference: Settings.LanguagePreference }; }, results: function (data, params) { return { results: $.map(data.Items, function(obj) { return { id: obj.Id, text: obj.Text + " " + obj.Description, IsRetrievable: obj.Next }; }) }; } }, minimumInputLength: 3, formatInputTooShort: function () { return Settings.SearchCharacterLimitText; } }).on("select2-selecting", function(e) { var id = e.val; $('#' + name).val(e.choice.text); Adoxio.CanadaPost.AddressRetrieveDetails(id, fieldMappings, Settings, e.object, name); }); }, AddressRetrieveDetails: function (Id, fieldMappings, Settings, details, FieldName) { if(details.IsRetrievable == "Retrieve"){ Adoxio.CanadaPost.RemoveCanadaPostFieldMessage(FieldName, fieldMappings); $.getJSON(Adoxio.CanadaPost.CanadaPostUrls.AddressRetrieveDetailsUrl, { Key: Settings.Key, Id: Id }, function (data) { // Test for an error if (data.Items.length == 1 && typeof(data.Items[0].Error) != "undefined") { // Show the error message console.log(data.Items[0].Description); } else { // Check if there were any items found if (data.Items.length == 0) console.log("Sorry, there were no results"); else { // PUT YOUR CODE HERE //FYI: The output is a JS object (e.g. data.Items[0].Id), the keys being: var Item = data.Items.filter(function(item) { return item.Language === Settings.LanguagePreference; })[0]; if(!Item) { Item = data.Items[0]; } console.log(Item); for (var k in fieldMappings){ if (fieldMappings.hasOwnProperty(k)) { if(fieldMappings[k] != null || fieldMappings[k] != ""){ //console.log("Key is " + k + ", value is " + fieldMappings[k]); if($("#" + fieldMappings[k]).length > 0){ if($("#" + fieldMappings[k]).hasClass('picklist')){ $("#" + fieldMappings[k]).val($('#' + fieldMappings[k] +' option:contains("'+ Item[k] +'")').attr('value')).addClass('dirty').trigger('change'); }else{ $("#" + fieldMappings[k]).val(Item[k]).addClass('dirty').trigger('change'); } } } } } } } }); }else{ Adoxio.CanadaPost.ShowCanadaPostFieldMessage(FieldName, Settings.ErrorMessage, fieldMappings); } }, GetCanadaPostSetting: function(FieldName){ // var lang = window.location.pathname.split('/')[1]; return $.ajax({ type: "GET", cache: false, url: Adoxio.CanadaPost.CanadaPostUrls.CanadaPostLiquidTemplateUrl + "&FieldName=" + FieldName, }).then(function (data) { return JSON.parse(data); }); }, ShowCanadaPostFieldMessage: function(FieldName, message, fieldMappings){ var alert = $('