// ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_CHECKLISTRULES_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_CHECKLISTRULES_DELETE"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["rule_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in checklist_rules_delete.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfRuleId.Value = Request.QueryString["rule_id"].ToString();

                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int ruleId = Int32.Parse(hdfRuleId.Value.Trim());

                // If coming from
                // ... checklist_rules_navigator.aspx
                if (Request.QueryString["source_page"] == "checklist_rules_navigator.aspx")
                {
                    ViewState["update"] = "no";

                    ruleTDS = new RuleTDS();
                    LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule rule = new LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule(ruleTDS);
                    rule.LoadByRuleId(ruleId, companyId);

                    // Store dataset
                    Session["ruleTDS"] = ruleTDS;
                }

                // ... checklist_rules_summary.aspx
                if (Request.QueryString["source_page"] == "checklist_rules_summary.aspx")
                {
                    ViewState["update"] = Request.QueryString["update"];

                    // Restore datasets
                    ruleTDS = (RuleTDS)Session["ruleTDS"];
                }
            }
            else
            {
                // Restore datasets
                ruleTDS = (RuleTDS)Session["ruleTDS"];
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // INITIAL EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_CHECKLISTRULES_VIEW"]) && Convert.ToBoolean(Session["sgLFS_FLEETMANAGEMENT_CHECKLISTRULES_EDIT"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["rule_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in checklist_rules_edit.aspx");
                }

                // If comming from
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfRuleId.Value = Request.QueryString["rule_id"].ToString();
                hdfCheckUnitsFlag.Value = "No";

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

                // ... Checklist_rules_navigator.aspx
                if (Request.QueryString["source_page"] == "checklist_rules_navigator.aspx")
                {
                    ViewState["update"] = "no";

                    arrayCategoriesSelectedForEdit = new ArrayList();
                    arrayCompanyLevelsSelectedForEdit = new ArrayList();
                    arrayUnitsSelectedForEdit = new ArrayList();
                    Session["arrayCategoriesSelectedForEdit"] = arrayCategoriesSelectedForEdit;
                    Session["arrayCompanyLevelsSelectedForEdit"] = arrayCompanyLevelsSelectedForEdit;
                    Session["arrayUnitsSelectedForEdit"] = arrayUnitsSelectedForEdit;

                    // ... ... For rule
                    ruleTDS = new RuleTDS();
                    LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule rule = new LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule(ruleTDS);
                    rule.LoadByRuleId(ruleId, companyId);

                    // ... ... For Categories
                    categoriesTDS = new CategoriesTDS();
                    Category category = new Category(categoriesTDS);
                    category.Load(int.Parse(hdfCompanyId.Value));

                    // ... ... For Company Levels
                    companyLevelsTDS = new CompanyLevelsTDS();
                    CompanyLevel companyLevel = new CompanyLevel(companyLevelsTDS);
                    companyLevel.Load(int.Parse(hdfCompanyId.Value));

                    // Store dataset
                    Session["ruleTDS"] = ruleTDS;
                    Session["categoriesTDSForChecklist"] = categoriesTDS;
                    Session["companyLevelsTDS"] = companyLevelsTDS;
                }

                // ... Checklist_rules_add.aspx
                if (Request.QueryString["source_page"] == "checklist_rules_add.aspx")
                {
                    ViewState["update"] = "yes";

                    arrayCategoriesSelectedForEdit = new ArrayList();
                    arrayCompanyLevelsSelectedForEdit = new ArrayList();
                    arrayUnitsSelectedForEdit = new ArrayList();
                    Session["arrayCategoriesSelectedForEdit"] = arrayCategoriesSelectedForEdit;
                    Session["arrayCompanyLevelsSelectedForEdit"] = arrayCompanyLevelsSelectedForEdit;
                    Session["arrayUnitsSelectedForEdit"] = arrayUnitsSelectedForEdit;

                    // ... ... For rule
                    ruleTDS = new RuleTDS();
                    LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule rule = new LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule(ruleTDS);
                    rule.LoadByRuleId(ruleId, companyId);

                    // ... ... For Categories
                    categoriesTDS = new CategoriesTDS();
                    Category category = new Category(categoriesTDS);
                    category.Load(int.Parse(hdfCompanyId.Value));

                    // ... ... For Company Levels
                    companyLevelsTDS = new CompanyLevelsTDS();
                    CompanyLevel companyLevel = new CompanyLevel(companyLevelsTDS);
                    companyLevel.Load(int.Parse(hdfCompanyId.Value));

                    // Store dataset
                    Session["ruleTDS"] = ruleTDS;
                    Session["categoriesTDSForChecklist"] = categoriesTDS;
                    Session["companyLevelsTDS"] = companyLevelsTDS;
                }

                // ... Checklist_rules_delete.aspx or checklist_rules_summary.aspx
                if ((Request.QueryString["source_page"] == "checklist_rules_delete.aspx") || (Request.QueryString["source_page"] == "checklist_rules_summary.aspx"))
                {
                    ViewState["update"] = Request.QueryString["update"];

                    // Restore dataset
                    ruleTDS = (RuleTDS)Session["ruleTDS"];
                    categoriesTDS = (CategoriesTDS)Session["categoriesTDSForChecklist"];
                    companyLevelsTDS = (CompanyLevelsTDS)Session["companyLevelsTDS"];

                    arrayCategoriesSelectedForEdit = new ArrayList();
                    arrayCompanyLevelsSelectedForEdit = new ArrayList();
                    arrayUnitsSelectedForEdit = new ArrayList();
                    Session["arrayCategoriesSelectedForEdit"] = arrayCategoriesSelectedForEdit;
                    Session["arrayCompanyLevelsSelectedForEdit"] = arrayCompanyLevelsSelectedForEdit;
                    Session["arrayUnitsSelectedForEdit"] = arrayUnitsSelectedForEdit;
                }

                // Load data for current rule
                LoadData(ruleId);

                // Databind
                Page.DataBind();
            }
            else
            {
                // Restore datasets
                ruleTDS = (RuleTDS)Session["ruleTDS"];
                categoriesTDS = (CategoriesTDS)Session["categoriesTDSForChecklist"];
                companyLevelsTDS = (CompanyLevelsTDS)Session["companyLevelsTDS"];

                arrayCategoriesSelectedForEdit = (ArrayList)Session["arrayCategoriesSelectedForEdit"];
                arrayCompanyLevelsSelectedForEdit = (ArrayList)Session["arrayCompanyLevelsSelectedForEdit"];
                arrayUnitsSelectedForEdit = (ArrayList)Session["arrayUnitsSelectedForEdit"];
            }
        }
        /// <summary>
        /// Save
        /// </summary>
        /// <param name="categoriesSelected">categoriesSelected</param>
        /// <param name="companyLevelsSelected">companyLevelsSelected</param>
        /// <param name="unitsSelected">unitsSelected</param>
        public void Save(ArrayList categoriesSelected, ArrayList companyLevelsSelected, ArrayList unitsSelected)
        {
            ChecklistRulesAddTDS checklistRulesAddChanges = (ChecklistRulesAddTDS)Data.GetChanges();

            if (checklistRulesAddChanges.ChecklistRulesAddNew.Rows.Count > 0)
            {
                foreach (ChecklistRulesAddTDS.ChecklistRulesAddNewRow row in (ChecklistRulesAddTDS.ChecklistRulesAddNewDataTable)checklistRulesAddChanges.ChecklistRulesAddNew)
                {
                    string name = row.Name;
                    string description = ""; if (!row.IsDescriptionNull()) description = row.Description;
                    bool mto = row.MTO;
                    string frequency = row.Frequency;
                    bool alarm = row.Alarm;
                    int? alarmDays = null; if (!row.IsAlarmDaysNull()) alarmDays = row.AlarmDays;
                    bool serviceRequest = row.ServiceRequest;
                    int? serviceRequestDays = null; if (!row.IsServiceRequestDaysNull()) serviceRequestDays = row.ServiceRequestDays;
                    bool deleted = row.Deleted;
                    int companyId = row.COMPANY_ID;

                    Rule rule = new Rule(null);
                    int ruleId = rule.InsertDirect(name, description, mto, frequency, alarm, alarmDays, serviceRequest, serviceRequestDays, deleted, companyId);

                    UpdateRuleCategory(ruleId, companyId, companyLevelsSelected, categoriesSelected, unitsSelected);
                    UpdateRuleCompanyLevel(ruleId, companyId, companyLevelsSelected);
                }
            }
        }
 /// <summary>
 /// UpdateRule
 /// </summary>
 /// <param name="ruleId">ruleId</param>
 /// <param name="name">name</param>
 /// <param name="description">description</param>
 /// <param name="mto">mto</param>
 /// <param name="frequency">frequency</param>
 /// <param name="alarm">alarm</param>
 /// <param name="alarmDays">alarmDays</param>
 /// <param name="serviceRequest">serviceRequest</param>
 /// <param name="serviceRequestDays">serviceRequestDays</param>
 /// <param name="deleted">deleted</param>
 /// <param name="companyId">companyId</param>
 private void UpdateRule(int ruleId, string name, string description, bool mto, string frequency, bool alarm, int? alarmDays, bool serviceRequest, int? serviceRequestDays, bool deleted, int companyId)
 {
     LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule rule = new LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule(null);
     rule.UpdateDirect(ruleId, name, description, mto, frequency, alarm, alarmDays, serviceRequest, serviceRequestDays, false, companyId);
 }
 // ////////////////////////////////////////////////////////////////////////
 // PRIVATE METHODS
 //            
 /// <summary>
 /// DeleteRule
 /// </summary>
 /// <param name="ruleId">ruleId</param>
 /// <param name="companyId">companyId</param>
 private void DeleteRule(int ruleId, int companyId)
 {
     LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule rule = new LiquiForce.LFSLive.BL.FleetManagement.ChecklistRules.Rule(null);
     rule.DeleteDirect(ruleId, companyId);
 }