protected void btnSave_Click(object sender, EventArgs e)
        {
            UserBo          userBo         = new UserBo();
            UserVo          userVo         = new UserVo();
            UserVo          userVo2        = new UserVo();
            bool            branchDeletion = true;
            Random          id             = new Random();
            AdvisorBo       advisorBo      = new AdvisorBo();
            AdvisorBranchBo advisorBrBo    = new AdvisorBranchBo();

            bool   blUpdate    = true;
            string association = "";

            try
            {
                if (Validation())
                {
                    userId            = int.Parse(Session["userId"].ToString());
                    userVo            = userBo.GetUserDetails(rmVo.UserId);
                    userVo.Email      = txtEmail.Text.ToString();
                    userVo.FirstName  = txtFirstName.Text.ToString();
                    userVo.LastName   = txtLastName.Text.ToString();
                    userVo.MiddleName = txtMiddleName.Text.ToString();



                    rmVo.RMId  = int.Parse(Session["rmId"].ToString());
                    rmVo.Email = txtEmail.Text.ToString();
                    if (txtFaxNumber.Text == string.Empty || txtFaxNumber.Text == "")
                    {
                        rmVo.Fax = 0;
                    }
                    else
                    {
                        rmVo.Fax = int.Parse(txtFaxNumber.Text.ToString());
                    }
                    if (txtFaxISD.Text == string.Empty || txtFaxISD.Text == "")
                    {
                        rmVo.FaxIsd = 0;
                    }
                    else
                    {
                        rmVo.FaxIsd = int.Parse(txtFaxISD.Text.ToString());
                    }
                    if (txtExtSTD.Text == string.Empty || txtExtSTD.Text == "")
                    {
                        rmVo.FaxStd = 0;
                    }
                    else
                    {
                        rmVo.FaxStd = int.Parse(txtExtSTD.Text.ToString());
                    }
                    rmVo.FirstName  = txtFirstName.Text.ToString();
                    rmVo.LastName   = txtLastName.Text.ToString();
                    rmVo.MiddleName = txtMiddleName.Text.ToString();
                    if (txtMobileNumber.Text == string.Empty || txtMobileNumber.Text == "")
                    {
                        rmVo.Mobile = 0;
                    }
                    else
                    {
                        rmVo.Mobile = Convert.ToInt64(txtMobileNumber.Text.ToString());
                    }
                    if (txtPhDirectISD.Text == string.Empty || txtPhDirectISD.Text == "")
                    {
                        rmVo.OfficePhoneDirectIsd = 0;
                    }
                    else
                    {
                        rmVo.OfficePhoneDirectIsd = int.Parse(txtPhDirectISD.Text.ToString());
                    }
                    if (txtPhDirectPhoneNumber.Text == string.Empty || txtPhDirectPhoneNumber.Text == "")
                    {
                        rmVo.OfficePhoneDirectNumber = 0;
                    }
                    else
                    {
                        rmVo.OfficePhoneDirectNumber = int.Parse(txtPhDirectPhoneNumber.Text.ToString());
                    }
                    if (txtPhDirectSTD.Text == string.Empty || txtPhDirectSTD.Text == "")
                    {
                        rmVo.OfficePhoneDirectStd = 0;
                    }
                    else
                    {
                        rmVo.OfficePhoneDirectStd = int.Parse(txtPhDirectSTD.Text.ToString());
                    }
                    if (txtPhExtISD.Text == string.Empty || txtPhExtISD.Text == "")
                    {
                        rmVo.OfficePhoneExtIsd = 0;
                    }
                    else
                    {
                        rmVo.OfficePhoneExtIsd = int.Parse(txtPhExtISD.Text.ToString());
                    }
                    if (txtPhExtPhoneNumber.Text == string.Empty || txtPhExtPhoneNumber.Text == "")
                    {
                        rmVo.OfficePhoneExtNumber = 0;
                    }
                    else
                    {
                        rmVo.OfficePhoneExtNumber = int.Parse(txtPhExtPhoneNumber.Text.ToString());
                    }
                    if (txtExtSTD.Text == string.Empty || txtExtSTD.Text == "")
                    {
                        rmVo.OfficePhoneExtStd = 0;
                    }
                    else
                    {
                        rmVo.OfficePhoneExtStd = int.Parse(txtExtSTD.Text.ToString());
                    }
                    if (txtPhResiISD.Text == string.Empty || txtPhResiISD.Text == "")
                    {
                        rmVo.ResPhoneIsd = 0;
                    }
                    else
                    {
                        rmVo.ResPhoneIsd = int.Parse(txtPhResiISD.Text.ToString());
                    }
                    if (txtPhResiPhoneNumber.Text == string.Empty || txtPhResiPhoneNumber.Text == "")
                    {
                        rmVo.ResPhoneNumber = 0;
                    }
                    else
                    {
                        rmVo.ResPhoneNumber = int.Parse(txtPhResiPhoneNumber.Text.ToString());
                    }
                    if (txtResiSTD.Text == string.Empty || txtResiSTD.Text == "")
                    {
                        rmVo.ResPhoneStd = 0;
                    }
                    else
                    {
                        rmVo.ResPhoneStd = int.Parse(txtResiSTD.Text.ToString());
                    }
                    if (txtCTC.Text == string.Empty || txtCTC.Text == "")
                    {
                        rmVo.CTC = 0;
                    }
                    else
                    {
                        rmVo.CTC = Convert.ToDouble(txtCTC.Text.Trim());
                    }

                    if (ChklistRMBM.Items[0].Selected == true)
                    {
                        rmVo.RMRole = "RM";
                    }
                    else
                    {
                        rmVo.RMRole = "BM";
                    }

                    if (chkExternalStaff.Checked)
                    {
                        // Check for Internal Associations, if they exist do not update.
                        if (advisorBrBo.CheckInternalBranchAssociations(rmVo.RMId))
                        {
                            association = "internal";
                            blUpdate    = false;
                        }
                        else
                        {
                            rmVo.IsExternal = 1;
                        }
                    }
                    else
                    {
                        // Check for External Associations, if they exist do not update.
                        if (advisorBrBo.CheckExternalBranchAssociations(rmVo.RMId))
                        {
                            association = "external";
                            blUpdate    = false;
                        }
                        else
                        {
                            rmVo.IsExternal = 0;
                        }
                    }



                    //*************Role Association Creation and deletion************************

                    bool     RMBMRole      = false;
                    string[] RoleListArray = rmVo.RMRoleList.Split(new char[] { ',' });
                    foreach (ListItem Items in ChklistRMBM.Items)
                    {
                        if (Items.Text == "RM")
                        {
                            foreach (string Role in RoleListArray)
                            {
                                if (Role == "RM")
                                {
                                    RMBMRole = true;
                                }
                            }
                            // Create Role Association for RM
                            if (RMBMRole == true && Items.Selected == false)
                            {
                                userBo.DeleteRoleAssociation(rmVo.UserId, Int16.Parse(Items.Value.ToString()));
                            }
                            else if (RMBMRole == false && Items.Selected == true)
                            {
                                userBo.CreateRoleAssociation(rmVo.UserId, Int16.Parse(Items.Value.ToString()));
                            }
                            RMBMRole = false;
                        }
                        else if (Items.Text == "BM")
                        {
                            foreach (string Role in RoleListArray)
                            {
                                if (Role == "BM")
                                {
                                    RMBMRole = true;
                                }
                            }
                            // Create Role Association for BM
                            if (RMBMRole == false && Items.Selected == true)
                            {
                                userBo.CreateRoleAssociation(rmVo.UserId, Int16.Parse(Items.Value.ToString()));
                            }
                            else if (RMBMRole == true && Items.Selected == false)
                            {
                                branchHead = advisorBranchBo.CheckBranchHead(rmVo.RMId, 0);
                                if (branchHead == 0)
                                {
                                    userBo.DeleteRoleAssociation(rmVo.UserId, Int16.Parse(Items.Value.ToString()));
                                }
                                else
                                {
                                    ChklistRMBM.Items[1].Selected = true;
                                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Pageloadscript", "alert('BM Role can not be removed as this staff is a Branch Head');", true);
                                }
                            }

                            RMBMRole = false;
                        }
                    }

                    //*************Role Association Creation and deletion************************

                    //Update the User details in the User Table
                    userBo.UpdateUser(userVo);
                    //Update the RM details in the AdvisorRM Table
                    advisorStaffBo.UpdateStaff(rmVo);



                    //**************Branch Association Creation and deletion*********************


                    //string hdnExistingString = hdnExistingBranches.Value.ToString();
                    string hdnSelectedString = hdnSelectedBranches.Value.ToString();
                    //string[] existingBranchesList = hdnExistingString.Split(',');
                    string[]  selectedBranchesList = hdnSelectedString.Split(',');
                    DataTable dtSelectedBranch     = new DataTable();
                    dtSelectedBranch.Columns.Add("Branch");
                    dtSelectedBranch.Columns.Add("BranchId");
                    DataTable  dtAssociated  = new DataTable();
                    List <int> deletedBRList = new List <int>();
                    List <int> addedBRList   = new List <int>();
                    dtAssociated = (DataTable)Session["AssociatedBranch"];
                    if (dtAssociated != null)
                    {
                        foreach (string str in selectedBranchesList)
                        {
                            if (str != "")
                            {
                                dtAssociated.DefaultView.Sort = "BranchId";
                                int i = dtAssociated.DefaultView.Find(str);
                                if (i == (0 - 1))
                                {
                                    addedBRList.Add(int.Parse(str));
                                }
                            }
                            DataRow dr = dtSelectedBranch.NewRow();
                            dr["Branch"]   = str;
                            dr["BranchId"] = str;
                            dtSelectedBranch.Rows.Add(dr);
                        }



                        foreach (DataRow dr in dtAssociated.Rows)
                        {
                            dtSelectedBranch.DefaultView.Sort = "BranchId";
                            int j = dtSelectedBranch.DefaultView.Find(dr["BranchId"].ToString());
                            if (j == (0 - 1))
                            {
                                if (dr["BranchId"].ToString() != "")
                                {
                                    deletedBRList.Add(int.Parse(dr["BranchId"].ToString()));
                                }
                            }
                        }

                        for (int i = 0; i < addedBRList.Count; i++)
                        {
                            advisorBranchBo.CreateRMBranchAssociation(rmVo.RMId, addedBRList[i], advisorVo.advisorId, advisorVo.advisorId);
                        }
                        for (int i = 0; i < deletedBRList.Count; i++)
                        {
                            bool IsBranchDependency = false;
                            IsBranchDependency = advisorBranchBo.CheckRMBranchDependency(rmVo.RMId, deletedBRList[i]);
                            if (IsBranchDependency == true)
                            {
                                branchDeletion = false;
                                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Pageloadscript", "alert('Sorry... You need to delete your internal associations first');", true);
                            }
                            else
                            {
                                advisorBranchBo.DeleteBranchAssociation(rmVo.RMId, deletedBRList[i]);
                            }
                        }

                        if (branchDeletion == false)
                        {
                            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "pageloadscript", "loadcontrol('ViewRMDetails','none');", true);
                        }
                    }
                    else
                    {
                        foreach (string str in selectedBranchesList)
                        {
                            if (str != "")
                            {
                                advisorBranchBo.CreateRMBranchAssociation(rmVo.RMId, int.Parse(str), advisorVo.advisorId, advisorVo.advisorId);
                            }
                        }
                    }
                    //advisorBranchBo.DeleteRMBranchAssociation1(rmIDRef);

                    //**************Branch Association Creation and deletion*********************



                    if (blUpdate)
                    {
                        btnSave.Enabled = false;
                        if (Session["FromAdvisorView"] != null)
                        {
                            if (Session["FromAdvisorView"].ToString() == "FromAdvView")
                            {
                                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "pageloadscript", "loadcontrol('ViewRM','none');", true);
                            }
                            else
                            {
                                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "pageloadscript", "loadcontrol('ViewRMDetails','none');", true);
                            }
                            Session.Remove("FromAdvisorView");
                        }
                        else
                        {
                            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "pageloadscript", "loadcontrol('ViewRM','none');", true);
                        }
                    }
                    else
                    {
                        if (association == "internal")
                        {
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Pageloadscript", "alert('Sorry... You need to delete your internal associations first');", true);
                        }
                        else if (association == "external")
                        {
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Pageloadscript", "alert('Sorry... You need to delete your external associations first');", true);
                        }
                    }
                }
            }
            catch (BaseApplicationException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                BaseApplicationException exBase       = new BaseApplicationException(Ex.Message, Ex);
                NameValueCollection      FunctionInfo = new NameValueCollection();

                FunctionInfo.Add("Method", "EditRMDetails.ascx:btnSave_Click()");

                object[] objects = new object[6];

                objects[0] = advisorVo;
                objects[1] = userVo;
                objects[2] = userBo;
                objects[3] = userId;
                objects[4] = rmId;
                objects[5] = userVo2;


                FunctionInfo = exBase.AddObject(FunctionInfo, objects);
                exBase.AdditionalInformation = FunctionInfo;
                ExceptionManager.Publish(exBase);
                throw exBase;
            }
        }