/// <summary>
        /// Save
        /// </summary>
        /// <param name="unitId">unitId</param>
        /// <param name="companyId">companyId</param>
        public void Save(int unitId, int companyId)
        {
            foreach (UnitsAddTDS.UnitsChecklistRulesTempRow row in (UnitsAddTDS.UnitsChecklistRulesTempDataTable)Data.Tables["UnitsChecklistRulesTemp"])
            {
                string state = "Healthy";
                if (row.Selected)
                {
                    DateTime? lastService = null; if (!row.IsLastServiceNull()) lastService = row.LastService;
                    DateTime? nextDue = null; if (!row.IsNextDueNull()) nextDue = row.NextDue;

                    // Get state
                    state = GetChecklistState(row.RuleID, row.Frequency, lastService, nextDue, companyId);

                    if (row.Frequency != "Only once")
                    {
                        if (lastService.HasValue)
                        {
                            // Get next due
                            DateTime timeToAdded = new DateTime(((DateTime)lastService).Year, ((DateTime)lastService).Month, ((DateTime)lastService).Day);

                            if (row.Frequency == "Monthly") nextDue = timeToAdded.AddMonths(1);
                            if (row.Frequency == "Every 2 months") nextDue = timeToAdded.AddMonths(2);
                            if (row.Frequency == "Every 3 months") nextDue = timeToAdded.AddMonths(3);
                            if (row.Frequency == "Every 4 months") nextDue = timeToAdded.AddMonths(4);
                            if (row.Frequency == "Every 6 months") nextDue = timeToAdded.AddMonths(6);
                            if (row.Frequency == "Yearly") nextDue = timeToAdded.AddYears(1);
                        }
                    }

                    Checklist checklist = new Checklist(null);
                    checklist.InsertDirect(unitId, row.RuleID, lastService, nextDue, row.Done, state, false, companyId);
                }
                else
                {
                    state = "Inactive";

                    DateTime? lastService = null; if (!row.IsLastServiceNull()) lastService = row.LastService;
                    DateTime? nextDue = null; if (!row.IsNextDueNull()) nextDue = row.NextDue;

                    Checklist checklist = new Checklist(null);
                    checklist.InsertDirect(unitId, row.RuleID, lastService, nextDue, row.Done, state, false, companyId);
                }
            }
        }
        /// <summary>
        /// UpdateUnitChecklists
        /// </summary>
        /// <param name="unitId">unitId</param>
        /// <param name="companyLevelId">companyLevelId</param>
        /// <param name="companyId">companyId</param>
        private void UpdateUnitChecklists(int unitId, int companyLevelId, int companyId)
        {
            UnitsCategoryGateway unitsCategoryGateway = new UnitsCategoryGateway();
            unitsCategoryGateway.LoadByUnitId(unitId, companyId);

            foreach (UnitsTDS.LFS_FM_UNIT_CATEGORYRow row in (UnitsTDS.LFS_FM_UNIT_CATEGORYDataTable)unitsCategoryGateway.Table)
            {
                int categoryId = row.CategoryID;

                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;

                    RuleCompanyLevelGateway ruleCompanyLevelGateway = new RuleCompanyLevelGateway(null);

                    if (ruleCompanyLevelGateway.IsUsedInRuleCompanyLevel(ruleId, 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>
        /// UpdateRuleCategory
        /// </summary>
        /// <param name="ruleId">ruleId</param>
        /// <param name="companyId">companyId</param>
        /// <param name="companyLevelsSelected">companyLevelsSelected</param>
        /// <param name="categoriesSelected">categoriesSelected</param>
        /// <param name="unitsSelected">unitsSelected</param>
        private void UpdateRuleCategory(int ruleId, int companyId, ArrayList companyLevelsSelected, ArrayList categoriesSelected, ArrayList unitsSelected)
        {
            // Insert into checklist only selected units
            foreach (int categoryId in categoriesSelected)
            {
                RuleCategory ruleCategory = new RuleCategory(null);
                ruleCategory.InsertDirect(ruleId, categoryId, false, companyId);

                foreach (int unitId in unitsSelected)
                {
                    //int unitId = row.UnitID;

                    // Save categories and selected units
                    UnitsCategoryGateway unitsCategoryGatewayExist = new UnitsCategoryGateway(null);
                    if (unitsCategoryGatewayExist.IsUsedInUnitCategory(unitId, categoryId, false))
                    {
                        RuleCategoryUnits ruleCategoryUnits = new RuleCategoryUnits(null);
                        ruleCategoryUnits.InsertDirect(ruleId, categoryId, unitId, false, companyId);
                    }

                    // Save Chechklists for selected units
                    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))
                            {
                                Checklist checklist = new Checklist(null);
                                checklist.InsertDirect(unitId, ruleId, null, null, false, "Unknown", false, companyId);
                            }
                        }
                    }
                }
            }
        }
        /// <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);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }