//select an account from list
        protected void SelectLinkButton_Command(object sender, CommandEventArgs e)
        {
            //highlight selected row start
            int gindex = Convert.ToInt32(ViewAccountsGridView.SelectedIndex);
            if (gindex > -1)
            {
                LinkButton nlbtn = ViewAccountsGridView.Rows[gindex].FindControl("SelectLinkButton") as LinkButton;
                nlbtn.CssClass = "";
            }
            LinkButton lbtn = sender as LinkButton;
            if (lbtn != null) lbtn.CssClass = "active";
            //highlight selected row end

            if (CheckEdit() != false)
                EditLinkButton.Visible = true;
            if (CheckDelete() != false)
                DeleteLinkButton.Visible = true;
            ModalPopButton2.Visible = true;
            NotifyLinkButton.Visible = true;

            // Session["EditAccountID"] = e.CommandArgument.ToString();
            //added on 17.01.2013
            try
            {
                UserBL userBL = new UserBL();
                string arguments = Convert.ToString(e.CommandArgument);
                string[] arg = arguments.Split(';');
                Session["EditAccountID"] = Convert.ToInt64(arg[0]);
                if (arg[1] != string.Empty)
                {
                    long assignedUserID = Convert.ToInt64(arg[1]);
                    Session["AccountName"] = Convert.ToString(arg[2]);
                    try
                    {
                        IEnumerable<DAL.User> userdetails = userBL.GetUsersByID(assignedUserID);
                        foreach (var user in userdetails)
                        {
                            Session["ReceiverName"] = user.FirstName;
                            Session["ReceiverEmail"] = user.PrimaryEmail;
                        }

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                MiniAccountsFormView.DataBind();
                miniAccountDetailsView.DataBind();
                miniAccountDetailsUpdatePanel.Update();
            }

            catch (Exception ex)
            {
                throw ex;
            }
            //end on 17.01.2013
            //comment out to enable singleaccount in formview
        }
        protected void UsersDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            string ID = UsersDetailsView.DataKey[0].ToString();
            Int64 userid = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
            Int64 currentuserid = Int64.Parse(ID);
            UserBL userBL = new UserBL();
            long edituser = Convert.ToInt32(HttpContext.Current.Session["EditUserID"]);
            string password = null;
            string passwordsalt = null;
            var user = userBL.GetUsersByID(edituser);

            if (user.Count() > 0)
            {
                password = user.ElementAt(0).Password;
                passwordsalt = user.ElementAt(0).PasswordSalt;
            }

            e.NewValues["Password"] = password;
            e.NewValues["PasswordSalt"] = passwordsalt;
            e.NewValues["Gender"] = genderDropDownList.SelectedValue;
            e.NewValues["ModifiedBy"] = userid;
            e.NewValues["ModifiedTime"] = DateTime.Now;
            e.NewValues["IsActive"] = isActiveDropDownList.SelectedValue;
            e.NewValues["MessengerType"] = messengerTypeDropDownList.SelectedValue;
               // e.NewValues["SecurityQuestion"] = securityQuestionDropDownList.SelectedValue;
            e.NewValues["TeamID"] = teamDropDownList.SelectedValue;
            Int64 teamid = Convert.ToInt64(teamDropDownList.SelectedValue);
            e.NewValues["TeamSetID"] = userBL.GetTeamSetID(teamid);

            if (Convert.ToInt32(countryDropDownList.SelectedValue) == 0) { e.NewValues["CountryID"] = null; }
            else { e.NewValues["CountryID"] = countryDropDownList.SelectedValue; }

            if (Convert.ToInt32(roleDropDownList.SelectedValue) == 0) { e.NewValues["RoleID"] = null; e.NewValues["IsAdmin"] = null; }
            else
            {
                e.NewValues["RoleID"] = roleDropDownList.SelectedValue;
                int roleid = Convert.ToInt32(roleDropDownList.SelectedValue);
                string rolename = userBL.GetRoleName(roleid);
                if (rolename == "Administrator" || rolename == "Admin")
                {
                    e.NewValues["IsAdmin"] = true;
                }
                else
                {
                    e.NewValues["IsAdmin"] = false;
                }

            }

            if (Convert.ToInt32(reportsToDropDownList.SelectedValue) == 0) { e.NewValues["ReportsTo"] = null; }
            else { e.NewValues["ReportsTo"] = reportsToDropDownList.SelectedValue; }
        }
        //after updating an account
        protected void AccountDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                var customValidator = new CustomValidator();
                customValidator.IsValid = false;
                customValidator.ErrorMessage = "Save failed: " + e.Exception.InnerException.Message;
                customValidator.ValidationGroup = "sum";
                Page.Validators.Add(customValidator);
                e.ExceptionHandled = true;
            }
            else
            {
                int rowcount = e.AffectedRows;
                string receiverName = string.Empty;
                string receiverEmail = string.Empty;
                if (rowcount == -1)
                {
                    string name = e.NewValues["Name"].ToString();
                    MsgLiteral.Text = "Success";
                    alertLabel.Text = "This account " + name + " has been saved";
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "ShowAlertModal();", true);
                }

                //else
                //{
                //    MsgLiteral.Text = "Save failed";
                //    alertLabel.Text = "Sorry,this Account has not been saved";
                //}

                if (Session["NotifyStatus"] != null)
                {
                    UserBL userBL = new UserBL();
                    long assignedUserID = Convert.ToInt64(e.NewValues["AssignedUserID"]);
                    string accountName = e.NewValues["Name"].ToString();
                    IEnumerable<DAL.User> userdetails = userBL.GetUsersByID(assignedUserID);
                    foreach (var user in userdetails)
                    {
                        receiverName = user.FirstName;
                        receiverEmail = user.PrimaryEmail;
                    }
                    const string subject = "Account Notification";
                    string body = "Dear " + receiverName + ",Your new assignment is to deal with " + accountName + " ";
                    try
                    {
                        Notification.SendEmail(receiverEmail, subject, body);
                        Session["NotifyStatus"] = 0;
                        MiniAccountsFormView.DataBind();
                        miniAccountDetailsView.DataBind();
                        ViewAccountsGridView.DataBind();
                        miniAccountDetailsUpdatePanel.Update();
                        Session["EditAccountID"] = 0;
                        AccountDetailsView.DataBind();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
        protected void UsersDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            string ID = UsersDetailsView.DataKey[0].ToString();
            long userid = Convert.ToInt32(HttpContext.Current.Session["UserID"]);
            long currentuserid = Int64.Parse(ID);
            string password = null;
            string passwordsalt = null;
            UserBL userBL = new UserBL();
            var user = userBL.GetUsersByID(userid);

            if (user.Count() > 0)
            {
                password = user.ElementAt(0).Password;
                passwordsalt = user.ElementAt(0).PasswordSalt;
            }
            //FileUpload imageFileUpload = (FileUpload)UsersDetailsView.FindControl("fileUpload");
            //if (imageFileUpload.HasFile)
            //{
            //    const string fileUploadDirectory = "Images/";
            //    string fileName = Server.HtmlEncode(imageFileUpload.FileName);
            //    string extension = System.IO.Path.GetExtension(fileName);
            //    string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imageFileUpload.FileName);
            //    string imagefilename = userid + "_" + "ProfilePic";
            //    string encodedImageName = Encryptdata(imagefilename);

            //    string imagePath = fileUploadDirectory + encodedImageName + extension;

            //    string fileString = System.IO.Path.GetExtension(imageFileUpload.FileName);
            //    if (extension == ".jpg" || extension == ".bmp" || extension == ".gif")
            //    {
            //        string file = imagefilename + extension;
            //        //imageFileUpload.SaveAs(Path.Combine(fileUploadDirectory, file));
            //       imageFileUpload.SaveAs(Server.MapPath("Images/" + imagefilename + extension));
            //        // imageFileUpload.SaveAs(fileUploadDirectory + imageFileUpload.FileName);
            //        e.NewValues["Image"] = imagePath;
            //    }
            //    else
            //    {
            //        ScriptManager.RegisterStartupScript(this,this.GetType(), "script", "alert('Only jpg, bmp and gif files are allowed.')",true);

            //    }
            //}

            e.NewValues["Image"] = Convert.ToString(Session["ImagePath"]);
            e.NewValues["Password"] = password;
            e.NewValues["PasswordSalt"] = passwordsalt;
            e.NewValues["Gender"] = genderDropDownList.SelectedValue;
            e.NewValues["ModifiedBy"] = userid;
            e.NewValues["ModifiedTime"] = DateTime.Now;
               // e.NewValues["IsActive"] = isActiveDropDownList.SelectedValue;
              //  e.NewValues["MaritalStatus"] = maritalStatusDropDownList.SelectedValue;
            e.NewValues["MessengerType"] = messengerTypeDropDownList.SelectedValue;
            e.NewValues["SecurityQuestion"] = securityQuestionDropDownList.SelectedValue;
            //e.NewValues["TeamID"] = teamDropDownList.SelectedValue;

            if (Convert.ToInt32(countryDropDownList.SelectedValue) == 0) { e.NewValues["CountryID"] = null; }
            else { e.NewValues["CountryID"] = countryDropDownList.SelectedValue; }

            //if (Convert.ToInt32(roleDropDownList.SelectedValue) == 0) { e.NewValues["RoleID"] = null; }
            //else { e.NewValues["RoleID"] = roleDropDownList.SelectedValue; }

            if (Convert.ToInt32(reportsToDropDownList.SelectedValue) == 0) { e.NewValues["ReportsTo"] = null; }
            else { e.NewValues["ReportsTo"] = reportsToDropDownList.SelectedValue; }
        }
        protected void UsersDetailsView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            Int64 id = 0;
            string value = Convert.ToString(Session["UpdateUserProfile"]);
            if (value != "")
            id = int.Parse(value);

            if (id > 0)
            {
                //Literal1.Text = "Success";
                //Label6.Text =  "Profile has been saved successfully.";
                long userid = Convert.ToInt64(Session["UserID"]);
                UserBL userBL = new UserBL();
                var user = userBL.GetUsersByID(userid);
                string image = user.ElementAt(0).Image;
                UserImage.ImageUrl = "~/Users/" + Decryptdata(image);

                Literal1.Text = "Success !</br></br> <p>Profile information has been updated successfully</p>";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$('#myModal').reveal();", true);
                UsersDetailsView.DataBind();
            }
            else
            {
               // Literal1.Text = "Error";
               // Label6.Text =  "Profile did not save.";
                Literal1.Text = "Error !</br></br> <p>Profile information did not save.</p>";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$('#myModal').reveal();", true);
                UsersDetailsView.DataBind();
            }
        }
        protected void PasswordSaveButton_Click(object sender, EventArgs e)
        {
            long userid = Convert.ToInt64(Session["UserID"]);
            string oldpassword = OldPasswordTextBox.Text;
            string newpassword = NewPasswordTextBox.Text;
            string confirmpassword = Server.HtmlEncode(ConfirmPasswordTextBox.Text);
            string hash = null;
            string salt = null;
            string goodHash = null;
            bool chk = false;
            UserBL userBL = new UserBL();
            var user = userBL.GetUsersByID(userid);

            if(user.Count()>0)
            {
                    hash = user.ElementAt(0).Password;
                    salt = user.ElementAt(0).PasswordSalt;
                    goodHash = "1000:" + salt + ":" + hash;
                    chk =  Authentication.ValidatePassword(oldpassword,goodHash);
             }

            if (chk == true)
            {
                if(newpassword.Length >=6)
                {
                    if(newpassword == confirmpassword)
                    {
                        // save password
                        ErrorLabel.Text = "Success and Saved!";
                        userBL.SetPassword(newpassword);

                            TextBox pe = (TextBox)UsersDetailsView.FindControl("PasswordTextBox");
                            pe.Text = newpassword;
                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "CloseModal('BodyContent_ModalPanel1');", true);

                    }
                    else
                    {
                        // new and confirm did not match
                        ErrorLabel.Text = "Confirm Password did not match! Please try again";
                    }

                }
                else
                {
                    // more than 6 char
                    ErrorLabel.Text = "Password length should be more than 5 characters";
                }

            }
            else
            {
                // password not match;
                ErrorLabel.Text = "Wrong Password! Please try again.";

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            long userid = Convert.ToInt64(Session["UserID"]);
            UserBL userBL = new UserBL();
            var user = userBL.GetUsersByID(userid);
            string image = user.ElementAt(0).Image;

            UserImage.ImageUrl = "~/Users/" + Decryptdata(image);
        }