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

            if (aubcontractorHoursInformationChanges.BasicInformation.Rows.Count > 0)
            {
                SubcontractorHoursInformationBasicInformationGateway employeeInformationBasicInformationGateway = new SubcontractorHoursInformationBasicInformationGateway(aubcontractorHoursInformationChanges);

                // Update employee
                foreach (SubcontractorHoursInformationTDS.BasicInformationRow row in (SubcontractorHoursInformationTDS.BasicInformationDataTable)aubcontractorHoursInformationChanges.BasicInformation)
                {
                    // Insert new hours
                    if ((!row.Deleted) && (!row.InDatabase))
                    {
                        string comment = ""; if (!row.IsCommentNull()) comment = row.Comment;
                        SubcontractorHoursSubcontractorHours subcontractorHoursSubcontractorHours = new SubcontractorHoursSubcontractorHours(null);
                        subcontractorHoursSubcontractorHours.InsertDirect(row.ProjectID, row.RefID, row.SubcontractorID, row.Date, row.Quantity, row.RateCad, row.TotalCad, row.RateUsd, row.TotalUsd, 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 originalSubcontractorId = employeeInformationBasicInformationGateway.GetSubcontractorIDOriginal(projectId, refId);
                        DateTime originalDate = employeeInformationBasicInformationGateway.GetDateOriginal(projectId, refId);
                        double originalQuantity = employeeInformationBasicInformationGateway.GetQuantityOriginal(projectId, refId);
                        decimal originalRateCad = employeeInformationBasicInformationGateway.GetRateCadOriginal(projectId, refId);
                        decimal originalTotalCad = employeeInformationBasicInformationGateway.GetTotalCadOriginal(projectId, refId);
                        decimal originalRateUsd = employeeInformationBasicInformationGateway.GetRateUsdOriginal(projectId, refId);
                        decimal originalTotalUsd = employeeInformationBasicInformationGateway.GetTotalUsdOriginal(projectId, refId);
                        string originalComment = employeeInformationBasicInformationGateway.GetCommentOriginal(projectId, refId);

                        // New values
                        int newSubcontractorId = employeeInformationBasicInformationGateway.GetSubcontractorID(projectId, refId);
                        DateTime newDate = employeeInformationBasicInformationGateway.GetDate(projectId, refId);
                        double newQuantity = employeeInformationBasicInformationGateway.GetQuantity(projectId, refId);
                        decimal newRateCad = employeeInformationBasicInformationGateway.GetRateCad(projectId, refId);
                        decimal newTotalCad = employeeInformationBasicInformationGateway.GetTotalCad(projectId, refId);
                        decimal newRateUsd = employeeInformationBasicInformationGateway.GetRateUsd(projectId, refId);
                        decimal newTotalUsd = employeeInformationBasicInformationGateway.GetTotalUsd(projectId, refId);
                        string newComment = employeeInformationBasicInformationGateway.GetComment(projectId, refId);

                        SubcontractorHoursSubcontractorHours subcontractorHoursSubcontractorHours = new SubcontractorHoursSubcontractorHours(null);
                        subcontractorHoursSubcontractorHours.UpdateDirect(projectId, refId, originalSubcontractorId, originalDate, originalQuantity, originalRateCad, originalTotalCad, originalRateUsd, originalTotalUsd, originalComment, originalDeleted, originalCompanyId, projectId, refId, newSubcontractorId, newDate, newQuantity, newRateCad, newTotalCad, newRateUsd, newTotalUsd, newComment, originalDeleted, originalCompanyId);
                    }

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

            if (!IsPostBack)
            {
                if (!(Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_SUBCONTRACTOR_HOURS_ADMIN"])))
                {
                    // Security check
                    if (!(Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_SUBCONTRACTOR_HOURS_VIEW"]) && Convert.ToBoolean(Session["sgLFS_LABOUR_HOURS_SUBCONTRACTOR_HOURS_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 subcontractor_hours_summary.aspx");
                    }
                }

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

                // If coming from
                int refId = Int32.Parse(hdfRefId.Value);
                int projectId = Int32.Parse(hdfProjectId.Value);

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

                    int companyId = Int32.Parse(hdfCompanyId.Value);

                    subcontractorHoursInformationTDS = new SubcontractorHoursInformationTDS();

                    SubcontractorHoursInformationBasicInformationGateway subcontractorHoursInformationBasicInformationGateway = new SubcontractorHoursInformationBasicInformationGateway(subcontractorHoursInformationTDS);
                    subcontractorHoursInformationBasicInformationGateway.LoadByProjectIdRefId(projectId, refId,companyId);

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

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

                    int companyId = Int32.Parse(hdfCompanyId.Value);

                    // ... Restore dataset
                    subcontractorHoursInformationTDS = (SubcontractorHoursInformationTDS)Session["subcontractorHoursInformationTDS"];
                }

                // ... Data for current employee
                LoadData(projectId, refId);
            }
            else
            {
                // Restore datasets
                subcontractorHoursInformationTDS = (SubcontractorHoursInformationTDS)Session["subcontractorHoursInformationTDS"];
            }
        }
        private void LoadData(int projectId, int refId)
        {
            // Load Data
            SubcontractorHoursInformationBasicInformationGateway subcontractorHoursInformationBasicInformationGateway = new SubcontractorHoursInformationBasicInformationGateway(subcontractorHoursInformationTDS);
            if (subcontractorHoursInformationBasicInformationGateway.Table.Rows.Count > 0)
            {
                // Load subcontractor basic data
                tbxSubcontractor.Text = subcontractorHoursInformationBasicInformationGateway.GetSubcontractor(projectId, refId);

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

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

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

                decimal quantity = decimal.Parse(subcontractorHoursInformationBasicInformationGateway.GetQuantity(projectId, refId).ToString());
                tbxQuantity.Text = decimal.Round(quantity,1).ToString();

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

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

                    decimal total = decimal.Parse(subcontractorHoursInformationBasicInformationGateway.GetTotalUsd(projectId, refId).ToString());
                    tbxTotal.Text = decimal.Round(total, 2).ToString();
                }

                hdfCountryId.Value = subcontractorHoursInformationBasicInformationGateway.GetCountryId(projectId, refId).ToString();
                tbxComments.Text =  subcontractorHoursInformationBasicInformationGateway.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)
 {
     SubcontractorHoursInformationBasicInformationGateway subcontractorHoursInformationBasicInformationGateway = new SubcontractorHoursInformationBasicInformationGateway(Data);
     subcontractorHoursInformationBasicInformationGateway.LoadByProjectIdRefId(projectId, refId, companyId);
 }