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

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_RESOURCES_MATERIALS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_RESOURCES_MATERIALS_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["material_id"] == null) || ((string)Request.QueryString["active_tab"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in materials_edit.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfResourceType.Value = "Materials";
                hdfActiveTab.Value = Request.QueryString["active_tab"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfCurrentMaterialId.Value = Request.QueryString["material_id"];

                // Prepare initial data
                Session.Remove("materialsNotesDummy");
                Session.Remove("materialsCostsDummy");
                Session.Remove("materialsCostsExceptionsDummy");
                Session.Remove("costIdSelected");

                // ... for team member title
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

                MaterialsGateway materialsGateway = new MaterialsGateway();
                materialsGateway.LoadByMaterialId(materialId, companyId);
                lblTitleMaterials.Text = "Material: " + materialsGateway.GetDescription(materialId);

                // If coming from
                string resourceType = hdfResourceType.Value;

                // ... materials_navigator2.aspx or materials_add.aspx
                if ((Request.QueryString["source_page"] == "materials_navigator2.aspx")|| (Request.QueryString["source_page"] == "materials_add.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    // ... Set initial tab
                    if ((string)Session["dialogOpenedMaterials"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];

                        materialsInformationTDS = new MaterialsInformationTDS();
                        materialsTDS = new MaterialsTDS();

                        MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                        materialsInformationBasicInformation.LoadByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                        materialsInformationCostHistoryInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                        materialsInformationCostHistoryExceptionsInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationNoteInformation materialsInformationNoteInformationForEdit = new MaterialsInformationNoteInformation(materialsInformationTDS);
                        materialsInformationNoteInformationForEdit.LoadAllByMaterialId(materialId, companyId);
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabMaterials"];

                        // Restore datasets
                        materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                        materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
                    }

                    // ... Store dataset
                    Session["materialsInformationTDS"] = materialsInformationTDS;
                    Session["materialsTDS"] = materialsTDS;
                }

                // ... materials_summary.aspx or materials_edit
                if ((Request.QueryString["source_page"] == "materials_summary.aspx") || (Request.QueryString["source_page"] == "materials_edit.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // ... Restore dataset
                    materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                    materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];

                    // ... Set initial tab
                    if ((string)Session["dialogOpenedMaterials"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabMaterials"];
                    }

                    if (ViewState["update"].ToString().Trim() == "yes")
                    {
                        MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                        materialsInformationBasicInformation.LoadByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                        materialsInformationCostHistoryInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                        materialsInformationCostHistoryExceptionsInformation.LoadAllByMaterialId(materialId, companyId);

                        MaterialsInformationNoteInformation materialsInformationNoteInformationForEdit = new MaterialsInformationNoteInformation(materialsInformationTDS);
                        materialsInformationNoteInformationForEdit.LoadAllByMaterialId(materialId, companyId);

                        // ... Store dataset
                        Session["materialsInformationTDS"] = materialsInformationTDS;
                        Session["materialsTDS"] = materialsTDS;
                    }
                }

                Session["costIdSelected"] = 0;
                grdCostsExceptions.ShowFooter = false;
                string filterOptions = string.Format("CostID = {0} AND Deleted = {1}", 0, 0);
                odsCostsExceptions.FilterExpression = filterOptions;

                // Prepare initial data
                // ... Set initial tab
                int activeTabMaterials = Int32.Parse(hdfActiveTab.Value);
                tcDetailedInformation.ActiveTabIndex = activeTabMaterials;

                // ... Data for current material
                LoadData(companyId);

                // Databind
                Page.DataBind();
            }
            else
            {
                // Restore datasets
                materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];

                // Set initial tab
                int activeTabMaterials = Int32.Parse(hdfActiveTab.Value);
                tcDetailedInformation.ActiveTabIndex = activeTabMaterials;
            }
        }
        private void Save()
        {
            // Validate data
            bool validData = false;

            Page.Validate("generalInformation");
            if (Page.IsValid)
            {
                validData = true;

                // ... Validate notes tab
                if (ValidateNotesFooter())
                {
                    Page.Validate("notesDataAdd");
                    if (!Page.IsValid)
                    {
                        validData = false;
                    }
                }

                Page.Validate("notesDataEdit");
                if (!Page.IsValid)
                {
                    validData = false;
                }
            }

            // For valid data
            if (validData)
            {
                // Notes Gridview, if the gridview is edition mode
                if (grdNotes.EditIndex >= 0)
                {
                    grdNotes.UpdateRow(grdNotes.EditIndex, true);
                }

                // Save notes data
                GrdNotesAdd();

                // Costs Gridview, if the gridview is edition mode
                if (grdCosts.EditIndex >= 0)
                {
                    grdCosts.UpdateRow(grdCosts.EditIndex, true);
                }

                // Save costs data
                GrdCostsAdd();

                // Costs Exceptions Gridview, if the gridview is edition mode
                if (grdCostsExceptions.EditIndex >= 0)
                {
                    grdCostsExceptions.UpdateRow(grdCostsExceptions.EditIndex, true);
                }

                // Save costs exceptions data
                GrdCostsExceptionsAdd();

                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

                // ... Get basic material data
                string thickness = "";
                string size = "";
                string length = "";
                string description = "";
                string state = tbxState.Text.Trim();

                if (hdfProcess.Value == "Full Length Lining")
                {
                    thickness =  ddlThickness.SelectedValue;
                    size = tbxSize.Text.Trim();
                    description = "Full Length Liner " + size;
                    if (thickness != "")
                    {
                        description = description + " / T." + thickness;
                    }
                }

                if (hdfProcess.Value == "Point Repairs")
                {
                    size = ddlPrSize.SelectedValue;
                    length = ddlPrLength.SelectedValue;
                    string formatedSize = size.Remove(size.Length - 1, 1);
                    formatedSize = formatedSize.PadLeft(3, '0');
                    string formatedLength = length.Remove(length.Length - 1, 1);
                    formatedLength = formatedLength.PadLeft(3, '0');
                    description = "PR-" + formatedSize + "-" + formatedLength + "-000";
                }

                if (hdfProcess.Value == "Junction Lining")
                {
                    size = "";
                    if (rbtnMiscSupplies.Checked) description = "Lateral / Misc Supplies";
                    if (rbtnCleanoutMaterial.Checked) description = "Lateral / Cleanout Material";
                    if (rbtnBackfillCleanout.Checked) description = "Lateral / Backfill - Cleanout";
                    if (rbtnRestorationCrowleCap.Checked) description = "Lateral / Restoration & Crowle Cap";
                    if (rbtnOther.Checked)
                    {
                        if (!tbxDescription.Text.Contains("Lateral"))
                        {
                            size = tbxJLSize.Text.Trim();
                            description = "Junction Liner " + size;
                        }
                        else
                        {
                            description = tbxDescription.Text;
                        }
                    }
                }

                // Update service data
                MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                materialsInformationBasicInformation.Update(materialId, description, size, length, thickness, hdfProcess.Value, state, false, companyId);

                // Store datasets
                Session["materialsInformationTDS"] = materialsInformationTDS;
                Session["materialsTDS"] = materialsTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";

                // Redirect
                string url = "";
                if (Request.QueryString["source_page"] == "materials_navigator2.aspx")
                {
                    url = "./materials_navigator2.aspx?source_page=materials_edit.aspx&material_id=" + hdfCurrentMaterialId.Value + GetNavigatorState() + "&update=yes";
                }

                if (Request.QueryString["source_page"] == "materials_summary.aspx")
                {
                    string activeTabMaterials = hdfActiveTab.Value;
                    url = "./materials_summary.aspx?source_page=materials_edit.aspx&material_id=" + hdfCurrentMaterialId.Value + "&active_tab=" + activeTabMaterials + GetNavigatorState() + "&update=yes";
                }

                Response.Redirect(url);
            }
        }
        private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                // Save notes information
                MaterialsInformationNoteInformation materialsInformationNoteInformation = new MaterialsInformationNoteInformation(materialsInformationTDS);
                materialsInformationNoteInformation.Save(companyId);

                // Save costs information
                MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                materialsInformationCostHistoryInformation.Save(companyId);

                // Save costs exceptions information
                MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                materialsInformationCostHistoryExceptionsInformation.Save(companyId, materialId);

                // Save material information
                MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                materialsInformationBasicInformation.Save(companyId);

                DB.CommitTransaction();

                // Store datasets
                materialsInformationTDS.AcceptChanges();
                Session["materialsInformationTDS"] = materialsInformationTDS;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void Apply()
        {
            // Validate data
            bool validData = false;

            Page.Validate("generalInformation");
            if (Page.IsValid)
            {
                validData = true;

                // ... Validate notes tab
                if (ValidateNotesFooter())
                {
                    Page.Validate("notesDataAdd");
                    if (!Page.IsValid)
                    {
                        validData = false;
                    }
                }

                Page.Validate("notesDataEdit");
                if (!Page.IsValid)
                {
                    validData = false;
                }
            }

            // For valid data
            if (validData)
            {
                // Notes Gridview, if the gridview is edition mode
                if (grdNotes.EditIndex >= 0)
                {
                    grdNotes.UpdateRow(grdNotes.EditIndex, true);
                }

                // Costs Gridview, if the gridview is edition mode
                if (grdCosts.EditIndex >= 0)
                {
                    grdCosts.UpdateRow(grdCosts.EditIndex, true);
                }

                // Costs Exceptions Gridview, if the gridview is edition mode
                if (grdCostsExceptions.EditIndex >= 0)
                {
                    grdCostsExceptions.UpdateRow(grdCostsExceptions.EditIndex, true);
                }

                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int materialId = Int32.Parse(hdfCurrentMaterialId.Value);

                // ... Get basic material data
                string thickness = "";
                string size = "";
                string length = "";
                string description = "";
                string state = tbxState.Text.Trim();

                if (hdfProcess.Value == "Full Length Lining")
                {
                    thickness = ddlThickness.SelectedValue;
                    size = tbxSize.Text.Trim();
                    description = "Full Length Liner " + size;
                    if (thickness != "")
                    {
                        description = description + " / T." + thickness;
                    }
                }

                if (hdfProcess.Value == "Point Repairs")
                {
                    size = ddlPrSize.SelectedValue;
                    length = ddlPrLength.SelectedValue;
                    string formatedSize = size.Remove(size.Length - 1, 1);
                    formatedSize = formatedSize.PadLeft(3, '0');
                    string formatedLength = length.Remove(length.Length - 1, 1);
                    formatedLength = formatedLength.PadLeft(3, '0');
                    description = "PR-" + formatedSize + "-" + formatedLength + "-000";
                }

                if (hdfProcess.Value == "Junction Lining")
                {
                    size = "";
                    if (rbtnMiscSupplies.Checked) description = "Lateral / Misc Supplies";
                    if (rbtnCleanoutMaterial.Checked) description = "Lateral / Cleanout Material";
                    if (rbtnBackfillCleanout.Checked) description = "Lateral / Backfill - Cleanout";
                    if (rbtnOther.Checked)
                    {
                        size = tbxJLSize.Text.Trim();
                        description = "Junction Liner " + size;
                    }
                }

                // Update service data
                MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                materialsInformationBasicInformation.Update(materialId, description, size, length, thickness, hdfProcess.Value, state, false, companyId);

                // Store datasets
                Session["materialsInformationTDS"] = materialsInformationTDS;
                Session["materialsTDS"] = materialsTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_RESOURCES_EMPLOYEES_VIEW"]) && Convert.ToBoolean(Session["sgLFS_RESOURCES_EMPLOYEES_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["material_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in materials_delete.aspx");
                }

                // Tag page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfMaterialId.Value = Request.QueryString["material_id"];
                hdfActiveTab.Value = Request.QueryString["active_tab"];

                // Prepare initial data
                // ... for material title
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int materialId = Int32.Parse(hdfMaterialId.Value);

                MaterialsGateway materialsGateway = new MaterialsGateway();
                materialsGateway.LoadByMaterialId(materialId, companyId);
                lblTitleMaterial.Text = "Material: " + materialsGateway.GetDescription(materialId);

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

                    materialsInformationTDS = new MaterialsInformationTDS();
                    materialsTDS = new MaterialsTDS();

                    MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                    materialsInformationBasicInformation.LoadByMaterialId(materialId, companyId);

                    MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
                    materialsInformationCostHistoryInformation.LoadAllByMaterialId(materialId, companyId);

                    MaterialsInformationCostHistoryExceptionsInformation materialsInformationCostHistoryExceptionsInformation = new MaterialsInformationCostHistoryExceptionsInformation(materialsInformationTDS);
                    materialsInformationCostHistoryExceptionsInformation.LoadAllByMaterialId(materialId, companyId);

                    MaterialsInformationNoteInformation materialsInformationNoteInformation = new MaterialsInformationNoteInformation(materialsInformationTDS);
                    materialsInformationNoteInformation.LoadAllByMaterialId(materialId, companyId);

                    // Store dataset
                    Session["materialsInformationTDS"] = materialsInformationTDS;
                    Session["materialsTDS"] = materialsTDS;
                }

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

                    // Restore dataset
                    materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                    materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
                }
            }
            else
            {
                // Restore datasets
                materialsTDS = (MaterialsTDS)Session["materialsTDS"];
                materialsInformationTDS = (MaterialsInformationTDS)Session["materialsInformationTDS"];
            }
        }
        private void UpdateDatabase()
        {
            // Get ids
            int materialId = Int32.Parse(hdfMaterialId.Value);
            int companyId = Int32.Parse(hdfCompanyId.Value);

            // Delete
            DB.Open();
            DB.BeginTransaction();
            try
            {
                MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
                materialsInformationBasicInformation.DeleteDirect(materialId, companyId);

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void Delete()
        {
            int materialId = int.Parse(hdfMaterialId.Value);

            MaterialsInformationNoteInformation materialsInformationNoteInformation = new MaterialsInformationNoteInformation(materialsInformationTDS);
            materialsInformationNoteInformation.DeleteAll(materialId);

            MaterialsInformationCostHistoryInformation materialsInformationCostHistoryInformation = new MaterialsInformationCostHistoryInformation(materialsInformationTDS);
            materialsInformationCostHistoryInformation.DeleteAll(materialId);

            MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
            materialsInformationBasicInformation.Delete(materialId);

            // Store datasets
            Session["materialsInformationTDS"] = materialsInformationTDS;
        }
        private void UpdateState()
        {
            int materialId = Int32.Parse(hdfMaterialId.Value);
            MaterialsInformationBasicInformationGateway materialsInformationBasicInformationGateway = new MaterialsInformationBasicInformationGateway(materialsInformationTDS);

            // General Data
            string description = materialsInformationBasicInformationGateway.GetDescription(materialId);
            string size = materialsInformationBasicInformationGateway.GetSize(materialId);
            string length = materialsInformationBasicInformationGateway.GetLength(materialId);
            string thickness = materialsInformationBasicInformationGateway.GetThickness(materialId);
            string type = materialsInformationBasicInformationGateway.GetType(materialId);
            int companyId = Int32.Parse(hdfCompanyId.Value);

            // ... Get new values
            string newState = null;
            switch ((string)Request.QueryString["material_state"])
            {
                case "Available":
                    newState = "Not Available";
                    break;

                case "Not Available":
                    newState = "Available";
                    break;
            }

            // Update service data
            MaterialsInformationBasicInformation materialsInformationBasicInformation = new MaterialsInformationBasicInformation(materialsInformationTDS);
            materialsInformationBasicInformation.Update(materialId, description, size, length, thickness, type, newState, false, companyId);
        }