// ////////////////////////////////////////////////////////////////////////
        // 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"];
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // STEP7 - OTHER CATEGORY INFORMATION - METHODS
        //
        private void StepOtherCategoryInformationIn()
        {
            // Set instruction
            Label instruction = (Label)this.Master.FindControl("lblInstruction");
            instruction.Text = "Please verify Other Category information";

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

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

            // Store tables
            Session.Remove("otherCategoryInformationDummy");
            otherCategoryInformation = (ProjectCostingSheetAddTDS.OtherCategoryInformationDataTable)model.Table;
            Session["otherCategoryInformation"] = otherCategoryInformation;
            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
            {
                // OtherCategory Grid
                lblOtherCategoryTotalCosts.Text = "Total Cost (CAD) : ";
                tbxOtherCategoryTotalCostsCAD.Visible = true;
                tbxOtherCategoryTotalCostsUSD.Visible = false;
            }
            else
            {
                // OtherCategory Grid
                lblOtherCategoryTotalCosts.Text = "Total Cost (USD) : ";
                tbxOtherCategoryTotalCostsCAD.Visible = false;
                tbxOtherCategoryTotalCostsUSD.Visible = true;
            }

            grdOtherCategory.DataBind();
            StepOtherCategoryInformationProcessGrid();
        }
        protected void OtherCategoryInformationEmptyFix(GridView grdView)
        {
            if (grdView.Rows.Count == 0)
            {
                ProjectCostingSheetAddTDS.OtherCategoryInformationDataTable dt = new ProjectCostingSheetAddTDS.OtherCategoryInformationDataTable();
                dt.AddOtherCategoryInformationRow(0, "", 0, 0, false, 0, false, DateTime.Now, DateTime.Now, false, "");
                Session["otherCategoryInformationDummy"] = dt;

                grdView.DataBind();
            }

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

            if (otherCategoryInformation == null)
            {
                otherCategoryInformation = (ProjectCostingSheetAddTDS.OtherCategoryInformationDataTable)Session["otherCategoryInformation"];
            }

            return otherCategoryInformation;
        }
        protected void grdOtherCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Validate general data
            Page.Validate("bondingEdit");

            if (Page.IsValid)
            {
                int costingSheetId = (int)e.Keys["CostingSheetID"];
                string category = (string)e.Keys["Category"];
                int refId = (int)e.Keys["RefID"];

                int companyId = Int32.Parse(hdfCompanyId.Value);
                decimal rate = Decimal.Parse(((TextBox)grdOtherCategory.Rows[e.RowIndex].Cells[0].FindControl("tbxRateEdit")).Text.Trim());
                DateTime startDate = ((RadDatePicker)grdOtherCategory.Rows[e.RowIndex].Cells[0].FindControl("tkrdpStartDateEdit")).SelectedDate.Value;
                DateTime endDate = ((RadDatePicker)grdOtherCategory.Rows[e.RowIndex].Cells[0].FindControl("tkrdpEndDateEdit")).SelectedDate.Value;

                // Update data
                ProjectCostingSheetAddOtherCategoryInformation model = new ProjectCostingSheetAddOtherCategoryInformation(projectCostingSheetAddTDS);
                model.Update(costingSheetId, category, refId, rate, false, companyId, startDate, endDate);

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

                StepOtherCategoryInformationProcessGrid();
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdOtherCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // OtherCategory Gridview, if the gridview is edition mode
            if (grdOtherCategory.EditIndex >= 0)
            {
                grdOtherCategory.UpdateRow(grdOtherCategory.EditIndex, true);
            }

            // Delete bonding
            int costingSheetId = (int)e.Keys["CostingSheetID"];
            string category = (string)e.Keys["Category"];
            int refId = (int)e.Keys["RefID"];

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

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

            StepOtherCategoryInformationProcessGrid();
        }
        // /////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        // STEP7 - OTHER CATEGORY INFORMATION
        //
        // ////////////////////////////////////////////////////////////////////////
        // STEP7 - OTHER CATEGORY INFORMATION - EVENTS
        //
        protected void grdOtherCategory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Add":
                    // OtherCategory Gridview, if the gridview is edition mode
                    if (grdOtherCategory.EditIndex >= 0)
                    {
                        grdOtherCategory.UpdateRow(grdOtherCategory.EditIndex, true);
                    }

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

                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        string category = ((DropDownList)grdOtherCategory.FooterRow.FindControl("ddlOtherCategoryNew")).SelectedItem.Text;
                        decimal rate = decimal.Parse(((TextBox)grdOtherCategory.FooterRow.FindControl("tbxRateNew")).Text.Trim());

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

                        ProjectCostingSheetAddOtherCategoryInformation model = new ProjectCostingSheetAddOtherCategoryInformation(projectCostingSheetAddTDS);
                        model.Insert(0, category, rate, false, companyId, startDate, endDate);

                        Session.Remove("otherCategoryInformationDummy");
                        otherCategoryInformation = (ProjectCostingSheetAddTDS.OtherCategoryInformationDataTable)model.Table;
                        Session["otherCategoryInformation"] = otherCategoryInformation;
                        Session["projectCostingSheetAddTDS"] = projectCostingSheetAddTDS;

                        grdOtherCategory.DataBind();

                        StepOtherCategoryInformationProcessGrid();
                    }
                    break;
            }
        }