// **************************************
        // URL: /Account/SignUp
        // **************************************

        public ActionResult SignUp()
        {
            if (!_repository.RuleAnswers.IsUserEligible(Session.SessionID))
            {
                return(RedirectToAction("Index", "Rules"));
            }
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;

            var model = new SignUpClientModel();

            return(View(model));
        }
        public ActionResult SignUp(SignUpClientModel model, string returnUrl)
        {
            if (!_repository.RuleAnswers.IsUserEligible(Session.SessionID))
            {
                return(RedirectToAction("Index", "Rules"));
            }

            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    var profile = UserProfile.GetUserProfile(model.UserName);
                    profile.FirstName        = model.FirstName;
                    profile.MiddleInitial    = model.MiddleInitial;
                    profile.LastName         = model.LastName;
                    profile.RegistrationDate = DateTime.Now;
                    if (Session["County"] != null)
                    {
                        var countyId = int.Parse(Session["County"].ToString());
                        profile.County = _repository.Counties.First(x => x.Id == countyId).CountyName;
                    }

                    profile.Save();

                    Roles.AddUserToRole(model.UserName, UserRoles.BasicUser);
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);

                    _emailService.SendEmailTo(model.Email, new ClientRegistrationEmail(model.UserName));

                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return(View(model));
        }