private List<Models.CustomCommerceParty> AllAddresses(AddressListItemJsonResult result)
        {
            var addresses = new List<Models.CustomCommerceParty>();
            var response = this.AccountManager.GetCurrentUserParties(this.CurrentStorefront, this.CurrentVisitorContext);
            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                addresses = response.Result.ToList();
            }

            result.SetErrors(response.ServiceProviderResult);
            return addresses;
        }
        private Dictionary<string, string> GetAvailableCountries(AddressListItemJsonResult result)
        {
            var countries = new Dictionary<string, string>();
            var response = OrderManager.GetAvailableCountries();
            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                countries = response.Result;
            }

            result.SetErrors(response.ServiceProviderResult);
            return countries;
        }
        public JsonResult AddressModify(PartyInputModelItem model)
        {
            try
            {
                Assert.ArgumentNotNull(model, "model");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return Json(validationResult, JsonRequestBehavior.AllowGet);
                }

                var addresses = new List<Models.CustomCommerceParty>();
                var userResponse = this.AccountManager.GetUser(Context.User.Name);
                var result = new AddressListItemJsonResult(userResponse.ServiceProviderResult);
                if (userResponse.ServiceProviderResult.Success && userResponse.Result != null)
                {
                    var commerceUser = userResponse.Result;
                    var customer = new CommerceCustomer { ExternalId = commerceUser.ExternalId };
                    var party = new Models.CustomCommerceParty
                                {
                                    ExternalId = model.ExternalId,
                                    Name = model.Name,
                                    Address1 = model.Address1,
                                    City = model.City,
                                    Country = model.Country,
                                    State = model.State,
                                    ZipPostalCode = model.ZipPostalCode,
                                    PartyId = model.PartyId,
                                    IsPrimary = model.IsPrimary
                                };

                    if (string.IsNullOrEmpty(party.ExternalId))
                    {
                        // Verify we have not reached the maximum number of addresses supported.
                        int numberOfAddresses = this.AllAddresses(result).Count;
                        if (numberOfAddresses >= StorefrontManager.CurrentStorefront.MaxNumberOfAddresses)
                        {
                            var message = StorefrontManager.GetSystemMessage("MaxAddresseLimitReached");
                            result.Errors.Add(string.Format(CultureInfo.InvariantCulture, message, numberOfAddresses));
                            result.Success = false;
                        }
                        else
                        {
                            var response = this.AccountManager.AddParties(this.CurrentStorefront, customer, new List<Models.CustomCommerceParty> { party });
                        result.SetErrors(response.ServiceProviderResult);
                        if (response.ServiceProviderResult.Success)
                        {
                            addresses = this.AllAddresses(result);
                        }

                        result.Initialize(addresses, null);
                    }
                    }
                    else
                    {
                        var response = this.AccountManager.UpdateParties(this.CurrentStorefront, customer, new List<Models.CustomCommerceParty> { party });
                        result.SetErrors(response.ServiceProviderResult);
                        if (response.ServiceProviderResult.Success)
                        {
                            addresses = this.AllAddresses(result);
                        }

                        result.Initialize(addresses, null);
                    }
                }

                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("AddressModify", this, e);
                return Json(new BaseJsonResult("AddressModify", e), JsonRequestBehavior.AllowGet);
            }
        }