protected void Page_Load(object sender, EventArgs e)
        {
            RunSecurity(CRM.Code.Models.Admin.AllowedSections.NotSet);
            PersonFamily = db.CRM_FamilyPersons.SingleOrDefault(c => c.ID.ToString() == Request.QueryString["pid"]);

            btnSubmitChanges.Visible = PermissionManager.CanUpdate;
            if (!PermissionManager.CanAdd && Entity == null)
            {
                Response.Redirect("list.aspx");
            }

            ucNavPerson.Entity = Entity;
            ucLogNotes.INotes  = PersonFamily;

            ucACFamily.EventHandler = lnkSelect_Click;
            ucACFamily.Config       = new AutoCompleteConfig(JSONSet.DataSets.family);

            // buttons //

            btnSubmit.EventHandler        = btnSubmit_Click;
            btnSubmitChanges.EventHandler = btnSubmitChanges_Click;
            btnDelete.EventHandler        = btnDelete_Click;

            // Security //

            btnSubmitChanges.Visible = PermissionManager.CanUpdate;
            btnDelete.Visible        = PermissionManager.CanDelete;
            if (!PermissionManager.CanAdd && Entity == null)
            {
                Response.Redirect("list.aspx");
            }

            // confirmations //

            confirmationDelete.PermanentDeleteHidden("personal family record", btnRealDelete_Click);

            // process //


            CRMContext = PersonFamily;

            if (!IsPostBack)
            {
                if (PersonFamily != null)
                {
                    PopulateFields();
                }
                else if (!String.IsNullOrEmpty(Request.QueryString["familycheck"]))
                {
                    CRM_Family family = db.CRM_Families.Single(v => v.Reference.ToString() == Request.QueryString["familycheck"]);
                    ucACFamily.Populate(family);
                }
            }

            PopulateFamilyList();
        }
        protected void SaveRecord(bool newRecord)
        {
            // new record / exiting record //

            object oldEntity = null;

            if (newRecord)
            {
                PersonFamily = new CRM_FamilyPerson();
                PersonFamily.CRM_PersonID = Entity.ID;
                db.CRM_FamilyPersons.InsertOnSubmit(PersonFamily);
            }
            else
            {
                oldEntity = PersonFamily.ShallowCopy();
            }

            if (ucACFamily.SelectedID == "")
            {
                CRM_Family family = new CRM_Family()
                {
                    Name = ucACFamily.Text
                };

                db.CRM_Families.InsertOnSubmit(family);
                PersonFamily.CRM_Family = family;
            }
            else
            {
                PersonFamily.CRM_FamilyID = CRM_Family.BaseSet(db).Single(s => s.Reference == ucACFamily.SelectedID).ID;
            }

            db.SubmitChanges();

            if (oldEntity != null)
            {
                CRM.Code.History.History.RecordLinqUpdate(db, AdminUser, oldEntity, PersonFamily);
                db.SubmitChanges();
            }
            else
            {
                CRM.Code.History.History.RecordLinqInsert(AdminUser, PersonFamily);
            }
        }