/// <summary>
        /// DeleteDirectByRuleId
        /// </summary>
        /// <param name="ruleId">ruleId</param>
        /// <param name="categoryId">categoryId</param>
        /// <param name="companyId">companyId</param>
        /// <returns>True</returns>
        public bool DeleteDirectByRuleIdCategoryId(int ruleId, int categoryId, int companyId)
        {
            RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway(Data);
            ruleCategoryUnitsGateway.DeleteByRuleIdCategoryId(ruleId, categoryId, companyId);

            return true;
        }
        /// <summary>
        /// UpdateUnitCategory
        /// </summary>
        /// <param name="unitId">unitId</param>
        /// <param name="companyId">companyId</param>
        /// <param name="categoriesSelected">categoriesSelected</param>
        /// <param name="companyLevelId">companyLevelId</param>
        private void UpdateUnitCategory(int unitId, int companyId, ArrayList categoriesSelected, int companyLevelId)
        {
            RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway(null);

            CategoryGateway categoryGateway = new CategoryGateway();
            categoryGateway.Load(companyId);

            if (categoryGateway.Table.Rows.Count > 0)
            {
                foreach (CategoriesTDS.LFS_FM_CATEGORYRow row in (CategoriesTDS.LFS_FM_CATEGORYDataTable)categoryGateway.Table)
                {
                    int categoryId = row.CategoryID;

                    UnitsCategoryGateway unitsCategoryGateway = new UnitsCategoryGateway(null);

                    // Exists in DB
                    if (unitsCategoryGateway.IsUsedInUnitCategory(unitId, categoryId, true))
                    {
                        if (!categoriesSelected.Contains(categoryId))
                        {
                            UnitsCategory unitsCategory = new UnitsCategory(null);
                            unitsCategory.DeleteDirect(unitId, categoryId, companyId);

                            RuleCategoryGateway ruleCategoryGateway = new RuleCategoryGateway();
                            ruleCategoryGateway.LoadByCategoryId(categoryId, companyId);

                            foreach (RuleTDS.LFS_FM_RULE_CATEGORYRow rowRuleCategory in (RuleTDS.LFS_FM_RULE_CATEGORYDataTable)ruleCategoryGateway.Table)
                            {
                                int ruleId = rowRuleCategory.RuleID;

                                if (ruleCategoryUnitsGateway.IsUsedInRuleCategoryUnits(ruleId, categoryId, unitId))
                                {
                                    // Delete
                                    RuleCategoryUnits ruleCategoryUnits = new RuleCategoryUnits(null);
                                    ruleCategoryUnits.DeleteDirect(ruleId, categoryId, unitId, companyId);
                                }
                            }
                        }
                        else
                        {
                            UnitsCategory unitsCategory = new UnitsCategory(null);
                            unitsCategory.UnDeleteDirect(unitId, categoryId, companyId);
                        }
                    }
                    else
                    {
                        if (categoriesSelected.Contains(categoryId))
                        {
                            UnitsCategory unitsCategory = new UnitsCategory(null);
                            unitsCategory.InsertDirect(unitId, categoryId, false, companyId);
                        }
                    }
                }
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Set active toolbar
            mForm6 master = (mForm6)this.Master;
            master.ActiveToolbar = "FleetManagement";

            int ruleId = Int32.Parse(hdfRuleId.Value);

            foreach (int categoryId in arrayCategoriesSelected)
            {
                // Mark selected units
                RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway();

                if (cbxlUnitsSelected.Items.Count > 0)
                {
                    foreach (ListItem lst in cbxlUnitsSelected.Items)
                    {
                        if (ruleCategoryUnitsGateway.IsUsedInRuleCategoryUnits(ruleId, categoryId, Int32.Parse(lst.Value)))
                        {
                            lst.Selected = true;
                        }
                    }
                }
            }

            ServiceInformationBasicInformation serviceInformationBasicInformation = new ServiceInformationBasicInformation();
            serviceInformationBasicInformation.LoadInProgressByRuleId(ruleId, int.Parse(hdfCompanyId.Value));

            if (serviceInformationBasicInformation.Table.Rows.Count > 0)
            {
                tkrmTop.Items[1].Visible = false;
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            // Set active toolbar
            mForm6 master = (mForm6)this.Master;
            master.ActiveToolbar = "FleetManagement";

            int ruleId = Int32.Parse(hdfRuleId.Value);
            foreach (int categoryId in arrayCategoriesSelectedForEdit)
            {
                // Mark selected units
                if (cbxlUnitsSelected.Items.Count > 0)
                {
                    RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway();
                    foreach (ListItem lst in cbxlUnitsSelected.Items)
                    {
                        if (ruleCategoryUnitsGateway.IsUsedInRuleCategoryUnits(ruleId, categoryId, Int32.Parse(lst.Value)))
                        {
                            lst.Selected = true;
                            arrayUnitsSelectedForEdit.Add(int.Parse(lst.Value));
                            Session["arrayUnitsSelectedForEdit"] = arrayUnitsSelectedForEdit;
                        }
                    }
                }
            }
        }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// InsertDirect
 /// </summary>
 /// <param name="ruleId">ruleId</param>
 /// <param name="categoryId">categoryId</param>
 /// <param name="unitId">unitId</param>
 /// <param name="deleted">deleted</param>
 /// <param name="companyId">companyId</param>
 public void InsertDirect(int ruleId, int categoryId, int unitId, bool deleted, int companyId)
 {
     RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway(null);
     ruleCategoryUnitsGateway.Insert(ruleId, categoryId, unitId, deleted, companyId);
 }
        /// <summary>
        /// UnDeleteDirect
        /// </summary>
        /// <param name="ruleId">ruleId</param>
        /// <param name="categoryId">categoryId</param>
        /// <param name="unitId">unitId</param>
        /// <param name="companyId">companyId</param>
        /// <returns>True</returns>
        public bool UnDeleteDirect(int ruleId, int categoryId, int unitId, int companyId)
        {
            RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway(Data);
            ruleCategoryUnitsGateway.UnDelete(ruleId, categoryId, unitId, companyId);

            return true;
        }
        /// <summary>
        /// UpdateRuleCategoryUnits
        /// </summary>
        /// <param name="ruleId">ruleId</param>
        /// <param name="companyId">companyId</param>
        /// <param name="categoriesSelected">categoriesSelected</param>
        /// <param name="companyLevelsSelected">companyLevelsSelected</param>
        /// <param name="unitsSelected">unitsSelected</param>
        private void UpdateRuleCategoryUnits(int ruleId, int companyId, ArrayList categoriesSelected, ArrayList companyLevelsSelected, ArrayList unitsSelected)
        {
            // At each category
            CategoryGateway categoryGateway = new CategoryGateway();
            categoryGateway.Load(companyId);

            foreach (CategoriesTDS.LFS_FM_CATEGORYRow row in (CategoriesTDS.LFS_FM_CATEGORYDataTable)categoryGateway.Table)
            {
                int categoryId = row.CategoryID;

                // At each unit
                UnitsCategoryGateway unitsCategoryGateway = new UnitsCategoryGateway();
                unitsCategoryGateway.LoadByCategoryId(categoryId, companyId);

                foreach (UnitsTDS.LFS_FM_UNIT_CATEGORYRow rowUnitCategory in (UnitsTDS.LFS_FM_UNIT_CATEGORYDataTable)unitsCategoryGateway.Table)
                {
                    int unitId = rowUnitCategory.UnitID;

                    RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway(null);

                    // If it already exists and it's not deleted
                    if (ruleCategoryUnitsGateway.IsUsedInRuleCategoryUnits(ruleId, categoryId, unitId))
                    {
                        // Verify if it's at the current selection
                        if ((!categoriesSelected.Contains(categoryId)) || (categoriesSelected.Contains(categoryId) && (!unitsSelected.Contains(unitId))))
                        {
                            // Delete
                            RuleCategoryUnits ruleCategoryUnits = new RuleCategoryUnits(null);
                            ruleCategoryUnits.DeleteDirect(ruleId, categoryId, unitId, companyId);
                        }
                        else
                        {
                            RuleCategoryUnits ruleCategoryUnits = new RuleCategoryUnits(null);
                            ruleCategoryUnits.UnDeleteDirect(ruleId, categoryId, unitId, companyId);

                            // Insert checklist
                            foreach (int companyLevelId in companyLevelsSelected)
                            {
                                UnitsGateway unitsGateway = new UnitsGateway(null);

                                if (unitsGateway.IsUsedInUnitsAndNotIsDisposed(unitId, companyLevelId))
                                {
                                    ChecklistGateway checklistGateway = new ChecklistGateway(null);

                                    if (checklistGateway.IsUsedInChecklist(unitId, ruleId, true))
                                    {
                                        Checklist checklist = new Checklist(null);
                                        checklist.UnDeleteDirect(ruleId, unitId, companyId);
                                    }
                                    else
                                    {
                                        Checklist checklist = new Checklist(null);
                                        checklist.InsertDirect(unitId, ruleId, null, null, false, "Unknown", false, companyId);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // If it already exists and it's deleted
                        if (ruleCategoryUnitsGateway.IsUsedInRuleCategoryUnitsAsDeleted(ruleId, categoryId, unitId))
                        {
                            // Verify if it's at the current selection
                            if (categoriesSelected.Contains(categoryId) && (unitsSelected.Contains(unitId)))
                            {
                                // UnDelete
                                RuleCategoryUnits ruleCategoryUnits = new RuleCategoryUnits(null);
                                ruleCategoryUnits.UnDeleteDirect(ruleId, categoryId, unitId, companyId);

                                // Insert checklist
                                foreach (int companyLevelId in companyLevelsSelected)
                                {
                                    UnitsGateway unitsGateway = new UnitsGateway(null);

                                    if (unitsGateway.IsUsedInUnitsAndNotIsDisposed(unitId, companyLevelId))
                                    {
                                        ChecklistGateway checklistGateway = new ChecklistGateway(null);

                                        if (checklistGateway.IsUsedInChecklist(unitId, ruleId, true))
                                        {
                                            Checklist checklist = new Checklist(null);
                                            checklist.UnDeleteDirect(ruleId, unitId, companyId);
                                        }
                                        else
                                        {
                                            Checklist checklist = new Checklist(null);
                                            checklist.InsertDirect(unitId, ruleId, null, null, false, "Unknown", false, companyId);
                                        }
                                    }
                                }
                            }
                        }
                        // If it's not at bd
                        else
                        {
                            // Verify if it's at the current selection
                            if (categoriesSelected.Contains(categoryId) && (unitsSelected.Contains(unitId)))
                            {
                                // Insert rule category units
                                RuleCategoryUnits ruleCategoryUnits = new RuleCategoryUnits(null);
                                ruleCategoryUnits.InsertDirect(ruleId, categoryId, unitId, false, companyId);

                                // Insert checklist
                                foreach (int companyLevelId in companyLevelsSelected)
                                {
                                    UnitsGateway unitsGateway = new UnitsGateway(null);

                                    if (unitsGateway.IsUsedInUnitsAndNotIsDisposed(unitId, companyLevelId))
                                    {
                                        ChecklistGateway checklistGateway = new ChecklistGateway(null);

                                        if (checklistGateway.IsUsedInChecklist(unitId, ruleId, true))
                                        {
                                            Checklist checklist = new Checklist(null);
                                            checklist.UnDeleteDirect(ruleId, unitId, companyId);
                                        }
                                        else
                                        {
                                            Checklist checklist = new Checklist(null);
                                            checklist.InsertDirect(unitId, ruleId, null, null, false, "Unknown", false, companyId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// DeleteRuleCategory
        /// </summary>
        /// <param name="ruleId">ruleId</param>
        /// <param name="companyId">companyId</param>
        private void DeleteRuleCategory(int ruleId, int companyId)
        {
            // Delete RuleCategoryUnits
            RuleCategoryUnits ruleCategoryUnits = new RuleCategoryUnits(null);
            RuleCategoryUnitsGateway ruleCategoryUnitsGateway = new RuleCategoryUnitsGateway(null);

            if (ruleCategoryUnitsGateway.IsRuleUsed(ruleId))
            {
                ruleCategoryUnits.DeleteDirectByRuleId(ruleId, companyId);
            }

            // Load all categories
            CategoryGateway categoryGateway = new CategoryGateway();
            categoryGateway.Load(companyId);

            // Delete rule categories
            if (categoryGateway.Table.Rows.Count > 0)
            {
                foreach (CategoriesTDS.LFS_FM_CATEGORYRow row in (CategoriesTDS.LFS_FM_CATEGORYDataTable)categoryGateway.Table)
                {
                    int categoryId = row.CategoryID;
                    RuleCategoryGateway ruleCategoryGateway = new RuleCategoryGateway(null);
                    if (ruleCategoryGateway.IsUsedInRuleCategory(ruleId, categoryId))
                    {
                        RuleCategory ruleCategory = new RuleCategory(null);
                        ruleCategory.DeleteDirect(ruleId, categoryId, companyId);
                    }
                }
            }
        }