/// <summary>
        /// Save all personalAgencyName to database (direct)
        /// </summary>
        /// <param name="companyId">companyId</param>        
        public void Save(int companyId)
        {
            PersonalAgencyInformationTDS personalAgencyInformationChanges = (PersonalAgencyInformationTDS)Data.GetChanges();

            if (personalAgencyInformationChanges != null)
            {
                if (personalAgencyInformationChanges.PersonalAgencyInformation.Rows.Count > 0)
                {
                    PersonalAgencyInformationGateway sizeInformationGateway = new PersonalAgencyInformationGateway(personalAgencyInformationChanges);

                    foreach (PersonalAgencyInformationTDS.PersonalAgencyInformationRow row in (PersonalAgencyInformationTDS.PersonalAgencyInformationDataTable)personalAgencyInformationChanges.PersonalAgencyInformation)
                    {
                        // Insert new personal agency
                        if ((!row.Deleted) && (!row.InDatabase))
                        {
                            EmployeePersonalAgencyGateway employeePersonalAgencyGateway = new EmployeePersonalAgencyGateway();
                            EmployeePersonalAgency employeePersonalAgency = new EmployeePersonalAgency(null);

                            if (!employeePersonalAgencyGateway.ExistPersonalAgency(row.PersonalAgencyName, row.COMPANY_ID))
                            {
                                employeePersonalAgency.InsertDirect(row.PersonalAgencyName, row.COMPANY_ID, row.Deleted);
                            }
                            else
                            {
                                employeePersonalAgency.UnDeleteDirect(row.PersonalAgencyName, row.COMPANY_ID);
                            }
                        }

                        // Update personal agency
                        if ((!row.Deleted) && (row.InDatabase))
                        {
                            string originalPersonalAgencyName = row.NewPersonalAgencyName;
                            string newPersonalAgencyName = row.PersonalAgencyName;
                            int originalCompanyId = companyId;

                            EmployeePersonalAgencyGateway employeePersonalAgencyGateway = new EmployeePersonalAgencyGateway();
                            EmployeePersonalAgency employeePersonalAgency = new EmployeePersonalAgency(null);

                            if (!employeePersonalAgencyGateway.ExistPersonalAgency(row.PersonalAgencyName, row.COMPANY_ID))
                            {
                                employeePersonalAgency.InsertDirect(newPersonalAgencyName, originalCompanyId, false);
                            }
                            else
                            {
                                employeePersonalAgency.UnDeleteDirect(newPersonalAgencyName, originalCompanyId);
                            }

                            if (employeePersonalAgencyGateway.IsUsedInEmployee(originalPersonalAgencyName, companyId))
                            {
                                EmployeeGateway employeeGateway = new EmployeeGateway(null);
                                employeeGateway.UpdatePersonalAgencyName(originalPersonalAgencyName, newPersonalAgencyName);
                            }

                            employeePersonalAgency.DeleteDirect(originalPersonalAgencyName, companyId);
                        }

                        // Delete personal agency
                        if ((row.Deleted) && (row.InDatabase))
                        {
                            EmployeePersonalAgency employeePersonalAgency = new EmployeePersonalAgency(null);
                            employeePersonalAgency.DeleteDirect(row.PersonalAgencyName, 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_RESOURCES_EMPLOYEES_ADMIN"]))
                {
                    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)
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in employees_personal_agency.aspx");
                }

                // Tag Page
                hdfCompanyId.Value = Session["companyID"].ToString();
                hdfUpdate.Value = "yes";
                Session.Remove("personalAgencyInformationDummy");
                Session.Remove("personalAgencyInformation");

                ViewState["update"] = Request.QueryString["update"];

                // ... Load personalAgency to edit
                personalAgencyInformationTDS = new PersonalAgencyInformationTDS();

                PersonalAgencyInformationGateway personalAgencyInformationGateway = new PersonalAgencyInformationGateway(personalAgencyInformationTDS);
                personalAgencyInformationGateway.Load(Int32.Parse(hdfCompanyId.Value.Trim()));

                // ... Store datasets
                Session["personalAgencyInformationTDS"] = personalAgencyInformationTDS;
                Session["personalAgencyInformation"] = personalAgencyInformationTDS.PersonalAgencyInformation;
            }
            else
            {
                // Restore datasets
                personalAgencyInformationTDS = (PersonalAgencyInformationTDS)Session["personalAgencyInformationTDS"];
                personalAgencyInformation = personalAgencyInformationTDS.PersonalAgencyInformation;
            }
        }
        protected void cvPersonalAgencyNameEdit_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            string originalPersonalAgencyName = ((HiddenField)grdPersonalAgency.Rows[grdPersonalAgency.EditIndex].FindControl("hdfPersonalAgencyNameEdit")).Value;

            if (originalPersonalAgencyName != args.Value.Trim())
            {
                PersonalAgencyInformationGateway personalAgencyInformationGateway = new PersonalAgencyInformationGateway(personalAgencyInformationTDS);

                if (personalAgencyInformationGateway.Table.Rows.Count > 0)
                {
                    foreach (PersonalAgencyInformationTDS.PersonalAgencyInformationRow row in (PersonalAgencyInformationTDS.PersonalAgencyInformationDataTable)personalAgencyInformationGateway.Table)
                    {
                        if (row.PersonalAgencyName == args.Value.Trim())
                        {
                            args.IsValid = false;
                        }
                    }
                }
            }
        }
        protected void cvPersonalAgencyNameNew_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            PersonalAgencyInformationGateway personalAgencyInformationGateway = new PersonalAgencyInformationGateway(personalAgencyInformationTDS);

            if (personalAgencyInformationGateway.Table.Rows.Count > 0)
            {
                foreach (PersonalAgencyInformationTDS.PersonalAgencyInformationRow row in (PersonalAgencyInformationTDS.PersonalAgencyInformationDataTable)personalAgencyInformationGateway.Table)
                {
                    if (row.PersonalAgencyName == args.Value.Trim())
                    {
                        args.IsValid = false;
                    }
                }
            }
        }