private string LoadProfile()
        {
            Uri requestUri = Context.Request.Url;
            string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);
            string username = string.Empty, fullname = string.Empty, email = string.Empty, link_logout = string.Empty;
            string result = string.Empty;
            link_logout = baseUrl + "/logout.aspx";


            if (Session["UserName"] != null && Session["UserName"].ToString() != string.Empty)
            {
                username = Session["UserName"].ToString();

                UserController user_controller_obj = new UserController();
                DataTable dt = user_controller_obj.GetDetailsByUserName(username);
                email = dt.Rows[0]["Email"].ToString();
                fullname = dt.Rows[0]["FullName"].ToString();
                Session.Timeout = 216000;

                result = "<div class=\"userinfo\">"
                              + "<img src='" + baseUrl + "/images/shopcarts/user_avatar.png' alt=\"user_avatar\" />"
                              + "<span>" + username + "</span>"
                         + "</div>"
                         + "<div style=\"display: none;\" class=\"userinfodrop\">"
                              + "<div class=\"avatar\">"
                                    + "<a href=\"#\"><img src='" + baseUrl + "/images/shopcarts/avatarbig.png' alt='big_user' /></a>"
                              + "</div>"
                              + "<div class=\"userdata\">"
                                    + "<h4>" + fullname + "</h4>"
                                    + "<p class='email'>" + email + "</p>"
                                    + "<ul>"
                                        + "<li><a class=\"userdata_link\" onclick=\"javascript:return ShowUserProfileModal('" + username + "');\">Edit Profile</a></li>"
                                        + "<li><a class=\"userdata_link\" href='" + link_logout + "'>Sign Out</a></li>"
                                    + "</ul>"
                              + "</div>"
                         + "</div>";

            }
            return result;
        }
        public string GetUserByEmail(string Email)
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            bool Check = regex.IsMatch(Email);
            string o_return = string.Empty;
            if (Check == true)
            {
                string[] result = new string[3];
                string UserName = string.Empty, PassWord = string.Empty;
                UserController user_obj = new UserController();
                result = user_obj.GetUserPassByEmail(Email);
                int i = Convert.ToInt32(result[0].ToString());
                if (i == 1)
                {
                    UserName = result[1];
                    PassWord = result[2];

                    string sender_email = ConfigurationManager.AppSettings["mailaddress"].ToString();
                    string sender_account = ConfigurationManager.AppSettings["mailaccount"].ToString();
                    string sender_password = ConfigurationManager.AppSettings["mailpassword"].ToString();

                    string receiver_name = Email.Substring(0, Email.LastIndexOf("@")); ;
                    string receiver_email = Email;

                    string subject = "Thông tin tài khoản đăng nhập Admin";
                    string body_content = "<div>UserName: "******"<br/>PassWord: "******"</div>";
                    bool send_mail = EmailClass.send_mail(sender_account, sender_password, sender_account, sender_email, receiver_name, receiver_email, subject, body_content);
                    if (send_mail == true)
                        o_return = "Thông tin đã được gửi đến Email của bạn. Vui lòng kiểm tra hộp thư.";
                }
                else if (i == -2)
                    o_return = "Email xác nhận không đúng";
                else
                    o_return = "Vui lòng nhập Email";
            }
            else
                o_return = "Email không hợp lệ.";
            return o_return;
        }
        private void LoadProfile()
        {
            Uri requestUri = Context.Request.Url;
            string baseUrl = requestUri.Scheme + Uri.SchemeDelimiter + requestUri.Host + (requestUri.IsDefaultPort ? "" : ":" + requestUri.Port);
            string fullname = string.Empty, email = string.Empty;
            string result = string.Empty;

            if (UserName != string.Empty)
            {
                UserController user_controller_obj = new UserController();
                DataTable dt = user_controller_obj.GetDetailsByUserName(UserName);
                email = dt.Rows[0]["Email"].ToString();
                fullname = dt.Rows[0]["FullName"].ToString();
                Session.Timeout = 216000;

                result = "<div class=\"userinfo\">"
                              + "<img src=\"../images/icons/16/user_avatar.png\" alt=\"user_avatar\" />"
                              + "<span>" + UserName + "</span>"
                         + "</div>"
                         + "<div style=\"display: none;\" class=\"userinfodrop\">"
                              + "<div class=\"avatar\">"
                                    + "<a href=\"#\"><img src='" + baseUrl + "/images/icons/avatarbig.png' alt='big_user' /></a>"
                              + "</div>"
                              + "<div class=\"userdata\">"
                                    + "<h4>" + fullname + "</h4>"
                                    + "<p class='email'>" + email + "</p>"
                                    + "<ul>"
                                        + "<li><a class=\"userdata_link\" onclick=\"javascript:return ShowUserProfileModal('" + UserName + "');\">Edit Profile</a></li>"
                                        + "<li><a class=\"userdata_link\" rel=\"nofollow\" href='/logout.aspx'>Sign Out</a></li>"
                                    + "</ul>"
                              + "</div>"
                         + "</div>";
                Literal_Head.Text = result;
            }else
                Response.RedirectToRoutePermanent("admin_login");
            
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string TabId = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
                string RoleId = ddlRoleList.SelectedValue;
                int PermissionId = Convert.ToInt32(ddlPermissionList.SelectedValue);
                string SelectedTabId = null, UserId = null;
                ModulePermissions module_permission_obj = new ModulePermissions();
                DataTable dt = module_permission_obj.GetListByRoleIdPermissionId(RoleId, PermissionId);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    SelectedTabId = dt.Rows[i]["TabId"].ToString();
                    UserId = dt.Rows[i]["UserId"].ToString();



                    if (TabId == SelectedTabId)
                    {
                        CheckBox chkSelectedPermissionRight = (CheckBox)e.Row.Cells[2].FindControl("chkSelectedPermissionRight");
                        if (chkSelectedPermissionRight != null)
                            chkSelectedPermissionRight.Checked = true;

                        CheckBox chkSelectedAllowAccessRight = (CheckBox)e.Row.Cells[3].FindControl("chkSelectedAllowAccessRight");
                        if (chkSelectedAllowAccessRight != null)
                            chkSelectedAllowAccessRight.Checked = true;

                        //TextBox txtUserId = (TextBox)e.Row.Cells[4].FindControl("txtUserId");
                        //txtUserId.Text = UserId;
                        //txtUserId.Attributes.Add("value", txtUserId.Text);

                        string ApplicationId = ddlApplicationList.SelectedValue;
                        DropDownList ddlUserList = (DropDownList)e.Row.FindControl("ddlUserList");
                        UserController user_obj = new UserController();
                        DataTable dt_app = user_obj.GetUsers(ApplicationId);
                        ddlUserList.DataSource = dt_app;
                        ddlUserList.DataTextField = "UserName";
                        ddlUserList.DataValueField = "UserId";
                        ddlUserList.DataBind();
                        ddlUserList.Items.Insert(0, new ListItem("- Chọn -", ""));
                        if (UserId != string.Empty)
                            ddlUserList.SelectedValue = UserId;
                        else
                            ddlUserList.SelectedIndex = 0;
                    }
                }
            }
        }
        protected void check_login(string Username, string Password)
        {
            if (Username == string.Empty)
            {
                string scriptCode = "<script>alert('Username is not empty.');document.location='login.aspx'</script>";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
            }
            else if (Password == string.Empty)
            {
                string scriptCode = "<script>alert('Password is not empty.');document.location='login.aspx'</script>";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
            }
            else if (Username == string.Empty && Password == string.Empty)
            {
                string scriptCode = "<script>alert('Username and Password are not empty.');document.location='login.aspx'</script>";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
            }
            else
            {
                string[] array_list = new string[10];
                UserController users_obj = new UserController();
                array_list = users_obj.CheckLogin(Username, Password);

                string ApplicationId = array_list[0].ToString();
                string UserId = array_list[1].ToString();
                string RoleId = array_list[2].ToString();
                string PortalId = array_list[3].ToString();
                string VendorId = array_list[4].ToString();
                string HomeDirectory = array_list[5].ToString();
                string IsSuperUser = array_list[6].ToString();
                string UpdatePassword = array_list[7].ToString();
                string IsDeleted = array_list[8].ToString();
                string Status = array_list[9].ToString();
                string scriptCode = string.Empty;

                switch (Status)
                {
                    case "-1":
                        scriptCode = "<script>alert('Username or password is empty.');document.location='login.aspx'</script>";
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
                        break;
                    case "-2":
                        scriptCode = "<script>alert('Username or password is incorrect.');document.location='login.aspx'</script>";
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
                        break;
                    case "-3":
                        scriptCode = "<script>alert('Error to write log.');document.location='login.aspx'</script>";
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
                        break;
                    case "1":
                        Session["ApplicationId"] = ApplicationId;
                        Session["UserId"] = UserId;
                        Session["RoleId"] = RoleId;
                        Session["PortalId"] = PortalId;
                        Session["VendorId"] = VendorId;
                        Session["IsSuperUser"] = IsSuperUser;
                        Session["UpdatePassword"] = UpdatePassword;
                        Session["IsDeleted"] = IsDeleted;
                        Session["UserName"] = Username;
                        Session["HomeDirectory"] = HomeDirectory;
                        Session.Timeout = 216000;
                        if (chkRemmberMe.Checked == true)
                        {
                            WriteCookies(UserId, Username, Password);
                        }
                        else
                        {
                            DeleteCookies();
                        }
                        //users_obj.CreateSessionLog(UserID, Username);
                        string portal_url = "~/" + HomeDirectory + "/index.aspx";
                        Response.Redirect(portal_url);
                        break;
                    case "2":
                        scriptCode = "<script>alert('Username or password has not yet activated.');document.location='login.aspx'</script>";
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
                        break;
                    case "3":
                        scriptCode = "<script>alert('Username or password is blocked.');document.location='login.aspx'</script>";
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
                        break;
                    default:
                        scriptCode = "<script>alert('System Error.');document.location='login.aspx'</script>";
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
                        break;
                }
            }

        }
 private void PopulateUserList(DropDownList ddlUserList, string selected_value)
 {
     string ApplicationId = ddlApplicationList.SelectedValue;
     UserController user_obj = new UserController();
     DataTable dt_app = user_obj.GetUsers(ApplicationId);            
     ddlUserList.Items.Clear();
     ddlUserList.DataSource = dt_app;
     ddlUserList.DataTextField = "UserName";
     ddlUserList.DataValueField = "UserId";
     ddlUserList.DataBind();
     ddlUserList.Items.Insert(0, new ListItem("- Chọn -", ""));
     if (selected_value != string.Empty)
         ddlUserList.SelectedValue = selected_value;
     else
         ddlUserList.SelectedIndex = 0;
 }
        private int UpdateData()
        {
            string ApplicationId = ddlApplicationList.SelectedValue;
            int PortalId = Convert.ToInt32(ddlPortalList.SelectedValue);
            int VendorId = Convert.ToInt32(ddlVendorList.SelectedValue);
            string RoleId = ddlRoleList.SelectedValue;
            string FullName = txtFullName.Text;
            string DisplayName = txtDisplayName.Text;                
            string MobilePIN = txtMobilePIN.Text;
            string Address = txtAddress.Text;
            string Phone = txtPhone.Text;
            string Email = txtEmail.Text;         

            //=================================================
            bool _IsSuperUser = chkIsSuperUser.Checked;                
            int IsSuperUser;
            if (_IsSuperUser == true)
            {
                IsSuperUser = 1;
            }
            else
            {
                IsSuperUser = 0;
            }
            //=================================================
            bool _IsDeleted = chkIsDeleted.Checked;
            int IsDeleted;
            if (_IsDeleted == true)
            {
                IsDeleted = 1;
            }
            else
            {
                IsDeleted = 0;
            }
            //=================================================
            bool _IsApproved = chkIsApproved.Checked;
            int IsApproved;
            if (_IsApproved == true)
            {
                IsApproved = 1;
            }
            else
            {
                IsApproved = 0;
            }
            //=================================================
            bool _UpdatePassword = chkUpdatePassword.Checked;
            int UpdatePassword;
            if (_UpdatePassword == true)
            {
                UpdatePassword = 1;
            }
            else
            {
                UpdatePassword = 0;
            }
            //=================================================
            bool _IsLockedOut = chkIsLockedOut.Checked;
            int IsLockedOut;
            if (_IsLockedOut == true)
            {
                IsLockedOut = 1;
            }
            else
            {
                IsLockedOut = 0;
            }
            //=================================================
            ModuleClass module_obj = new ModuleClass();
            string Username = string.Empty;
            if (module_obj.CheckInput(txtUserName.Text))
            {
                Username = txtUserName.Text;
            }
            else
            {
                Response.Write("<script>alert('Invalid Username');</script>");
                Response.End();
            }

            MD5CryptEncrypt md5_obj = new MD5CryptEncrypt();
            string Password = md5_obj.getMd5Hash(txtPassword.Text);
            string PasswordSalt = txtPassword.Text;
            string passwordConfirm = txtPasswordConfirm.Text;
            string PasswordQuestion = ddlPassQuestion.SelectedValue;
            string PasswordAnswer = txtPassAnswer.Text;

            string Comment = txtComment.Text;

            UserController user_obj = new UserController();
            int result = user_obj.Update(_idx, ApplicationId, PortalId, VendorId, RoleId, Username,
            Password, PasswordSalt, PasswordQuestion, PasswordAnswer,
            FullName, DisplayName, Address, MobilePIN, Phone, Email,
            IsSuperUser, UpdatePassword,IsDeleted, IsApproved, IsLockedOut, Comment);           
            
            return result;

        }
        private int AddData()
        {          
            string ApplicationId = ddlApplicationList.SelectedValue;
            int PortalId = Convert.ToInt32(ddlPortalList.SelectedValue);
            int VendorId = Convert.ToInt32(ddlVendorList.SelectedValue);
            string RoleId = ddlRoleList.SelectedValue;
            string FullName = txtFullName.Text;
            string DisplayName = txtDisplayName.Text;                
            string MobilePIN = txtMobilePIN.Text;
            string Address = txtAddress.Text;
            string Phone = txtPhone.Text;
            string Email = txtEmail.Text;          

            bool _IsSuperUser = chkIsSuperUser.Checked;                
            int IsSuperUser;
            if (_IsSuperUser == true)
            {
                IsSuperUser = 1;
            }
            else
            {
                IsSuperUser = 0;
            }

            bool _IsDeleted = chkIsDeleted.Checked;
            int IsDeleted;
            if (_IsDeleted == true)
            {
                IsDeleted = 1;
            }
            else
            {
                IsDeleted = 0;
            }

            bool _IsApproved = chkIsApproved.Checked;
            int IsApproved;
            if (_IsApproved == true)
            {
                IsApproved = 1;
            }
            else
            {
                IsApproved = 0;
            }

            bool _UpdatePassword = chkUpdatePassword.Checked;
            int UpdatePassword;
            if (_UpdatePassword == true)
            {
                UpdatePassword = 1;
            }
            else
            {
                UpdatePassword = 0;
            }

            ModuleClass module_obj = new ModuleClass();
            string Username = string.Empty;
            if (module_obj.CheckInput(txtUserName.Text))
            {
                Username = txtUserName.Text;
            }
            else
            {
                string scriptCode = "<script>alert('Invalid Username.');</script>";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", scriptCode);
            }

            MD5CryptEncrypt md5_obj = new MD5CryptEncrypt();
            string Password = md5_obj.getMd5Hash(txtPassword.Text);
            string PasswordSalt = txtPassword.Text;
            string passwordConfirm = txtPasswordConfirm.Text;
            string PasswordQuestion = ddlPassQuestion.SelectedValue;
            string PasswordAnswer = txtPassAnswer.Text;

            string Comment = txtComment.Text;

            UserController user_obj = new UserController();
            int result = user_obj.Insert(ApplicationId, PortalId, VendorId, RoleId, Username, Password, PasswordSalt,
                PasswordQuestion, PasswordAnswer, FullName, DisplayName, Address, MobilePIN,
                Phone, Email, IsSuperUser, UpdatePassword, IsDeleted, IsApproved, Comment);  
            return result;
        }
        public CascadingDropDownNameValue[] GetUserList(string knownCategoryValues, string category)
        {
            StringDictionary dataValues = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            string ApplicationId = dataValues["ApplicationId"];
            UserController user_obj = new UserController();
            DataTable dt = user_obj.GetUsers(ApplicationId);

            List<CascadingDropDownNameValue> user_list = new List<CascadingDropDownNameValue>();
            foreach (DataRow dRow in dt.Rows)
            {
                string UserId = dRow["UserId"].ToString();
                string UserName = dRow["UserName"].ToString();
                user_list.Add(new CascadingDropDownNameValue(UserName, UserId));
            }
            return user_list.ToArray();
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string ID = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();              
                e.Row.Attributes["ondblclick"] = "this.style.background='#BFFF00';ShowEditModal('" + ID + "');";
                //e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                //e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");  
                //e.Row.Attributes["onmousedown"] = "this.style.background='#FACC2E';";
                //e.Row.Attributes["onmouseclick"] = "this.style.background='##228b22';this.style.color='#FFFFFF';";

                DropDownList ddlUserList = (DropDownList)e.Row.FindControl("ddlUserList"); 
                ddlUserList.Items.Clear();
                string ApplicationId = ddlApplicationList.SelectedValue;
                UserController user_obj = new UserController();
                DataTable dt_app = user_obj.GetUsers(ApplicationId);
                ddlUserList.DataSource = dt_app;
                ddlUserList.DataTextField = "UserName";
                ddlUserList.DataValueField = "UserId";
                ddlUserList.DataBind();
                if (Session["UserId"] != null && Session["UserId"].ToString() != string.Empty)
                    ddlUserList.SelectedValue = Session["UserId"].ToString();
                else
                    ddlUserList.Items.Insert(0, new ListItem("- Chọn -", ""));
                ddlUserList.SelectedIndex = 0;            
            }

            //if(e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer && e.Row.RowType != DataControlRowType.Pager)
            //{
            //    e.Row.Cells[0].Text = "<input type=\"checkbox\" value=\"" + e.Row.Cells[1].Text + "\" name=\"chkRecordId\" id=\"chkRecordId\"/>";
            //}
        }
        private int UpdateData()
        {           
            string FullName = txtFullName.Text;
            string DisplayName = txtDisplayName.Text;           
            string Address = txtAddress.Text;
            string Phone = txtPhone.Text;
            string Email = txtEmail.Text;

           
            ModuleClass module_obj = new ModuleClass();
            string Username = string.Empty;
            if (module_obj.CheckInput(txtUserName.Text))
            {
                Username = txtUserName.Text;
            }
            else
            {
                Response.Write("<script>alert('Invalid Username');</script>");
                Response.End();
            }

            MD5CryptEncrypt md5_obj = new MD5CryptEncrypt();
            string Password = md5_obj.getMd5Hash(txtPassword.Text);
            string PasswordSalt = txtPassword.Text;
            string passwordConfirm = txtPasswordConfirm.Text;
            string PasswordQuestion = ddlPassQuestion.SelectedValue;
            string PasswordAnswer = txtPassAnswer.Text;

            UserController user_obj = new UserController();
            int result = user_obj.Edit(Username,Password, PasswordSalt, PasswordQuestion, PasswordAnswer,
                                 FullName, DisplayName, Address, Phone, Email);
            return result;

        }
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         DropDownList ddlUserList = (DropDownList)e.Row.FindControl("ddlUserList");
         ddlUserList.Items.Clear();
         string ApplicationId = ddlApplicationList.SelectedValue;
         UserController user_obj = new UserController();
         DataTable dt_app = user_obj.GetUsers(ApplicationId);
         ddlUserList.DataSource = dt_app;
         ddlUserList.DataTextField = "UserName";
         ddlUserList.DataValueField = "UserId";
         ddlUserList.DataBind();
         ddlUserList.Items.Insert(0, new ListItem("- Chọn -", ""));
         ddlUserList.SelectedIndex = 0;
     }
 }
        protected void check_login(string Username, string Password)
        {            
            string[] array_list = new string[10];
            UserController users_obj = new UserController();
            array_list = users_obj.CheckLogin(Username, Password);

            string ApplicationId = array_list[0].ToString();
            string UserId = array_list[1].ToString();
            string RoleId = array_list[2].ToString();
            string PortalId = array_list[3].ToString();
            string VendorId = array_list[4].ToString();
            string HomeDirectory = array_list[5].ToString();
            string IsSuperUser = array_list[6].ToString();
            string UpdatePassword = array_list[7].ToString();
            string IsDeleted = array_list[8].ToString();
            string Status = array_list[9].ToString();

            switch (Status)
            {
                case "-1":
                    Response.Write("<script>alert('Username or password is empty');document.location='login'</script>");
                    Response.End();
                    break;
                case "-2":
                    Response.Write("<script>alert('Username or password is incorrect');document.location='login'</script>");
                    Response.End();
                    break;
                case "-3":
                    Response.Write("<script>alert('Error to write log');document.location='login'</script>");
                    Response.End();
                    break;
                case "1":
                    Session["ApplicationId"] = ApplicationId;
                    Session["UserId"] = UserId;
                    Session["RoleId"] = RoleId;
                    Session["PortalId"] = PortalId;
                    Session["VendorId"] = VendorId;
                    Session["IsSuperUser"] = IsSuperUser;
                    Session["UpdatePassword"] = UpdatePassword;
                    Session["IsDeleted"] = IsDeleted;
                    Session["UserName"] = Username;
                    Session["HomeDirectory"] = HomeDirectory;
                    Session.Timeout = 216000;

                    if (chkRemmberMe.Checked)
                        WriteCookies(Username, Password, UserId, RoleId, PortalId, VendorId, IsSuperUser, UpdatePassword, IsDeleted, HomeDirectory);
                    else
                        DeleteCookies();

                    //users_obj.CreateSessionLog(UserId, Username);
                    //GetVirtualPath(RequestContext, RouteValueDictionary)                                              
                    //Response.RedirectToRoutePermanent("admin_index", new { tabid = "13"});
                    Response.RedirectToRoutePermanent("admin_index_tabid", new { tabid = "13" });
                    break;
                case "2":
                    Response.Write("<script>alert('Username or password has not yet activated');document.location='login'</script>");
                    Response.End();
                    break;
                case "3":
                    Response.Write("<script>alert('Username or password is blocked');document.location='login'</script>");
                    Response.End();
                    break;
                default:
                    Response.Write("<script>alert('System Error');document.location='login'</script>");
                    Response.End();
                    break;
            }
        }