protected void grdOtherCosts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Other costs Gridview, if the gridview is edition mode
            if (grdOtherCosts.EditIndex >= 0)
            {
                grdOtherCosts.UpdateRow(grdOtherCosts.EditIndex, true);
            }

            // Delete Other Cost
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            int refId = (int)e.Keys["RefID"];

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

            // Store dataset
            otherCostsInformation = (ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable)model.Table;
            Session["otherCostsInformation"] = otherCostsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            StepOtherCostsInformationProcessGrid();
        }
        protected void grdOtherCosts_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("otherCostsEdit");

            // Validate costs
            int projectId = 0;

            foreach (int projectIdSelected in projectsSelected)
            {
                projectId = projectIdSelected;
            }

            ProjectGateway projectGateway = new ProjectGateway();

            if (Page.IsValid)
            {
                projectGateway.LoadByProjectId(projectId);

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

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

                string description = ((TextBox)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("tbxDescriptionEdit")).Text.Trim();
                string unitOfMeasurement = ((DropDownList)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementOthersEdit")).SelectedValue;
                double quantity = double.Parse(((TextBox)grdOtherCosts.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)grdOtherCosts.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)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("tbxCostUSDEdit")).Text.Trim());
                    totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                    totalCostUsd = Decimal.Round(totalCostUsd, 2);
                }

                string workFunctionConcat = "";
                string work_ = "";
                string function_ = "";
                workFunctionConcat = ((DropDownList)grdOtherCosts.Rows[e.RowIndex].Cells[0].FindControl("ddlWorkFunctionEdit")).SelectedValue;
                if (workFunctionConcat != "(Select)")
                {
                    string[] workFunction = workFunctionConcat.ToString().Trim().Split('.');
                    work_ = workFunction[0].Trim();
                    function_ = workFunction[1].Trim();
                }

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

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

                otherCostsInformation = (ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable)model.Table;
                Session["otherCostsInformation"] = otherCostsInformation;
                Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                StepOtherCostsInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP8 - OTHER COST INFORMATION
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP8 - OTHER COST INFORMATION - EVENTS
        //
        protected void grdOtherCosts_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Other costs Gridview, if the gridview is edition mode
                    if (grdOtherCosts.EditIndex >= 0)
                    {
                        grdOtherCosts.UpdateRow(grdOtherCosts.EditIndex, true);
                    }

                    // Validate general data
                    Page.Validate("otherCostsNew");

                    // Validate costs
                    int projectId = 0;

                    foreach (int projectIdSelected in projectsSelected)
                    {
                        projectId = projectIdSelected;
                    }

                    ProjectGateway projectGateway = new ProjectGateway();

                    if (Page.IsValid)
                    {
                        projectGateway.LoadByProjectId(projectId);

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

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        string description = ((TextBox)grdOtherCosts.FooterRow.FindControl("tbxDescriptionNew")).Text.Trim();
                        string unitOfMeasurement = ((DropDownList)grdOtherCosts.FooterRow.FindControl("ddlUnitOfMeasurementOthersNew")).SelectedValue;
                        double quantity = double.Parse(((TextBox)grdOtherCosts.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)grdOtherCosts.FooterRow.FindControl("tbxCostCADNew")).Text.Trim());
                            totalCostCad = (costCad * decimal.Parse(quantity.ToString()));
                            totalCostCad = Decimal.Round(totalCostCad, 2);
                        }
                        else
                        {
                            costUsd = Decimal.Parse(((TextBox)grdOtherCosts.FooterRow.FindControl("tbxCostUSDNew")).Text.Trim());
                            totalCostUsd = (costUsd * decimal.Parse(quantity.ToString()));
                            totalCostUsd = Decimal.Round(totalCostUsd, 2);
                        }

                        string workFunctionConcat = "";
                        string work_ = "";
                        string function_ = "";
                        workFunctionConcat = ((DropDownList)grdOtherCosts.FooterRow.FindControl("ddlWorkFunctionNew")).SelectedValue;
                        if (workFunctionConcat != "(Select)")
                        {
                            string[] workFunction = workFunctionConcat.ToString().Trim().Split('.');
                            work_ = workFunction[0].Trim();
                            function_ = workFunction[1].Trim();
                        }

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

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

                        Session.Remove("otherCostsInformationDummy");
                        otherCostsInformation = (ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable)model.Table;
                        Session["otherCostsInformation"] = otherCostsInformation;
                        Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                        grdOtherCosts.DataBind();

                        StepOtherCostsInformationProcessGrid();
                    }
                    break;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP8 - OTHER COST INFORMATION - METHODS
        //
        private void StepOtherCostInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Please provide other costs to cost sheet";

            int projectId = 0;

            foreach (int projectIdSelected in projectsSelected)
            {
                projectId = projectIdSelected;
            }

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

            // Store tables
            Session.Remove("otherCostsInformationDummy");
            otherCostsInformation = (ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable)model.Table;
            Session["otherCostsInformation"] = otherCostsInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            // Validate grid columns
            ProjectGateway projectGateway = new ProjectGateway();
            projectGateway.LoadByProjectId(projectId);

            if (projectGateway.GetCountryID(projectId) == 1) //Canada
            {
                // Other costs
                grdOtherCosts.Columns[9].Visible = true;
                grdOtherCosts.Columns[10].Visible = true;
                grdOtherCosts.Columns[11].Visible = false;
                grdOtherCosts.Columns[12].Visible = false;

                lblOtherCostsTotalCosts.Text = "Total Cost (CAD) : ";
                tbxOtherCostsTotalCostsCAD.Visible = true;
                tbxOtherCostsTotalCostsUSD.Visible = false;
            }
            else
            {
                // Other costs
                grdOtherCosts.Columns[9].Visible = false;
                grdOtherCosts.Columns[10].Visible = false;
                grdOtherCosts.Columns[11].Visible = true;
                grdOtherCosts.Columns[12].Visible = true;

                lblOtherCostsTotalCosts.Text = "Total Cost (USD) : ";
                tbxOtherCostsTotalCostsCAD.Visible = false;
                tbxOtherCostsTotalCostsUSD.Visible = true;
            }

            grdOtherCosts.DataBind();
            StepOtherCostsInformationProcessGrid();
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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["client_id"] == null) || ((string)Request.QueryString["project_id"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in project_combined_costing_sheets_add.aspx");
                }

                if ((string)Request.QueryString["project_id"] == "0")
                {
                    if (Session["projectsSelected"] != null)
                    {
                        projectsSelected = (ArrayList)Session["projectsSelected"];
                    }
                    else
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in project_combined_costing_sheets_add.aspx");
                    }
                }
                else
                {
                    projectsSelected = new ArrayList();
                    projectsSelected.Add(int.Parse((string)Request.QueryString["project_id"]));
                    Session["projectsSelected"] = projectsSelected;
                }

                // 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");

                // ... Initialize tables
                projectCostingSheetAddTDS = new ProjectCostingSheetAddTDS();
                labourHoursInformation = new ProjectCostingSheetAddTDS.CombinedLabourHoursInformationDataTable();
                unitsInformation = new ProjectCostingSheetAddTDS.CombinedUnitsInformationDataTable();
                subcontractorsInformation = new ProjectCostingSheetAddTDS.CombinedSubcontractorsInformationDataTable();
                materialsInformation = new ProjectCostingSheetAddTDS.CombinedMaterialsInformationDataTable();
                otherCostsInformation = new ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable();
                revenueInformation = new ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable();
                templateInformation = new ProjectCostingSheetAddTDS.TemplateInformationDataTable();

                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";

                // ... 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;

                // StepGeneralInformation
                wzProjectCostinsSheetsAdd.ActiveStepIndex = 0;
            }
            else
            {
                // Restore tables
                projectCostingSheetAddTDS = (ProjectCostingSheetAddTDS)Session["projectCostingSheetAddTDS"];
                labourHoursInformation = (ProjectCostingSheetAddTDS.CombinedLabourHoursInformationDataTable)Session["labourHoursInformation"];
                unitsInformation = (ProjectCostingSheetAddTDS.CombinedUnitsInformationDataTable)Session["unitsInformation"];
                subcontractorsInformation = (ProjectCostingSheetAddTDS.CombinedSubcontractorsInformationDataTable)Session["subcontractorsInformation"];
                materialsInformation = (ProjectCostingSheetAddTDS.CombinedMaterialsInformationDataTable)Session["materialsInformation"];
                otherCostsInformation = (ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable)Session["otherCostsInformation"];
                revenueInformation = (ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable)Session["revenueInformation"];
                templateInformation = (ProjectCostingSheetAddTDS.TemplateInformationDataTable)Session["templateInformation"];
                projectsSelected = (ArrayList)Session["projectsSelected"];

                foreach (int projectIdSelected in projectsSelected)
                {
                    ProjectGateway projectGateway = new ProjectGateway();
                    projectGateway.LoadByProjectId(projectIdSelected);

                    string name = projectGateway.GetName(projectIdSelected);

                    projectList.Add(new ListItem(name, projectIdSelected.ToString()));
                }
            }
        }
        protected void OtherCostsInformationEmptyFix(GridView grdView)
        {
            if (grdView.Rows.Count == 0)
            {
                ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable dt = new ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable();
                dt.AddCombinedOtherCostsInformationRow(0, 0, "", "", "", "", 0, 0, 0, 0, 0, false, 3, false, "", DateTime.Now, DateTime.Now, 0, 0, "");
                Session["otherCostsInformationDummy"] = dt;

                grdView.DataBind();
            }

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

            if (otherCostsInformation == null)
            {
                otherCostsInformation = (ProjectCostingSheetAddTDS.CombinedOtherCostsInformationDataTable)Session["otherCostsInformation"];
            }

            return otherCostsInformation;
        }