/// <summary>
        /// Save
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void Save(int companyId)
        {
            ActualCostsInformationTDS hotelCostsInformationChanges = (ActualCostsInformationTDS)Data.GetChanges();

            if (hotelCostsInformationChanges.HotelCostsBasicInformation.Rows.Count > 0)
            {
                ActualCostsInformationHotelCostsBasicInformationGateway actualCostsInformationHotelCostsBasicInformationGateway = new ActualCostsInformationHotelCostsBasicInformationGateway(hotelCostsInformationChanges);

                // Update employee
                foreach (ActualCostsInformationTDS.HotelCostsBasicInformationRow row in (ActualCostsInformationTDS.HotelCostsBasicInformationDataTable)hotelCostsInformationChanges.HotelCostsBasicInformation)
                {
                    // Insert new hours
                    if ((!row.Deleted) && (!row.InDatabase))
                    {
                        string comment = ""; if (!row.IsCommentNull()) comment = row.Comment;
                        ActualCostsHotelCosts actualCostsHotelCosts = new ActualCostsHotelCosts(null);
                        actualCostsHotelCosts.InsertDirect(row.ProjectID, row.RefID, row.HotelID, row.Date, row.RateCad, row.RateUsd, comment, row.Deleted, row.COMPANY_ID);
                    }

                    // Update hours
                    if ((!row.Deleted) && (row.InDatabase))
                    {
                        int projectId = row.ProjectID;
                        int refId = row.RefID;
                        bool originalDeleted = false;
                        int originalCompanyId = companyId;

                        // Original values
                        int originalHotelId = actualCostsInformationHotelCostsBasicInformationGateway.GetHotelIDOriginal(projectId, refId);
                        DateTime originalDate = actualCostsInformationHotelCostsBasicInformationGateway.GetDateOriginal(projectId, refId);
                        decimal originalRateCad = actualCostsInformationHotelCostsBasicInformationGateway.GetRateCadOriginal(projectId, refId);
                        decimal originalRateUsd = actualCostsInformationHotelCostsBasicInformationGateway.GetRateUsdOriginal(projectId, refId);
                        string originalComment = actualCostsInformationHotelCostsBasicInformationGateway.GetCommentOriginal(projectId, refId);

                        // New values
                        int newHotelId = actualCostsInformationHotelCostsBasicInformationGateway.GetHotelID(projectId, refId);
                        DateTime newDate = actualCostsInformationHotelCostsBasicInformationGateway.GetDate(projectId, refId);
                        decimal newRateCad = actualCostsInformationHotelCostsBasicInformationGateway.GetRateCad(projectId, refId);
                        decimal newRateUsd = actualCostsInformationHotelCostsBasicInformationGateway.GetRateUsd(projectId, refId);
                        string newComment = actualCostsInformationHotelCostsBasicInformationGateway.GetComment(projectId, refId);

                        ActualCostsHotelCosts actualCostsHotelCosts = new ActualCostsHotelCosts(null);
                        actualCostsHotelCosts.UpdateDirect(projectId, refId, originalHotelId, originalDate, originalRateCad, originalRateUsd, originalComment, originalDeleted, originalCompanyId, projectId, refId, newHotelId, newDate, newRateCad, newRateUsd, newComment, originalDeleted, originalCompanyId);
                    }

                    // Delete hours
                    if ((row.Deleted) && (row.InDatabase))
                    {
                        ActualCostsHotelCosts actualCostsHotelCosts = new ActualCostsHotelCosts(null);
                        actualCostsHotelCosts.DeleteDirect(row.ProjectID, row.RefID, row.COMPANY_ID);
                    }
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                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_DELETE"])))
                    {
                        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) || ((string)Request.QueryString["category"] == null))
                    {
                        Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in actual_costs_delete.aspx");
                    }
                }

                // Tag page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfCurrentProjectId.Value = Request.QueryString["project_id"];
                hdfCurrentRefId.Value = Request.QueryString["ref_id"];
                hdfCategory.Value = Request.QueryString["category"];

                // ... for titles
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int currentProjectId = int.Parse(hdfCurrentProjectId.Value);
                int currentRefId = int.Parse(hdfCurrentRefId.Value);
                string category = hdfCategory.Value;

                if (category == "Subcontractors")
                {
                    lblDelete.Text = "Are you sure you want to delete this subcontractor cost?";
                    ActualCostsInformationSubcontractorBasicInformationGateway actualCostsInformationSubcontractorBasicInformationGatewayForTitle = new ActualCostsInformationSubcontractorBasicInformationGateway();
                    actualCostsInformationSubcontractorBasicInformationGatewayForTitle.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                    lblTitle.Text = "Subcontractor: " + actualCostsInformationSubcontractorBasicInformationGatewayForTitle.GetSubcontractor(currentProjectId, currentRefId);
                }
                else
                {
                    if (category == "Hotels")
                    {
                        lblDelete.Text = "Are you sure you want to delete this hotel cost?";
                        ActualCostsInformationHotelCostsBasicInformationGateway actualCostsInformationHotelCostsBasicInformationGateway = new ActualCostsInformationHotelCostsBasicInformationGateway();
                        actualCostsInformationHotelCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                        lblTitle.Text = "Hotel: " + actualCostsInformationHotelCostsBasicInformationGateway.GetHotel(currentProjectId, currentRefId);
                    }
                    else
                    {
                        if (category == "Insurance")
                        {
                            lblDelete.Text = "Are you sure you want to delete this insurance company cost?";
                            ActualCostsInformationInsuranceCostsBasicInformationGateway actualCostsInformationInsuranceCostsBasicInformationGateway = new ActualCostsInformationInsuranceCostsBasicInformationGateway();
                            actualCostsInformationInsuranceCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                            lblTitle.Text = "Insurance Company:  " + actualCostsInformationInsuranceCostsBasicInformationGateway.GetInsurance(currentProjectId, currentRefId);
                        }
                        else
                        {
                            if (category == "Bonding")
                            {
                                lblDelete.Text = "Are you sure you want to delete this bonding company cost?";
                                ActualCostsInformationBondingCostsBasicInformationGateway actualCostsInformationBondingCostsBasicInformationGateway = new ActualCostsInformationBondingCostsBasicInformationGateway();
                                actualCostsInformationBondingCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                                lblTitle.Text = "Bonding Company: " + actualCostsInformationBondingCostsBasicInformationGateway.GetBonding(currentProjectId, currentRefId);
                            }
                            else
                            {
                                // for other categories
                                lblDelete.Text = "Are you sure you want to delete this insurance other cost?";
                                ActualCostsInformationOtherCostsBasicInformationGateway actualCostsInformationOtherCostsBasicInformationGateway = new ActualCostsInformationOtherCostsBasicInformationGateway();
                                actualCostsInformationOtherCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                                lblTitle.Text = "Category: " + actualCostsInformationOtherCostsBasicInformationGateway.GetCategory(currentProjectId, currentRefId);
                            }
                        }
                    }
                }

                // If coming from
                // ... actual_costs_navigator2.aspx
                if (Request.QueryString["source_page"] == "actual_costs_navigator2.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = "no";

                    actualCostsInformationTDS = new ActualCostsInformationTDS();

                    if (category == "Subcontractors")
                    {
                        ActualCostsInformationSubcontractorBasicInformationGateway actualCostsInformationSubcontractorBasicInformationGateway = new ActualCostsInformationSubcontractorBasicInformationGateway(actualCostsInformationTDS);
                        actualCostsInformationSubcontractorBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                    }
                    else
                    {
                        if (category == "Hotels")
                        {
                            ActualCostsInformationHotelCostsBasicInformationGateway actualCostsInformationHotelCostsBasicInformationGateway = new ActualCostsInformationHotelCostsBasicInformationGateway(actualCostsInformationTDS);
                            actualCostsInformationHotelCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                        }
                        else
                        {
                            if (category == "Insurance")
                            {
                                ActualCostsInformationInsuranceCostsBasicInformationGateway actualCostsInformationInsuranceCostsBasicInformationGateway = new ActualCostsInformationInsuranceCostsBasicInformationGateway(actualCostsInformationTDS);
                                actualCostsInformationInsuranceCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                            }
                            else
                            {
                                if (category == "Bonding")
                                {
                                    ActualCostsInformationBondingCostsBasicInformationGateway actualCostsInformationBondingCostsBasicInformationGateway = new ActualCostsInformationBondingCostsBasicInformationGateway(actualCostsInformationTDS);
                                    actualCostsInformationBondingCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);
                                }
                                else
                                {
                                    // other categories
                                    ActualCostsInformationOtherCostsBasicInformationGateway actualCostsInformationOtherCostsBasicInformationGateway = new ActualCostsInformationOtherCostsBasicInformationGateway(actualCostsInformationTDS);
                                    actualCostsInformationOtherCostsBasicInformationGateway.LoadByProjectIdRefId(currentProjectId, currentRefId, companyId);

                                }
                            }
                        }
                    }

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

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

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

            }
            else
            {
                // Restore dataset
                actualCostsInformationTDS = (ActualCostsInformationTDS)Session["actualCostsInformationTDS"];
            }
        }
        private void LoadData(int projectId, int refId)
        {
            string category = hdfCategory.Value;

            // Load Data
            if (category == "Subcontractors")
            {
                ActualCostsInformationSubcontractorBasicInformationGateway actualCostsInformationSubcontractorBasicInformationGateway = new ActualCostsInformationSubcontractorBasicInformationGateway(actualCostsInformationTDS);
                if (actualCostsInformationSubcontractorBasicInformationGateway.Table.Rows.Count > 0)
                {
                    // Load subcontractor basic data
                    tbxCategory.Text = actualCostsInformationSubcontractorBasicInformationGateway.GetSubcontractor(projectId, refId);

                    if (tbxState.Visible)
                    {
                        if (actualCostsInformationSubcontractorBasicInformationGateway.GetActive(projectId, refId))
                        {
                            tbxState.Text = "Active";
                        }
                        else
                        {
                            tbxState.Text = "Inactive";
                        }
                    }

                    tbxClient.Text = actualCostsInformationSubcontractorBasicInformationGateway.GetClient(projectId, refId);
                    tbxProject.Text = actualCostsInformationSubcontractorBasicInformationGateway.GetProject(projectId, refId);
                    hdfClientId.Value = actualCostsInformationSubcontractorBasicInformationGateway.GetClientID(projectId, refId).ToString();

                    tkrdpDate_.SelectedDate = (DateTime)(actualCostsInformationSubcontractorBasicInformationGateway.GetDate(projectId, refId));

                    if (tbxQuantity.Visible)
                    {
                        decimal quantity = decimal.Parse(actualCostsInformationSubcontractorBasicInformationGateway.GetQuantity(projectId, refId).ToString());
                        tbxQuantity.Text = decimal.Round(quantity, 1).ToString();
                    }

                    if (actualCostsInformationSubcontractorBasicInformationGateway.GetCountryId(projectId, refId) == 1) // Canada
                    {
                        decimal rate = decimal.Parse(actualCostsInformationSubcontractorBasicInformationGateway.GetRateCad(projectId, refId).ToString());
                        tbxRate.Text = decimal.Round(rate, 2).ToString();

                        if (tbxTotal.Visible)
                        {
                            decimal total = decimal.Parse(actualCostsInformationSubcontractorBasicInformationGateway.GetTotalCad(projectId, refId).ToString());
                            tbxTotal.Text = decimal.Round(total, 2).ToString();
                        }
                    }
                    else // Usa
                    {
                        decimal rate = decimal.Parse(actualCostsInformationSubcontractorBasicInformationGateway.GetRateUsd(projectId, refId).ToString());
                        tbxRate.Text = decimal.Round(rate, 2).ToString();

                        if (tbxTotal.Visible)
                        {
                            decimal total = decimal.Parse(actualCostsInformationSubcontractorBasicInformationGateway.GetTotalUsd(projectId, refId).ToString());
                            tbxTotal.Text = decimal.Round(total, 2).ToString();
                        }
                    }

                    hdfCountryId.Value = actualCostsInformationSubcontractorBasicInformationGateway.GetCountryId(projectId, refId).ToString();
                    tbxComments.Text = actualCostsInformationSubcontractorBasicInformationGateway.GetComment(projectId, refId);
                }
            }
            else
            {
                if (category == "Hotels")
                {
                    ActualCostsInformationHotelCostsBasicInformationGateway actualCostsInformationHotelCostsBasicInformationGateway = new ActualCostsInformationHotelCostsBasicInformationGateway(actualCostsInformationTDS);
                    if (actualCostsInformationHotelCostsBasicInformationGateway.Table.Rows.Count > 0)
                    {
                        // Load subcontractor basic data
                        tbxCategory.Text = actualCostsInformationHotelCostsBasicInformationGateway.GetHotel(projectId, refId);
                        tbxClient.Text = actualCostsInformationHotelCostsBasicInformationGateway.GetClient(projectId, refId);
                        tbxProject.Text = actualCostsInformationHotelCostsBasicInformationGateway.GetProject(projectId, refId);
                        hdfClientId.Value = actualCostsInformationHotelCostsBasicInformationGateway.GetClientID(projectId, refId).ToString();

                        tkrdpDate_.SelectedDate = (DateTime)(actualCostsInformationHotelCostsBasicInformationGateway.GetDate(projectId, refId));

                        if (actualCostsInformationHotelCostsBasicInformationGateway.GetCountryId(projectId, refId) == 1) // Canada
                        {
                            decimal rate = decimal.Parse(actualCostsInformationHotelCostsBasicInformationGateway.GetRateCad(projectId, refId).ToString());
                            tbxRate.Text = decimal.Round(rate, 2).ToString();
                        }
                        else // Usa
                        {
                            decimal rate = decimal.Parse(actualCostsInformationHotelCostsBasicInformationGateway.GetRateUsd(projectId, refId).ToString());
                            tbxRate.Text = decimal.Round(rate, 2).ToString();
                        }

                        hdfCountryId.Value = actualCostsInformationHotelCostsBasicInformationGateway.GetCountryId(projectId, refId).ToString();
                        tbxComments.Text = actualCostsInformationHotelCostsBasicInformationGateway.GetComment(projectId, refId);
                    }
                }
                else
                {
                    if (category == "Insurance")
                    {
                        ActualCostsInformationInsuranceCostsBasicInformationGateway actualCostsInformationInsuranceCostsBasicInformationGateway = new ActualCostsInformationInsuranceCostsBasicInformationGateway(actualCostsInformationTDS);
                        if (actualCostsInformationInsuranceCostsBasicInformationGateway.Table.Rows.Count > 0)
                        {
                            // Load subcontractor basic data
                            tbxCategory.Text = actualCostsInformationInsuranceCostsBasicInformationGateway.GetInsurance(projectId, refId);
                            tbxClient.Text = actualCostsInformationInsuranceCostsBasicInformationGateway.GetClient(projectId, refId);
                            tbxProject.Text = actualCostsInformationInsuranceCostsBasicInformationGateway.GetProject(projectId, refId);
                            hdfClientId.Value = actualCostsInformationInsuranceCostsBasicInformationGateway.GetClientID(projectId, refId).ToString();

                            tkrdpDate_.SelectedDate = (DateTime)(actualCostsInformationInsuranceCostsBasicInformationGateway.GetDate(projectId, refId));

                            if (actualCostsInformationInsuranceCostsBasicInformationGateway.GetCountryId(projectId, refId) == 1) // Canada
                            {
                                decimal rate = decimal.Parse(actualCostsInformationInsuranceCostsBasicInformationGateway.GetRateCad(projectId, refId).ToString());
                                tbxRate.Text = decimal.Round(rate, 2).ToString();
                            }
                            else // Usa
                            {
                                decimal rate = decimal.Parse(actualCostsInformationInsuranceCostsBasicInformationGateway.GetRateUsd(projectId, refId).ToString());
                                tbxRate.Text = decimal.Round(rate, 2).ToString();
                            }

                            hdfCountryId.Value = actualCostsInformationInsuranceCostsBasicInformationGateway.GetCountryId(projectId, refId).ToString();
                            tbxComments.Text = actualCostsInformationInsuranceCostsBasicInformationGateway.GetComment(projectId, refId);
                        }
                    }
                    else
                    {
                        if (category == "Bonding")
                        {
                            ActualCostsInformationBondingCostsBasicInformationGateway actualCostsInformationBondingCostsBasicInformationGateway = new ActualCostsInformationBondingCostsBasicInformationGateway(actualCostsInformationTDS);
                            if (actualCostsInformationBondingCostsBasicInformationGateway.Table.Rows.Count > 0)
                            {
                                // Load subcontractor basic data
                                tbxCategory.Text = actualCostsInformationBondingCostsBasicInformationGateway.GetBonding(projectId, refId);
                                tbxClient.Text = actualCostsInformationBondingCostsBasicInformationGateway.GetClient(projectId, refId);
                                tbxProject.Text = actualCostsInformationBondingCostsBasicInformationGateway.GetProject(projectId, refId);
                                hdfClientId.Value = actualCostsInformationBondingCostsBasicInformationGateway.GetClientID(projectId, refId).ToString();

                                tkrdpDate_.SelectedDate = (DateTime)(actualCostsInformationBondingCostsBasicInformationGateway.GetDate(projectId, refId));

                                if (actualCostsInformationBondingCostsBasicInformationGateway.GetCountryId(projectId, refId) == 1) // Canada
                                {
                                    decimal rate = decimal.Parse(actualCostsInformationBondingCostsBasicInformationGateway.GetRateCad(projectId, refId).ToString());
                                    tbxRate.Text = decimal.Round(rate, 2).ToString();
                                }
                                else // Usa
                                {
                                    decimal rate = decimal.Parse(actualCostsInformationBondingCostsBasicInformationGateway.GetRateUsd(projectId, refId).ToString());
                                    tbxRate.Text = decimal.Round(rate, 2).ToString();
                                }

                                hdfCountryId.Value = actualCostsInformationBondingCostsBasicInformationGateway.GetCountryId(projectId, refId).ToString();
                                tbxComments.Text = actualCostsInformationBondingCostsBasicInformationGateway.GetComment(projectId, refId);
                            }
                        }
                        else
                        {
                            // for other categories
                            ActualCostsInformationOtherCostsBasicInformationGateway actualCostsInformationOtherCostsBasicInformationGateway = new ActualCostsInformationOtherCostsBasicInformationGateway(actualCostsInformationTDS);
                            if (actualCostsInformationOtherCostsBasicInformationGateway.Table.Rows.Count > 0)
                            {
                                // Load subcontractor basic data
                                tbxCategory.Text = actualCostsInformationOtherCostsBasicInformationGateway.GetCategory(projectId, refId);
                                tbxClient.Text = actualCostsInformationOtherCostsBasicInformationGateway.GetClient(projectId, refId);
                                tbxProject.Text = actualCostsInformationOtherCostsBasicInformationGateway.GetProject(projectId, refId);
                                hdfClientId.Value = actualCostsInformationOtherCostsBasicInformationGateway.GetClientID(projectId, refId).ToString();

                                tkrdpDate_.SelectedDate = (DateTime)(actualCostsInformationOtherCostsBasicInformationGateway.GetDate(projectId, refId));

                                if (actualCostsInformationOtherCostsBasicInformationGateway.GetCountryId(projectId, refId) == 1) // Canada
                                {
                                    decimal rate = decimal.Parse(actualCostsInformationOtherCostsBasicInformationGateway.GetRateCad(projectId, refId).ToString());
                                    tbxRate.Text = decimal.Round(rate, 2).ToString();
                                }
                                else // Usa
                                {
                                    decimal rate = decimal.Parse(actualCostsInformationOtherCostsBasicInformationGateway.GetRateUsd(projectId, refId).ToString());
                                    tbxRate.Text = decimal.Round(rate, 2).ToString();
                                }

                                hdfCountryId.Value = actualCostsInformationOtherCostsBasicInformationGateway.GetCountryId(projectId, refId).ToString();
                                tbxComments.Text = actualCostsInformationOtherCostsBasicInformationGateway.GetComment(projectId, refId);
                            }
                        }
                    }
                }
            }
        }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// LoadByProjectIdRefId
 /// </summary>
 /// <param name="projectId">projectId</param>    
 /// <param name="refId">refId</param>
 /// <param name="companyId">companyId</param>
 public void LoadByProjectIdRefId(int projectId, int refId, int companyId)
 {
     ActualCostsInformationHotelCostsBasicInformationGateway actualCostsInformationHotelCostsBasicInformationGateway = new ActualCostsInformationHotelCostsBasicInformationGateway(Data);
     actualCostsInformationHotelCostsBasicInformationGateway.LoadByProjectIdRefId(projectId, refId, companyId);
 }