protected void grdRevenue_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Revenue Gridview, if the gridview is edition mode
            if (grdRevenue.EditIndex >= 0)
            {
                grdRevenue.UpdateRow(grdRevenue.EditIndex, true);
            }

            // Delete revenue
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            int refIdRevenue = (int)e.Keys["RefIDRevenue"];

            ProjectCombinedCostingSheetAddRevenueInformation model = new ProjectCombinedCostingSheetAddRevenueInformation(projectCostingSheetAddTDS);
            model.Delete(costingSheetId, refIdRevenue);

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

            StepRevenueInformationProcessGrid();
        }
        protected void grdRevenue_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("revenueEdit");

            if (Page.IsValid)
            {
                int costingSheetId = (int)e.Keys["CostingSheetID"];
                int refIdRevenue = (int)e.Keys["RefIDRevenue"];

                int companyId = Int32.Parse(hdfCompanyId.Value);
                string comment = ((TextBox)grdRevenue.Rows[e.RowIndex].Cells[0].FindControl("tbxCommentEdit")).Text;

                decimal revenue = 0.0M;
                revenue = Decimal.Parse(((TextBox)grdRevenue.Rows[e.RowIndex].Cells[0].FindControl("tbxRevenueEdit")).Text.Trim());
                revenue = Decimal.Round(revenue, 2);

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

                // Update data
                ProjectCombinedCostingSheetAddRevenueInformation model = new ProjectCombinedCostingSheetAddRevenueInformation(projectCostingSheetAddTDS);
                model.Update(costingSheetId, refIdRevenue, revenue, false, companyId, startDate, endDate, comment);

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

                StepRevenueInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP9 - REVENUE INFORMATION
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP9 - REVENUE INFORMATION - EVENTS
        //
        protected void grdRevenue_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // Revenue Gridview, if the gridview is edition mode
                    if (grdRevenue.EditIndex >= 0)
                    {
                        grdRevenue.UpdateRow(grdRevenue.EditIndex, true);
                    }

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

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        string comment = ""; if (((TextBox)grdRevenue.FooterRow.FindControl("tbxCommentNew")).Text.Trim() != "") comment = ((TextBox)grdRevenue.FooterRow.FindControl("tbxCommentNew")).Text.Trim();

                        decimal revenue = 0.0M;
                        revenue = Decimal.Parse(((TextBox)grdRevenue.FooterRow.FindControl("tbxRevenueNew")).Text.Trim());
                        revenue = Decimal.Round(revenue, 2);

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

                        ProjectCombinedCostingSheetAddRevenueInformation model = new ProjectCombinedCostingSheetAddRevenueInformation(projectCostingSheetAddTDS);
                        model.Insert(0, revenue, false, companyId, startDate, endDate, comment, 1);

                        Session.Remove("revenueInformationDummy");
                        revenueInformation = (ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable)model.Table;
                        Session["revenueInformation"] = revenueInformation;
                        Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                        grdRevenue.DataBind();

                        StepRevenueInformationProcessGrid();
                    }
                    break;
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP9 - REVENUE INFORMATION - METHODS
        //
        private void StepRevenueInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Please verify Revenue information";

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

            if (projectCostingSheetAddTDS.CombinedRevenueInformation.Rows.Count <= 0)
            {
                model.Load(projectsSelected, tkrdpFrom.SelectedDate.Value, tkrdpTo.SelectedDate.Value, int.Parse(hdfCompanyId.Value));
            }

            // Store tables
            Session.Remove("revenueInformationDummy");
            revenueInformation = (ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable)model.Table;
            Session["revenueInformation"] = revenueInformation;
            Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

            grdRevenue.DataBind();
            StepRevenueInformationProcessGrid();

            GetRevenueSummary();
        }
        protected void RevenueInformationEmptyFix(GridView grdView)
        {
            if (grdView.Rows.Count == 0)
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable dt = new ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable();
                dt.AddCombinedRevenueInformationRow(0, 0, 0, "", companyId, DateTime.Now, DateTime.Now, false, false, false, 0, 0, "");
                Session["revenueInformationDummy"] = dt;

                grdView.DataBind();
            }

            // Normally executes at all postbacks
            if (grdView.Rows.Count == 1)
            {
                ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable dt = (ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable)Session["revenueInformationDummy"];
                if (dt != null)
                {
                    // Hide row
                    grdView.Rows[0].Visible = false;
                    grdView.Rows[0].Controls.Clear();
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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()));
                }
            }
        }
        public ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable GetRevenueInformation()
        {
            revenueInformation = (ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable)Session["revenueInformationDummy"];

            if (revenueInformation == null)
            {
                revenueInformation = (ProjectCostingSheetAddTDS.CombinedRevenueInformationDataTable)Session["revenueInformation"];
            }

            return revenueInformation;
        }