context: using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  using OrganizationSetup.Classes;
  using System.Diagnostics;
  using OrganizationSetup.Dialogs;
  using Npgsql;
  
  namespace OrganizationSetup.Forms
  {
      public partial class mainForm : WeifenLuo.WinFormsUI.Docking.DockContent
      {
          #region "GLOBAL VARIABLES..."
          public CommonCode.CommonCodes cmCde = new CommonCode.CommonCodes();
          cadmaFunctions.NavFuncs myNav = new cadmaFunctions.NavFuncs();
          cadmaFunctions.NavFuncs myNav1 = new cadmaFunctions.NavFuncs();
          //public NpgsqlConnection gnrlSQLConn = new NpgsqlConnection();
          public Int64 usr_id = -1;
          public int[] role_st_id = new int[0];
          public Int64 lgn_num = -1;
          public int Og_id = -1;
          public bool txtChngd = false;
          public string srchWrd = "%";
          //  string[] menuItems = {"Organization's Details", "Divisions/Groups", 
          //  "Sites/Locations","Jobs", "Grades", "Positions", "Pay Items", 
          //"Working Hours", "Gathering Types"};
          //  string[] menuImages = {"groupings.png", "staffs.png", "shield_64.png"
          //    ,"SecurityLock.png", "73.ico", "54.png", "staffs.png", "shield_64.png"
          //    ,"SecurityLock.png"};
          string[] menuItems = {"Organization's Details", "Divisions/Groups",
          "Sites/Locations","Jobs", "Grades", "Positions"};
          string[] menuImages = {"1098_png_icons_refresh.png", "images (1).jpg",
  "1283107630I68HM7.jpg","Hallmark_job_openings2.jpg", "images (4).jpg",
  "supervisor.jpg"};
          /*, 
      "Working Hours", "Gathering Types", "working_overtime.jpg"	,"Gathering-of-Women-Art.jpg"*/
          bool beenToCheckBx = false;
  
          //Org Panel Variables;
          Int64 orgDet_cur_indx = 0;
          bool is_last_orgDet = false;
          Int64 totl_orgDet = 0;
          long last_org_num = 0;
          public string orgDet_SQL = "";
          public string orgDetHrchy_SQL = "";
          bool obey_orgDet_evnts = false;
          bool addOrg = false;
          bool editOrg = false;
          bool addOrgs = false;
          bool editOrgs = false;
          //Div Panel Variables;
          Int64 divDet_cur_indx = 0;
          bool is_last_divDet = false;
          Int64 totl_divDet = 0;
          long last_div_num = 0;
          public string divDet_SQL = "";
          public string divDetHrchy_SQL = "";
          bool obey_divDet_evnts = false;
          bool addDiv = false;
          bool editDiv = false;
          bool addDivs = false;
          bool editDivs = false;
          bool delDivs = false;
          //Div Panel Variables;
          //Sites Panel Variables;
          Int64 site_cur_indx = 0;
          bool is_last_site = false;
          Int64 totl_site = 0;
          long last_site_num = 0;
          public string site_SQL = "";
          public string siteDet_SQL = "";
          bool obey_site_evnts = false;
          bool addSite = false;
          bool editSite = false;
          bool addSites = false;
          bool editSites = false;
          bool delSites = false;
          //Jobs Panel Variables;
          Int64 jobs_cur_indx = 0;
          bool is_last_job = false;
          Int64 totl_jobs = 0;
          long last_job_num = 0;
          public string jobs_SQL = "";
          public string jobHrchy_SQL = "";
          bool obey_jobs_evnts = false;
          bool addJob = false;
          bool editJob = false;
          bool addJobs = false;
          bool editJobs = false;
          bool delJobs = false;
          //Grades Panel Variables;
          Int64 grd_cur_indx = 0;
          bool is_last_grd = false;
          Int64 totl_grd = 0;
          long last_grd_num = 0;
          public string grd_SQL = "";
          public string grdHrchy_SQL = "";
          bool obey_grd_evnts = false;
          bool addgrd = false;
          bool editgrd = false;
          bool addgrds = false;
          bool editgrds = false;
          bool delgrds = false;
          //Position Panel Variables;
          long pos_cur_indx = 0;
          bool is_last_pos = false;
          long totl_pos = 0;
          long last_pos_num = 0;
          public string pos_SQL = "";
          public string posHrchy_SQL = "";
          bool obey_pos_evnts = false;
          bool addpos = false;
          bool editpos = false;
          bool addposs = false;
          bool editposs = false;
          bool delposs = false;
  
          //Benefits & Contributions Panel Variables;
          long itm_cur_indx = 0;
          bool is_last_itm = false;
          long totl_itm = 0;
          long last_itm_num = 0;
          public string itm_SQL = "";
          public string itmPval_SQL = "";
          public string itmFeed_SQL = "";
          bool obey_itm_evnts = false;
          bool additm = false;
          bool edititm = false;
          bool additms = false;
          bool edititms = false;
          bool delitms = false;
          //Payitems
          long pyitm_cur_indx = 0;
          bool is_last_pyitm = false;
          long totl_pyitm = 0;
          long last_pyitm_num = 0;
          public string pyitm_SQL = "";
          bool obey_pyitm_evnts = false;
          //feeditems
          long feed_cur_indx = 0;
          bool is_last_feed = false;
          long totl_feed = 0;
          long last_feed_num = 0;
          public string feed_SQL = "";
          bool obey_feed_evnts = false;
          //Work Hours Panel Variables;
          long wkh_cur_indx = 0;
          bool is_last_wkh = false;
          long totl_wkh = 0;
          long last_wkh_num = 0;
          public string wkh_SQL = "";
          public string wkhDet_SQL = "";
          bool obey_wkh_evnts = false;
          bool addwkh = false;
          bool editwkh = false;
          bool addwkhs = false;
          bool editwkhs = false;
          bool delwkhs = false;
          //Gathering Types Panel Variables;
          long gth_cur_indx = 0;
          bool is_last_gth = false;
          long totl_gth = 0;
          long last_gth_num = 0;
          public string gth_SQL = "";
          public string gthHrchy_SQL = "";
          bool obey_gth_evnts = false;
          bool addgth = false;
          bool editgth = false;
          bool addgths = false;
          bool editgths = false;
          bool delgths = false;
          //Segment Values
          public string segmentValsSQL = "";
          #endregion
  
          #region "FORM EVENTS..."
          public mainForm()
          {
              InitializeComponent();
          }
  
          private void mainForm_Load(object sender, EventArgs e)
          {
              this.accDndLabel.Visible = false;
              Global.myOrgStp.Initialize();
              Global.mnFrm = this;
              //Global.mnFrm.cmCde.pgSqlConn = this.gnrlSQLConn;
              Global.mnFrm.cmCde.Login_number = this.lgn_num;
              Global.mnFrm.cmCde.Role_Set_IDs = this.role_st_id;
              Global.mnFrm.cmCde.User_id = this.usr_id;
              Global.mnFrm.cmCde.Org_id = this.Og_id;
  
              this.hideAllPanels();
              Global.refreshRqrdVrbls();
              System.Windows.Forms.Application.DoEvents();
              Color[] clrs = Global.mnFrm.cmCde.getColors();
              this.BackColor = clrs[0];
              this.glsLabel1.TopFill = clrs[0];
              this.glsLabel1.BackColor = clrs[0];
              this.glsLabel1.BottomFill = clrs[1];
              this.glsLabel2.TopFill = clrs[0];
              this.glsLabel2.BackColor = clrs[0];
              this.glsLabel2.BottomFill = clrs[1];
              this.glsLabel3.TopFill = clrs[0];
              this.glsLabel3.BackColor = clrs[0];
              this.glsLabel3.BottomFill = clrs[1];
              this.glsLabel4.TopFill = clrs[0];
              this.glsLabel4.BackColor = clrs[0];
              this.glsLabel4.BottomFill = clrs[1];
              this.glsLabel5.TopFill = clrs[0];
              this.glsLabel5.BackColor = clrs[0];
              this.glsLabel5.BottomFill = clrs[1];
              this.glsLabel6.TopFill = clrs[0];
              this.glsLabel6.BackColor = clrs[0];
              this.glsLabel6.BottomFill = clrs[1];
              //this.glsLabel12.TopFill = clrs[0];
              //this.glsLabel12.BackColor = clrs[0];
              //this.glsLabel12.BottomFill = clrs[1];
              this.glsLabel13.TopFill = clrs[0];
              this.glsLabel13.BackColor = clrs[0];
              this.glsLabel13.BottomFill = clrs[1];
              this.tabPage1.BackColor = clrs[0];
              this.tabPage2.BackColor = clrs[0];
              this.tabPage3.BackColor = clrs[0];
              this.tabPage4.BackColor = clrs[0];
              this.tabPage5.BackColor = clrs[0];
              this.tabPage6.BackColor = clrs[0];
  
              System.Windows.Forms.Application.DoEvents();
  
              Global.myOrgStp.loadMyRolesNMsgtyps();
              bool vwAct = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[0]);
              if (!vwAct)
              {
                  this.Controls.Clear();
                  this.Controls.Add(this.accDndLabel);
                  this.accDndLabel.Visible = true;
                  return;
              }
              this.disableFormButtons();
  
              this.showAllPanels();
              System.Windows.Forms.Application.DoEvents();
              this.populateTreeView();
              System.Windows.Forms.Application.DoEvents();
              if (this.leftTreeView.Nodes.Count > 0 &&
                Global.currentPanel == "")
              {
                  TreeViewEventArgs ex = new TreeViewEventArgs(this.leftTreeView.Nodes[0], TreeViewAction.ByMouse);
                  this.leftTreeView_AfterSelect(this.leftTreeView, ex);
              }
              if (this.tabControl1.Controls.Count <= 0
                && this.leftTreeView.Nodes.Count > 0)
              {
                  this.loadCorrectPanel(this.leftTreeView.Nodes[0].Text);
              }
          }
  
          private void mainForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
          {
              Global.myOrgStp.Dispose();
          }
          #endregion
  
          #region "GENERAL..."
          private void populateTreeView()
          {
              this.leftTreeView.Nodes.Clear();
              if (!Global.mnFrm.cmCde.isThsMchnPrmtd())
              {
                  Global.mnFrm.cmCde.showMsg("This Machine is not Permitted to run this software!\r\nContact the Vendor for Assistance!", 4);
                  return;
              }
              this.tabControl1.Controls.Clear();
              for (int i = 0; i < menuItems.Length; i++)
              {
                  if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[0] +
                      "~" + Global.dfltPrvldgs[i + 1]) == false)
                  {
                      continue;
                  }
                  TreeNode nwNode = new TreeNode();
                  nwNode.Name = "myNode" + i.ToString();
                  nwNode.Text = menuItems[i];
                  nwNode.ImageKey = menuImages[i];
                  this.leftTreeView.Nodes.Add(nwNode);
              }
          }
  
          private void showATab(ref TabPage my_tab)
          {
              //my_panel.Dock = DockStyle.Fill;
              //System.Windows.Forms.Application.DoEvents();
              //my_panel.Enabled = true;
              //my_panel.Visible = true;
              bool found = false;
              foreach (TabPage tab1 in this.tabControl1.TabPages)
              {
                  if (tab1 == my_tab)
                  {
                      found = true;
                      break;
                  }
              }
              if (!found)
              {
                  this.tabControl1.Controls.Add(my_tab);
              }
              this.tabControl1.SelectedTab = my_tab;
              my_tab.Select();
              my_tab.Show();
              System.Windows.Forms.Application.DoEvents();
          }
  
          private void loadCorrectPanel(string inpt_name)
          {
              Global.currentPanel = inpt_name;
              //this.disableFormButtons(inpt_name);
  
              if (inpt_name == menuItems[0])
              {
                  this.showATab(ref this.tabPage1);
                  this.changeOrg();
                  this.loadOrgDetPanel();
              }
              else
              {
                  if (int.Parse(this.orgIDTextBox.Text) <= 0)
                  {
                      Global.mnFrm.cmCde.showMsg("Please Create an Organisation and Select it First!", 0);
                      return;
                  }
                  if (inpt_name == menuItems[1])
                  {
                      this.showATab(ref this.tabPage2);
                      this.changeOrg();
                      this.loadDivDetPanel();
                  }
                  else if (inpt_name == menuItems[2])
                  {
                      this.showATab(ref this.tabPage3);
                      this.changeOrg();
                      this.loadSitePanel();
                  }
                  else if (inpt_name == menuItems[3])
                  {
                      this.showATab(ref this.tabPage4);
                      this.changeOrg();
                      this.loadJobsPanel();
                  }
                  else if (inpt_name == menuItems[4])
                  {
                      this.showATab(ref this.tabPage5);
                      this.changeOrg();
                      this.loadGradesPanel();
                  }
                  else if (inpt_name == menuItems[5])
                  {
                      this.showATab(ref this.tabPage6);
                      this.changeOrg();
                      this.loadPositionPanel();
                  }
  
              }
          }
  
          private void changeOrg()
          {
              //      if (this.crntOrgIDTextBox.Text == "-1"
              //|| this.crntOrgIDTextBox.Text == "")
              //      {
              //        this.crntOrgIDTextBox.Text = Global.mnFrm.cmCde.Org_id.ToString();
              //        this.crntOrgTextBox.Text = Global.mnFrm.cmCde.getOrgName(Global.mnFrm.cmCde.Org_id);
              //        Global.mnFrm.cmCde.getDBImageFile(this.crntOrgIDTextBox.Text + ".png",
              //            0, ref this.curOrgPictureBox);
  
              //        if (this.crntOrgIDTextBox.Text == "-1"
              //|| this.crntOrgIDTextBox.Text == "")
              //        {
              //          EventArgs e = new EventArgs();
              //          this.crntOrgButton_Click(this.crntOrgButton, e);
              //        }
              //      }
          }
  
          private void hideAllPanels()
          {
              this.orgDetailsPanel.Visible = false;
              this.orgDetailsPanel.Enabled = false;
              System.Windows.Forms.Application.DoEvents();
              this.orgDetailsPanel.Dock = DockStyle.None;
              //this.orgAttrbtsPanel.Visible = false;
              //this.orgAttrbtsPanel.Enabled = false;
              //System.Windows.Forms.Application.DoEvents();
              //this.orgAttrbtsPanel.Dock = DockStyle.None;
              this.divGrpsPanel.Visible = false;
              this.divGrpsPanel.Enabled = false;
              System.Windows.Forms.Application.DoEvents();
              this.divGrpsPanel.Dock = DockStyle.None;
              this.sitesPanel.Visible = false;
              this.sitesPanel.Enabled = false;
              System.Windows.Forms.Application.DoEvents();
              this.sitesPanel.Dock = DockStyle.None;
              this.jobsPanel.Visible = false;
              this.jobsPanel.Enabled = false;
              System.Windows.Forms.Application.DoEvents();
              this.jobsPanel.Dock = DockStyle.None;
              this.gradesPanel.Visible = false;
              this.gradesPanel.Enabled = false;
              System.Windows.Forms.Application.DoEvents();
              this.gradesPanel.Dock = DockStyle.None;
              this.positionsPanel.Visible = false;
              this.positionsPanel.Enabled = false;
              System.Windows.Forms.Application.DoEvents();
              this.positionsPanel.Dock = DockStyle.None;
              System.Windows.Forms.Application.DoEvents();
          }
  
          private void showAllPanels()
          {
              this.orgDetailsPanel.Visible = true;
              this.orgDetailsPanel.Enabled = true;
              System.Windows.Forms.Application.DoEvents();
              this.orgDetailsPanel.Dock = DockStyle.Fill;
              //this.orgAttrbtsPanel.Visible = true;
              //this.orgAttrbtsPanel.Enabled = true;
              //System.Windows.Forms.Application.DoEvents();
              //this.orgAttrbtsPanel.Dock = DockStyle.Fill;
              this.divGrpsPanel.Visible = true;
              this.divGrpsPanel.Enabled = true;
              System.Windows.Forms.Application.DoEvents();
              this.divGrpsPanel.Dock = DockStyle.Fill;
              this.sitesPanel.Visible = true;
              this.sitesPanel.Enabled = true;
              System.Windows.Forms.Application.DoEvents();
              this.sitesPanel.Dock = DockStyle.Fill;
              this.jobsPanel.Visible = true;
              this.jobsPanel.Enabled = true;
              System.Windows.Forms.Application.DoEvents();
              this.jobsPanel.Dock = DockStyle.Fill;
              this.gradesPanel.Visible = true;
              this.gradesPanel.Enabled = true;
              System.Windows.Forms.Application.DoEvents();
              this.gradesPanel.Dock = DockStyle.Fill;
              this.positionsPanel.Visible = true;
              this.positionsPanel.Enabled = true;
              System.Windows.Forms.Application.DoEvents();
              this.positionsPanel.Dock = DockStyle.Fill;
              System.Windows.Forms.Application.DoEvents();
          }
  
          private void showAPanel(ref Panel my_panel)
          {
              my_panel.Dock = DockStyle.Fill;
              System.Windows.Forms.Application.DoEvents();
              my_panel.Enabled = true;
              my_panel.Visible = true;
              System.Windows.Forms.Application.DoEvents();
          }
  
          private void leftTreeView_AfterSelect(object sender, TreeViewEventArgs e)
          {
              //this.hideAllPanels();
              if (e.Node == null)
              {
                  return;
              }
              this.loadCorrectPanel(e.Node.Text);
          }
  
          private void disableFormButtons()
          {
              bool vwSQL = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[12]);
              bool rcHstry = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[13]);
              //Organization's Details
              this.saveOrgDetButton.Enabled = false;
              this.addOrgs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[14]);
              this.addOrgDetButton.Enabled = this.addOrgs;
              this.imprtOrgTmpltButton.Enabled = this.addOrgs;
  
              this.editOrgs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[15]);
              this.editOrgDetButton.Enabled = this.editOrgs;
              this.changeLogoButton.Enabled = this.editOrgs;
  
              //this.delOrgs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[16]);
              //this.delOrgDetButton.Enabled = this.delOrgs;
  
              this.vwSQLOrgDetButton.Enabled = vwSQL;
              this.recHstryOrgDetButton.Enabled = rcHstry;
              //Divisions/Groups
              this.saveDivButton.Enabled = false;
              this.addDivs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[16]);
              this.addDivButton.Enabled = this.addDivs;
              this.addDivMenuItem.Enabled = this.addDivs;
              this.imprtDivButton.Enabled = this.addDivs;
  
              this.editDivs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[17]);
              this.editDivButton.Enabled = this.editDivs;
              this.editDivMenuItem.Enabled = this.editDivs;
              this.changeDivLogoButton.Enabled = this.editDivs;
  
              this.delDivs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[18]);
              this.delDivButton.Enabled = this.delDivs;
              this.delDivMenuItem.Enabled = this.delDivs;
  
              this.vwSQLDivButton.Enabled = vwSQL;
              this.rcHstryDivMenuItem.Enabled = rcHstry;
              this.vwSQLDivMenuItem.Enabled = vwSQL;
              this.recHstryDivButton.Enabled = rcHstry;
              //Sites/Locations
              this.saveSiteButton.Enabled = false;
              this.addSites = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[19]);
              this.addSiteButton.Enabled = this.addSites;
              this.addSiteMenuItem.Enabled = this.addSites;
              this.imprtSiteButton.Enabled = this.addSites;
  
              this.editSites = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[20]);
              this.editSiteButton.Enabled = this.editSites;
              this.editSiteMenuItem.Enabled = this.editSites;
  
              this.delSites = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[21]);
              this.delSiteButton.Enabled = this.delSites;
              this.delSiteMenuItem.Enabled = this.delSites;
  
              this.vwSQLSiteButton.Enabled = vwSQL;
              this.rcHstrySiteMenuItem.Enabled = rcHstry;
              this.vwSQLSiteMenuItem.Enabled = vwSQL;
              this.recHstrySiteButton.Enabled = rcHstry;
              //Jobs
              this.saveJobsButton.Enabled = false;
              this.addJobs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[22]);
              this.addJobsButton.Enabled = this.addJobs;
              this.addJobMenuItem.Enabled = this.addJobs;
              this.imprtJobsButton.Enabled = this.addJobs;
  
              this.editJobs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[23]);
              this.editJobsButton.Enabled = this.editJobs;
              this.editJobMenuItem.Enabled = this.editJobs;
  
              this.delJobs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[24]);
              this.delJobButton.Enabled = this.delJobs;
              this.delJobMenuItem.Enabled = this.delJobs;
  
              this.vwSQLJobsButton.Enabled = vwSQL;
              this.rcHstryJobMenuItem.Enabled = rcHstry;
              this.vwSQLJobMenuItem.Enabled = vwSQL;
              this.recHstryJobButton.Enabled = rcHstry;
              //Grades
              this.saveGrdButton.Enabled = false;
              this.addgrds = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[25]);
              this.addGrdButton.Enabled = this.addgrds;
              this.addGradesMenuItem.Enabled = this.addgrds;
              this.imprtGradesButton.Enabled = this.addgrds;
  
              this.editgrds = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[26]);
              this.editGrdButton.Enabled = this.editgrds;
              this.editGradesMenuItem.Enabled = this.editgrds;
  
              this.delgrds = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[27]);
              this.delGrdButton.Enabled = this.delgrds;
              this.delGradesMenuItem.Enabled = this.delgrds;
  
              this.vwSQLGrdButton.Enabled = vwSQL;
              this.rcHstryGradesMenuItem.Enabled = rcHstry;
              this.vwSQLGradesMenuItem.Enabled = vwSQL;
              this.rcHstryGrdButton.Enabled = rcHstry;
  
              //Positions
              this.savePosButton.Enabled = false;
              this.addposs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[28]);
              this.addPosButton.Enabled = this.addposs;
              this.addPosMenuItem.Enabled = this.addposs;
              this.imprtPosButton.Enabled = this.addposs;
  
              this.editposs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[29]);
              this.editPosButton.Enabled = this.editposs;
              this.editPosMenuItem.Enabled = this.editposs;
  
              this.delposs = Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[30]);
              this.deletePosButton.Enabled = this.delposs;
              this.delPosMenuItem.Enabled = this.delposs;
  
              this.vwSQLPosButton.Enabled = vwSQL;
              this.rcHstryPosMenuItem.Enabled = rcHstry;
              this.vwSQLPosMenuItem.Enabled = vwSQL;
              this.recHstryPosButton.Enabled = rcHstry;
  
          }
          #endregion
  
          #region "Organization Details..."
          private void loadOrgDetPanel()
          {
              this.obey_orgDet_evnts = false;
              if (this.searchInOrgDetComboBox.SelectedIndex < 0)
              {
                  this.searchInOrgDetComboBox.SelectedIndex = 0;
              }
              if (this.searchForOrgDetTextBox.Text.Contains("%") == false)
              {
                  this.searchForOrgDetTextBox.Text = "%" + this.searchForOrgDetTextBox.Text.Replace(" ", "%") + "%";
              }
              if (this.searchForOrgDetTextBox.Text == "%%")
              {
                  this.searchForOrgDetTextBox.Text = "%";
              }
              this.orgLogoPictureBox.Image = OrganizationSetup.Properties.Resources.blank;
              int dsply = 0;
              if (this.dsplySizeOrgDetComboBox.Text == ""
                  || int.TryParse(this.dsplySizeOrgDetComboBox.Text, out dsply) == false)
              {
                  this.dsplySizeOrgDetComboBox.Text = Global.mnFrm.cmCde.get_CurPlcy_Mx_Dsply_Recs().ToString();
              }
              //this.groupBox4.Height = this.orgDetailsPanel.Bottom - this.toolStrip3.Bottom - 65;
              this.is_last_orgDet = false;
              this.totl_orgDet = Global.mnFrm.cmCde.Big_Val;
              this.getOrgDetPnlData();
              this.populateOrgDetTreeView();
              this.obey_orgDet_evnts = true;
          }
  
          private void getOrgDetPnlData()
          {
              this.updtOrgDetTotals();
              this.populateOrgDet();
              this.updtOrgDetNavLabels();
          }
  
          private void updtOrgDetTotals()
          {
              Global.mnFrm.cmCde.navFuncts.FindNavigationIndices(1, this.totl_orgDet);
              if (this.orgDet_cur_indx >= Global.mnFrm.cmCde.navFuncts.totalGroups)
              {
                  this.orgDet_cur_indx = Global.mnFrm.cmCde.navFuncts.totalGroups - 1;
              }
  
              if (this.orgDet_cur_indx < 0)
              {
                  this.orgDet_cur_indx = 0;
              }
              Global.mnFrm.cmCde.navFuncts.currentNavigationIndex = this.orgDet_cur_indx;
          }
  
          private void updtOrgDetNavLabels()
          {
              this.moveFirstOrgDetButton.Enabled = Global.mnFrm.cmCde.navFuncts.moveFirstBtnStatus();
              this.movePreviousOrgDetButton.Enabled = Global.mnFrm.cmCde.navFuncts.movePrevBtnStatus();
              this.moveNextOrgDetButton.Enabled = Global.mnFrm.cmCde.navFuncts.moveNextBtnStatus();
              this.moveLastOrgDetButton.Enabled = Global.mnFrm.cmCde.navFuncts.moveLastBtnStatus();
              this.positionOrgDetTextBox.Text = Global.mnFrm.cmCde.navFuncts.displayedRecordsNumbers();
              if (this.is_last_orgDet == true ||
                  this.totl_orgDet != Global.mnFrm.cmCde.Big_Val)
              {
                  this.totalRecOrgDetLabel.Text = Global.mnFrm.cmCde.navFuncts.totalRecordsLabel();
              }
              else
              {
                  this.totalRecOrgDetLabel.Text = "of Total";
              }
          }
  
          private void populateOrgDetTreeView()
          {
              this.obey_orgDet_evnts = false;
              DataSet dtst = Global.get_Hrchy_OrgDet(this.searchForOrgDetTextBox.Text,
                  this.searchInOrgDetComboBox.Text, 0, int.Parse(this.dsplySizeOrgDetComboBox.Text));
              this.orgDetTreeView.Nodes.Clear();
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[1]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                      " this action!\nContact your System Administrator!", 0);
                  this.obey_orgDet_evnts = true;
                  return;
              }
              TreeNode[] nwNode = new TreeNode[dtst.Tables[0].Rows.Count];
  
              for (int i = 0; i < dtst.Tables[0].Rows.Count; i++)
              {
                  TreeNode aNode = new TreeNode();
                  aNode.Name = "orgNode" + int.Parse(dtst.Tables[0].Rows[i][0].ToString()).ToString();
                  aNode.Text = dtst.Tables[0].Rows[i][2].ToString();
                  //aNode.ImageKey = menuImages[i];
                  nwNode[i] = aNode;
                  if (int.Parse(dtst.Tables[0].Rows[i][3].ToString()) == 1)
                  {
                      this.orgDetTreeView.Nodes.Add(nwNode[i]);
                  }
                  else
                  {
                      try
                      {
                          string prntNodeNm = "orgNode" + int.Parse(dtst.Tables[0].Rows[i][1].ToString()).ToString();
                          this.getNode(nwNode, prntNodeNm).Nodes.Add(nwNode[i]);
                      }
                      catch (Exception ex)
                      {
                      }
                  }
                  this.orgDetTreeView.ExpandAll();
              }
              this.obey_orgDet_evnts = true;
          }
  
          private TreeNode getNode(TreeNode[] ndeList, string ndeName)
          {
              for (int i = 0; i < ndeList.Length; i++)
              {
                  if (ndeList[i].Name == ndeName)
                  {
                      return ndeList[i];
                  }
              }
              return null;
          }
  
          private void populateOrgDet(int orgID)
          {
              this.clearOrgDetInfo();
              this.disableOrgEdit();
              this.disableLnsEdit();
              this.obey_orgDet_evnts = false;
              DataSet dtst = Global.get_One_OrgDet(orgID);
              for (int i = 0; i < dtst.Tables[0].Rows.Count; i++)
              {
                  this.orgIDTextBox.Text = dtst.Tables[0].Rows[i][0].ToString();
                  this.orgNameTextBox.Text = dtst.Tables[0].Rows[i][1].ToString();
                  this.orgPrntIDTextBox.Text = dtst.Tables[0].Rows[i][2].ToString();
                  this.orgParentTextBox.Text = dtst.Tables[0].Rows[i][3].ToString();
                  this.resAddrsTextBox.Text = dtst.Tables[0].Rows[i][4].ToString();
                  this.websiteTextBox.Text = dtst.Tables[0].Rows[i][7].ToString();
                  this.crncyIDTextBox.Text = dtst.Tables[0].Rows[i][13].ToString();
                  this.crncyCodeTextBox.Text = dtst.Tables[0].Rows[i][14].ToString();
                  this.postalAddrsTextBox.Text = dtst.Tables[0].Rows[i][5].ToString();
                  this.emailAddrsTextBox.Text = dtst.Tables[0].Rows[i][6].ToString();
                  this.contactNosTextBox.Text = dtst.Tables[0].Rows[i][8].ToString();
                  this.sloganTextBox.Text = dtst.Tables[0].Rows[i][16].ToString();
                  this.orgTypIDTextBox.Text = dtst.Tables[0].Rows[i][9].ToString();
                  this.orgTypTextBox.Text = dtst.Tables[0].Rows[i][10].ToString();
                  this.orgEnabledCheckBox.Checked = Global.mnFrm.cmCde.cnvrtBitStrToBool(dtst.Tables[0].Rows[i][12].ToString());
                  this.orgDescTextBox.Text = dtst.Tables[0].Rows[i][15].ToString();
                  this.noOfSgmntsNumUpDown.Value = decimal.Parse(dtst.Tables[0].Rows[i][17].ToString());
                  this.delimiterComboBox.SelectedItem = dtst.Tables[0].Rows[i][18].ToString();
                  this.locSgmtNoNumericUpDown.Value = decimal.Parse(dtst.Tables[0].Rows[i][19].ToString());
                  this.subLocSgmtNoNumericUpDown.Value = decimal.Parse(dtst.Tables[0].Rows[i][20].ToString());
                  Global.mnFrm.cmCde.getDBImageFile(dtst.Tables[0].Rows[i][11].ToString(), 0, ref this.orgLogoPictureBox);
              }
              this.obey_orgDet_evnts = true;
          }
  
          private void populateOrgDet()
          {
              this.clearOrgDetInfo();
              this.disableOrgEdit();
              this.disableLnsEdit();
              this.obey_orgDet_evnts = false;
              DataSet dtst = Global.get_Basic_OrgDet(this.searchForOrgDetTextBox.Text,
                  this.searchInOrgDetComboBox.Text, this.orgDet_cur_indx, 1);
              for (int i = 0; i < dtst.Tables[0].Rows.Count; i++)
              {
                  this.last_org_num = Global.mnFrm.cmCde.navFuncts.startIndex() + i;
                  this.orgIDTextBox.Text = dtst.Tables[0].Rows[i][0].ToString();
                  this.orgNameTextBox.Text = dtst.Tables[0].Rows[i][1].ToString();
                  this.orgPrntIDTextBox.Text = dtst.Tables[0].Rows[i][2].ToString();
                  this.orgParentTextBox.Text = dtst.Tables[0].Rows[i][3].ToString();
                  this.resAddrsTextBox.Text = dtst.Tables[0].Rows[i][4].ToString();
                  this.websiteTextBox.Text = dtst.Tables[0].Rows[i][7].ToString();
                  this.crncyIDTextBox.Text = dtst.Tables[0].Rows[i][13].ToString();
                  this.crncyCodeTextBox.Text = dtst.Tables[0].Rows[i][14].ToString();
                  this.postalAddrsTextBox.Text = dtst.Tables[0].Rows[i][5].ToString();
                  this.emailAddrsTextBox.Text = dtst.Tables[0].Rows[i][6].ToString();
                  this.contactNosTextBox.Text = dtst.Tables[0].Rows[i][8].ToString();
                  this.orgTypIDTextBox.Text = dtst.Tables[0].Rows[i][9].ToString();
                  this.orgTypTextBox.Text = dtst.Tables[0].Rows[i][10].ToString();
                  this.sloganTextBox.Text = dtst.Tables[0].Rows[i][16].ToString();
                  this.orgDescTextBox.Text = dtst.Tables[0].Rows[i][15].ToString();
                  this.noOfSgmntsNumUpDown.Value = decimal.Parse(dtst.Tables[0].Rows[i][17].ToString());
                  this.delimiterComboBox.SelectedItem = dtst.Tables[0].Rows[i][18].ToString();
                  this.locSgmtNoNumericUpDown.Value = decimal.Parse(dtst.Tables[0].Rows[i][19].ToString());
                  this.subLocSgmtNoNumericUpDown.Value = decimal.Parse(dtst.Tables[0].Rows[i][20].ToString());
                  this.orgEnabledCheckBox.Checked = Global.mnFrm.cmCde.cnvrtBitStrToBool(dtst.Tables[0].Rows[i][12].ToString());
                  Global.mnFrm.cmCde.getDBImageFile(dtst.Tables[0].Rows[i][11].ToString(), 0, ref this.orgLogoPictureBox);
              }
              this.correctOrgNavLbls(dtst);
              this.obey_orgDet_evnts = true;
              this.populateSegments();
          }
  
          private void correctOrgNavLbls(DataSet dtst)
          {
              long totlRecs = dtst.Tables[0].Rows.Count;
              if (this.orgDet_cur_indx == 0 && totlRecs == 0)
              {
                  this.is_last_orgDet = true;
                  this.totl_orgDet = 0;
                  this.last_org_num = 0;
                  this.orgDet_cur_indx = 0;
                  this.updtOrgDetTotals();
                  this.updtOrgDetNavLabels();
              }
              else if (this.totl_orgDet == Global.mnFrm.cmCde.Big_Val
        && totlRecs < 1)
              {
                  this.totl_orgDet = this.last_org_num;
                  if (totlRecs == 0)
                  {
                      this.orgDet_cur_indx -= 1;
                      this.updtOrgDetTotals();
                      this.populateOrgDet();
                  }
                  else
                  {
                      this.updtOrgDetTotals();
                  }
              }
          }
  
          private void clearOrgDetInfo()
          {
              this.obey_orgDet_evnts = false;
              this.saveOrgDetButton.Enabled = false;
              this.addOrgDetButton.Enabled = this.addOrgs;
              this.editOrgDetButton.Enabled = this.editOrgs;
              this.orgIDTextBox.Text = "-1";
              this.orgNameTextBox.Text = "";
              this.orgPrntIDTextBox.Text = "-1";
              this.orgParentTextBox.Text = "";
              this.resAddrsTextBox.Text = "";
              this.websiteTextBox.Text = "";
              this.crncyIDTextBox.Text = "-1";
              this.crncyCodeTextBox.Text = "";
              this.postalAddrsTextBox.Text = "";
              this.emailAddrsTextBox.Text = "";
              this.contactNosTextBox.Text = "";
              this.orgTypIDTextBox.Text = "-1";
              this.orgTypTextBox.Text = "";
              this.orgDescTextBox.Text = "";
              this.sloganTextBox.Text = "";
              this.noOfSgmntsNumUpDown.Value = 1;
              this.locSgmtNoNumericUpDown.Value = 0;
              this.subLocSgmtNoNumericUpDown.Value = 0;
              this.delimiterComboBox.SelectedIndex = 1;
              this.orgEnabledCheckBox.Checked = false;
              this.orgLogoPictureBox.Image = OrganizationSetup.Properties.Resources.blank;
              this.obey_orgDet_evnts = true;
          }
  
          private void prpareForOrgEdit()
          {
              this.saveOrgDetButton.Enabled = true;
              this.orgNameTextBox.ReadOnly = false;
              this.orgNameTextBox.BackColor = Color.FromArgb(255, 255, 118);
              this.orgTypTextBox.ReadOnly = false;
              this.orgTypTextBox.BackColor = Color.FromArgb(255, 255, 118);
              this.crncyCodeTextBox.ReadOnly = false;
              this.crncyCodeTextBox.BackColor = Color.FromArgb(255, 255, 118);
              this.resAddrsTextBox.ReadOnly = false;
              this.resAddrsTextBox.BackColor = Color.White;
              this.websiteTextBox.ReadOnly = false;
              this.websiteTextBox.BackColor = Color.White;
              this.postalAddrsTextBox.ReadOnly = false;
              this.postalAddrsTextBox.BackColor = Color.White;
              this.emailAddrsTextBox.ReadOnly = false;
              this.emailAddrsTextBox.BackColor = Color.White;
              this.contactNosTextBox.ReadOnly = false;
              this.contactNosTextBox.BackColor = Color.White;
              this.orgDescTextBox.ReadOnly = false;
              this.orgDescTextBox.BackColor = Color.White;
              this.sloganTextBox.ReadOnly = false;
              this.sloganTextBox.BackColor = Color.White; 
              this.noOfSgmntsNumUpDown.ReadOnly = false;
              this.noOfSgmntsNumUpDown.BackColor = Color.FromArgb(255, 255, 118);
              this.noOfSgmntsNumUpDown.Increment = 1;
              this.locSgmtNoNumericUpDown.ReadOnly = false;
              this.locSgmtNoNumericUpDown.BackColor = Color.FromArgb(255, 255, 118);
              this.locSgmtNoNumericUpDown.Increment = 1;
              this.subLocSgmtNoNumericUpDown.ReadOnly = false;
              this.subLocSgmtNoNumericUpDown.BackColor = Color.FromArgb(255, 255, 118);
              this.subLocSgmtNoNumericUpDown.Increment = 1;
              this.delimiterComboBox.BackColor = Color.FromArgb(255, 255, 118);
          }
  
          private void disableOrgEdit()
          {
              this.addOrg = false;
              this.editOrg = false;
              this.orgNameTextBox.ReadOnly = true;
              this.orgNameTextBox.BackColor = Color.WhiteSmoke;
              this.orgTypTextBox.ReadOnly = true;
              this.orgTypTextBox.BackColor = Color.WhiteSmoke;
              this.crncyCodeTextBox.ReadOnly = true;
              this.crncyCodeTextBox.BackColor = Color.WhiteSmoke;
              this.resAddrsTextBox.ReadOnly = true;
              this.resAddrsTextBox.BackColor = Color.WhiteSmoke;
              this.websiteTextBox.ReadOnly = true;
              this.websiteTextBox.BackColor = Color.WhiteSmoke;
              this.postalAddrsTextBox.ReadOnly = true;
              this.postalAddrsTextBox.BackColor = Color.WhiteSmoke;
              this.emailAddrsTextBox.ReadOnly = true;
              this.emailAddrsTextBox.BackColor = Color.WhiteSmoke;
              this.contactNosTextBox.ReadOnly = true;
              this.contactNosTextBox.BackColor = Color.WhiteSmoke;
              this.orgIDTextBox.ReadOnly = true;
              this.orgIDTextBox.BackColor = Color.WhiteSmoke;
              this.orgPrntIDTextBox.ReadOnly = true;
              this.orgPrntIDTextBox.BackColor = Color.WhiteSmoke;
              this.orgParentTextBox.ReadOnly = true;
              this.orgParentTextBox.BackColor = Color.WhiteSmoke;
              this.crncyIDTextBox.ReadOnly = true;
              this.crncyIDTextBox.BackColor = Color.WhiteSmoke;
              this.crncyCodeTextBox.ReadOnly = true;
              this.crncyCodeTextBox.BackColor = Color.WhiteSmoke;
              this.orgTypIDTextBox.ReadOnly = true;
              this.orgTypIDTextBox.BackColor = Color.WhiteSmoke;
              this.orgTypTextBox.ReadOnly = true;
              this.orgTypTextBox.BackColor = Color.WhiteSmoke;
              this.orgDescTextBox.ReadOnly = true;
              this.orgDescTextBox.BackColor = Color.WhiteSmoke;
              this.sloganTextBox.ReadOnly = true;
              this.sloganTextBox.BackColor = Color.WhiteSmoke;
              this.noOfSgmntsNumUpDown.ReadOnly = true;
              this.noOfSgmntsNumUpDown.BackColor = Color.WhiteSmoke;
              this.noOfSgmntsNumUpDown.Increment = 0;
              this.locSgmtNoNumericUpDown.ReadOnly = true;
              this.locSgmtNoNumericUpDown.BackColor = Color.WhiteSmoke;
              this.locSgmtNoNumericUpDown.Increment = 0;
              this.subLocSgmtNoNumericUpDown.ReadOnly = true;
              this.subLocSgmtNoNumericUpDown.BackColor = Color.WhiteSmoke;
              this.subLocSgmtNoNumericUpDown.Increment = 0;
              this.delimiterComboBox.BackColor = Color.WhiteSmoke;
          }
  
          private bool shdObeyOrgDetEvts()
          {
              return this.obey_orgDet_evnts;
          }
  
          private void OrgDetPnlNavButtons(object sender, System.EventArgs e)
          {
              System.Windows.Forms.ToolStripButton sentObj = (System.Windows.Forms.ToolStripButton)sender;
              this.totalRecOrgDetLabel.Text = "";
              if (sentObj.Name.ToLower().Contains("first"))
              {
                  this.is_last_orgDet = false;
                  this.orgDet_cur_indx = 0;
              }
              else if (sentObj.Name.ToLower().Contains("previous"))
              {
                  this.is_last_orgDet = false;
                  this.orgDet_cur_indx -= 1;
              }
              else if (sentObj.Name.ToLower().Contains("next"))
              {
                  this.is_last_orgDet = false;
                  this.orgDet_cur_indx += 1;
              }
              else if (sentObj.Name.ToLower().Contains("last"))
              {
                  this.is_last_orgDet = true;
                  this.totl_orgDet = Global.get_Total_OrgDet(this.searchForOrgDetTextBox.Text,
                      this.searchInOrgDetComboBox.Text);
                  this.updtOrgDetTotals();
                  this.orgDet_cur_indx = Global.mnFrm.cmCde.navFuncts.totalGroups - 1;
              }
              this.getOrgDetPnlData();
          }
  
          private void searchForOrgDetTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
          {
              if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
              {
                  this.loadOrgDetPanel();
              }
          }
  
          private void refreshOrgDetButton_Click(object sender, EventArgs e)
          {
              this.loadOrgDetPanel();
              this.Refresh();
          }
  
          private void extraInfoButton_Click(object sender, EventArgs e)
          {
              if (this.orgIDTextBox.Text == "" || this.orgIDTextBox.Text == "-1")
              {
                  Global.mnFrm.cmCde.showMsg("No record to View!", 0);
                  return;
              }
              DialogResult dgres = this.cmCde.showRowsExtInfDiag(this.cmCde.getMdlGrpID("Organization's Details"),
                  long.Parse(this.orgIDTextBox.Text), "org.org_all_other_info_table", this.orgNameTextBox.Text, this.editOrgs, 12, 13,
                  "org.org_all_other_info_table_dflt_row_id_seq");
              if (dgres == DialogResult.OK)
              {
              }
          }
  
          private void changeLogoButton_Click(object sender, EventArgs e)
          {
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[15]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                      " this action!\nContact your System Administrator!", 0);
                  return;
              }
              if (this.orgIDTextBox.Text == "" || this.orgIDTextBox.Text == "-1")
              {
                  Global.mnFrm.cmCde.showMsg("Can only change the logo of a saved organization", 0);
                  return;
              }
              this.orgLogoPictureBox.Image.Dispose();
              if (Global.mnFrm.cmCde.pickAnImage(int.Parse(this.orgIDTextBox.Text),
                  ref this.orgLogoPictureBox, 0) == true)
              {
                  Global.updtOrgImg(int.Parse(this.orgIDTextBox.Text));
              }
              this.populateOrgDet(int.Parse(this.orgIDTextBox.Text));
          }
  
          private void addOrgDetButton_Click(object sender, EventArgs e)
          {
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[14]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                      " this action!\nContact your System Administrator!", 0);
                  return;
              }
              this.clearOrgDetInfo();
              this.addOrg = true;
              this.editOrg = false;
              this.prpareForOrgEdit();
              this.prpareForLnsEdit();
              this.addOrgDetButton.Enabled = false;
              this.editOrgDetButton.Enabled = false;
          }
  
          private void editOrgDetButton_Click(object sender, EventArgs e)
          {
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[15]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                      " this action!\nContact your System Administrator!", 0);
                  return;
              }
              if (this.orgIDTextBox.Text == "" || this.orgIDTextBox.Text == "-1")
              {
                  Global.mnFrm.cmCde.showMsg("No record to Edit!", 0);
                  return;
              }
              this.addOrg = false;
              this.editOrg = true;
              this.prpareForOrgEdit();
              this.prpareForLnsEdit();
              this.addOrgDetButton.Enabled = false;
              this.editOrgDetButton.Enabled = false;
          }
  
          private void saveOrgDetButton_Click(object sender, EventArgs e)
          {
              if (this.addOrg == true)
              {
                  if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[14]) == false)
                  {
                      Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                       " this action!\nContact your System Administrator!", 0);
                      return;
                  }
              }
              else
              {
                  if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[15]) == false)
                  {
                      Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                       " this action!\nContact your System Administrator!", 0);
                      return;
                  }
              }
              if (this.orgNameTextBox.Text == "")
              {
                  Global.mnFrm.cmCde.showMsg("Please enter an Organization name!", 0);
                  return;
              }
              if (this.orgTypTextBox.Text == "")
              {
                  Global.mnFrm.cmCde.showMsg("Please enter an Organization Type!", 0);
                  return;
              }
              if (this.crncyCodeTextBox.Text == "")
              {
                  Global.mnFrm.cmCde.showMsg("Please enter an Operational Currency!", 0);
                  return;
              }
              int oldOrgID = Global.mnFrm.cmCde.getOrgID(this.orgNameTextBox.Text);
              if (oldOrgID > 0
               && this.addOrg == true)
              {
                  Global.mnFrm.cmCde.showMsg("Organisation Name is already in use!", 0);
                  return;
              }
              if (oldOrgID > 0
               && this.editOrg == true
               && oldOrgID.ToString() != this.orgIDTextBox.Text)
              {
                  Global.mnFrm.cmCde.showMsg("New Organisation Name is already in use!", 0);
                  return;
              }
  
              if (this.addOrg == true)
              {
                  Global.createOrg(this.orgNameTextBox.Text, int.Parse(this.orgPrntIDTextBox.Text),
                      this.resAddrsTextBox.Text, this.postalAddrsTextBox.Text,
                      this.websiteTextBox.Text, int.Parse(this.crncyIDTextBox.Text),
                      this.emailAddrsTextBox.Text, this.contactNosTextBox.Text,
                      int.Parse(this.orgTypIDTextBox.Text), this.orgEnabledCheckBox.Checked,
                      this.orgDescTextBox.Text, this.sloganTextBox.Text, (int)this.noOfSgmntsNumUpDown.Value, 
                      this.delimiterComboBox.Text, (int)this.locSgmtNoNumericUpDown.Value, (int)this.subLocSgmtNoNumericUpDown.Value);
                  this.saveOrgDetButton.Enabled = false;
                  this.addOrg = false;
                  this.editOrg = false;
                  this.editOrgDetButton.Enabled = this.editOrgs;
                  this.addOrgDetButton.Enabled = this.addOrgs;
                  System.Windows.Forms.Application.DoEvents();
                  oldOrgID = Global.mnFrm.cmCde.getOrgID(this.orgNameTextBox.Text);
                  this.saveGridView(oldOrgID);
                  this.loadOrgDetPanel();
                  Global.updtOrgAccntCurrID(oldOrgID, Global.mnFrm.cmCde.getOrgFuncCurID(oldOrgID));
                  long rowid = Global.mnFrm.cmCde.getGnrlRecID("scm.scm_dflt_accnts", "rho_name",
          "row_id", "Default Accounts", oldOrgID);
                  if (rowid <= 0)
                  {
                      Global.createDfltAcnts(oldOrgID);
                  }
              }
              else if (this.editOrg == true)
              {
                  Global.updateOrgDet(int.Parse(this.orgIDTextBox.Text), this.orgNameTextBox.Text, int.Parse(this.orgPrntIDTextBox.Text),
                      this.resAddrsTextBox.Text, this.postalAddrsTextBox.Text,
                      this.websiteTextBox.Text, int.Parse(this.crncyIDTextBox.Text),
                      this.emailAddrsTextBox.Text, this.contactNosTextBox.Text,
                      int.Parse(this.orgTypIDTextBox.Text), this.orgEnabledCheckBox.Checked,
                      this.orgDescTextBox.Text, this.sloganTextBox.Text, (int)this.noOfSgmntsNumUpDown.Value, 
                      this.delimiterComboBox.Text, (int)this.locSgmtNoNumericUpDown.Value, (int)this.subLocSgmtNoNumericUpDown.Value);
                  this.saveGridView(int.Parse(this.orgIDTextBox.Text));
                  this.saveOrgDetButton.Enabled = false;
                  this.editOrg = false;
                  this.editOrgDetButton.Enabled = this.editOrgs;
                  this.addOrgDetButton.Enabled = this.addOrgs;
                  this.loadOrgDetPanel();
                  oldOrgID = int.Parse(this.orgIDTextBox.Text);
                  Global.updtOrgAccntCurrID(oldOrgID, Global.mnFrm.cmCde.getOrgFuncCurID(oldOrgID));
                  long rowid = Global.mnFrm.cmCde.getGnrlRecID("scm.scm_dflt_accnts", "rho_name",
          "row_id", "Default Accounts", oldOrgID);
                  if (rowid <= 0)
                  {
                      Global.createDfltAcnts(oldOrgID);
                  }
              }
          }
  
          private bool checkDtRqrmnts(int rwIdx)
          {
              if (this.accntSgmntsDataGridView.Rows[rwIdx].Cells[0].Value == null)
              {
                  this.accntSgmntsDataGridView.Rows[rwIdx].Cells[0].Value = "-1";
                  return false;
              }
              if (this.accntSgmntsDataGridView.Rows[rwIdx].Cells[1].Value == null)
              {
                  return false;
              }
              if (this.accntSgmntsDataGridView.Rows[rwIdx].Cells[1].Value.ToString() == "")
              {
                  return false;
              }
  
              if (this.accntSgmntsDataGridView.Rows[rwIdx].Cells[3].Value == null)
              {
                  return false;
              }
              if (this.accntSgmntsDataGridView.Rows[rwIdx].Cells[3].Value.ToString() == "")
              {
                  return false;
              }
  
              return true;
          }
  
          private void saveGridView(long hdrID)
          {
              int svd = 0;
              if (this.accntSgmntsDataGridView.Rows.Count > 0)
              {
                  this.accntSgmntsDataGridView.EndEdit();
                  System.Windows.Forms.Application.DoEvents();
              }
  
              for (int i = 0; i < this.accntSgmntsDataGridView.Rows.Count; i++)
              {
                  if (!this.checkDtRqrmnts(i))
                  {
                      this.accntSgmntsDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(255, 100, 100);
                      continue;
                  }
                  else
                  {
                      //Check if Doc Ln Rec Exists
                      //Create if not else update
                      long segmentID = long.Parse(this.accntSgmntsDataGridView.Rows[i].Cells[5].Value.ToString());
                      int segmentNum = int.Parse(this.accntSgmntsDataGridView.Rows[i].Cells[0].Value.ToString());
                      string segmentName = this.accntSgmntsDataGridView.Rows[i].Cells[1].Value.ToString();
                      string sysClsfctn = this.accntSgmntsDataGridView.Rows[i].Cells[3].Value.ToString();
                      int prntSegmentNum = int.Parse(this.accntSgmntsDataGridView.Rows[i].Cells[6].Value.ToString());
                      if (segmentID <= 0)
                      {
                          segmentID = Global.get_SegmnetID(hdrID, segmentNum);
                      }
                      if (segmentID <= 0)
                      {
                          Global.createAcntSegment(hdrID, segmentNum, segmentName, sysClsfctn, prntSegmentNum);
                          svd++;
                          this.accntSgmntsDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Lime;
                      }
                      else
                      {
                          Global.updtAcntSegment(segmentID, segmentNum, segmentName, sysClsfctn, prntSegmentNum);
                          svd++;
                          this.accntSgmntsDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Lime;
                      }
                  }
              }
              Global.mnFrm.cmCde.showMsg(svd + " Line(s) Saved Successfully!", 3);
          }
  
          private void vwSQLOrgDetButton_Click(object sender, EventArgs e)
          {
              Global.mnFrm.cmCde.showSQL(this.orgDet_SQL, 12);
          }
  
          private void recHstryOrgDetButton_Click(object sender, EventArgs e)
          {
              if (this.orgIDTextBox.Text == "-1"
        || this.orgIDTextBox.Text == "")
              {
                  Global.mnFrm.cmCde.showMsg("Please select a Record First!", 0);
                  return;
              }
              Global.mnFrm.cmCde.showRecHstry(Global.get_Org_Rec_Hstry(int.Parse(this.orgIDTextBox.Text)), 13);
          }
  
          private void saveLogoButton_Click(object sender, EventArgs e)
          {
              Global.mnFrm.cmCde.saveImageToFile(ref this.orgLogoPictureBox);
          }
  
          private void selPrntOrgButton_Click(object sender, EventArgs e)
          {
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[15]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                   " this action!\nContact your System Administrator!", 0);
                  return;
              }
              string[] selVals = new string[1];
              selVals[0] = this.orgPrntIDTextBox.Text;
              DialogResult dgRes = Global.mnFrm.cmCde.showPssblValDiag(
                  Global.mnFrm.cmCde.getLovID("Organisations"), ref selVals, true, false,
               this.srchWrd, "Both", false);
              if (dgRes == DialogResult.OK)
              {
                  for (int i = 0; i < selVals.Length; i++)
                  {
                      this.orgPrntIDTextBox.Text = selVals[i];
                      this.orgParentTextBox.Text = Global.mnFrm.cmCde.getOrgName(int.Parse(selVals[i]));
                  }
              }
              if (int.Parse(this.orgIDTextBox.Text) > 0)
              {
                  Global.updtOrgPrntID(int.Parse(this.orgIDTextBox.Text), int.Parse(this.orgPrntIDTextBox.Text));
              }
          }
  
          private void orgTypButton_Click(object sender, EventArgs e)
          {
              //Organisation Types
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[15]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                   " this action!\nContact your System Administrator!", 0);
                  return;
              }
              int[] selValIDs = new int[1];
              selValIDs[0] = int.Parse(this.orgTypIDTextBox.Text);
              DialogResult dgRes = Global.mnFrm.cmCde.showPssblValDiag(
                  Global.mnFrm.cmCde.getLovID("Organisation Types"), ref selValIDs, true, true,
               this.srchWrd, "Both", true);
              if (dgRes == DialogResult.OK)
              {
                  for (int i = 0; i < selValIDs.Length; i++)
                  {
                      this.orgTypIDTextBox.Text = selValIDs[i].ToString();
                      this.orgTypTextBox.Text = Global.mnFrm.cmCde.getPssblValNm(selValIDs[i]);
                  }
              }
              if (int.Parse(this.orgIDTextBox.Text) > 0)
              {
                  Global.updtOrgTypID(int.Parse(this.orgIDTextBox.Text), int.Parse(this.orgTypIDTextBox.Text));
              }
          }
  
          private void selCrncyButton_Click(object sender, EventArgs e)
          {
              //Currencies
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[15]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                   " this action!\nContact your System Administrator!", 0);
                  return;
              }
              int[] selValIDs = new int[1];
              selValIDs[0] = int.Parse(this.orgTypIDTextBox.Text);
              DialogResult dgRes = Global.mnFrm.cmCde.showPssblValDiag(
                  Global.mnFrm.cmCde.getLovID("Currencies"), ref selValIDs, true, true,
               this.srchWrd, "Both", true);
              if (dgRes == DialogResult.OK)
              {
                  for (int i = 0; i < selValIDs.Length; i++)
                  {
                      this.crncyIDTextBox.Text = selValIDs[i].ToString();
                      this.crncyCodeTextBox.Text = Global.mnFrm.cmCde.getPssblValNm(selValIDs[i]);
                  }
              }
              if (int.Parse(this.orgIDTextBox.Text) > 0)
              {
                  Global.updtOrgCrncyID(int.Parse(this.orgIDTextBox.Text), int.Parse(this.crncyIDTextBox.Text));
              }
          }
  
          private void orgDetTreeView_AfterSelect(object sender, TreeViewEventArgs e)
          {
              this.clearOrgDetInfo();
              if (e.Node == null)
              {
                  return;
              }
              this.populateOrgDet(Global.mnFrm.cmCde.getOrgID(e.Node.Text));
          }
  
          private void exprtOrgTmpltButton_Click(object sender, EventArgs e)
          {
              Global.mnFrm.cmCde.exprtOrgTmp();
          }
  
          private void imprtOrgTmpltButton_Click(object sender, EventArgs e)
          {
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[14]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                   " this action!\nContact your System Administrator!", 0);
                  return;
              }
              this.openFileDialog1.RestoreDirectory = true;
              this.openFileDialog1.Filter = "All Files|*.*|Excel Files|*.xls;*.xlsx";
              this.openFileDialog1.FilterIndex = 2;
              this.openFileDialog1.Title = "Select an Excel File to Upload...";
              this.openFileDialog1.FileName = "";
              if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
              {
                  Global.mnFrm.cmCde.imprtOrgTmp(this.openFileDialog1.FileName);
              }
              this.populateOrgDet();
          }
  
          private void positionOrgDetTextBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
          {
              EventArgs ex = new EventArgs();
              if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up)
              {
                  this.OrgDetPnlNavButtons(this.movePreviousOrgDetButton, ex);
              }
              else if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Down)
              {
                  this.OrgDetPnlNavButtons(this.moveNextOrgDetButton, ex);
              }
          }
  
          private void delOrgDetButton_Click(object sender, EventArgs e)
          {
              if (Global.mnFrm.cmCde.test_prmssns(Global.dfltPrvldgs[16]) == false)
              {
                  Global.mnFrm.cmCde.showMsg("You don't have permission to perform" +
                   " this action!\nContact your System Administrator!", 0);
                  return;
              }
              if (this.orgIDTextBox.Text == "" || this.orgIDTextBox.Text == "-1")
              {
                  Global.mnFrm.cmCde.showMsg("Please select the Organisation to DELETE!", 0);
                  return;
              }
              if (Global.isOrgInUse(int.Parse(this.orgIDTextBox.Text)))
              {
                  Global.mnFrm.cmCde.showMsg("This Orgaisation either has Accounts and Persons attached or \r\nis Parent to another Organisation hence cannot be DELETED!", 0);
                  return;
              }
              if (Global.mnFrm.cmCde.showMsg("Are you sure you want to DELETE the selected Orgaisation?" +
         "\r\nThis action cannot be undone!", 1) == DialogResult.No)
              {
                  //Global.myNwMainFrm.cmmnCode.showMsg("Operation Cancelled!", 4);
                  return;
              }
              Global.deleteOrg(long.Parse(this.orgIDTextBox.Text), this.orgNameTextBox.Text);
  
              this.loadCorrectPanel("Organization's Details");
  
          }
  
          private void orgEnabledCheckBox_CheckedChanged(object sender, EventArgs e)
          {
              if (this.shdObeyOrgDetEvts() == false
         || beenToCheckBx == true)
              {
                  beenToCheckBx = false;
                  return;
              }
              beenToCheckBx = true;
              if (this.addOrg == false && this.editOrg == false)
              {
                  this.orgEnabledCheckBox.Checked = !this.orgEnabledCheckBox.Checked;
              }
          }
          #endregion
  
          #region "Divisions/Groups..."
          private void loadDivDetPanel()
          {
              this.obey_divDet_evnts = false;
              if (this.searchInDivComboBox.SelectedIndex < 0)
              {
                  this.searchInDivComboBox.SelectedIndex = 0;
              }
              if (this.searchForDivTextBox.Text.Contains("%") == false)
              {
                  this.searchForDivTextBox.Text = "%" + this.searchForDivTextBox.Text.Replace(" ", "%") + "%";
              }
  
gt:             if (this.searchForDivTextBox.Text == "%%")