protected void Page_Load(object sender, EventArgs e)
        {
            RunSecurity(CRM.Code.Models.Admin.AllowedSections.NotSet);

            Entity = db.CRM_Persons.SingleOrDefault(c => c.ID.ToString() == Request.QueryString["id"]);
            ucLogHistory.IHistory       = Entity;
            ucLogNotes.INotes           = Entity;
            ucCustomFields._DataTableID = db._DataTables.Single(c => c.TableReference == "CRM_Person").ID;
            // buttons //
            btnDelete.EventHandler        = btnDelete_Click;
            btnSubmit.EventHandler        = btnSubmit_Click;
            btnSubmitChanges.EventHandler = btnSubmitChanges_Click;
            btnReinstate.EventHandler     = btnReinstate_Click;

            ucNavPerson.Entity = Entity;

            // Security //

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

            // confirmations //

            confirmationDelete.StandardDeleteHidden("person", btnRealDelete_Click);

            // process //


            ucDuplicate.BaseObject = new CRM_Person();
            txtFirstname.TextBox.Attributes[CRM_Person.DataKey]    = ((byte)CRM_Person.SearchKeys.Firstname).ToString();
            txtLastname.TextBox.Attributes[CRM_Person.DataKey]     = ((byte)CRM_Person.SearchKeys.Lastname).ToString();
            txtPrimaryEmail.TextBox.Attributes[CRM_Person.DataKey] = ((byte)CRM_Person.SearchKeys.PrimaryEmail).ToString();
            txtTelephone.TextBox.Attributes[CRM_Person.DataKey]    = ((byte)CRM_Person.SearchKeys.Telephone).ToString();
            txtDateOfBirth.TextBox.Attributes[CRM_Person.DataKey]  = ((byte)CRM_Person.SearchKeys.DoB).ToString();

            ((UserControlTextBox)ucAddress.FindControl("txtPostcode")).TextBox.Attributes[CRM_Person.DataKey] = ((byte)CRM_Person.SearchKeys.Postcode).ToString();

            ucDuplicate.OriginalID = Entity == null ? 0 : Entity.ID;
            CRMContext             = Entity;
            ucAddress.TownRequired = false;
            if (!IsPostBack)
            {
                ddlTitles.DataSource = CRM_Title.SetDropDownWithString(db.CRM_Titles.Cast <IArchivable>(), Entity == null ? "" : Entity.Title);
                ddlTitles.DataBind();

                if (Entity != null)
                {
                    PopulateFields();
                }
                else
                {
                    ucCustomFields.Populate(String.Empty);
                }
            }

            pnlAdultInfo.Visible = !chkIsChild.Checked;
            pnlAdultLeft.Visible = !chkIsChild.Checked;
        }
        private void PopulateFields()
        {
            ddlTitles.DataSource = CRM_Title.SetDropDownWithString(db.CRM_Titles.Cast <IArchivable>(), Entity.InvoiceTitle);
            ddlTitles.DataBind();

            txtPriceAgreed.Text        = Entity.PriceAgreed.ToString("N2");
            ddlPriceType.SelectedValue = Entity.PriceType.ToString();
            txtFirstname.Text          = Entity.InvoiceFirstname;
            txtLastname.Text           = Entity.InvoiceLastname;
            ddlTitles.SelectedValue    = Entity.InvoiceTitle;
            ucLogNotes.INotes          = Entity;
            ucLogHistory.IHistory      = Entity;
            ucLogHistory.ParentID      = Entity.ID.ToString();
            txtPONumber.Text           = Entity.PONumber;


            if (Entity.DatePaid != null)
            {
                txtDatePaid.Value = (DateTime)Entity.DatePaid;
            }

            if (Entity.InvoiceAddressID == null && Entity.PrimaryContactReference != "")
            {
                IContact Contact = ContactManager.GetIContactByReference(Entity.PrimaryContactReference);

                ucAddress.Populate(Contact.PrimaryAddress);

                ddlTitles.DataSource = CRM_Title.SetDropDownWithString(db.CRM_Titles.Cast <IArchivable>(), Contact.Title);
                ddlTitles.DataBind();

                ddlTitles.SelectedValue = Contact.Title;
                txtFirstname.Text       = Contact.Firstname;
                txtLastname.Text        = Contact.Lastname;
            }
            else
            {
                CRM_Address invoiceAddress = db.CRM_Addresses.SingleOrDefault(c => c.ID == Entity.InvoiceAddressID);

                if (invoiceAddress != null)
                {
                    ucAddress.Populate(invoiceAddress);
                }
                else
                {
                    IContact contact = new Code.Managers.ContactManager().Contacts.SingleOrDefault(c => c.Reference == Entity.PrimaryContactReference);


                    if (contact != null && contact.PrimaryAddress != null)
                    {
                        ucAddress.Populate(contact.PrimaryAddress);
                    }
                }

                ddlTitles.SelectedValue = Entity.InvoiceTitle;
                txtFirstname.Text       = Entity.InvoiceFirstname;
                txtLastname.Text        = Entity.InvoiceLastname;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ContactManager = new Code.Managers.ContactManager();
            RunSecurity(CRM.Code.Models.Admin.AllowedSections.NotSet);

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

            ucNavCal.Entity   = Entity;
            ucLogNotes.INotes = Entity;

            CRMContext = Entity;
            // buttons //

            btnSubmitChanges.EventHandler = btnSubmitChanges_Click;

            // Security //

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


            // process //

            ucAddCustomer.Config       = new AutoCompleteConfig(JSONSet.DataSets.contact);
            ucAddCustomer.EventHandler = lnkSelectCustomer_Click;

            if (!IsPostBack)
            {
                ddlTitles.DataSource = CRM_Title.SetDropDownWithString(db.CRM_Titles.Cast <IArchivable>(), String.Empty);
                ddlTitles.DataBind();
                PopulateFields();



                ReloadCalPerHead();
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            MainDataContext db = new MainDataContext();

            foreach (CRM_Person person in db.CRM_Persons)
            {
                CRM_Title title = db.CRM_Titles.FirstOrDefault(f => f.Name.ToLower().Trim() == person.Title.ToLower().Trim());

                if (title == null)
                {
                    title = new CRM_Title()
                    {
                        Name       = person.Title,
                        IsArchived = false,
                        IsActive   = true,
                        OrderNo    = Ordering.GetNextOrderID(db.CRM_Titles.OrderBy(o => o.OrderNo))
                    };

                    db.CRM_Titles.InsertOnSubmit(title);
                    db.SubmitChanges();
                }
            }
        }