public ActionResult Register(RegisterModel model, string returnUrl)
        {
            if (!VerifyRecaptcha())
            {
                ModelState.AddModelError(string.Empty, "Sorry, please try again.");
                return(View(model));
            }

            if (_workContext.CurrentProfile.IsAnonymous == false)
            {
                _authenticationService.SignOut();
            }

            if (ModelState.IsValid)
            {
                var account = new Account
                {
                    Name          = model.Name,
                    Email         = model.Email.ToLower(),
                    ContactNumber = model.ContactNumber,
                    DOB           = model.ParseDateOfBirth().HasValue ? model.ParseDateOfBirth().Value.ToString("dd/MM/yyyy") : null,
                    Username      = model.Email.ToLower(),
                    DisplayContactNumberInDespatch = model.DisplayContactNumberInDespatch,
                    ProfileId = _workContext.CurrentProfile.Id,
                };

                var result = _accountService.ProcessRegistration(account, model.Password, sendEmailFlag: true);

                if (string.IsNullOrEmpty(result.Message))
                {
                    account.Id = result.UserId;
                    _authenticationService.SignIn(account.Username, model.Password, isPersistent: true, shouldLockOut: true);

                    return(RedirectToRoute("Register Result", new { resultId = Convert.ToInt32(UserRegistrationType.Standard), returnUrl }));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, result.Message);
                }
            }

            return(View(model));
        }
        //available even when navigation is not allowed
        //[PublicStoreAllowNavigation(true)]
        public virtual ActionResult Register(RegisterModel model, string returnUrl, bool captchaValid, FormCollection form)
        {
            //check whether registration is allowed
            if (_userSettings.UserRegistrationType == UserRegistrationType.Disabled)
            {
                return(RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Disabled }));
            }

            if (_workContext.CurrentUser.IsRegistered())
            {
                //Already registered customer.
                _authenticationService.SignOut();

                //raise logged out event
                _eventPublisher.Publish(new UserLoggedOutEvent(_workContext.CurrentUser));

                //Save a new record
                // _workContext.CurrentUser = _userService.InsertGuestCustomer();
            }
            var customer = _workContext.CurrentUser;
            //customer.RegisteredInStoreId = _storeContext.CurrentStore.Id;

            //custom customer attributes
            var customerAttributesXml     = ParseCustomUserAttributes(form);
            var customerAttributeWarnings = _userAttributeParser.GetAttributeWarnings(customerAttributesXml);

            foreach (var error in customerAttributeWarnings)
            {
                ModelState.AddModelError("", error);
            }

            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnRegistrationPage && !captchaValid)
            {
                ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
            }

            if (ModelState.IsValid)
            {
                if (_userSettings.UsernamesEnabled && model.Username != null)
                {
                    model.Username = model.Username.Trim();
                }

                bool isApproved          = _userSettings.UserRegistrationType == UserRegistrationType.Standard;
                var  registrationRequest = new UserRegistrationRequest(customer,
                                                                       model.Email,
                                                                       _userSettings.UsernamesEnabled ? model.Username : model.Email,
                                                                       model.Password,
                                                                       _userSettings.DefaultPasswordFormat,
                                                                       0,
                                                                       isApproved);
                var registrationResult = _userRegistrationService.RegisterUser(registrationRequest);
                if (registrationResult.Success)
                {
                    //properties
                    if (_dateTimeSettings.AllowCustomersToSetTimeZone)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.TimeZoneId, model.TimeZoneId);
                    }

                    //form fields
                    if (_userSettings.GenderEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Gender, model.Gender);
                    }
                    _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.FirstName, model.FirstName);
                    _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.LastName, model.LastName);
                    if (_userSettings.DateOfBirthEnabled)
                    {
                        DateTime?dateOfBirth = model.ParseDateOfBirth();
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.DateOfBirth, dateOfBirth);
                    }
                    if (_userSettings.CompanyEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Company, model.Company);
                    }
                    if (_userSettings.StreetAddressEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.StreetAddress, model.StreetAddress);
                    }
                    if (_userSettings.StreetAddress2Enabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.StreetAddress2, model.StreetAddress2);
                    }
                    if (_userSettings.ZipPostalCodeEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.ZipPostalCode, model.ZipPostalCode);
                    }
                    if (_userSettings.CityEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.City, model.City);
                    }
                    if (_userSettings.CountryEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.CountryId, model.CountryId);
                    }
                    if (_userSettings.CountryEnabled && _userSettings.StateProvinceEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.StateProvinceId,
                                                               model.StateProvinceId);
                    }
                    if (_userSettings.PhoneEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Phone, model.Phone);
                    }
                    if (_userSettings.FaxEnabled)
                    {
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.Fax, model.Fax);
                    }


                    //save customer attributes
                    _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.CustomUserAttributes, customerAttributesXml);

                    //login customer now
                    if (isApproved)
                    {
                        _authenticationService.SignIn(customer, true);
                    }



                    //notifications
                    //if (_customerSettings.NotifyNewCustomerRegistration)
                    //    _workflowMessageService.SendCustomerRegisteredNotificationMessage(customer,
                    //        _localizationSettings.DefaultAdminLanguageId);

                    //raise event
                    _eventPublisher.Publish(new UserRegisteredEvent(customer));

                    switch (_userSettings.UserRegistrationType)
                    {
                    case UserRegistrationType.EmailValidation:
                    {
                        //email validation message
                        _genericAttributeService.SaveAttribute(customer, SystemUserAttributeNames.AccountActivationToken, Guid.NewGuid().ToString());
                        //_workflowMessageService.SendCustomerEmailValidationMessage(customer, _workContext.WorkingLanguage.Id);

                        //result
                        return(RedirectToRoute("RegisterResult",
                                               new { resultId = (int)UserRegistrationType.EmailValidation }));
                    }

                    case UserRegistrationType.AdminApproval:
                    {
                        return(RedirectToRoute("RegisterResult",
                                               new { resultId = (int)UserRegistrationType.AdminApproval }));
                    }

                    case UserRegistrationType.Standard:
                    {
                        //send customer welcome message
                        //_workflowMessageService.SendCustomerWelcomeMessage(customer, _workContext.WorkingLanguage.Id);

                        var redirectUrl = Url.RouteUrl("RegisterResult", new { resultId = (int)UserRegistrationType.Standard });
                        if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                        {
                            redirectUrl = _webHelper.ModifyQueryString(redirectUrl, "returnurl=" + HttpUtility.UrlEncode(returnUrl), null);
                        }
                        return(Redirect(redirectUrl));
                    }

                    default:
                    {
                        return(RedirectToRoute("HomePage"));
                    }
                    }
                }

                //errors
                foreach (var error in registrationResult.Errors)
                {
                    ModelState.AddModelError("", error);
                }
            }

            //If we got this far, something failed, redisplay form
            model = _userModelFactory.PrepareRegisterModel(model, true, customerAttributesXml);
            return(View(model));
        }