// /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP6 - MATERIAL INFORMATION
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP6 - MATERIAL INFORMATION - EVENTS
        //
        protected void grdMaterials_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Materials Gridview, if the gridview is edition mode
                    if (grdMaterials.EditIndex >= 0)
                    {
                        grdMaterials.UpdateRow(grdMaterials.EditIndex, true);
                    }

                    // Validate general data
                    Page.Validate("materialsNew");
                    ProjectGateway projectGateway = new ProjectGateway();
                    int projectId = Int32.Parse(hdfProjectId.Value);

                    if (Page.IsValid)
                    {
                        // Validate costs
                        projectGateway.LoadByProjectId(projectId);

                        if (projectGateway.GetCountryID(projectId) == 1) //Canada
                        {
                            Page.Validate("materialsCadNew");
                        }
                        else
                        {
                            Page.Validate("materialsUsdNew");
                        }
                    }

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        string description = ((DropDownList)grdMaterials.FooterRow.FindControl("ddlDescriptionNew")).SelectedItem.Text;
                        int materialId = Int32.Parse(((DropDownList)grdMaterials.FooterRow.FindControl("ddlDescriptionNew")).SelectedValue);
                        string unitOfMeasurement = ((DropDownList)grdMaterials.FooterRow.FindControl("ddlUnitOfMeasurementMaterialsNew")).SelectedValue;
                        double quantity = double.Parse(((TextBox)grdMaterials.FooterRow.FindControl("tbxQuantityNew")).Text.Trim());

                        decimal costCad = 0.0M;
                        decimal totalCostCad = 0.0M;
                        decimal costUsd = 0.0M;
                        decimal totalCostUsd = 0.0M;

                        if (projectGateway.GetCountryID(projectId) == 1) //Canada
                        {
                            costCad = Decimal.Parse(((TextBox)grdMaterials.FooterRow.FindControl("tbxCostCADNew")).Text.Trim());
                            totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                            totalCostCad = Decimal.Round(totalCostCad, 2);
                        }
                        else
                        {
                            costUsd = Decimal.Parse(((TextBox)grdMaterials.FooterRow.FindControl("tbxCostUSDNew")).Text.Trim());
                            totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                            totalCostUsd = Decimal.Round(totalCostUsd, 2);
                        }

                        LFSLive.DA.Resources.Materials.MaterialsGateway m = new LiquiForce.LFSLive.DA.Resources.Materials.MaterialsGateway();
                        m.LoadByMaterialId(materialId, companyId);
                        string process = m.GetType(materialId);

                        DateTime startDate = ((RadDatePicker)grdMaterials.FooterRow.FindControl("tkrdpStartDateNew")).SelectedDate.Value;
                        DateTime endDate = ((RadDatePicker)grdMaterials.FooterRow.FindControl("tkrdpEndDateNew")).SelectedDate.Value;

                        string workFunctionConcat = "Full Length . Install";
                        string function_ = "Install";

                        ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
                        model.Insert(0, materialId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, process, description, startDate, endDate, function_, workFunctionConcat);

                        // Store tables
                        Session.Remove("materialsInformationDummy");
                        materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
                        Session["materialsInformation"] = materialsInformation;
                        Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                        grdMaterials.DataBind();

                        StepMaterialInformationProcessGrid();
                    }
                    break;
            }
        }
        protected void grdMaterials_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Materials Gridview, if the gridview is edition mode
            if (grdMaterials.EditIndex >= 0)
            {
                grdMaterials.UpdateRow(grdMaterials.EditIndex, true);
            }

            // Delete material
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            int materialId = (int)e.Keys["MaterialID"];
            int refId = (int)e.Keys["RefID"];

            ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
            model.Delete(costingSheetId, materialId, refId);

            // Store tables
            Session.Remove("materialsInformationDummy");
            materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
            Session["materialsInformation"] = materialsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            StepMaterialInformationProcessGrid();
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP6 - MATERIAL INFORMATION - METHODS
        //
        private void StepMaterialInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Please verify Material information";

            // Load
            ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);

            if (projectCostingSheetAddTDS.MaterialsInformation.Rows.Count <= 0)
            {
                ArrayList works = new ArrayList();
                if (cbxFullLengthLiningData.Checked) works.Add("Full Length Lining");
                if (cbxManholeRehabData.Checked) works.Add("Manhole Rehab");
                if (cbxPointRepairData.Checked) works.Add("Point Repairs");
                if (cbxJunctionLiningData.Checked) works.Add("Junction Lining");

                model.Load(works, int.Parse(hdfProjectId.Value), tkrdpFrom.SelectedDate.Value, tkrdpTo.SelectedDate.Value, int.Parse(hdfCompanyId.Value));
            }

            // Store tables
            Session.Remove("materialsInformationDummy");
            materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
            Session["materialsInformation"] = materialsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            // Validate grid columns
            int projectId = Int32.Parse(hdfProjectId.Value);
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(projectId);

            if (projectGateway.GetCountryID(projectId) == 1) //Canada
            {
                // Materials grid
                grdMaterials.Columns[9].Visible = true;
                grdMaterials.Columns[10].Visible = true;
                grdMaterials.Columns[11].Visible = false;
                grdMaterials.Columns[12].Visible = false;

                lblMaterialsTotalCosts.Text = "Total Cost (CAD) : ";
                tbxMaterialsTotalCostsCAD.Visible = true;
                tbxMaterialsTotalCostsUSD.Visible = false;
            }
            else
            {
                // Materials grid
                grdMaterials.Columns[9].Visible = false;
                grdMaterials.Columns[10].Visible = false;
                grdMaterials.Columns[11].Visible = true;
                grdMaterials.Columns[12].Visible = true;

                lblMaterialsTotalCosts.Text = "Total Cost (USD) : ";
                tbxMaterialsTotalCostsCAD.Visible = false;
                tbxMaterialsTotalCostsUSD.Visible = true;
            }

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

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

                // Tag Page
                TagPage();

                // Initialize viewstate variables
                ViewState["StepFrom"] = "Out";
                Session.Remove("labourHoursInformationDummy");
                Session.Remove("labourHoursInformation");
                Session.Remove("unitsInformationDummy");
                Session.Remove("unitsInformation");
                Session.Remove("subcontractorsInformationDummy");
                Session.Remove("subcontractorsInformation");
                Session.Remove("materialsInformationDummy");
                Session.Remove("materialsInformation");
                Session.Remove("otherCostsInformationDummy");
                Session.Remove("otherCostsInformation");
                Session.Remove("revenueInformationDummy");
                Session.Remove("revenueInformation");
                Session.Remove("templateInformationDummy");
                Session.Remove("templateInformation");

                Session.Remove("hotelsInformationDummy");
                Session.Remove("hotelsInformation");

                Session.Remove("bondingsInformationDummy");
                Session.Remove("bondingsInformation");

                Session.Remove("insurancesInformationDummy");
                Session.Remove("insurancesInformation");

                Session.Remove("otherCategoryInformationDummy");
                Session.Remove("otherCategoryInformation");

                // ... Initialize tables
                projectCostingSheetAddTDS = new ProjectCostingSheetAddTDS();
                labourHoursInformation = new ProjectCostingSheetAddTDS.LabourHoursInformationDataTable();
                unitsInformation = new ProjectCostingSheetAddTDS.UnitsInformationDataTable();
                subcontractorsInformation = new ProjectCostingSheetAddTDS.SubcontractorsInformationDataTable();
                materialsInformation = new ProjectCostingSheetAddTDS.MaterialsInformationDataTable();
                otherCostsInformation = new ProjectCostingSheetAddTDS.OtherCostsInformationDataTable();
                revenueInformation = new ProjectCostingSheetAddTDS.RevenueInformationDataTable();
                templateInformation = new ProjectCostingSheetAddTDS.TemplateInformationDataTable();
                hotelsInformation = new ProjectCostingSheetAddTDS.HotelsInformationDataTable();
                bondingsInformation = new ProjectCostingSheetAddTDS.BondingsInformationDataTable();
                insurancesInformation = new ProjectCostingSheetAddTDS.InsurancesInformationDataTable();
                otherCategoryInformation = new ProjectCostingSheetAddTDS.OtherCategoryInformationDataTable();

                tbxTeamMembersTotalCostCAD.Text = "0";
                tbxTeamMembersTotalCostUSD.Text = "0";
                tbxUnitsTotalCostsCAD.Text = "0";
                tbxUnitsTotalCostsUSD.Text = "0";
                tbxSubcontractorsTotalCostsCAD.Text = "0";
                tbxSubcontractorsTotalCostsUSD.Text = "0";
                tbxMaterialsTotalCostsCAD.Text = "0";
                tbxMaterialsTotalCostsUSD.Text = "0";
                tbxOtherCostsTotalCostsCAD.Text = "0";
                tbxOtherCostsTotalCostsUSD.Text = "0";
                tbxRevenueTotal.Text = "0";
                tbxGradRevenue.Text = "0";
                tbxGrandProfit.Text = "0";
                tbxGrandGrossMargin.Text = "0";

                tbxHotelsTotalCostsCAD.Text = "0";
                tbxHotelsTotalCostsUSD.Text = "0";

                tbxBondingsTotalCostsCAD.Text = "0";
                tbxBondingsTotalCostsUSD.Text = "0";

                tbxInsurancesTotalCostsCAD.Text = "0";
                tbxInsurancesTotalCostsUSD.Text = "0";

                tbxOtherCategoryTotalCostsCAD.Text = "0";
                tbxOtherCategoryTotalCostsUSD.Text = "0";

                // ... Store tables
                Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;
                Session["labourHoursInformation"] = labourHoursInformation;
                Session["unitsInformation"] = unitsInformation;
                Session["subcontractorsInformation"] = subcontractorsInformation;
                Session["materialsInformation"] = materialsInformation;
                Session["otherCostsInformation"] = otherCostsInformation;
                Session["revenueInformation"] = revenueInformation;
                Session["templateInformation"] = templateInformation;

                Session["hotelsInformation"] = hotelsInformation;
                Session["bondingsInformation"] = bondingsInformation;
                Session["insurancesInformation"] = insurancesInformation;
                Session["otherCategoryInformation"] = otherCategoryInformation;

                // StepGeneralInformation
                wzProjectCostinsSheetsAdd.ActiveStepIndex = 0;
            }
            else
            {
                // Restore tables
                projectCostingSheetAddTDS = (ProjectCostingSheetAddTDS)Session["projectCostingSheetAddTDS"];
                labourHoursInformation = (ProjectCostingSheetAddTDS.LabourHoursInformationDataTable)Session["labourHoursInformation"];
                unitsInformation = (ProjectCostingSheetAddTDS.UnitsInformationDataTable)Session["unitsInformation"];
                subcontractorsInformation = (ProjectCostingSheetAddTDS.SubcontractorsInformationDataTable)Session["subcontractorsInformation"];
                materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)Session["materialsInformation"];
                otherCostsInformation = (ProjectCostingSheetAddTDS.OtherCostsInformationDataTable)Session["otherCostsInformation"];
                revenueInformation = (ProjectCostingSheetAddTDS.RevenueInformationDataTable)Session["revenueInformation"];
                templateInformation = (ProjectCostingSheetAddTDS.TemplateInformationDataTable)Session["templateInformation"];

                hotelsInformation = (ProjectCostingSheetAddTDS.HotelsInformationDataTable)Session["hotelsInformation"];
                bondingsInformation = (ProjectCostingSheetAddTDS.BondingsInformationDataTable)Session["bondingsInformation"];
                insurancesInformation = (ProjectCostingSheetAddTDS.InsurancesInformationDataTable)Session["insurancesInformation"];
                otherCategoryInformation = (ProjectCostingSheetAddTDS.OtherCategoryInformationDataTable)Session["otherCategoryInformation"];
            }
        }
        protected void MaterialsInformationEmptyFix(GridView grdView)
        {
            if (grdView.Rows.Count == 0)
            {
                ProjectCostingSheetAddTDS.MaterialsInformationDataTable dt = new ProjectCostingSheetAddTDS.MaterialsInformationDataTable();
                dt.AddMaterialsInformationRow(0, 0, 0, 0, 0, 0, 0, 0, false, 3, false, "", "", "", DateTime.Now, DateTime.Now, false, "", "", "", "", "", 0, 0);
                Session["materialsInformationDummy"] = dt;

                grdView.DataBind();
            }

            // Normally executes at all postbacks
            if (grdView.Rows.Count == 1)
            {
                ProjectCostingSheetAddTDS.MaterialsInformationDataTable dt = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)Session["materialsInformationDummy"];
                if (dt != null)
                {
                    // Hide row
                    grdView.Rows[0].Visible = false;
                    grdView.Rows[0].Controls.Clear();
                }
            }
        }
        public ProjectCostingSheetAddTDS.MaterialsInformationDataTable GetMaterialsInformation()
        {
            materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)Session["materialsInformationDummy"];

            if (materialsInformation == null)
            {
                materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)Session["materialsInformation"];
            }

            return materialsInformation;
        }
        protected void grdMaterials_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("materialsEdit");
            int projectId = Int32.Parse(hdfProjectId.Value);
            ProjectGateway projectGateway = new ProjectGateway();

            if (Page.IsValid)
            {
                // Validate costs
                projectGateway.LoadByProjectId(projectId);

                if (projectGateway.GetCountryID(projectId) == 1) //Canada
                {
                    Page.Validate("materialsCadEdit");
                }
                else
                {
                    Page.Validate("materialsUsdEdit");
                }
            }

            if (Page.IsValid)
            {
                int costingSheetId = (int)e.Keys["CostingSheetID"];
                int materialId = (int)e.Keys["MaterialID"];
                int refId = (int)e.Keys["RefID"];
                int companyId = Int32.Parse(hdfCompanyId.Value);

                string unitOfMeasurement = ((DropDownList)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementMaterialsEdit")).SelectedValue;
                double quantity = double.Parse(((TextBox)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("tbxQuantityEdit")).Text.Trim());

                decimal costCad = 0.0M;
                decimal totalCostCad = 0.0M;
                decimal costUsd = 0.0M;
                decimal totalCostUsd = 0.0M;

                if (projectGateway.GetCountryID(projectId) == 1) //Canada
                {
                    costCad = Decimal.Parse(((TextBox)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("tbxCostCADEdit")).Text.Trim());
                    totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                    totalCostCad = Decimal.Round(totalCostCad, 2);
                }
                else
                {
                    costUsd = Decimal.Parse(((TextBox)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("tbxCostUSDEdit")).Text.Trim());
                    totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                    totalCostUsd = Decimal.Round(totalCostUsd, 2);
                }

                LFSLive.DA.Resources.Materials.MaterialsGateway m = new LiquiForce.LFSLive.DA.Resources.Materials.MaterialsGateway();
                m.LoadByMaterialId(materialId, companyId);
                string process = m.GetType(materialId);
                string description = m.GetDescription(materialId);

                DateTime startDate = ((RadDatePicker)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("tkrdpStartDateEdit")).SelectedDate.Value;
                DateTime endDate = ((RadDatePicker)grdMaterials.Rows[e.RowIndex].Cells[0].FindControl("tkrdpEndDateEdit")).SelectedDate.Value;

                string workFunctionConcat = "Full Length . Install";
                string function_ = "Install";

                // Update data
                ProjectCostingSheetAddMaterialsInformation model = new ProjectCostingSheetAddMaterialsInformation(projectCostingSheetAddTDS);
                model.Update(costingSheetId, materialId, refId, unitOfMeasurement, quantity, costCad, totalCostCad, costUsd, totalCostUsd, false, companyId, process, description, startDate, endDate, function_, workFunctionConcat);

                // Store tables
                Session.Remove("materialsInformationDummy");
                materialsInformation = (ProjectCostingSheetAddTDS.MaterialsInformationDataTable)model.Table;
                Session["materialsInformation"] = materialsInformation;
                Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                StepMaterialInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }