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

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_RESOURCES_EMPLOYEES_VIEW"]) && Convert.ToBoolean(Session["sgLFS_RESOURCES_EMPLOYEES_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["employee_id"] == null) || ((string)Request.QueryString["active_tab"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in employees_summary.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfResourceType.Value = "Employees";
                hdfActiveTab.Value = Request.QueryString["active_tab"].ToString();
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfCurrentEmployeeId.Value = Request.QueryString["employee_id"];

                // Prepare initial data
                Session.Remove("employeesNotesDummy");
                Session.Remove("employeesCostsDummy");
                Session.Remove("employeesCostsExceptionsDummy");
                Session.Remove("employeesVacationsDummy");
                Session.Remove("costIdSelected");

                // ... for team member title
                int companyId = Int32.Parse(hdfCompanyId.Value.Trim());
                int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);

                EmployeeGateway employeeGateway = new EmployeeGateway();
                employeeGateway.LoadByEmployeeId(employeeId);
                lblTitleTeamMember.Text = "Team Member: " + employeeGateway.GetFullName(employeeId);

                // If coming from
                // ... employees_navigator2.aspx, employees_add.aspx
                if ((Request.QueryString["source_page"] == "employees_navigator2.aspx") || (Request.QueryString["source_page"] == "employees_add.aspx"))
                {
                    StoreNavigatorState();
                    ViewState["update"] = "yes";

                    // ... ... Set initial tab
                    if ((string)Session["dialogOpenedEmployees"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];

                        employeeInformationTDS = new EmployeeInformationTDS();

                        EmployeeInformationBasicInformation employeeInformationBasicInformation = new EmployeeInformationBasicInformation(employeeInformationTDS);
                        employeeInformationBasicInformation.LoadByEmployeeId(employeeId, companyId);

                        EmployeeInformationCostInformation employeeInformationCostInformation = new EmployeeInformationCostInformation(employeeInformationTDS);
                        employeeInformationCostInformation.LoadAllByEmployeeId(employeeId, companyId);

                        EmployeeInformationCostExceptionsInformation employeeInformationCostExceptionsInformation = new EmployeeInformationCostExceptionsInformation(employeeInformationTDS);
                        employeeInformationCostExceptionsInformation.LoadAllByEmployeeId(employeeId, companyId);

                        Session["costIdSelected"] = 0;

                        EmployeeInformationNoteInformation employeeInformationNoteInformationForEdit = new EmployeeInformationNoteInformation(employeeInformationTDS);
                        employeeInformationNoteInformationForEdit.LoadAllByEmployeeId(employeeId, companyId);

                        EmployeeInformationCategoryApproveTimesheetsInformation employeeInformationCategoryApproveTimesheetsInformation = new EmployeeInformationCategoryApproveTimesheetsInformation(employeeInformationTDS);
                        employeeInformationCategoryApproveTimesheetsInformation.LoadByEmployeeId(employeeId);
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabEmployees"];

                        // Restore datasets
                        employeeInformationTDS = (EmployeeInformationTDS)Session["employeeInformationTDS"];
                    }

                    tcDetailedInformation.ActiveTabIndex = Int32.Parse(hdfActiveTab.Value);

                    // Store dataset
                    Session["employeeInformationTDS"] = employeeInformationTDS;
                }

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

                    // Restore dataset
                    employeeInformationTDS = (EmployeeInformationTDS)Session["employeeInformationTDS"];

                    // ... Set initial tab
                    if ((string)Session["dialogOpenedEmployees"] != "1")
                    {
                        hdfActiveTab.Value = Request.QueryString["active_tab"];
                    }
                    else
                    {
                        hdfActiveTab.Value = (string)Session["activeTabEmployees"];
                    }

                    tcDetailedInformation.ActiveTabIndex = Int32.Parse(hdfActiveTab.Value);
                }

                string filterOptions = string.Format("CostID = {0} AND Deleted = {1}", 0, 0);
                odsCostsExceptions.FilterExpression = filterOptions;

                // Prepare initial data
                // ... Data for current employee
                LoadData();

                // Databind
                Page.DataBind();
            }
            else
            {
                // Restore datasets
                employeeInformationTDS = (EmployeeInformationTDS)Session["employeeInformationTDS"];

                // Set initial tab
                int activeTab = Int32.Parse(hdfActiveTab.Value);
                tcDetailedInformation.ActiveTabIndex = activeTab;
            }
        }
        protected void grdCosts_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("EditCost");
            if (Page.IsValid)
            {
                int costId = (int)e.Keys["CostID"];
                int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);

                string unitOfMeasurement = ((DropDownList)grdCosts.Rows[e.RowIndex].Cells[0].FindControl("ddlUnitOfMeasurementEdit")).SelectedValue;
                decimal payRateCad = Decimal.Parse(((TextBox)grdCosts.Rows[e.RowIndex].Cells[0].FindControl("tbxPayRateCadEdit")).Text.Trim());
                decimal payRateUsd = Decimal.Parse(((TextBox)grdCosts.Rows[e.RowIndex].Cells[0].FindControl("tbxPayRateUsdEdit")).Text.Trim());

                decimal burdenRateCad = 0;
                decimal burdenRateUsd = 0;
                decimal totalCostCad = 0;
                decimal totalCostUsd = 0;
                if (tbxBourdenFactor.Text != "")
                {
                    decimal boudenFactor = decimal.Parse(tbxBourdenFactor.Text) / 100;
                    burdenRateCad = payRateCad * boudenFactor;
                    totalCostCad = payRateCad + burdenRateCad;
                    totalCostCad = Decimal.Round(totalCostCad, 2);

                    burdenRateUsd = payRateUsd * boudenFactor;
                    totalCostUsd = payRateUsd + burdenRateUsd;
                    totalCostUsd = Decimal.Round(totalCostUsd, 2);
                }

                DateTime date = ((RadDatePicker)grdCosts.Rows[e.RowIndex].Cells[0].FindControl("tkrdpDateEdit")).SelectedDate.Value;

                decimal healthBenefitUsd = 0;
                if (tbxUSHealthBenefitFactor.Text != "")
                {
                    if (tbxBenefitFactorUsd.Text != "")
                    {
                        decimal healthBenefitFactor = Decimal.Parse(tbxUSHealthBenefitFactor.Text) / 100;
                        healthBenefitUsd = decimal.Parse(tbxBenefitFactorUsd.Text) * healthBenefitFactor;
                    }
                }

                // Update data
                EmployeeInformationCostInformation model = new EmployeeInformationCostInformation(employeeInformationTDS);
                model.Update(costId, employeeId, unitOfMeasurement, payRateCad, burdenRateCad, totalCostCad, payRateUsd, burdenRateUsd, totalCostUsd, date, healthBenefitUsd);

                // Store dataset
                Session["employeeInformationTDS"] = employeeInformationTDS;
            }
            else
            {
                e.Cancel = true;
            }
        }
        private void UpdateDatabase()
        {
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);

            DB.Open();
            DB.BeginTransaction();
            try
            {
                // Save type history information
                EmployeeInformationTypeHistoryInformation employeeInformationTypeHistoryInformation = new EmployeeInformationTypeHistoryInformation(employeeInformationTDS);
                employeeInformationTypeHistoryInformation.Save(employeeId, companyId);

                // Save notes information
                EmployeeInformationNoteInformation employeeInformationNoteInformation = new EmployeeInformationNoteInformation(employeeInformationTDS);
                employeeInformationNoteInformation.Save(companyId);

                // Save costs information
                EmployeeInformationCostInformation employeeInformationCostInformation = new EmployeeInformationCostInformation(employeeInformationTDS);
                employeeInformationCostInformation.Save(companyId);

                // Save costs exceptions information
                EmployeeInformationCostExceptionsInformation employeeInformationCostExceptionsInformation = new EmployeeInformationCostExceptionsInformation(employeeInformationTDS);
                employeeInformationCostExceptionsInformation.Save(companyId, employeeId);

                // Save categories approve timesheets information
                EmployeeInformationCategoryApproveTimesheetsInformation employeeInformationCategoryApproveTimesheetsInformation = new EmployeeInformationCategoryApproveTimesheetsInformation(employeeInformationTDS);
                employeeInformationCategoryApproveTimesheetsInformation.Save();

                // Save employee information
                EmployeeInformationBasicInformation employeeInformationBasicInformation = new EmployeeInformationBasicInformation(employeeInformationTDS);
                employeeInformationBasicInformation.Save(companyId);

                DB.CommitTransaction();

                // Store datasets
                employeeInformationTDS.AcceptChanges();
                Session["employeeInformationTDS"] = employeeInformationTDS;
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        protected void grdCosts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Costs Gridview, if the gridview is edition mode
            if (grdCosts.EditIndex >= 0)
            {
                grdCosts.UpdateRow(grdCosts.EditIndex, true);
            }

            int costId = (int)e.Keys["CostID"];
            int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);

            EmployeeInformationCostInformation model = new EmployeeInformationCostInformation(employeeInformationTDS);

            // Delete cost
            model.Delete(costId, employeeId);

            // Store dataset
            Session["employeeInformationTDS"] = employeeInformationTDS;
        }
        private void GrdCostsAdd()
        {
            if (ValidateCostsFooter())
            {
                Page.Validate("AddCost");
                if (Page.IsValid)
                {
                    int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);
                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    DateTime date_ = ((RadDatePicker)grdCosts.FooterRow.FindControl("tkrdpDateNew")).SelectedDate.Value;
                    string unitOfMeasurement = ((DropDownList)grdCosts.FooterRow.FindControl("ddlUnitOfMeasurementNew")).SelectedValue;
                    decimal payRateCad = Decimal.Parse(((TextBox)grdCosts.FooterRow.FindControl("tbxPayRateCadNew")).Text.Trim());
                    decimal payRateUsd = Decimal.Parse(((TextBox)grdCosts.FooterRow.FindControl("tbxPayRateUsdNew")).Text.Trim());

                    decimal burdenRateCad = 0;
                    decimal totalCostCad = 0;
                    decimal burdenRateUsd = 0;
                    decimal totalCostUsd = 0;

                    if (tbxBourdenFactor.Text != "")
                    {
                        decimal boudenFactor = decimal.Parse(tbxBourdenFactor.Text) / 100;
                        burdenRateCad = payRateCad * boudenFactor;
                        totalCostCad = payRateCad + burdenRateCad;
                        totalCostCad = Decimal.Round(totalCostCad, 2);

                        burdenRateUsd = payRateUsd * boudenFactor;
                        totalCostUsd = payRateUsd + burdenRateUsd;
                        totalCostUsd = Decimal.Round(totalCostUsd, 2);
                    }

                    decimal benefitFactorCad = Decimal.Parse(tbxBenefitFactorCad.Text);
                    decimal benefitFactorUsd = Decimal.Parse(tbxBenefitFactorUsd.Text);

                    decimal healthBenefitUsd = 0;
                    if (tbxUSHealthBenefitFactor.Text != "")
                    {
                        if (tbxBenefitFactorUsd.Text != "")
                        {
                            decimal healthBenefitFactor = Decimal.Parse(tbxUSHealthBenefitFactor.Text) / 100;
                            healthBenefitUsd = decimal.Parse(tbxBenefitFactorUsd.Text) * healthBenefitFactor;
                        }
                    }

                    EmployeeInformationCostInformation model = new EmployeeInformationCostInformation(employeeInformationTDS);
                    model.Insert(employeeId, date_, unitOfMeasurement, payRateCad, burdenRateCad, totalCostCad, payRateUsd, burdenRateUsd, totalCostUsd, false, companyId, false, benefitFactorCad, benefitFactorUsd, healthBenefitUsd);

                    Session.Remove("employeesCostsDummy");
                    Session["employeeInformationTDS"] = employeeInformationTDS;

                    grdCosts.DataBind();
                }
            }
        }