//JIRA-746 CHanges by Ravi on 05/03/2019 -- End


        #endregion EVENTS

        #region METHODS

        private void LoadTerritoryList()
        {
            territorySearchBL = new TerritorySearchBL();
            DataSet territoriesList = territorySearchBL.GetInitialData(out errorId);

            territorySearchBL = null;

            if (territoriesList.Tables.Count != 0 && errorId != 2)
            {
                Session["FuzzyTerritorySearchSellerGrpList"] = territoriesList.Tables[0];
                Session["TerritoyrSearhCountryList"]         = territoriesList.Tables[2];

                ddlTerritoryType.DataSource     = territoriesList.Tables[1];
                ddlTerritoryType.DataTextField  = "territory_description";
                ddlTerritoryType.DataValueField = "territory_type";
                ddlTerritoryType.DataBind();
                ddlTerritoryType.Items.Insert(0, new ListItem("-"));

                ddlGridTerritoryType.DataSource     = territoriesList.Tables[1];
                ddlGridTerritoryType.DataTextField  = "territory_description";
                ddlGridTerritoryType.DataValueField = "territory_type";
                ddlGridTerritoryType.DataBind();
                ddlGridTerritoryType.Items.Insert(0, new ListItem("-"));
            }
            else if (territoriesList.Tables.Count == 0 && errorId != 2)
            {
                Session["FuzzyTerritorySearchSellerGrpList"] = null;
            }
            else
            {
                ExceptionHandler("Error in loading territories", string.Empty);
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string territoryCode;
                //changes maded by Harish - 27-10-16
                //if (!string.IsNullOrWhiteSpace(txtNewTerritory.Text) & !string.IsNullOrWhiteSpace(txtNewTerritoryLoc.Text))
                if (string.IsNullOrWhiteSpace(txtNewTerritory.Text) & string.IsNullOrWhiteSpace(txtNewTerritoryLoc.Text))
                {
                    msgView.SetMessage("Please select a territory to add to the groups",
                                       MessageType.Warning, PositionType.Auto);
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(txtNewTerritory.Text))
                    {
                        territoryCode = txtNewTerritory.Text.Substring(0, txtNewTerritory.Text.IndexOf("-") - 1);
                    }
                    else
                    {
                        territoryCode = txtNewTerritoryLoc.Text.Substring(0, txtNewTerritoryLoc.Text.IndexOf("-") - 1);
                    }
                    string territoryGroupCode;

                    List <string> territoryGroupCodes = new List <string>();

                    foreach (GridViewRow gridrow in gvTerritoryGroup.Rows)
                    {
                        territoryGroupCode = (gridrow.FindControl("lblTerritoryGroupCode") as Label).Text;
                        territoryGroupCodes.Add(territoryGroupCode);
                    }

                    if (territoryGroupCodes.Count > 0)
                    {
                        string userCode = WebUtility.HtmlDecode(System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString());

                        territorySearchBL = new TerritorySearchBL();
                        territorySearchBL.AddTerritoryToGroups(territoryCode, territoryGroupCodes.ToArray(), userCode, out errorId);
                        territorySearchBL = null;

                        if (errorId == 0)
                        {
                            msgView.SetMessage("New territory has been added to the groups",
                                               MessageType.Warning, PositionType.Auto);
                        }
                        else
                        {
                            ExceptionHandler("Error in adding the territory to groups.", "");
                        }
                    }
                }
                mpeInsertGroup.Show();
            }
            catch (Exception ex)
            {
                ExceptionHandler("Error in adding the territory to groups.", ex.Message);
            }
        }
        private void LoadGridData(string territoryCode)
        {
            territorySearchBL = new TerritorySearchBL();
            DataSet territoryData = territorySearchBL.GetTerritoryData(territoryCode, out errorId);

            territorySearchBL = null;

            BindGrids(territoryData);
        }
        protected void btnAddTerritory_Click(object sender, EventArgs e)
        {
            try
            {
                Page.Validate("valAddTerritoryGroup");
                if (!Page.IsValid)
                {
                    mpeAddTerritoryCode.Show();
                }
                else
                {
                    string userCode = Convert.ToString(Session["UserCode"]);

                    territorySearchBL = new TerritorySearchBL();
                    DataSet dtTerritoryList = territorySearchBL.InsertUpdateTerritoryGroup("I", txtTerritoryCode.Text.ToUpper(), txtTerritoryName.Text.ToUpper(), txtTerritoryLocation.Text.ToUpper(), txtCountryCode.Text.ToUpper(), ddlTerritoryType.SelectedValue, userCode, out errorId);
                    territorySearchBL = null;

                    if (dtTerritoryList.Tables.Count > 0 && errorId == 0)
                    {
                        Session["FuzzyTerritorySearchSellerGrpList"] = dtTerritoryList.Tables[0];
                        msgView.SetMessage("Territory code added successfully.", MessageType.Warning, PositionType.Auto);
                        mpeAddTerritoryCode.Hide();
                        txtTerritoryCode.Text          = "";
                        txtTerritoryName.Text          = "";
                        txtTerritoryLocation.Text      = "";
                        txtCountryCode.Text            = "";
                        ddlTerritoryType.SelectedIndex = 0;
                    }
                    else if (errorId == 1)
                    {
                        msgView.SetMessage("Territory code already exists.", MessageType.Warning, PositionType.Auto);
                        mpeAddTerritoryCode.Show();
                    }
                    else
                    {
                        ExceptionHandler("Error in adding the new territory code.", "");
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler("Error in adding the new territory code.", ex.Message);
            }
        }
        protected void imgBtnTerritoryUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string userCode = Convert.ToString(Session["UserCode"]);

                Page.Validate("valUpdateTerritoryGroup");
                if (!Page.IsValid)
                {
                    return;
                }

                territorySearchBL = new TerritorySearchBL();
                DataSet dtTerritoryList = territorySearchBL.InsertUpdateTerritoryGroup("U", lblTerritoryCode.Text.ToUpper(), txtGridTerritoryName.Text.ToUpper(), txtGridTerritoryLocation.Text.ToUpper(), txtGridCountryCode.Text.ToUpper(), ddlGridTerritoryType.SelectedValue, userCode, out errorId);
                territorySearchBL = null;

                if (dtTerritoryList.Tables.Count != 0 && errorId != 2)
                {
                    Session["FuzzyTerritorySearchSellerGrpList"] = dtTerritoryList.Tables[0];

                    DataTable dtTerritoryDtls = dtTerritoryList.Tables[1];
                    lblTerritoryCode.Text          = dtTerritoryDtls.Rows[0]["seller_code"].ToString();
                    hdnGridTerritoryName.Value     = txtGridTerritoryName.Text = dtTerritoryDtls.Rows[0]["seller_name"].ToString();
                    hdnGridTerritoryLocation.Value = txtGridTerritoryLocation.Text = dtTerritoryDtls.Rows[0]["seller_location"].ToString();
                    hdnGridCountryCode.Value       = txtGridCountryCode.Text = dtTerritoryDtls.Rows[0]["country_code"].ToString();
                    hdnGridTerritoryType.Value     = ddlGridTerritoryType.SelectedValue = dtTerritoryDtls.Rows[0]["territory_type"].ToString();

                    msgView.SetMessage("Territory code updated successfully.", MessageType.Warning, PositionType.Auto);
                }
                else
                {
                    msgView.SetMessage("Failed to update territory code.", MessageType.Warning, PositionType.Auto);
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler("Error in updating territory code.", ex.Message);
            }
        }