private void RenderAddShippingView()
        {
            FetchShippingModel();
            ShowCheckboxes();

            ////Pull in the right AddressWindow definition (declarative controlset) from XML and load them
            ordering.AddressBase shippingAddressCtrl = getShippingControl(colNewShippingAddress);

            IShippingProvider shippingProvider = (Page as ProductsBase).GetShippingProvider();

            //set default data context & put it in session
            //ShippingAddress_V02 shipAddr = new ShippingAddress_V02();
            //shipAddr.Address = new Address_V01();
            //shipAddr.Address.Country = CountryCode;

            var shipAddr = shippingProvider.GetDefaultAddress() as ShippingAddress_V02;

            if (null != shippingAddressCtrl)
            {
                if (ShoppingCart.IsFromInvoice)
                {
                    shippingAddressCtrl.DataContext = WorkedUponAddress;
                }
                else
                {
                    shippingAddressCtrl.DataContext = shipAddr;
                }
            }
            shipAddr.Address.Country = CountryCode;

            if (null == ShippingAddresses || ShippingAddresses.Count == 0) //Eval UC:3.5.3.1 (no existing saved address)
            {
                //Both “Save this shipping address” and “Make this my primary shipping address” are checked.
                //      The actor cannot uncheck either checkboxes
                cbMakePrimary.Checked = true;
                cbMakePrimary.Enabled = false;
                cbSaveThis.Checked    = true;
                cbSaveThis.Enabled    = false;
            }
            else //Eval UC:3.5.3.2 (atleast 1 address exists)
            {
                cbMakePrimary.Checked = false;
                cbMakePrimary.Enabled = true;
                cbSaveThis.Checked    = true;
                cbSaveThis.Enabled    = Locale == "de-DE" ? false : true;
            }

            //If the popup is called from “Order Preference” section, then 'Save' button is invisible.
            if (hfDiableSavedCheckbox.Value.ToLower().Equals("true"))
            {
                cbSaveThis.Visible = false;
            }
        }
        public static ordering.AddressBase Get(string locale, TemplateControl control, string controlPath, bool isXml)
        {
            ordering.AddressBase addressBase = null;

            try
            {
                if (!isXml)
                {
                    addressBase = control.LoadControl(controlPath) as ordering.AddressBase;
                    addressBase.LoadPage();
                }
                else
                {
                    addressBase         = new ordering.AddressControl();
                    addressBase.XMLFile = controlPath;
                }
            }
            catch (HttpException ex)
            {
                LoggerHelper.Error(ex.ToString());
            }

            return(addressBase);
        }
        private void UpdateViewChanges()
        {
            trError.Visible = false;
            blErrors.Items.Clear();
            //Branch out for DELETE
            // need to get to root cause of SourceCommand sometimes being null here. In the meantime check for null
            if (SourceCommand != null && SourceCommand.Mode == ShippingAddressCommandType.DELETE)
            {
                ProcessDeleteShippingSubmit();
                return;
            }

            string countryCode = CountryCode;

            //this.btnContinue.Text = GetLocalResourceObject("btnContinueContinue") as string;
            //1) Collect the final changes made by user from VIEW
            ordering.AddressBase shippingAddressCtrl = getShippingControl(colNewShippingAddress);

            // crashing - no point otherwise
            if (shippingAddressCtrl == null)
            {
                return;
            }

            ShippingAddress_V02 address = null;

            address = createAddressFromControl(shippingAddressCtrl, countryCode);
            if (address == null)
            {
                return;
            }
            WorkedUponAddress        = address;
            WorkedUponAddressControl = shippingAddressCtrl;

            //Process ADD/EDIT Common Logic
            address.IsPrimary = cbMakePrimary.Checked;

            //2) Validate user Changes on VIEW
            if (!WorkedUponAddressControl.Validate() ||
                (HLConfigManager.Configurations.AddressingConfiguration.HasCustomErrorExpression &&
                 !Page.IsValid))
            {
                // IsOkToSave moved to NorthAmericaAddressControl, since only US is calling it for AVS
                setUIErrorGrid();
                return;
            }

            // ****updateViewWithAVSRecommendation don't override phone any more, so don't need to call createAddressFromControl again for phone merge
            //US: after the AVS call to update the fields,'createAddressFromControl' is called to set the phone number field.
            //if (Locale.Substring(3, 2).Equals("US"))
            //    createAddressFromControl(this.WorkedUponAddressControl, countryCode);

            //for countries that do not have the phone split(areacode, phone and/or extension,
            //append '-' at the beginning and end of phone number.
            if (!HLConfigManager.Configurations.DOConfiguration.PhoneSplit)
            {
                if (WorkedUponAddress.Phone != null && !WorkedUponAddress.Equals(string.Empty))
                {
                    WorkedUponAddress.Phone = "-" + WorkedUponAddress.Phone + "-";
                }
            }

            //if ((this.txtNickname.Text.Trim() != null) && (!this.txtNickname.Text.Trim().Equals(string.Empty)))
            //{
            //    if (!System.Text.RegularExpressions.Regex.IsMatch(this.txtNickname.Text.Trim(),
            //          @"(^[A-Za-z0-9 ]+$)"))
            //    {
            //        this.trError.Visible = true;
            //        this.blErrors.Items.Add(new ListItem(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidNickname")));
            //        return;
            //    }
            //}

            IShippingProvider shippingProvider = (Page as ProductsBase).GetShippingProvider();

            if (HLConfigManager.Configurations.AddressingConfiguration.ValidatePostalCode)
            {
                if (shippingProvider != null)
                {
                    if (!shippingProvider.ValidatePostalCode(WorkedUponAddress.Address.Country,
                                                             WorkedUponAddress.Address.Country,
                                                             WorkedUponAddress.Address.Country,
                                                             WorkedUponAddress.Address.PostalCode))
                    {
                        trError.Visible = true;
                        blErrors.Items.Add(
                            new ListItem(PlatformResources.GetGlobalResourceString("ErrorMessage", "InvalidZipCode")));
                        return;
                    }
                }
            }
            if (HLConfigManager.Configurations.AddressingConfiguration.ValidateStreetAddress)
            {
                if (shippingProvider != null)
                {
                    if (!shippingProvider.ValidateAddress(WorkedUponAddress))
                    {
                        trError.Visible = true;
                        blErrors.Items.Add(
                            new ListItem(PlatformResources.GetGlobalResourceString("ErrorMessage", "GeneralInputError")));
                        return;
                    }
                }
            }

            //3) Branch out to perform ADD/EDIT specific Logic
            if (SourceCommand != null)
            {
                switch (SourceCommand.Mode)
                {
                case ShippingAddressCommandType.ADD:
                    ProcessAddShippingSubmit();
                    return;

                case ShippingAddressCommandType.EDIT:
                    ProcessEditShippingSubmit();
                    return;
                }
            }
        }
        private void RenderEditShippingView()
        {
            ShowCheckboxes();
            //load actual address user wants to edit
            ordering.AddressBase shippingAddressCtrl = getShippingControl(colNewShippingAddress);
            if (!HLConfigManager.Configurations.DOConfiguration.PhoneSplit)
            {
                if (WorkedUponAddress != null)
                {
                    shippingAddressCtrl.DataContext = WorkedUponAddress;
                    txtNickname.Text = WorkedUponAddress.Alias;
                }
            }

            var copyAddress = new ShippingAddress_V02();

            copyAddress.Address = new Address_V01();
            if (HLConfigManager.Configurations.DOConfiguration.PhoneSplit)
            {
                #region phone

                copyAddress.Address.Country = CountryCode;

                if (WorkedUponAddress != null)
                {
                    copyAddress = new ShippingAddress_V02(WorkedUponAddress);
                }
                //set the values for phoneNumber, areacode and extension.
                if (WorkedUponAddress != null && WorkedUponAddress.Phone != null)
                {
                    var phoneValues = WorkedUponAddress.Phone.Split(new[] { '-' });
                    switch (phoneValues.Length)
                    {
                    case 1:
                        copyAddress.Phone = phoneValues[0];
                        break;

                    case 2:
                        copyAddress.AreaCode = phoneValues[0];
                        copyAddress.Phone    = phoneValues[1];
                        break;

                    case 3:
                        copyAddress.AreaCode    = phoneValues[0];
                        copyAddress.Phone       = phoneValues[1];
                        copyAddress.AltAreaCode = phoneValues[2];
                        break;

                    default:
                        break;
                    }
                }

                shippingAddressCtrl.DataContext = copyAddress;
                txtNickname.Text = copyAddress.Alias;
            }
            else
            {
                if (WorkedUponAddress != null && WorkedUponAddress.Phone != null)
                {
                    copyAddress.Address.Country = CountryCode;
                    if (WorkedUponAddress != null)
                    {
                        copyAddress = new ShippingAddress_V02(WorkedUponAddress);
                    }
                    //set the values for phoneNumber, areacode and extension.
                    var phoneValues = WorkedUponAddress.Phone.Split(new[] { '-' });
                    switch (phoneValues.Length)
                    {
                    case 1:
                        copyAddress.Phone = phoneValues[0];
                        break;

                    case 2:
                        copyAddress.AreaCode = phoneValues[0];
                        copyAddress.Phone    = phoneValues[1];
                        break;

                    case 3:
                        copyAddress.AreaCode    = phoneValues[0];
                        copyAddress.Phone       = phoneValues[1];
                        copyAddress.AltAreaCode = phoneValues[2];
                        break;

                    default:
                        break;
                    }
                }
                shippingAddressCtrl.DataContext = copyAddress;
                txtNickname.Text = copyAddress.Alias;
            }

            #endregion phone

            if (WorkedUponAddress != null)
            {
                if (WorkedUponAddress.IsPrimary) //Eval UC:3.5.3.3 (editing primary)
                {
                    //•	“Make this my primary shipping address” is checked and not editable
                    //•	“Save this shipping address” checked and not editable
                    cbSaveThis.Visible    = true;
                    cbMakePrimary.Checked = true;
                    cbMakePrimary.Enabled = false;
                    cbSaveThis.Checked    = true;
                    cbSaveThis.Enabled    = false;
                    //return;
                }
                else //Eval UC:3.5.3.4 (editing non-primary)
                {
                    //•	“Make this my primary shipping address” is unchecked and editable
                    //•	“Save this shipping address” checked and editable.
                    cbSaveThis.Visible    = true;
                    cbMakePrimary.Checked = false;
                    cbMakePrimary.Enabled = true;
                    cbSaveThis.Checked    = true;
                    cbSaveThis.Enabled    = Locale == "de-DE" ? false : true;
                }
                //if the address is saved to session.
                if (WorkedUponAddress.ID < 0)
                {
                    cbSaveThis.Visible    = true;
                    cbSaveThis.Checked    = false;
                    cbSaveThis.Enabled    = Locale == "de-DE" ? false : true;
                    cbMakePrimary.Checked = false;
                    cbMakePrimary.Enabled = false;
                }

                //For all saved instances the "Save this shipping address" checkbox is invisible.
                if (WorkedUponAddress.ID > 0)
                {
                    cbSaveThis.Visible = false;
                }
            }

            //If the popup is called from “Order Preference” section, then 'Save' button is invisible.
            if (hfDiableSavedCheckbox.Value.ToLower().Equals("true"))
            {
                cbSaveThis.Visible = false;
            }
        }
        private void loadControls()
        {
            bool   isXML       = true;
            string controlPath = "";

            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "body_overflow_add", "$('body').css('overflow', 'hidden');", true);
            try
            {
                if (SourceCommand == null)
                {
                    return;
                }

                if ((SourceCommand.Mode == ShippingAddressCommandType.ADD) ||
                    (SourceCommand.Mode == ShippingAddressCommandType.EDIT))
                {
                    if (getShippingControl(colNewShippingAddress) != null)
                    {
                        return;
                    }

                    btnContinue.Text = GetLocalResourceObject("btnContinueContinue") as string;

                    divDeleteShippingAddress.Visible  = false;
                    divAddEditShippingAddress.Visible = true;
                    controlPath = getEditAddressControlPath(ref isXML);
                    //Pull in the right AddressWindow definition (declarative controlset) from XML and load them

                    ordering.AddressBase addressBase = createAddress(controlPath, isXML);
                    removeControl(colNewShippingAddress);
                    colNewShippingAddress.Controls.Add((Control)addressBase);
                }

                if (SourceCommand.Mode == ShippingAddressCommandType.DELETE)
                {
                    if (getShippingControl(colDeleteShippingAddress) != null)
                    {
                        return;
                    }

                    btnContinue.Text = GetLocalResourceObject("btnContinueDelete") as string;
                    divDeleteShippingAddress.Visible  = true;
                    divAddEditShippingAddress.Visible = false;

                    controlPath = getDeleteAddressControlPath(ref isXML);

                    //Pull in the right AddressWindow definition (declarative controlset) from XML and load them
                    //ordering.AddressBase addressBase = createAddress(controlPath, isXML);
                    ordering.AddressBase addressBase = new ordering.AddressControl();
                    if (addressBase == null)
                    {
                    }
                    addressBase.XMLFile = controlPath;
                    removeControl(colDeleteShippingAddress);
                    if (colDeleteShippingAddress.Controls.Count >= 1)
                    {
                        colDeleteShippingAddress.Controls.Clear();
                    }
                    colDeleteShippingAddress.Controls.Add((Control)addressBase);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.Error(ex.ToString());
            }
        }
        private ShippingAddress_V02 createAddressFromControl(ordering.AddressBase addressCntrl, string country)
        {
            var shippingAddress = addressCntrl.CreateAddressFromControl() as ShippingAddress_V02;

            if (HLConfigManager.Configurations.DOConfiguration.PhoneSplit)
            {
                string areaCode  = shippingAddress.AreaCode ?? string.Empty;
                string extension = shippingAddress.AltAreaCode ?? string.Empty;
                string phone     = shippingAddress.Phone ?? string.Empty;

                if (!string.IsNullOrEmpty(extension))
                {
                    if (addressCntrl.ErrorList == null)
                    {
                        addressCntrl.ErrorList = new List <string>();
                    }

                    if (!Regex.IsMatch(extension, @"^\d+$"))
                    {
                        addressCntrl.ErrorList.Add(PlatformResources.GetGlobalResourceString("ErrorMessage",
                                                                                             "InvalidExtension"));
                    }
                }

                if (!phone.Equals(string.Empty) && areaCode.Equals(string.Empty) && extension.Equals(string.Empty))
                {
                    phone = "-" + phone + "-";
                }
                if (!phone.Equals(string.Empty) && !areaCode.Equals(string.Empty) && extension.Equals(string.Empty))
                {
                    phone = areaCode + "-" + phone + "-";
                }
                if (!phone.Equals(string.Empty) && !areaCode.Equals(string.Empty) && !extension.Equals(string.Empty))
                {
                    phone = areaCode + "-" + phone + "-" + extension;
                }
                if (phone.Equals(string.Empty) && areaCode.Equals(string.Empty) && !extension.Equals(string.Empty))
                {
                    phone = extension + "--";
                }

                shippingAddress.AreaCode    = string.Empty;
                shippingAddress.AltAreaCode = string.Empty;

                shippingAddress.Phone = phone;
            }
            //else
            //    shippingAddress.Phone = "-" + shippingAddress.Phone + "-";

            if (shippingAddress != null)
            {
                shippingAddress.Address.Country = country;
            }
            //else
            //{
            //    shippingAddress = new ShippingAddress_V02();
            //    shippingAddress.Address = OrderCreationHelper.CreateDefaultAddress();
            //    shippingAddress.Address.Country = country;
            //}
            return(shippingAddress);
        }