private void UpdateDatabase()
        {
            // Get ids
            int companyId = int.Parse(hdfCompanyId.Value);

            // Delete
            DB.Open();
            DB.BeginTransaction();
            try
            {
                string category = hdfCategory.Value;

                if (category == "Subcontractors")
                {
                    ActualCostsInformationSubcontractorBasicInformation actualCostsInformationSubcontractorBasicInformation = new ActualCostsInformationSubcontractorBasicInformation(actualCostsInformationTDS);
                    actualCostsInformationSubcontractorBasicInformation.Save(companyId);
                }
                else
                {
                    if (category == "Hotels")
                    {
                        ActualCostsInformationHotelCostsBasicInformation actualCostsInformationHotelCostsBasicInformation = new ActualCostsInformationHotelCostsBasicInformation(actualCostsInformationTDS);
                        actualCostsInformationHotelCostsBasicInformation.Save(companyId);
                    }
                    else
                    {
                        if (category == "Insurance")
                        {
                            ActualCostsInformationInsuranceCostsBasicInformation actualCostsInformationInsuranceCostsBasicInformation = new ActualCostsInformationInsuranceCostsBasicInformation(actualCostsInformationTDS);
                            actualCostsInformationInsuranceCostsBasicInformation.Save(companyId);
                        }
                        else
                        {
                            if (category == "Bonding")
                            {
                                ActualCostsInformationBondingCostsBasicInformation actualCostsInformationBondingCostsBasicInformation = new ActualCostsInformationBondingCostsBasicInformation(actualCostsInformationTDS);
                                actualCostsInformationBondingCostsBasicInformation.Save(companyId);
                            }
                            else
                            {
                                // other categories
                                ActualCostsInformationOtherCostsBasicInformation actualCostsInformationOtherCostsBasicInformation = new ActualCostsInformationOtherCostsBasicInformation(actualCostsInformationTDS);
                                actualCostsInformationOtherCostsBasicInformation.Save(companyId);
                            }
                        }
                    }
                }

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        private void Delete()
        {
            int currentProjectId = int.Parse(hdfCurrentProjectId.Value);
            int currentRefId = int.Parse(hdfCurrentRefId.Value);

            string category = hdfCategory.Value;

            if (category == "Subcontractors")
            {
                    ActualCostsInformationSubcontractorBasicInformation actualCostsInformationSubcontractorBasicInformation = new ActualCostsInformationSubcontractorBasicInformation(actualCostsInformationTDS);
                    actualCostsInformationSubcontractorBasicInformation.Delete(currentProjectId, currentRefId);
            }
            else
            {
                if (category == "Hotels")
                {
                    ActualCostsInformationHotelCostsBasicInformation actualCostsInformationHotelCostsBasicInformation = new ActualCostsInformationHotelCostsBasicInformation(actualCostsInformationTDS);
                    actualCostsInformationHotelCostsBasicInformation.Delete(currentProjectId, currentRefId);
                }
                else
                {
                    if (category == "Insurance")
                    {
                        ActualCostsInformationInsuranceCostsBasicInformation actualCostsInformationInsuranceCostsBasicInformation = new ActualCostsInformationInsuranceCostsBasicInformation(actualCostsInformationTDS);
                        actualCostsInformationInsuranceCostsBasicInformation.Delete(currentProjectId, currentRefId);
                    }
                    else
                    {
                        if (category == "Bonding")
                        {
                            ActualCostsInformationBondingCostsBasicInformation actualCostsInformationBondingCostsBasicInformation = new ActualCostsInformationBondingCostsBasicInformation(actualCostsInformationTDS);
                            actualCostsInformationBondingCostsBasicInformation.Delete(currentProjectId, currentRefId);
                        }
                        else
                        {
                            // other categories
                            ActualCostsInformationOtherCostsBasicInformation actualCostsInformationOtherCostsBasicInformation = new ActualCostsInformationOtherCostsBasicInformation(actualCostsInformationTDS);
                            actualCostsInformationOtherCostsBasicInformation.Delete(currentProjectId, currentRefId);
                        }
                    }
                }
            }

            // Store datasets
            Session["actualCostsInformationTDS"] = actualCostsInformationTDS;
        }
        // ////////////////////////////////////////////////////////////////////////
        // 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_VIEW"]) && Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_ACTUAL_COSTS_EDIT"])))
                    {
                        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) || ((string)Request.QueryString["ref_id"] == null))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in actual_costs_edit.aspx");
                    }
                }

                // Initialize  variables
                hdfProjectId.Value = Request.QueryString["project_id"].ToString();
                hdfRefId.Value = Request.QueryString["ref_id"].ToString();
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfCategory.Value = Request.QueryString["category"].ToString();

                // If coming from
                int currentRefId = Int32.Parse(hdfRefId.Value);
                int currentProjectId = Int32.Parse(hdfProjectId.Value);
                int companyId = Int32.Parse(hdfCompanyId.Value);
                string category = hdfCategory.Value;

                // ... actual_costs_navigator2.aspx
                if (Request.QueryString["source_page"] == "actual_costs_navigator2.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = "si";

                    actualCostsInformationTDS = new ActualCostsInformationTDS();

                    if (category == "Subcontractors")
                    {
                        lblTitle.Text = "Subcontractor Costs";
                        lblCategoryName.Text = "Subcontractor";
                        ActualCostsInformationSubcontractorBasicInformation actualCostsInformationSubcontractorBasicInformation = new ActualCostsInformationSubcontractorBasicInformation(actualCostsInformationTDS);
                        actualCostsInformationSubcontractorBasicInformation.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                    }
                    else
                    {
                        if (category == "Hotels")
                        {
                            lblTitle.Text = "Hotel Costs";
                            lblCategoryName.Text = "Hotels";
                            tbxState.Visible = false;
                            ActualCostsInformationHotelCostsBasicInformation actualCostsInformationHotelCostsBasicInformation = new ActualCostsInformationHotelCostsBasicInformation(actualCostsInformationTDS);
                            actualCostsInformationHotelCostsBasicInformation.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                        }
                        else
                        {
                            if (category == "Insurance")
                            {
                                lblTitle.Text = "Insurance Company Costs";
                                lblCategoryName.Text = "Insurance Company";
                                tbxState.Visible = false;
                                ActualCostsInformationInsuranceCostsBasicInformation actualCostsInformationInsuranceCostsBasicInformation = new ActualCostsInformationInsuranceCostsBasicInformation(actualCostsInformationTDS);
                                actualCostsInformationInsuranceCostsBasicInformation.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                            }
                            else
                            {
                                if (category == "Bonding")
                                {
                                    lblTitle.Text = "Bonding Company Costs";
                                    lblCategoryName.Text = "Bonding Company";
                                    tbxState.Visible = false;
                                    ActualCostsInformationBondingCostsBasicInformation actualCostsInformationBondingCostsBasicInformation = new ActualCostsInformationBondingCostsBasicInformation(actualCostsInformationTDS);
                                    actualCostsInformationBondingCostsBasicInformation.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                                }
                                else
                                {
                                    // other categories
                                    lblTitle.Text = "Other Costs";
                                    lblCategoryName.Text = "Category";
                                    tbxState.Visible = false;
                                    ActualCostsInformationOtherCostsBasicInformation actualCostsInformationOtherCostsBasicInformation = new ActualCostsInformationOtherCostsBasicInformation(actualCostsInformationTDS);
                                    actualCostsInformationOtherCostsBasicInformation.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                                }
                            }
                        }
                    }

                    // ... Store dataset
                    Session["actualCostsInformationTDS"] = actualCostsInformationTDS;
                }

                // ... actual_costs_summary.aspx or actual_costs_edit.aspx
                if ((Request.QueryString["source_page"] == "actual_costs_summary.aspx") || (Request.QueryString["source_page"] == "actual_costs_edit.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];

                    // ... Restore dataset
                    actualCostsInformationTDS = (ActualCostsInformationTDS)Session["actualCostsInformationTDS"];
                }

                // ... Data for current employee
                LoadData(currentProjectId, currentRefId);
            }
            else
            {
                // Restore datasets
                actualCostsInformationTDS = (ActualCostsInformationTDS)Session["actualCostsInformationTDS"];
            }
        }
        private void Save()
        {
            // Validate data
            bool validData = false;

            Page.Validate("Data");
            if (Page.IsValid)
            {
                validData = true;
            }

            if (validData)
            {
                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int projectId = Int32.Parse(hdfProjectId.Value);
                int refId = Int32.Parse(hdfRefId.Value);
                Int64 countryId = Int64.Parse(hdfCountryId.Value);

                // ... Get subcontractor hors data
                DateTime? newDate = tkrdpDate_.SelectedDate;
                decimal newRateCad = 0;
                decimal newRateUsd = 0;

                if (countryId == 1) // Canada
                {
                    newRateCad = decimal.Parse(tbxRate.Text.Trim());
                }
                else
                {
                    newRateUsd = decimal.Parse(tbxRate.Text.Trim());
                }
                string newComment = tbxComments.Text.Trim();
                string category = hdfCategory.Value;

                // Update basic information data
                if (category == "Subcontractors")
                {
                    double newQuantity = double.Parse(tbxQuantity.Text.Trim());
                    decimal newTotalCad = 0;
                    decimal newTotalUsd = 0;

                    if (countryId == 1) // Canada
                    {
                        newTotalCad = decimal.Parse(tbxTotal.Text.Trim());
                    }
                    else
                    {
                        newTotalUsd = decimal.Parse(tbxTotal.Text.Trim());
                    }

                    ActualCostsInformationSubcontractorBasicInformation actualCostsInformationSubcontractorBasicInformation = new ActualCostsInformationSubcontractorBasicInformation(actualCostsInformationTDS);
                    actualCostsInformationSubcontractorBasicInformation.Update(projectId, refId, (DateTime)newDate, newQuantity, newRateCad, newTotalCad, newRateUsd, newTotalUsd, newComment);
                }
                else
                {
                    if (category == "Hotels")
                    {
                        ActualCostsInformationHotelCostsBasicInformation actualCostsInformationHotelCostsBasicInformation = new ActualCostsInformationHotelCostsBasicInformation(actualCostsInformationTDS);
                        actualCostsInformationHotelCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                    }
                    else
                    {
                        if (category == "Insurance")
                        {
                            ActualCostsInformationInsuranceCostsBasicInformation actualCostsInformationInsuranceCostsBasicInformation = new ActualCostsInformationInsuranceCostsBasicInformation(actualCostsInformationTDS);
                            actualCostsInformationInsuranceCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                        }
                        else
                        {
                            if (category == "Bonding")
                            {
                                ActualCostsInformationBondingCostsBasicInformation actualCostsInformationBondingCostsBasicInformation = new ActualCostsInformationBondingCostsBasicInformation(actualCostsInformationTDS);
                                actualCostsInformationBondingCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                            }
                            else
                            {
                                // other categories
                                ActualCostsInformationOtherCostsBasicInformation actualCostsInformationOtherCostsBasicInformation = new ActualCostsInformationOtherCostsBasicInformation(actualCostsInformationTDS);
                                actualCostsInformationOtherCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                            }
                        }
                    }
                }

                // Store datasets
                Session["actualCostsInformationTDS"] = actualCostsInformationTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";

                // Redirect
                string url = "";
                if (Request.QueryString["source_page"] == "actual_costs_navigator2.aspx" )
                {
                    url = "./actual_costs_navigator2.aspx?source_page=actual_costs_edit.aspx&project_id=" + hdfProjectId.Value + "&ref_id=" + hdfRefId.Value + GetNavigatorState() + "&update=yes" + "&category=" + hdfCategory.Value;
                }

                if (Request.QueryString["source_page"] == "actual_costs_summary.aspx")
                {
                    url = "./actual_costs_summary.aspx?source_page=actual_costs_edit.aspx&project_id=" + hdfProjectId.Value + "&ref_id=" + hdfRefId.Value + GetNavigatorState() + "&update=yes" + "&category=" + hdfCategory.Value;
                }

                Response.Redirect(url);
            }
        }
        private void Apply()
        {
            // Validate data
            bool validData = false;

            Page.Validate("Data");
            if (Page.IsValid)
            {
                validData = true;
            }

            if (validData)
            {
                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int projectId = Int32.Parse(hdfProjectId.Value);
                int refId = Int32.Parse(hdfRefId.Value);
                Int64 countryId = Int64.Parse(hdfCountryId.Value);

                // ... Get subcontractor hors data
                DateTime? newDate = tkrdpDate_.SelectedDate;

                decimal newRateCad = 0;
                decimal newRateUsd = 0;

                if (countryId == 1) // Canada
                {
                    newRateCad = decimal.Parse(tbxRate.Text.Trim());
                }
                else
                {
                    newRateUsd = decimal.Parse(tbxRate.Text.Trim());
                }
                string newComment = tbxComments.Text.Trim();
                string category = hdfCategory.Value;

                // Update basic information data
                if (category == "Subcontractors")
                {
                    double newQuantity = double.Parse(tbxQuantity.Text.Trim());
                    decimal newTotalCad = 0;
                    decimal newTotalUsd = 0;

                    if (countryId == 1) // Canada
                    {
                        newTotalCad = decimal.Parse(tbxTotal.Text.Trim());
                    }
                    else
                    {
                        newTotalUsd = decimal.Parse(tbxTotal.Text.Trim());
                    }

                    ActualCostsInformationSubcontractorBasicInformation actualCostsInformationSubcontractorBasicInformation = new ActualCostsInformationSubcontractorBasicInformation(actualCostsInformationTDS);
                    actualCostsInformationSubcontractorBasicInformation.Update(projectId, refId, (DateTime)newDate, newQuantity, newRateCad, newTotalCad, newRateUsd, newTotalUsd, newComment);
                }
                else
                {
                    if (category == "Hotels")
                    {
                        ActualCostsInformationHotelCostsBasicInformation actualCostsInformationHotelCostsBasicInformation = new ActualCostsInformationHotelCostsBasicInformation(actualCostsInformationTDS);
                        actualCostsInformationHotelCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                    }
                    else
                    {
                        if (category == "Insurance")
                        {
                            ActualCostsInformationInsuranceCostsBasicInformation actualCostsInformationInsuranceCostsBasicInformation = new ActualCostsInformationInsuranceCostsBasicInformation(actualCostsInformationTDS);
                            actualCostsInformationInsuranceCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                        }
                        else
                        {
                            if (category == "Bonding")
                            {
                                ActualCostsInformationBondingCostsBasicInformation actualCostsInformationBondingCostsBasicInformation = new ActualCostsInformationBondingCostsBasicInformation(actualCostsInformationTDS);
                                actualCostsInformationBondingCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                            }
                            else
                            {
                                // for other categories
                                ActualCostsInformationOtherCostsBasicInformation actualCostsInformationOtherCostsBasicInformation = new ActualCostsInformationOtherCostsBasicInformation(actualCostsInformationTDS);
                                actualCostsInformationOtherCostsBasicInformation.Update(projectId, refId, (DateTime)newDate, newRateCad, newRateUsd, newComment);
                            }
                        }
                    }
                }

                // Store datasets
                Session["actualCostsInformationTDS"] = actualCostsInformationTDS;

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";
            }
        }