public ActionResult Signup(UserModel.User user, System.Web.Mvc.FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var PlanId = String.Empty;
                NewSubscription UserData = new NewSubscription();
                UserData.Email = user.BillingEmail;
                UserData.Username = user.BillingName;
                UserData.Password = user.BillingPassword;
                UserData.Plan = "Premium";
                PlanId = "d5jb";
                UserData.Company = user.Company;
                UserData.TOSCheck = Convert.ToInt32(user.TOSCheck);
                var regexItem = new Regex(@"\d");
                string email = user.BillingEmail;
                Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
                Match match = regex.Match(email);
                if (!match.Success)
                {
                    //Bad Email
                    ModelState.AddModelError("BillingEmail", "Invalid Email");
                }
                else if (user.IsEmailValid(email))
                {
                    //Email Taken
                    ModelState.AddModelError("BillingEmail", "Email already in use");
                }
                if (user.BillingName.Length < 5)
                {
                    //Bad Username
                    ModelState.AddModelError("BillingName", "Username must be a least 5 characters in length");
                }
                else if (user.DoesUsernameExist(user.BillingName))
                {
                    //Username already exists
                    ModelState.AddModelError("BillingName", "Username is already in use");
                }
                if (user.BillingPassword != user.BillingPasswordTwo)
                {
                    //Passwords Don't Match
                    ModelState.AddModelError("BillingPassword", "Passwords do not match");

                }
                else if (user.BillingPassword.Length < 6)
                {
                    //Passwords Too Short
                    ModelState.AddModelError("BillingPassword", "Password must be at least 6 characters in length");
                }
                else if (!regexItem.IsMatch(user.BillingPassword))
                {
                    //Passwords do not contain number
                    ModelState.AddModelError("BillingPassword", "Password must contain at least one number");
                }
                if (user.Company.Length < 1)
                {
                    //Invalid Company
                    ModelState.AddModelError("Company", "Invalid Company Name");
                }
                if (!user.TOSCheck)
                {
                    //Terms of Service not checked
                    ModelState.AddModelError("General", "You must agree to terms of service");
                }
                //Write to DB if all is good
                if (ModelState.IsValid)
                {
                    CustomerRequest request = new CustomerRequest
                    {
                        CreditCard = new CreditCardRequest
                        {
                            CardholderName = collection["name"],
                            Number = collection["number"],
                            ExpirationMonth = collection["month"],
                            ExpirationYear = collection["year"],
                            CVV = collection["cvv"]
                        }
                    };
                    Result<Customer> result = Gateway.BrainTreeGateway.Customer.Create(request);
                    if (result.IsSuccess())
                    {
                        //Successful add to Braintree
                        UserData.BillingID = result.Target.Id;
                        if (user.SaveNewUser(UserData))
                        {
                            //Successful write to DB
                            try
                            {
                                Customer customer = Gateway.BrainTreeGateway.Customer.Find(UserData.BillingID);
                                string paymentMethodToken = customer.CreditCards[0].Token;
                                SubscriptionRequest subscriptionRequest = new SubscriptionRequest
                                {
                                    PaymentMethodToken = paymentMethodToken,
                                    PlanId = PlanId
                                };
                                Result<Subscription> subscriptionResult = Gateway.BrainTreeGateway.Subscription.Create(subscriptionRequest);
                                user.UpdateSubscriptionId(user.BillingName, subscriptionResult.Target.Id);
                                return RedirectToAction("Index", "Home");
                            }
                            catch (Braintree.Exceptions.NotFoundException)
                            {
                                //No customer found
                                return RedirectToAction("Error", "User");
                            }
                        }
                        else
                        {
                            //failure writing customer to database
                            return RedirectToAction("Error", "User");
                        }
                    }
                    else
                    {
                        //failure adding customer to Braintree
                        ModelState.AddModelError("General", result.Message);
                    }
                }
            }
            return View(user);
        }
        public ActionResult ResetLogin(UserModel.User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //If email is valid user email
                    if (user.IsEmailValid(user.Email))
                    {
                        var resetData = new PasswordResetInfo();
                        //Create token and write to DB
                        resetData = user.CreateKeyAndTable(user.Email);
                        if (resetData != null)
                        {
                            //If token creation successful, send email to user
                            if (user.SendResetEmail(user.Email, resetData))
                            {
                                //email success view
                                return RedirectToAction("EmailSent", "User");
                            }
                            else
                            {
                                //error view
                                return RedirectToAction("Error", "User");
                            }
                        }
                        else
                        {
                            //error view
                            return RedirectToAction("Error", "User");
                        }
                    }
                    else
                    {

                        return RedirectToAction("EmailSent", "User");
                    }
                }
                return View(user);
            }
            catch (Exception ex)
            {
                Logger.WriteErrorLog(ex);
                return View(user);
            }
        }
 public ActionResult SavePersonalInfo(UserModel.User user)
 {
     if (ModelState.IsValid)
     {
         var UpdateEmail = false;
         string email = user.BillingEmail;
         var UserCurrentEmail = user.GetCurrentEmail(User.Identity.Name);
         var UserCurrentCompany = user.GetCompany(User.Identity.Name);
         var EnteredEmailValid = user.IsEmailValid(email);
         var regexItem = new Regex(@"\d");
         Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
         Match match = regex.Match(email);
         if (!match.Success)
         {
             //Bad Email
             ModelState.AddModelError("BillingEmail", "Invalid Email");
         }
         else if (EnteredEmailValid && UserCurrentEmail != email)
         {
             //Email Taken
             ModelState.AddModelError("BillingEmail", "Email in use on another account");
             TempData["ViewData"] = ViewData;
         }
         else
         {
             UpdateEmail = true;
         }
         if (user.BillingName.Length < 5)
         {
             //Bad Username
             ModelState.AddModelError("BillingName", "Username must be a least 5 characters in length");
         }
         else if (user.DoesUsernameExist(user.BillingName) && user.BillingName != User.Identity.Name)
         {
             //Username already exists
             ModelState.AddModelError("BillingName", "Username is not available");
         }
         else if (user.Company.Length < 1)
         {
             //No company entered
             ModelState.AddModelError("Company", "Company name is not valid");
         }
         else if (UpdateEmail)
         {
             if (EnteredEmailValid && UserCurrentEmail == email)
             {
                 //Same email
             }
             else
             {
                 //Update email
                 var emailUpdateStatus = user.UpdateEmail(email, User.Identity.Name);
                 if (emailUpdateStatus == BusinessEntities.EmailUpdateStatus.Error)
                 {
                     //error view
                     return RedirectToAction("Error", "User");
                 }
             }
             //Update Username
             if (user.BillingName != User.Identity.Name)
             {
                 var userEmail = user.GetCurrentEmail(User.Identity.Name);
                 if (user.UpdateUsername(user.BillingName, User.Identity.Name))
                 {
                     //Authentication
                     HttpCookie userNameCookie = new HttpCookie("QueueViewUserName");
                     DateTime now = DateTime.Now;
                     userNameCookie.Value = User.Identity.Name;
                     userNameCookie.Expires = now.AddDays(-1D);
                     Response.Cookies.Add(userNameCookie);
                     FormsAuthentication.SignOut();
                     FormsAuthentication.SetAuthCookie(user.BillingName, false);
                 }
                 else
                 {
                     //error view
                     return RedirectToAction("Error", "User");
                 }
             }
             if (UserCurrentCompany != user.Company)
             {
                 if (!user.UpdateCompany(user.Company, User.Identity.Name))
                 {
                     //error view
                     return RedirectToAction("Error", "User");
                 }
             }
         }
     }
     TempData["ViewData"] = ViewData;
     return RedirectToAction("AccountDashboard", "User", new {ADID = "PersonalInfo"});
 }