private void grdSubcontractorCostsBySubcontractorDetailAdd()
        {
            Page.Validate("generalData");
            if (Page.IsValid)
            {
                if (FooterValidate())
                {
                    Page.Validate("DataNew");
                    if (Page.IsValid)
                    {
                        int companyId = Int32.Parse(hdfCompanyId.Value);
                        DateTime date = (DateTime)((RadDatePicker)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("tkrdpDateFooter")).SelectedDate;

                        int subcontractorId = Int32.Parse(ddlSubcontractor.SelectedValue);
                        string name = ddlSubcontractor.SelectedItem.Text;

                        int clientId = Int32.Parse(((DropDownList)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("ddlClientFooter")).SelectedValue);
                        string client = ((DropDownList)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("ddlClientFooter")).SelectedItem.Text;

                        int projectId = Int32.Parse(((DropDownList)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("ddlProjectFooter")).SelectedValue);
                        string project = ((DropDownList)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("ddlProjectFooter")).SelectedItem.Text;

                        string quantityString = ((TextBox)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("tbxQuantityFooter")).Text;
                        decimal quantity1 = decimal.Round(decimal.Parse(quantityString), 1);
                        double quantity = double.Parse(quantity1.ToString());
                        decimal rate = decimal.Round(decimal.Parse(((TextBox)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("tbxRateFooter")).Text), 2);
                        decimal total = decimal.Round(decimal.Parse(((TextBox)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("tbxTotalFooter")).Text), 2);
                        string comment = ((TextBox)grdSubcontractorCostsBySubcontractor.FooterRow.FindControl("tbxCommentFooter")).Text;
                        bool deleted = false;
                        bool inDatabase = false;
                        decimal rateCad = 0;
                        decimal totalCad = 0;
                        decimal rateUsd = 0;
                        decimal totalUsd = 0;
                        ProjectGateway projectGateway = new ProjectGateway();
                        projectGateway.LoadByProjectId(projectId);

                        if (projectGateway.GetCountryID(projectId) == 1) //Canada
                        {
                            rateCad = rate;
                            totalCad = total;
                            rateUsd = rate;
                            totalUsd = total;
                        }
                        else
                        {
                            rateCad = rate;
                            totalCad = total;
                            rateUsd = rate;
                            totalUsd = total;
                        }

                        // Insert Data
                        ActualCostsAddSubcontractorCosts actualCostsAddSubcontractorCosts = new ActualCostsAddSubcontractorCosts(actualCostsAddTDS);
                        actualCostsAddSubcontractorCosts.Insert(projectId, subcontractorId, date, quantity, rateCad, totalCad, rateUsd, totalUsd, comment, deleted, companyId, inDatabase, name, client, project, clientId);

                        // Store dataset
                        Session["actualCostsAddTDS"] = actualCostsAddTDS;
                        Session.Remove("subcontractorCostsBySubcontractorDummy");
                        subcontractorCosts = actualCostsAddTDS.SubcontractorCosts;
                        Session["subcontractorCosts"] = actualCostsAddTDS.SubcontractorCosts;

                        grdSubcontractorCostsBySubcontractor.DataBind();
                        grdSubcontractorCostsBySubcontractor.PageIndex = grdSubcontractorCostsBySubcontractor.PageCount - 1;
                    }
                }
            }
        }
        protected void AddSubcontractorsNewEmptyFix(GridView grdSubcontractorCostsBySubcontractor)
        {
            if (grdSubcontractorCostsBySubcontractor.Rows.Count == 0)
            {
                int companyId = Int32.Parse(hdfCompanyId.Value);
                ActualCostsAddTDS.SubcontractorCostsDataTable dt = new ActualCostsAddTDS.SubcontractorCostsDataTable();
                dt.AddSubcontractorCostsRow(-1, -1, -1, DateTime.Now, -1, -1, -1, -1, -1, "", false, companyId, false, "", 0, 0, "", "", 1);
                Session["subcontractorCostsBySubcontractorDummy"] = dt;

                grdSubcontractorCostsBySubcontractor.DataBind();
            }

            // normally executes at all postbacks
            if (grdSubcontractorCostsBySubcontractor.Rows.Count == 1)
            {
                ActualCostsAddTDS.SubcontractorCostsDataTable dt = (ActualCostsAddTDS.SubcontractorCostsDataTable)Session["subcontractorCostsBySubcontractorDummy"];
                if (dt != null)
                {
                    grdSubcontractorCostsBySubcontractor.Rows[0].Visible = false;
                    grdSubcontractorCostsBySubcontractor.Rows[0].Controls.Clear();
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        //  STEP3 - SUBCONTRACTOR COST BY CLIENT AND PROJECT - PUBLIC METHODS
        //
        public ActualCostsAddTDS.SubcontractorCostsDataTable GetSubcontractorsDetailByClientProject()
        {
            subcontractorCosts = (ActualCostsAddTDS.SubcontractorCostsDataTable)Session["subcontractorCostsByClientProjectDummy"];
            if (subcontractorCosts == null)
            {
                subcontractorCosts = ((ActualCostsAddTDS.SubcontractorCostsDataTable)Session["subcontractorCosts"]);
            }

            return subcontractorCosts;
        }
        // ////////////////////////////////////////////////////////////////////////
        //  STEP2 - SUBCONTRACTOR COST BY SUBCONTRACTORS - PUBLIC METHODS
        //
        public ActualCostsAddTDS.SubcontractorCostsDataTable GetSubcontractorCostsBySubcontractorDetail()
        {
            subcontractorCosts = (ActualCostsAddTDS.SubcontractorCostsDataTable)Session["subcontractorCostsBySubcontractorDummy"];
            if (subcontractorCosts == null)
            {
                subcontractorCosts = ((ActualCostsAddTDS.SubcontractorCostsDataTable)Session["subcontractorCosts"]);
            }

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

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

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                upnlSubcontractorCost.Visible = false;

                // ... Dummy values
                Session.Remove("subcontractorCostsBySubcontractorDummy");
                Session.Remove("subcontractorCostsByClientProjectDummy");
                Session.Remove("hotelCostsByClientProjectDummy");
                Session.Remove("bondingCompaniesCostsByClientProjectDummy");
                Session.Remove("insuranceCompaniesCostsByClientProjectDummy");
                Session.Remove("otherCostsByClientProjectDummy");

                // Store datasets
                actualCostsAddTDS = new ActualCostsAddTDS();
                Session["actualCostsAddTDS"] = actualCostsAddTDS;
                Session["hotelCosts"] = actualCostsAddTDS.HotelCosts;
                Session["subcontractorCosts"] = actualCostsAddTDS.SubcontractorCosts;
                Session["bondingCompaniesCosts"] = actualCostsAddTDS.BondingCompaniesCosts;
                Session["insuranceCompaniesCosts"] = actualCostsAddTDS.InsuranceCompaniesCosts;
                Session["otherCosts"] = actualCostsAddTDS.OtherCosts;

                StoreNavigatorState();

                // StepSection1In
                wzActualCostsAdd.ActiveStepIndex = 0;
                StepBeginIn();
            }
            else
            {
                // Restore datasets
                actualCostsAddTDS = (ActualCostsAddTDS)Session["actualCostsAddTDS"];
                subcontractorCosts = actualCostsAddTDS.SubcontractorCosts;
                Session["subcontractorCosts"] = actualCostsAddTDS.SubcontractorCosts;
                Session["hotelCosts"] = actualCostsAddTDS.HotelCosts;
                Session["bondingCompaniesCosts"] = actualCostsAddTDS.BondingCompaniesCosts;
                Session["insuranceCompaniesCosts"] = actualCostsAddTDS.InsuranceCompaniesCosts;
                Session["otherCosts"] = actualCostsAddTDS.OtherCosts;
            }
        }
        protected void grdSubcontractorCostsBySubcontractor_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("generalData");
            if (Page.IsValid)
            {
                Page.Validate("DataEdit");
                if (Page.IsValid)
                {
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    int refId = Int32.Parse(((Label)grdSubcontractorCostsBySubcontractor.Rows[e.RowIndex].FindControl("lblRefIdEdit")).Text);
                    DateTime date = (DateTime)((RadDatePicker)grdSubcontractorCostsBySubcontractor.Rows[e.RowIndex].FindControl("tkrdpDateEdit")).SelectedDate;

                    int subcontractorId = Int32.Parse(ddlSubcontractor.SelectedValue);
                    string name = ddlSubcontractor.SelectedItem.Text;

                    int projectId = Int32.Parse(((Label)grdSubcontractorCostsBySubcontractor.Rows[e.RowIndex].FindControl("lblProjectIdEdit")).Text);

                    string quantityString = ((TextBox)grdSubcontractorCostsBySubcontractor.Rows[e.RowIndex].FindControl("tbxQuantityEdit")).Text;
                    decimal quantity1 = decimal.Round(decimal.Parse(quantityString), 1);
                    double quantity = double.Parse(quantity1.ToString());

                    decimal rate = decimal.Round(decimal.Parse(((TextBox)grdSubcontractorCostsBySubcontractor.Rows[e.RowIndex].FindControl("tbxRateEdit")).Text), 2);
                    decimal total = decimal.Round(decimal.Parse(((TextBox)grdSubcontractorCostsBySubcontractor.Rows[e.RowIndex].FindControl("tbxTotalEdit")).Text), 2);
                    decimal rateCad = 0;
                    decimal totalCad = 0;
                    decimal rateUsd = 0;
                    decimal totalUsd = 0;

                    ProjectGateway projectGateway = new ProjectGateway();
                    projectGateway.LoadByProjectId(projectId);

                    if (projectGateway.GetCountryID(projectId) == 1) //Canada
                    {
                        rateCad = rate;
                        totalCad = total;
                    }
                    else
                    {
                        rateUsd = rate;
                        totalUsd = total;
                    }

                    string comment = ((TextBox)grdSubcontractorCostsBySubcontractor.Rows[e.RowIndex].FindControl("tbxCommentEdit")).Text;
                    bool deleted = false;

                    // Update Data
                    ActualCostsAddSubcontractorCosts subcontractorAddProjectSubcontractorsCosts = new ActualCostsAddSubcontractorCosts(actualCostsAddTDS);
                    subcontractorAddProjectSubcontractorsCosts.Update(projectId, refId, subcontractorId, date, quantity, rateCad, totalCad, rateUsd, totalUsd, comment, deleted, companyId, name);

                    // Store dataset
                    Session["actualCostsAddTDS"] = actualCostsAddTDS;
                    Session.Remove("subcontractorCostsBySubcontractorDummy");
                    subcontractorCosts = actualCostsAddTDS.SubcontractorCosts;
                    Session["subcontractorCosts"] = actualCostsAddTDS.SubcontractorCosts;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdSubcontractorCostsBySubcontractor_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Subcontractor Gridview, if the gridview is edition mode
            if (grdSubcontractorCostsBySubcontractor.EditIndex >= 0)
            {
                grdSubcontractorCostsBySubcontractor.UpdateRow(grdSubcontractorCostsBySubcontractor.EditIndex, true);
            }

            // Delete subcontractor
            int projectId = (int)e.Keys["ProjectID"];
            int refId = (int)e.Keys["RefID"];

            // Delete costs details
            ActualCostsAddSubcontractorCosts subcontractorAddProjectSubcontractorsCosts = new ActualCostsAddSubcontractorCosts(actualCostsAddTDS);
            subcontractorAddProjectSubcontractorsCosts.Delete(projectId, refId);

            // Store dataset
            Session["actualCostsAddTDS"] = actualCostsAddTDS;
            Session.Remove("subcontractorCostsBySubcontractorDummy");
            subcontractorCosts = actualCostsAddTDS.SubcontractorCosts;
            Session["subcontractorCosts"] = actualCostsAddTDS.SubcontractorCosts;
        }