// ////////////////////////////////////////////////////////////////////////
        // 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;
            }
        }
        private void Save()
        {
            // Validate data
            bool validData = false;

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

                // ... Validate notes tab
                if (ValidateNotesFooter())
                {
                    Page.Validate("notesDataAdd");
                    if (!Page.IsValid)
                    {
                        validData = false;
                    }
                }

                Page.Validate("notesDataEdit");
                if (!Page.IsValid)
                {
                    validData = false;
                }
            }

            // For valid data
            if (validData)
            {
                // Notes Gridview, if the gridview is edition mode
                if (grdNotes.EditIndex >= 0)
                {
                    grdNotes.UpdateRow(grdNotes.EditIndex, true);
                }

                // Save cost and notes data
                GrdNotesAdd();

                // Costs Gridview, if the gridview is edition mode
                if (grdCosts.EditIndex >= 0)
                {
                    grdCosts.UpdateRow(grdCosts.EditIndex, true);
                }

                // Save costs data
                GrdCostsAdd();

                // Costs Exceptions Gridview, if the gridview is edition mode
                if (grdCostsExceptions.EditIndex >= 0)
                {
                    grdCostsExceptions.UpdateRow(grdCostsExceptions.EditIndex, true);
                }

                // Save costs exceptions data
                GrdCostsExceptionsAdd();

                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);

                // ... Get basic employee data
                string newFirstName = tbxFisrtName.Text.Trim();
                string newLastName = tbxLastName.Text.Trim();
                string newEmail = tbxeMail.Text.Trim();
                string newType = ddlType.SelectedValue;
                string newState = ddlState.SelectedValue;
                bool newIsSalesman = ckbxIsSalesman.Checked;
                bool newRequestProjectTime = ckbxRequestTimesheet.Checked;
                bool newSalaried = ckbxSalaried.Checked;
                bool newAssignableSrs = ckbxAssignableSrs.Checked;
                string newJobClass = ddlJobClassType.SelectedValue;
                string newCategory = ddlCategory.SelectedValue;
                string newPersonalAgency = ddlPersonalAgency.SelectedValue;
                bool newVacationsManager = ckbxVacationsManager.Checked;
                bool newApproveTimesheets = ckbxApproveTimesheets.Checked;
                string newCrew = ""; if (ddlCrew.SelectedValue != "") newCrew = ddlCrew.SelectedValue;

                // Update basic information data
                EmployeeInformationBasicInformation employeeInformationBasicInformation = new EmployeeInformationBasicInformation(employeeInformationTDS);
                employeeInformationBasicInformation.Update(employeeId, newFirstName, newLastName, newType, newState, newIsSalesman, newRequestProjectTime, newSalaried, newEmail, newAssignableSrs, newJobClass, newCategory, newPersonalAgency, newVacationsManager, newApproveTimesheets, newCrew);

                // Update categories approve timesheets data
                EmployeeInformationCategoryApproveTimesheetsInformation employeeInformationCategoryApproveTimesheetsInformation = new EmployeeInformationCategoryApproveTimesheetsInformation(employeeInformationTDS);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Field", ckbxField.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Field 44", ckbxField44.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Mechanic/Manufactoring", ckbxMechanicManufactoring.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Office/admin", ckbxOfficeAdmin.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Special Forces", ckbxSpecialForces.Checked);

                // Insert type to history
                EmployeeInformationTypeHistoryInformation employeeInformationTypeHistoryInformation = new EmployeeInformationTypeHistoryInformation(employeeInformationTDS);
                employeeInformationTypeHistoryInformation.Insert(employeeId, DateTime.Now, newType, false, companyId, false);

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

                // Update database
                UpdateDatabase();

                ViewState["update"] = "yes";

                // Redirect
                string url = "";
                if (Request.QueryString["source_page"] == "employees_navigator2.aspx" )
                {
                    url = "./employees_navigator2.aspx?source_page=employees_edit.aspx&employee_id=" + hdfCurrentEmployeeId.Value + GetNavigatorState() + "&update=yes";
                }

                if (Request.QueryString["source_page"] == "employees_summary.aspx")
                {
                    string activeTab = hdfActiveTab.Value;
                    url = "./employees_summary.aspx?source_page=employees_edit.aspx&employee_id=" + hdfCurrentEmployeeId.Value + "&active_tab=" + activeTab + GetNavigatorState() + "&update=yes";
                }

                Response.Redirect(url);
            }
        }
        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);
            }
        }
        private void Apply()
        {
            // Validate data
            bool validData = false;

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

                // ... Validate notes tab
                if (ValidateNotesFooter())
                {
                    Page.Validate("notesDataAdd");
                    if (!Page.IsValid)
                    {
                        validData = false;
                    }
                }

                Page.Validate("notesDataEdit");
                if (!Page.IsValid)
                {
                    validData = false;
                }
            }

            // For valid data
            if (validData)
            {
                // Notes Gridview, if the gridview is edition mode
                if (grdNotes.EditIndex >= 0)
                {
                    grdNotes.UpdateRow(grdNotes.EditIndex, true);
                }

                // Costs Gridview, if the gridview is edition mode
                if (grdCosts.EditIndex >= 0)
                {
                    grdCosts.UpdateRow(grdCosts.EditIndex, true);
                }

                // Costs Exceptions Gridview, if the gridview is edition mode
                if (grdCostsExceptions.EditIndex >= 0)
                {
                    grdCostsExceptions.UpdateRow(grdCostsExceptions.EditIndex, true);
                }

                // Save data
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int employeeId = Int32.Parse(hdfCurrentEmployeeId.Value);

                // ... Get basic employee data
                string newFirstName = tbxFisrtName.Text.Trim();
                string newLastName = tbxLastName.Text.Trim();
                string newEmail = tbxeMail.Text.Trim();
                string newType = ddlType.SelectedValue;
                string newState = ddlState.SelectedValue;
                bool newIsSalesman = ckbxIsSalesman.Checked;
                bool newRequestProjectTime = ckbxRequestTimesheet.Checked;
                bool newSalaried = ckbxSalaried.Checked;
                bool newAssignableSrs = ckbxAssignableSrs.Checked;
                string newJobClass = ddlJobClassType.SelectedValue;
                string newCategory = ddlCategory.SelectedValue;
                string newPersonalAgency = ddlPersonalAgency.SelectedValue;
                bool newVacationsManager = ckbxVacationsManager.Checked;
                bool newApproveTimesheets = ckbxApproveTimesheets.Checked;
                string newCrew = ""; if (ddlCrew.SelectedValue != "") newCrew = ddlCrew.SelectedValue;

                // Update service data
                EmployeeInformationBasicInformation employeeInformationBasicInformation = new EmployeeInformationBasicInformation(employeeInformationTDS);
                employeeInformationBasicInformation.Update(employeeId, newFirstName, newLastName, newType, newState, newIsSalesman, newRequestProjectTime, newSalaried, newEmail, newAssignableSrs, newJobClass, newCategory, newPersonalAgency, newVacationsManager, newApproveTimesheets, newCrew);

                // Update categories approve timesheets data
                EmployeeInformationCategoryApproveTimesheetsInformation employeeInformationCategoryApproveTimesheetsInformation = new EmployeeInformationCategoryApproveTimesheetsInformation(employeeInformationTDS);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Field", ckbxField.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Field 44", ckbxField44.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Mechanic/Manufactoring", ckbxMechanicManufactoring.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Office/admin", ckbxOfficeAdmin.Checked);
                employeeInformationCategoryApproveTimesheetsInformation.Update(employeeId, "Special Forces", ckbxSpecialForces.Checked);

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

                // Update database
                UpdateDatabase();

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