public ActionResult CreateAccount(SignInViewModel posted)
        {
            SignInSetup();
            SignInViewModel model = new SignInViewModel();

            // bail out if this is a private store that doesn't allow registrations
            if (ViewBag.IsPrivateStore) return View("SignIn", model);

            // Process Requrest
            ValidateModelResponse validated = ValidateLoginModel(posted, false);
            if (validated.Success == false)
            {
                foreach(string s in validated.ResultMessages)
                {
                    FlashWarning(s);
                }
            }
            else
            {
                bool result = false;

                CustomerAccount u = new CustomerAccount();

                if (u != null)
                {
                    u.Email = posted.Email.Trim();
                    CreateUserStatus s = CreateUserStatus.None;
                    // Create new user
                    result = MTApp.MembershipServices.CreateCustomer(u, ref s, posted.Password.Trim());

                    if (result == false)
                    {
                        switch (s)
                        {
                            case CreateUserStatus.DuplicateUsername:
                                FlashWarning("That email already exists. Select another email or login to your current account.");
                                break;
                            default:
                                FlashWarning("Unable to save user. Unknown error.");
                                break;
                        }
                    }
                    else
                    {
                        // Update bvin field so that next save will call updated instead of create
                        MerchantTribe.Web.Cookies.SetCookieString(MerchantTribe.Commerce.WebAppSettings.CookieNameAuthenticationTokenCustomer(MTApp.CurrentStore.Id),
                                                                  u.Bvin,
                                                                  this.Request.RequestContext.HttpContext, false, new EventLog());
                        Redirect("~/account");
                    }
                }
            }
            return View("SignIn", model);
        }
        // GET: /account/signin
        public ActionResult SignIn()
        {
            SignInSetup();
            SignInViewModel model = new SignInViewModel();

            // Find email view cookie
            string uid = SessionManager.GetCookieString(WebAppSettings.CustomerIdCookieName, MTApp.CurrentStore);
            if (uid != string.Empty)
            {
                CustomerAccount u = MTApp.MembershipServices.Customers.Find(uid);
                if (u != null)
                {
                    model.Email = u.Email;
                }
            }

            return View(model);
        }
        public ActionResult AjaxSignIn()
        {
            string email = Request.Form["email"] ?? string.Empty;
            string password = Request.Form["password"] ?? string.Empty;

            SignInViewModel posted = new SignInViewModel() { Email = email,
                                                             Password = password };

            ValidateModelResponse validated = ValidateLoginModel(posted, false);
            if (validated.Success)
            {
                string errorMessage = string.Empty;
                string userId = string.Empty;
                if (MTApp.MembershipServices.LoginCustomer(posted.Email.Trim(),
                                        posted.Password.Trim(),
                                        ref errorMessage,
                                        this.Request.RequestContext.HttpContext,
                                        ref userId, MTApp))
                {
                    MerchantTribe.Commerce.Orders.Order cart = SessionManager.CurrentShoppingCart(MTApp.OrderServices, MTApp.CurrentStore);
                    if (cart != null && !string.IsNullOrEmpty(cart.bvin))
                    {
                        cart.UserEmail = posted.Email.Trim();
                        cart.UserID = userId;
                        MTApp.CalculateOrderAndSave(cart);
                        SessionManager.SaveOrderCookies(cart, MTApp.CurrentStore);
                    }
                    validated.Success = true;
                }
                else
                {
                    validated.ResultMessages.Add(errorMessage);
                    validated.Success = false;
                }
            }

            return new PreJsonResult(MerchantTribe.Web.Json.ObjectToJson(validated));
        }
        public ActionResult SignInPost(SignInViewModel posted)
        {
            SignInSetup();

            if (Request.QueryString["mode"] != null)
            {
                posted.Mode = Request.QueryString["mode"];
            }

            ValidateModelResponse validated = ValidateLoginModel(posted, false);
            if (validated.Success == false)
            {
                foreach (string s in validated.ResultMessages)
                {
                    FlashWarning(s);
                }
            }
            else
            {
                string errorMessage = string.Empty;
                string userId = string.Empty;
                if (MTApp.MembershipServices.LoginCustomer(posted.Email.Trim(),
                                        posted.Password.Trim(),
                                        ref errorMessage,
                                        this.Request.RequestContext.HttpContext,
                                        ref userId, MTApp))
                {
                    MerchantTribe.Commerce.Orders.Order cart = SessionManager.CurrentShoppingCart(MTApp.OrderServices, MTApp.CurrentStore);
                    if (cart != null && !string.IsNullOrEmpty(cart.bvin))
                    {
                        cart.UserEmail = posted.Email.Trim();
                        cart.UserID = userId;
                        MTApp.CalculateOrderAndSave(cart);
                        SessionManager.SaveOrderCookies(cart, MTApp.CurrentStore);
                    }

                    // if we got here from checkout, return to checkout
                    if (posted.Mode.Trim().ToLowerInvariant() == "checkout")
                    {
                        return Redirect("~/checkout");
                    }
                    // otherwise send to account home
                    return Redirect("~/account");
                }
                else
                {
                    string errorMessage2 = string.Empty;
                    // Failed to Login as Customer, Try admin account
                    if (MTApp.AccountServices.LoginAdminUser(posted.Email.Trim(), 
                                                posted.Password.Trim(),
                                                ref errorMessage2, 
                                                this.Request.RequestContext.HttpContext,
                                                MTApp))
                    {
                        return Redirect("~/bvadmin");
                    }
                    this.FlashWarning(errorMessage);
                }
            }
            
            return View(posted);
        }
        private ValidateModelResponse ValidateLoginModel(SignInViewModel posted, bool isCreate)
        {
            ValidateModelResponse resp = new ValidateModelResponse();
            resp.Success = true;
            
            if (posted == null) 
            {
                resp.Success = false;
                return resp;
            }
            if (!MerchantTribe.Web.Validation.EmailValidation.MeetsEmailFormatRequirements(posted.Email))
            {
                resp.Success = false;
                resp.ResultMessages.Add("Please enter a valid email address");
            }
            if (posted.Password.Trim().Length < WebAppSettings.PasswordMinimumLength)
            {
                resp.Success = false;
                resp.ResultMessages.Add("Password must be at least " + WebAppSettings.PasswordMinimumLength + " characters long.");
            }

            if (isCreate)
            {
                if (posted.PasswordConfirm != posted.Password)
                {
                    resp.Success = false;
                    resp.ResultMessages.Add("Passwords don't match. Please try again.");
                }
            }
            return resp;
        }