protected void Page_Load(object sender, EventArgs e)
        {
            pnlImportPrompt.Visible = false;
            RunSecurity(CRM.Code.Models.Admin.AllowedSections.NotSet);
            Entity = db.CRM_Fundraisings.SingleOrDefault(c => c.ID.ToString() == Request.QueryString["id"]);

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

            ucCustomFields._DataTableID = db._DataTables.Single(c => c.TableReference == "CRM_Fundraising").ID;
            // buttons //

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

            ucACPrimaryContact.EventHandler = lnkSelect_Click;
            ucACPrimaryContact.Config       = new AutoCompleteConfig(JSONSet.DataSets.contact);

            // Security //

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

            // confirmations //

            confirmationDelete.StandardDeleteHidden("donation", btnRealDelete_Click);

            CRMContext        = Entity;
            ucLogNotes.INotes = Entity;

            // process //

            if (!IsPostBack)
            {
                Session.Remove(SplitListConst);
                ddlPaymentType.DataSource = CRM_PaymentType.SetDropDown(db.CRM_PaymentTypes.OrderBy(o => o.OrderNo).Cast <IArchivable>(),
                                                                        Entity == null ? null : Entity.CRM_PaymentType);
                ddlPaymentType.DataBind();

                ddlFundReason.DataSource = CRM_FundraisingReason.SetDropDown(db.CRM_FundraisingReasons.OrderBy(o => o.OrderNo).Cast <IArchivable>(),
                                                                             Entity == null ? null : Entity.CRM_FundraisingReason);
                ddlFundReason.DataBind();

                BindFundDDL(ddlFund);

                if (Entity != null)
                {
                    PopulateFields();
                    SplitList = Entity.CRM_FundraisingSplits.ToList();

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

            object oldEntity  = null;
            object oldAddress = null;


            if (newRecord)
            {
                Entity = new CRM_Fundraising();
                db.CRM_Fundraisings.InsertOnSubmit(Entity);
            }
            else
            {
                oldEntity  = Entity.ShallowCopy();
                oldAddress = Entity.CRM_Address.ShallowCopy();
            }


            int recurring = 0;

            Int32.TryParse(txtRecurringWeeks.Text, out recurring);

            Entity.IsInKind                = chkIsInKind.Checked;
            Entity.PledgedAmount           = Convert.ToDecimal(txtAmount.Text);
            Entity.IsGiftAid               = chkIsGiftAid.Checked;
            Entity.GiftAidFirstname        = txtGiftaidFirstname.Text;
            Entity.GiftAidLastname         = txtGiftaidLastname.Text;
            Entity.IsRecurring             = chkIsRecurring.Checked;
            Entity.RecurringEveryWeeks     = recurring;
            Entity.Duration                = Convert.ToInt32(txtDuration.Text);
            Entity.CRM_PaymentTypeID       = Convert.ToInt32(ddlPaymentType.SelectedValue);
            Entity.CRM_FundraisingReasonID = Convert.ToInt32(ddlFundReason.SelectedValue);
            Entity.PrimaryContactReference = ucACPrimaryContact.SelectedID;

            using (ContactManager manager = new ContactManager())
            {
                IContact selectedContact = new ContactManager().Contacts.Single(c => c.Reference == ucACPrimaryContact.SelectedID);

                Entity.ContactName = selectedContact.Fullname;
                Entity.ContactType = selectedContact.DisplayName;
                manager.Dispose();
            }

            if (newRecord)
            {
                CRM_Address address = new CRM_Address();
                Entity.CRM_Address = (CRM_Address)((Address)ucGiftAidAddress).Save(address);
            }
            else
            {
                Entity.CRM_Address = (CRM_Address)((Address)ucGiftAidAddress).Save(Entity.CRM_Address);
            }

            db.SubmitChanges();

            db.CRM_FundraisingSplits.DeleteAllOnSubmit(Entity.CRM_FundraisingSplits);
            db.SubmitChanges();

            foreach (CRM_FundraisingSplit serialSplit in GetSplitsFromRepeater())
            {
                CRM_FundraisingSplit split = new CRM_FundraisingSplit()
                {
                    Amount            = serialSplit.Amount,
                    CRM_FundID        = serialSplit.CRM_FundID,
                    DateGiven         = serialSplit.DateGiven,
                    GiftAidRate       = serialSplit.GiftAidRate,
                    CRM_FundRaisingID = Entity.ID
                };

                db.CRM_FundraisingSplits.InsertOnSubmit(split);
                db.SubmitChanges();
            }

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

            ucCustomFields.Save(Entity.Reference);

            Session.Remove(SplitListConst);
        }