protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            SetEditorMode();

            if (!Page.IsPostBack)
            {

                if (Request.QueryString["UserID"] != null)
                {
                    this.UserIDField.Value = Request.QueryString["UserID"];
                }

                if (Request.QueryString["id"] != null)
                {
                    LoadAddressForUser(Request.QueryString["id"]);
                }
                else
                {
                    Address a = new Address();
                    a.CountryBvin = WebAppSettings.ApplicationCountryBvin;
                    this.AddressEditor1.LoadFromAddress(a);
                }

            }
        }
 public AddressViewModel(Address a)
 {
     Init();
     this.StoreId = a.StoreId;
     this.NickName = a.NickName;
     this.FirstName = a.FirstName;
     this.MiddleInitial = a.MiddleInitial;
     this.LastName = a.LastName;
     this.Company = a.Company;
     this.Line1 = a.Line1;
     this.Line2 = a.Line2;
     this.Line3 = a.Line3;
     this.City = a.City;
     this.RegionName = a.RegionName;
     this.RegionBvin = a.RegionBvin;
     this.PostalCode = a.PostalCode;
     this.CountryName = a.CountryName;
     this.CountryBvin = a.CountryBvin;
     this.CountyBvin = a.CountyBvin;
     this.CountyName = a.CountyName;
     this.Phone = a.Phone;
     this.Fax = a.Fax;
     this.WebSiteUrl = a.WebSiteUrl;
     this.UserBvin = a.UserBvin;
     this.AddressType = a.AddressType;
     this.LastUpdatedUtc = a.LastUpdatedUtc;
 }
        private Address LoadAddress(string bvin)
        {
            CustomerAccount u = MTApp.CurrentCustomer;

            if (u != null)
            {
                switch (bvin.ToLower())
                {
                case "new":
                    MerchantTribe.Commerce.Contacts.Address a = new MerchantTribe.Commerce.Contacts.Address();
                    a.Bvin = System.Guid.NewGuid().ToString();
                    return(a);

                default:
                    foreach (MerchantTribe.Commerce.Contacts.Address a2 in u.Addresses)
                    {
                        if (a2.Bvin == bvin)
                        {
                            return(a2);
                        }
                    }
                    break;
                }
            }

            return(new Address());
        }
        private Address LoadAddress(string bvin)
        {
            CustomerAccount u = MTApp.CurrentCustomer;
            if (u != null)
            {
                switch (bvin.ToLower())
                {
                    case "new":
                        MerchantTribe.Commerce.Contacts.Address a = new MerchantTribe.Commerce.Contacts.Address();
                        a.Bvin = System.Guid.NewGuid().ToString();
                        return a;                        
                    default:
                        foreach (MerchantTribe.Commerce.Contacts.Address a2 in u.Addresses)
                        {
                            if (a2.Bvin == bvin)
                            {
                                return a2;                                
                            }
                        }
                        break;
                }
            }

            return new Address();
        }
        public void CanCreateAnAddressAndAssignBvin()
        {
            RequestContext c = new RequestContext();
            c.CurrentStore.Id = 12;
            AddressRepository target = AddressRepository.InstantiateForMemory(c);

            Address item = new Address();
            item.City = "Richmond";

            bool expected = true;
            bool actual;
            actual = target.Create(item);

            Assert.AreEqual(expected, actual);
            Assert.AreNotEqual(string.Empty, item.Bvin, "Bvin should not be empty after create.");            
        }
        private List<MerchantTribe.Web.Validation.RuleViolation> ValidateAddress(Address a, string prefix)
        {
            List<MerchantTribe.Web.Validation.RuleViolation> result = new List<MerchantTribe.Web.Validation.RuleViolation>();

            string pre = prefix.Trim().ToLowerInvariant();

            MerchantTribe.Web.Validation.ValidationHelper.Required(prefix + " Country Name", a.CountryData.Name, result, pre + "countryname");
            MerchantTribe.Web.Validation.ValidationHelper.Required(prefix + " First Name", a.FirstName, result, pre + "firstname");
            MerchantTribe.Web.Validation.ValidationHelper.Required(prefix + " Last Name", a.LastName, result, pre + "lastname");
            MerchantTribe.Web.Validation.ValidationHelper.Required(prefix + " Street", a.Line1, result, pre + "address");
            MerchantTribe.Web.Validation.ValidationHelper.Required(prefix + " City", a.City, result, pre + "city");
            MerchantTribe.Web.Validation.ValidationHelper.Required(prefix + " Postal Code", a.PostalCode, result, pre + "zip");

            MerchantTribe.Web.Validation.ValidationHelper.Required(prefix + " Region/State",
                                                                a.RegionData.Abbreviation,
                                                                result, pre + "state");
            return result;
        }
 private void LoadAddressFromForm(string prefix, Address address)
 {
     address.Bvin = Request.Form[prefix + "addressbvin"] ?? address.Bvin;
     address.CountryBvin = Request.Form[prefix + "country"] ?? address.CountryBvin;
     address.FirstName = Request.Form[prefix + "firstname"] ?? address.FirstName;
     address.LastName = Request.Form[prefix + "lastname"] ?? address.LastName;
     address.Company = Request.Form[prefix + "company"] ?? address.Company;
     address.Line1 = Request.Form[prefix + "address"] ?? address.Line1;
     address.City = Request.Form[prefix + "city"] ?? address.City;
     address.RegionBvin = Request.Form[prefix + "state"] ?? address.RegionBvin;
     address.PostalCode = Request.Form[prefix + "zip"] ?? address.PostalCode;
     address.Phone = Request.Form[prefix + "phone"] ?? address.Phone;
 }
 public void CopyTo(Address a)
 {            
     a.StoreId = this.StoreId;
     a.NickName = this.NickName;
     a.FirstName = this.FirstName;
     a.MiddleInitial = this.MiddleInitial;
     a.LastName = this.LastName;
     a.Company = this.Company;
     a.Line1 = this.Line1;
     a.Line2 = this.Line2;
     a.Line3 = this.Line3;
     a.City = this.City;
     a.RegionName = this.RegionName;
     a.RegionBvin = this.RegionBvin;
     a.PostalCode = this.PostalCode;
     a.CountryName = this.CountryName;
     a.CountryBvin = this.CountryBvin;
     a.CountyBvin = this.CountyBvin;
     a.CountyName = this.CountyName;
     a.Phone = this.Phone;
     a.Fax = this.Fax;
     a.WebSiteUrl = this.WebSiteUrl;
     a.UserBvin = this.UserBvin;
     a.AddressType = this.AddressType;
     a.LastUpdatedUtc = this.LastUpdatedUtc;
 }
        // Create or Update
        public override string PostAction(string parameters, System.Collections.Specialized.NameValueCollection querystring, string postdata)
        {
            string data = string.Empty;
            string bvin = FirstParameter(parameters);
            ApiResponse<CustomerAccountDTO> response = new ApiResponse<CustomerAccountDTO>();

            CustomerAccountDTO postedItem = null;
            try
            {
                postedItem = MerchantTribe.Web.Json.ObjectFromJson<CustomerAccountDTO>(postdata);
            }
            catch(Exception ex)
            {
                response.Errors.Add(new ApiError("EXCEPTION", ex.Message));
                return MerchantTribe.Web.Json.ObjectToJson(response);                
            }

            CustomerAccount item = new CustomerAccount();
            item.FromDto(postedItem);

            if (bvin == string.Empty)
            {
                CustomerAccount existing = MTApp.MembershipServices.Customers.FindByEmail(item.Email);
                if (existing == null || existing.Bvin == string.Empty)
                {
                    string clearPassword = querystring["pwd"];
                    if (clearPassword.Trim().Length < 1)
                    {
                        clearPassword = MerchantTribe.Web.PasswordGenerator.GeneratePassword(10);
                    }
                    // Create
                    bool result = MTApp.MembershipServices.CreateCustomer(item, clearPassword);
                    bvin = item.Bvin;
                }
                else
                {
                    bvin = existing.Bvin;
                }
            }
            else
            {
                MTApp.MembershipServices.UpdateCustomer(item);
            }
            CustomerAccount resultItem = MTApp.MembershipServices.Customers.Find(bvin);
            if (resultItem != null)
            {
                response.Content = resultItem.ToDto();
                // Address Import
                foreach (AddressDTO a in postedItem.Addresses)
                {
                    Address addr = new Address();
                    addr.FromDto(a);
                    MTApp.MembershipServices.CheckIfNewAddressAndAddWithUpdate(resultItem,addr);
                }
            }
            
            data = MerchantTribe.Web.Json.ObjectToJson(response);            
            return data;
        }