private void RevertBtn_Click(object sender, EventArgs e)
 {
     try
     {
         //Deal with salutations
         if (client.salutations != null)
         {
             SalDDL.DataSource    = client.salutations;
             SalDDL.DisplayMember = "Salutation";
             SalDDL.ValueMember   = "Addressee";
             sal = (XLMain.Salutation)SalDDL.SelectedItem;
             if (sal != null)
             {
                 AddresseeTB.Text  = sal.addressee;
                 SalutationTb.Text = sal.salutation;
             }
         }
         //Deal with addresses
         if (docType == "Letter")
         {
             if (client.addresses != null)
             {
                 allAddresses               = client.addresses;
                 addressesDDL.DataSource    = allAddresses;
                 addressesDDL.DisplayMember = "address1";
                 addressesDDL.ValueMember   = "addressBlock";
                 add = (XLMain.Address)addressesDDL.SelectedItem;
                 if (add != null)
                 {
                     addTB.Text = add.addressBlock;
                 }
             }
         }
         else if (docType == "Fax")
         {
             XLMain.Number fax = XLMain.Number.GetNumber(client.crmID, "Fax");
             if (fax != null)
             {
                 FaxTB.Text = fax.number;
             }
         }
         RevertBtn.Visible = false;
         XLDocument.UpdateBookmark("Salutation", SalutationTb.Text);
         XLDocument.UpdateBookmark("Addressee", AddresseeTB.Text);
         XLDocument.UpdateBookmark("Addressee2", AddresseeTB.Text);
         XLDocument.UpdateBookmark("Address", addTB.Text);
         XLDocument.UpdateBookmark("Fax", FaxTB.Text);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Unable to revert");
         XLtools.LogException("RevertBtn", ex.ToString());
     }
 }
        private void IPSContactBtn_Click(object sender, EventArgs e)
        {
            try
            {
                IPSContForm myForm = new IPSContForm(client);
                myForm.ShowDialog();
                XLInsol.Contact selectContact = myForm.selectedContact;
                if (selectContact != null)
                {
                    if (docType == "Letter")
                    {
                        if (selectContact != null)
                        {
                            allAddresses            = null;
                            addressesDDL.DataSource = null;
                            string add = selectContact.name;
                            add       += Environment.NewLine + selectContact.addressBlock;
                            addTB.Text = add;
                        }
                    }
                    else if (docType == "Fax")
                    {
                        if (selectContact.fax != null)
                        {
                            FaxTB.Text = selectContact.fax;
                        }
                    }
                    SalDDL.DataSource = null;
                    AddresseeTB.Text  = "";
                    SalutationTb.Text = "";

                    RevertBtn.Visible = true;
                    XLDocument.UpdateBookmark("Salutation", SalutationTb.Text);
                    XLDocument.UpdateBookmark("Addressee", AddresseeTB.Text);
                    XLDocument.UpdateBookmark("Addressee2", AddresseeTB.Text);
                    XLDocument.UpdateBookmark("Address", addTB.Text);
                    XLDocument.UpdateBookmark("Fax", FaxTB.Text);
                }
                else
                {
                    MessageBox.Show("No address returned");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to fetch insolvency contact");
                XLtools.LogException("InsolContactBtn", ex.ToString());
            }
        }
        private void GetAddressBtn_Click(object sender, EventArgs e)
        {
            try
            {
                ContForm myForm = new ContForm();
                myForm.ShowDialog();
                XLMain.Contact selectContact = myForm.selectedContact;
                if (selectContact != null)
                {
                    if (docType == "Letter")
                    {
                        if (selectContact.addresses != null)
                        {
                            allAddresses               = selectContact.addresses;
                            addressesDDL.DataSource    = allAddresses;
                            addressesDDL.DisplayMember = "address1";
                            addressesDDL.ValueMember   = "addressBlock";
                            add = (XLMain.Address)addressesDDL.SelectedItem;
                            if (add != null)
                            {
                                addTB.Text = add.addressBlock;
                            }
                        }
                    }
                    else if (docType == "Fax")
                    {
                        XLMain.Number fax = XLMain.Number.GetNumber(client.crmID, "Fax");
                        if (fax != null)
                        {
                            FaxTB.Text = fax.number;
                        }
                    }

                    //Deal with salutations
                    if (selectContact.salutations != null)
                    {
                        SalDDL.DataSource    = selectContact.salutations;
                        SalDDL.DisplayMember = "Salutation";
                        SalDDL.ValueMember   = "Addressee";
                        sal = (XLMain.Salutation)SalDDL.SelectedItem;
                        if (sal != null)
                        {
                            AddresseeTB.Text  = sal.addressee;
                            SalutationTb.Text = sal.salutation;
                            SalutationTb_Leave(this, null);
                        }
                    }
                    else
                    {
                        SalDDL.DataSource    = null;
                        SalDDL.DisplayMember = "Salutation";
                        SalDDL.ValueMember   = "Addressee";
                        AddresseeTB.Text     = "";
                        SalutationTb.Text    = "";
                    }
                    RevertBtn.Visible = true;
                    XLDocument.UpdateBookmark("Salutation", SalutationTb.Text);
                    XLDocument.UpdateBookmark("Addressee", AddresseeTB.Text);
                    XLDocument.UpdateBookmark("Addressee2", AddresseeTB.Text);
                    XLDocument.UpdateBookmark("Address", addTB.Text);
                    XLDocument.UpdateBookmark("Fax", FaxTB.Text);
                }
                else
                {
                    MessageBox.Show("No address returned");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to fetch contact");
                XLtools.LogException("TaskPane-ContactBtn", ex.ToString());
            }
        }
        public XLTaskPane()
        {
            try
            {
                InitializeComponent();
                string str = XLDocument.ReadParameter("CRMid");
                docType = XLDocument.ReadParameter("DocType");

                client = XLMain.Client.FetchClient(str);
                //ClientIDLabel.Text = client.crmID;

                user  = XLMain.Staff.StaffFromUser(Environment.UserName);
                users = XLtools.StaffList(user, client, false, true);

                SenderDDL.DataSource    = users;
                SenderDDL.DisplayMember = "name";
                SenderDDL.ValueMember   = "crmID";
                SenderDDL.SelectedItem  = user;
                sender = user;
                XLDocument.UpdateParameter("Sender", sender.username);

                if (client != null)
                {
                    //set basic client info
                    string clientStr = client.clientcode + " - " + client.name;
                    ClientLbl.Text = clientStr;
                    SubjectTB.Text = client.name;
                    string fileStore = XLVirtualCabinet.FileStore(client.manager.office, client.department);
                    XLDocument.UpdateParameter("Cabinet", fileStore);
                    XLDocument.UpdateParameter("ClientStr", clientStr);

                    //Deal with salutations
                    if (client.salutations != null)
                    {
                        SalDDL.DataSource    = client.salutations;
                        SalDDL.DisplayMember = "Salutation";
                        SalDDL.ValueMember   = "Addressee";
                        sal = (XLMain.Salutation)SalDDL.SelectedItem;
                        if (sal != null)
                        {
                            AddresseeTB.Text  = sal.addressee;
                            SalutationTb.Text = sal.salutation;
                        }
                    }

                    //Add the appropriate header
                    XLDocument.Header clientHeader = XLDocument.MapHeader(client.office, client.department);


                    //Set up depending on whether it is insolvency or not
                    if (client.department == "INS")
                    {
                        //Deal with when calling ddl
                        WhencallingDDL.DataSource    = users;
                        WhencallingDDL.DisplayMember = "name";
                        WhencallingDDL.ValueMember   = "crmID";
                        WhenCallingCheck.Checked     = true;
                        SubjectTB.Text = XLInsol.GetSubject(client.crmID);
                    }
                    else
                    {
                        FAOBCheck.Checked        = true;
                        PandCCheck.Checked       = true;
                        WhenCallingCheck.Checked = false;
                    }

                    //Deal with addresses or fax no as appropriate
                    if (docType == "Letter")
                    {
                        if (client.addresses != null)
                        {
                            allAddresses               = client.addresses;
                            addressesDDL.DataSource    = allAddresses;
                            addressesDDL.DisplayMember = "address1";
                            addressesDDL.ValueMember   = "addressBlock";
                            add = (XLMain.Address)addressesDDL.SelectedItem;
                            if (add != null)
                            {
                                if (client.IsIndividual)
                                {
                                    addTB.Text = add.addressBlock;
                                }
                                else
                                {
                                    addTB.Text = client.name + Environment.NewLine + add.addressBlock;
                                }
                            }
                        }
                        if (clientHeader != null)
                        {
                            try
                            {
                                XLDocument.DeployHeader(clientHeader);
                            }
                            catch
                            {
                                MessageBox.Show("Unable to map header automatically." + Environment.NewLine + "Please run the Header/Footer menu.");
                            }
                        }
                    }
                    else if (docType == "Fax")
                    {
                        XLMain.Number fax = XLMain.Number.GetNumber(client.crmID, "Fax");
                        if (fax != null)
                        {
                            FaxTB.Text = fax.number;
                        }
                        if (clientHeader != null)
                        {
                            try
                            {
                                XLDocument.DeployHeader(clientHeader);
                            }
                            catch
                            {
                                MessageBox.Show("Unable to map header automatically." + Environment.NewLine + "Please run the Header/Footer menu.");
                            }
                        }
                    }

                    //set status
                    XLDocument.ChangeStatus("Draft");

                    //Alter fields dependant on doc type;
                    SetVisibility(docType);

                    if (client.department == "INS")
                    {
                        WhencallingDDL.Visible = true;
                        IPSContactBtn.Visible  = true;
                    }

                    //Update bookmarks dependant on doc type
                    UpdateBookmarks(docType);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to open taskpane");
                XLtools.LogException("TaskPane", ex.ToString());
            }
        }