//public ActionResult GoogleUser()
        //{
        //    // string txtEMailId = (string)this.RouteData.Values["id"].ToString();
        //    // return CheckUserLogin(txtEMailId, null);
        //    ViewBag.EMailId= Request.QueryString["id"].ToString();
        //    Response.Redirect("../Account/ForgotPassword");
        //    return null;
        //}
        public ActionResult ChangePassword(string txtNewPassword)
        {
            if (Request.Form["Cancel"] == null)
            {
                if (Session["User"] != null && !string.IsNullOrEmpty(txtNewPassword))
                {
                    user = (Users)Session["User"];
                    double dblUserID = 0;
                    SqlConnection LclConn = new SqlConnection();
                    SqlTransaction SetTransaction = null;
                    bool IsinTransaction = false;
                    if (LclConn.State != ConnectionState.Open)
                    {
                        user.SetConnection = user.OpenConnection(LclConn);
                        SetTransaction = LclConn.BeginTransaction(IsolationLevel.ReadCommitted);
                        IsinTransaction = true;
                    }
                    else
                    {
                        user.SetConnection = LclConn;
                    }
                    user.OptionID = 5;
                    user.Password = txtNewPassword;
                    user.ModifiedDateTime = DateTime.Now;
                    dblUserID = user.UserId;

                    bool result = user.CreateUsers(ref dblUserID, SetTransaction);
                    if (IsinTransaction && result)
                    {
                        SetTransaction.Commit();
                    }
                    else
                    {
                        SetTransaction.Rollback();
                    }

                    user.CloseConnection(LclConn);
                    //lblUserRegMsg.Visible = true;
                    ViewBag.Ack = "Password changed successfully";
                    //txtPassword.Text = "";
                    //txtConfirmPassword.Text = "";

                    ViewBag.OldPassword = txtNewPassword;
                }
                return View(user);
            }
            else
            {
                if (Session["User"] != null)
                {
                    user = (Users)Session["User"];
                }
                return View("../Account/ViewUser", user);
            }
        }
        public ActionResult CreateEditUser(Users user, HttpPostedFileBase fileUserPhoto, string txtPassword)
        {
            string activationCode = Guid.NewGuid().ToString();
            //AddEdit user
            if (Request.Form["Cancel"] == null)
            {
                if (ModelState.IsValid)
                {
                    if (fileUserPhoto != null && fileUserPhoto.ContentLength > 1048576)
                    {
                        ViewBag.Ack = "Image file size should be less than 1 mb";
                        //return;
                    }
                    else
                    {
                        //try
                        //{
                        ConnManager con = new ConnManager();
                        DataSet dsUser = con.GetData("Select * from Users where Email = '" + user.Email + "'");
                        con.DisposeConn();
                        if (dsUser.Tables[0].Rows.Count > 0)
                        {
                            ViewBag.Ack = "EMail id already exists. If you have forgotten password, please click forgot password link on the Sign In page.";
                            return View("Users", user);
                        }

                        DataTable dtUserActivation = con.GetDataTable("select * from UserActivation where  Email = '" + user.Email + "'");
                        if (dtUserActivation.Rows.Count > 0)
                        {
                            ViewBag.lblAck = "User activation pending";
                            ViewBag.Activation = "Resend Activation Code?";
                            return View("../Account/Login");
                        }

                        double dblUserID = 0;
                        SqlConnection LclConn = new SqlConnection();
                        SqlTransaction SetTransaction = null;
                        bool IsinTransaction = false;
                        if (LclConn.State != ConnectionState.Open)
                        {
                            user.SetConnection = user.OpenConnection(LclConn);
                            SetTransaction = LclConn.BeginTransaction(IsolationLevel.ReadCommitted);
                            IsinTransaction = true;
                        }
                        else
                        {
                            user.SetConnection = LclConn;
                        }

                        if (fileUserPhoto != null && fileUserPhoto.FileName != "")
                        {
                            try
                            {
                                string fileName = System.IO.Path.GetFileNameWithoutExtension(fileUserPhoto.FileName);
                                string fileExt = System.IO.Path.GetExtension(fileUserPhoto.FileName);
                                string fullFileName = System.IO.Path.GetFileName(fileUserPhoto.FileName);

                                if (!System.IO.File.Exists(Server.MapPath("~\\Images\\") + fullFileName))
                                {
                                    fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                }
                                else
                                {
                                    fullFileName = fileName + DateTime.Now.ToString("HHmmss") + fileExt;
                                    while (System.IO.File.Exists(fullFileName))
                                    {
                                        fileName = fileName + DateTime.Now.ToString("HHmmss");
                                        fullFileName = fileName + fileExt;
                                    }
                                    fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                }
                                user.ImageURL = "~/Images/" + fullFileName;
                            }
                            catch (Exception ex)
                            {
                                //ViewBag.Ack = "Please try again";
                                user.ImageURL = "~/Images/Person.JPG";
                            }
                        }
                        else
                        {
                            user.ImageURL = "~/Images/Person.JPG";
                        }

                        user.OptionID = 1;
                        user.CreatedDateTime = DateTime.Now;
                        user.Password = txtPassword;

                        bool result = user.CreateUsers(ref dblUserID, SetTransaction);
                        if (IsinTransaction && result)
                        {
                            SetTransaction.Commit();
                        }
                        else
                        {
                            SetTransaction.Rollback();
                        }

                        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCON"].ToString()))
                        {
                            using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivation VALUES(@UserId, @ActivationCode)"))
                            {
                                using (SqlDataAdapter sda = new SqlDataAdapter())
                                {
                                    cmd.CommandType = CommandType.Text;
                                    cmd.Parameters.AddWithValue("@UserId", dblUserID);
                                    cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                                    cmd.Connection = conn;
                                    conn.Open();
                                    cmd.ExecuteNonQuery();
                                    conn.Close();
                                }
                            }
                        }

                        user.CloseConnection(LclConn);

                        //ViewBag.Ack = "User Registered Successfully. Please login.";
                        ViewBag.Ack = "User Info Saved Successfully. An activation link has been sent to your email address, please check your inbox and activate your account";
                        //SendNewUserRegEMail(user.Email);
                        SendActivationEMail(user.Email, activationCode);
                        SendEMail(user.Email, user.FirstName, user.LastName);
                    }
                    Session["User"] = user;
                    //return View("ViewUser", user);
                    return Redirect("../Account/ViewUser");
                }
                else
                {
                    ViewBag.Ack = ModelState["ReCaptcha"].Errors[0].ErrorMessage;
                    return View("Users", user);
                }
            }
            else
            {
                return View("Users", user);
            }
        }
        public ActionResult EditUser(Users user, HttpPostedFileBase fileUserPhoto)
        {
            //AddEdit user
            if (Request.Form["Cancel"] == null)
            {
                if (ModelState.IsValid)
                {
                    if (fileUserPhoto != null && fileUserPhoto.ContentLength > 1048576)
                    {
                        ViewBag.Ack = "Image file size should be less than 1 mb";
                        //return;
                    }
                    else
                    {
                        //try
                        //{
                            ConnManager con = new ConnManager();
                            DataSet dsUser = con.GetData("Select * from Users where Email = '" + user.Email + "'");
                            con.DisposeConn();
                            if (dsUser.Tables[0].Rows.Count > 0)
                            {
                                //if (Session["User"] == null)
                                //{
                                //    ViewBag.Ack = "EMail id already exists. If you have forgotten password, please click forgot password link on the Sign In page.";
                                //    //return;
                                //}
                                user.UserId = double.Parse(dsUser.Tables[0].Rows[0]["UserId"].ToString());
                            }

                            double dblUserID = 0;
                            SqlConnection LclConn = new SqlConnection();
                            SqlTransaction SetTransaction = null;
                            bool IsinTransaction = false;
                            if (LclConn.State != ConnectionState.Open)
                            {
                                user.SetConnection = user.OpenConnection(LclConn);
                                SetTransaction = LclConn.BeginTransaction(IsolationLevel.ReadCommitted);
                                IsinTransaction = true;
                            }
                            else
                            {
                                user.SetConnection = LclConn;
                            }

                            if (fileUserPhoto != null && fileUserPhoto.FileName != "")
                            {
                                try
                                {
                                    string fileName = System.IO.Path.GetFileNameWithoutExtension(fileUserPhoto.FileName);
                                    string fileExt = System.IO.Path.GetExtension(fileUserPhoto.FileName);
                                    string fullFileName = System.IO.Path.GetFileName(fileUserPhoto.FileName);

                                    if (!System.IO.File.Exists(Server.MapPath("~\\Images\\") + fullFileName))
                                    {
                                        fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                    }
                                    else
                                    {
                                        fullFileName = fileName + DateTime.Now.ToString("HHmmss") + fileExt;
                                        while (System.IO.File.Exists(fullFileName))
                                        {
                                            fileName = fileName + DateTime.Now.ToString("HHmmss");
                                            fullFileName = fileName + fileExt;
                                        }
                                        fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                    }
                                    user.ImageURL = "~/Images/" + fullFileName;
                                }
                                catch (Exception ex)
                                {
                                //ViewBag.Ack = "Please try again";
                                user.ImageURL = "~/Images/Person.JPG";
                            }
                                user.OptionID = 5;
                            }
                            else
                            {
                                user.OptionID = 7;
                                Users tempUser = new CodeAnalyzeMVC2015.Users();
                                tempUser = (Users)Session["User"];
                                user.ImageURL = tempUser.ImageURL;
                            }

                            user.ModifiedDateTime = DateTime.Now;
                            dblUserID = user.UserId;

                            bool result = user.CreateUsers(ref dblUserID, SetTransaction);
                            if (IsinTransaction && result)
                            {
                                SetTransaction.Commit();
                            }
                            else
                            {
                                SetTransaction.Rollback();
                            }
                            user.CloseConnection(LclConn);

                            ViewBag.Ack = "User Updated Successfully.";
                        //}
                        //catch
                        //{

                        //}
                    }
                    Session["User"] = user;
                    //return View("ViewUser", user);
                    return Redirect("../Account/ViewUser");
                }
                else
                {
                    user = (Users)Session["User"];
                    return View("../Account/ViewUser", user);
                }
            }
            else
            {
                user = (Users)Session["User"];
                return View("../Account/ViewUser", user);
            }
        }