private void buttonFax_Click(object sender, EventArgs e)
        {
            string delimStr = " ";
            char[] delimiter = delimStr.ToCharArray();

            ComboBoxItems _coverSheet = (ComboBoxItems)comboCoverSheets.SelectedItem;

            _whichCoverPage = _coverSheet.GetValue();

            Cursor.Current = Cursors.WaitCursor;

            //Check box to indicate that user wants 
            //only cover sheet sent
            if (cbSendCoverOnly.Checked)
            {
                if (gridSendFax.RowCount > 0)
                {
                    try
                    {
                        for (int i = 0; i < gridSendFax.RowCount; i++)
                        {
                            _faxing.DisplayName = txtFrom.Text;
                            _faxing.FaxNumber = gridSendFax[i + 1, 3].Text.Replace("(", "")
                                .Replace(")", "")
                                .Replace("-", "")
                                .Replace(" ", "");
                            _faxing.RecipientName = gridSendFax[i + 1, 2].Text;

                            try
                            {
                                _faxing.FirstName = _add._first;
                            }
                            catch { }
                            try
                            {
                                _faxing.LastName = _add._last;
                            }
                            catch { }

                            if (gridFaxList.RowCount > 0)
                            {
                                _faxing.FaxPath = "C:\\";
                            }

                            try
                            {
                                if (_faxing.BrokenRulesCollection.Count > 1)
                                {
                                    MessageBox.Show(_faxing.GetBrokenRuleText(),
                                      "Missing Information", MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation);
                                    Cursor = Cursors.Default;
                                    return;
                                }
                            }
                            catch (Csla.Validation.ValidationException ex)
                            {
                                MessageBox.Show(ex.Message,
                                  "Error saving", MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation);
                                Cursor = Cursors.Default;
                                return;
                            }

                            if (_faxing.FaxPageCount == 1)
                            {
                                MessageBox.Show("Fax could not be created",
                                  "Problem", MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                                return;
                            }

                            //Create fax that only has cover sheet
                            string _return = CreateFaxCoverOnly(gridSendFax[i + 1, 2].Text,
                                gridSendFax[i + 1, 3].Text.Replace("(", "")
                                .Replace(")", "")
                                .Replace("-", "")
                                .Replace(" ", ""));
                            if (_return == "None")
                            {
                                MessageBox.Show("Problem with Creating Fax",
                                  "Problem", MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation);
                                return;
                            }
                            else
                            {
                                _faxing.FaxPageCount = Pdf.GetPDFPageCount(_return);
                                string _faxProcessPath = Common.StoreComputer + Common.StoreShare + "outboundfaxes\\" + Common.GetFileNameFromDateTimeString() + ".pdf";
                                //Copy created fax to process fax folder
                                try
                                {
                                    File.Copy(_return, _faxProcessPath, true);
                                }
                                catch (Exception er)
                                {
                                    MessageBox.Show(er.Message,
                                      "Error", MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation);
                                    Cursor = Cursors.Default;
                                }

                                _faxing.FaxPath = _faxProcessPath;

                                if (txtNotes.Text.Length > 0)
                                {
                                    _faxing.Notes = txtNotes.Text;
                                }
                                try
                                {
                                    if (chkAddDocName.Checked)
                                    {
                                        _faxing.DisplayName = string.Empty;

                                        for (int j = 1; j < gridFaxList.RowCount + 1; j++)
                                        {
                                            _faxing.DisplayName =
                                                _faxing.DisplayName + " " +
                                                gridFaxList[j, 2].Text;
                                        }
                                    }

                                    _faxing.Save();

                                    _faxing = null;
                                    InitiateFaxingClass();
                                }
                                catch (Exception er)
                                {
                                    MessageBox.Show(er.Message,
                                      "Error", MessageBoxButtons.OK,
                                      MessageBoxIcon.Exclamation);
                                    Cursor = Cursors.Default;
                                }
                            }
                        }
                    }
                    catch
                    {
                        try
                        {
                            if (_faxing.BrokenRulesCollection.Count > 0)
                            {
                                MessageBox.Show(_faxing.GetBrokenRuleText(),
                                  "Missing Information", MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation);
                                Cursor = Cursors.Default;
                                return;
                            }
                        }
                        catch (Csla.Validation.ValidationException ex)
                        {
                            MessageBox.Show(ex.Message,
                              "Error saving", MessageBoxButtons.OK,
                              MessageBoxIcon.Exclamation);
                            Cursor = Cursors.Default;
                            return;
                        }
                    }

                    try
                    {
                        RefreshFaxServer();
                        FaxLists.GetSendingFaxList();
                        PopulateToNames();
                        Cursor = Cursors.Default;
                        MessageBox.Show("Fax Created Successfully",
                          "Successful", MessageBoxButtons.OK,
                          MessageBoxIcon.Exclamation);
                    }
                    catch (Exception er)
                    {
                        MessageBox.Show(er.Message,
                          "Error", MessageBoxButtons.OK,
                          MessageBoxIcon.Exclamation);
                        Cursor = Cursors.Default;
                    }
                    ReduceMemoryFootprint();
                    cbSendCoverOnly.Checked = false;
                    buttonFax.Enabled = true;
                    return;
                }
                else
                {
                    try
                    {
                        if (_faxing.BrokenRulesCollection.Count > 0)
                        {
                            MessageBox.Show(_faxing.GetBrokenRuleText(),
                              "Missing Information", MessageBoxButtons.OK,
                              MessageBoxIcon.Exclamation);
                            Cursor = Cursors.Default;
                            return;
                        }
                    }
                    catch (Csla.Validation.ValidationException ex)
                    {
                        MessageBox.Show(ex.Message,
                          "Error saving", MessageBoxButtons.OK,
                          MessageBoxIcon.Exclamation);
                        Cursor = Cursors.Default;
                        return;
                    }
                }

            }

            if (gridSendFax.RowCount > 0)
            {
                try
                {
                    for (int i = 0; i < gridSendFax.RowCount; i++)
                    {
                        _faxing.DisplayName = txtFrom.Text;
                        _faxing.FaxNumber = gridSendFax[i + 1, 3].Text.Replace("(","")
                            .Replace(")","")
                            .Replace("-", "")
                            .Replace(" ", "");
                        _faxing.RecipientName = gridSendFax[i + 1, 2].Text;

                        try
                        {
                            _faxing.FirstName = _add._first;
                        }
                        catch { }
                        try
                        {
                            _faxing.LastName = _add._last;
                        }
                        catch { }

                        if (gridFaxList.RowCount > 0)
                        {
                            _faxing.FaxPath = "C:\\";
                        }

                        try
                        {
                            if (_faxing.BrokenRulesCollection.Count > 0)
                            {
                                MessageBox.Show(_faxing.GetBrokenRuleText(),
                                  "Missing Information", MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation);
                                Cursor = Cursors.Default;
                                return;
                            }
                        }
                        catch (Csla.Validation.ValidationException ex)
                        {
                            MessageBox.Show(ex.Message,
                              "Error saving", MessageBoxButtons.OK,
                              MessageBoxIcon.Exclamation);
                            Cursor = Cursors.Default;
                            return;
                        }

                        if (_faxing.FaxPageCount == 1)
                        {
                            MessageBox.Show("Fax could not be created",
                              "Problem", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
                            return;
                        }

                        string _return = CreateFax(gridSendFax[i + 1, 2].Text,
                            gridSendFax[i + 1, 3].Text.Replace("(","")
                            .Replace(")","")
                            .Replace("-", "")
                            .Replace(" ", ""));
                        if (_return == "None")
                        {
                            MessageBox.Show("Problem with Creating Fax",
                              "Problem", MessageBoxButtons.OK,
                              MessageBoxIcon.Exclamation);
                            return;
                        }
                        else
                        {
                            _faxing.FaxPageCount = Pdf.GetPDFPageCount(_return);
                            string _faxProcessPath = Common.StoreComputer + Common.StoreShare + "outboundfaxes\\" + Common.GetFileNameFromDateTimeString() + ".pdf";
                            //Copy created fax to process fax folder
                            try
                            {
                                File.Copy(_return, _faxProcessPath, true);
                            }
                            catch (Exception er)
                            {
                                MessageBox.Show(er.Message,
                                  "Error", MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation);
                                Cursor = Cursors.Default;
                            }

                            _faxing.FaxPath = _faxProcessPath;

                            if (txtNotes.Text.Length > 0)
                            {
                                _faxing.Notes = txtNotes.Text;
                            }
                            try
                            {
                                if (chkAddDocName.Checked)
                                {
                                    _faxing.DisplayName = string.Empty;

                                    for (int j = 1; j < gridFaxList.RowCount + 1; j++)
                                    {
                                        _faxing.DisplayName =
                                            _faxing.DisplayName + " " +
                                            gridFaxList[j, 2].Text;
                                    }
                                }

                                _faxing.Save();

                                _faxing = null;
                                InitiateFaxingClass();
                            }
                            catch (Exception er)
                            {
                                MessageBox.Show(er.Message,
                                  "Error", MessageBoxButtons.OK,
                                  MessageBoxIcon.Exclamation);
                                Cursor = Cursors.Default;
                            }
                        }
                    }
                }
                catch
                {
                    try
                    {
                        if (_faxing.BrokenRulesCollection.Count > 0)
                        {
                            MessageBox.Show(_faxing.GetBrokenRuleText(),
                              "Missing Information", MessageBoxButtons.OK,
                              MessageBoxIcon.Exclamation);
                            Cursor = Cursors.Default;
                            return;
                        }
                    }
                    catch (Csla.Validation.ValidationException ex)
                    {
                        MessageBox.Show(ex.Message,
                          "Error saving", MessageBoxButtons.OK,
                          MessageBoxIcon.Exclamation);
                        Cursor = Cursors.Default;
                        return;
                    }
                }
            }
            else
            {
                try
                {
                    if (_faxing.BrokenRulesCollection.Count > 0)
                    {
                        MessageBox.Show(_faxing.GetBrokenRuleText(),
                          "Missing Information", MessageBoxButtons.OK,
                          MessageBoxIcon.Exclamation);
                        Cursor = Cursors.Default;
                        return;
                    }
                }
                catch (Csla.Validation.ValidationException ex)
                {
                    MessageBox.Show(ex.Message,
                      "Error saving", MessageBoxButtons.OK,
                      MessageBoxIcon.Exclamation);
                    Cursor = Cursors.Default;
                    return;
                }
            }
            try
            {
                RefreshFaxServer();
                FaxLists.GetSendingFaxList();
                PopulateToNames();
                Cursor = Cursors.Default;
                MessageBox.Show("Fax Created Successfully",
                  "Successful", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
            }
            catch (Exception er)
            {
                MessageBox.Show(er.Message,
                  "Error", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
                Cursor = Cursors.Default;
            }
            ReduceMemoryFootprint();
        }
 private void InitiateFaxingClass()
 {
     try
     {
         _faxing = FaxingServer.NewFaxingServer();
     }
     catch (Exception er)
     {
         Common.Log("InitiateFaxingClass1: " + er.Message);  
     }
     try
     {
         _faxing.UseNine = System.Configuration
             .ConfigurationManager.AppSettings["UseNine"];
     }
     catch (Exception er)
     {
         Common.Log("InitiateFaxingClass2: " + er.Message);
     }
 }
        public GridControl InitializeAndPopulateProcessGrid(GridControl gridProcessing,
            FaxingServer _faxing, string _allChecked)
        {
            try
            {
                ArrayList aProccessList = null;
                int _counter = 1;

                gridProcessing.ColCount = 0;
                gridProcessing.RowCount = 0;

                gridProcessing.ColCount = 13;
                gridProcessing.ColWidths[0] = 35;
                gridProcessing.ColWidths[1] = 150;
                gridProcessing.ColWidths[2] = 75;
                gridProcessing.ColWidths[3] = 75;
                gridProcessing.ColWidths[4] = 220;
                gridProcessing.ColWidths[5] = 75;
                gridProcessing.ColWidths[6] = 190;
                gridProcessing.ColWidths[7] = 65;
                gridProcessing.ColWidths[8] = 65;
                gridProcessing.ColWidths[9] = 0;
                gridProcessing.ColWidths[10] = 0;
                gridProcessing.ColWidths[11] = 0;
                gridProcessing.ColWidths[12] = 0;
                gridProcessing.ColWidths[13] = 0;

                gridProcessing[0, 1].Text = "Time";
                gridProcessing[0, 2].Text = "View";
                gridProcessing[0, 3].Text = "Delete";
                //gridProcessing[0, 4].Text = "Action";
                //gridProcessing[0, 5].Text = "Update";
                //gridProcessing[0, 6].Text = "Patient Info";
                //gridProcessing[0, 7].Text = "Chart";
                //gridProcessing[0, 8].Text = "Process";

                gridProcessing[0, 0].BackColor = Color.LightGray;
                gridProcessing[0, 1].BackColor = Color.LightGray;
                gridProcessing[0, 2].BackColor = Color.LightGray;
                gridProcessing[0, 3].BackColor = Color.LightGray;
                gridProcessing[0, 4].BackColor = Color.LightGray;
                gridProcessing[0, 5].BackColor = Color.LightGray;
                gridProcessing[0, 6].BackColor = Color.LightGray;
                gridProcessing[0, 7].BackColor = Color.LightGray;
                gridProcessing[0, 8].BackColor = Color.LightGray;
                gridProcessing[0, 9].BackColor = Color.LightGray;
                gridProcessing[0, 10].BackColor = Color.LightGray;
                gridProcessing[0, 11].BackColor = Color.LightGray;
                aProccessList = ProcessList.GetProcessList(
                    Common.UserID.ToString(), "All");
                string[] sdelimStr = null;
                string delimStr = "~";
                char[] delimiter = delimStr.ToCharArray();

                gridProcessing.RowCount = aProccessList.Count;
                gridProcessing.Model.Options.NumberedColHeaders = false;
                gridProcessing.Model.Options.NumberedRowHeaders = false;

                for (int iLoop = 0; iLoop < aProccessList.Count; iLoop++)
                {
                    sdelimStr = aProccessList[iLoop].ToString().Split(delimiter, 11);


                    gridProcessing[iLoop + 1, 0].BackColor = Color.LightGray;
                    gridProcessing[iLoop + 1, 0].Text = _counter.ToString();
                    _counter++;

                    gridProcessing[iLoop + 1, 1].Text = sdelimStr[2];
                    gridProcessing[iLoop + 1, 1].Enabled = false;

                    gridProcessing[iLoop + 1, 2].Description = "View";
                    gridProcessing[iLoop + 1, 2].CellType = "PushButton";
                    gridProcessing[iLoop + 1, 2].CellAppearance = Syncfusion.Windows.Forms.Grid.GridCellAppearance.Raised;

                    gridProcessing[iLoop + 1, 3].Description = "Delete";
                    gridProcessing[iLoop + 1, 3].CellType = "PushButton";
                    gridProcessing[iLoop + 1, 3].CellAppearance = Syncfusion.Windows.Forms.Grid.GridCellAppearance.Raised;


                    //if (sdelimStr[5] == "--")
                    if (sdelimStr[5].IndexOf("Tab:  Desc:  DOS:") > 0)
                    {
                        //gridProcessing[iLoop + 1, 6].Text = "None";
                        //gridProcessing[iLoop + 1, 6].Enabled = false;
                    }
                    else
                    {
                        //gridProcessing[iLoop + 1, 6].Text = sdelimStr[5];
                        //gridProcessing[iLoop + 1, 6].Enabled = false;
                    }

                    //gridProcessing[iLoop + 1, 7].Description = "Chart";
                    //gridProcessing[iLoop + 1, 7].CellType = "PushButton";
                    //gridProcessing[iLoop + 1, 7].CellAppearance = Syncfusion.Windows.Forms.Grid.GridCellAppearance.Raised;

                    //gridProcessing[iLoop + 1, 8].Description = "Process";
                    //gridProcessing[iLoop + 1, 8].CellType = "PushButton";
                    //gridProcessing[iLoop + 1, 8].CellAppearance = Syncfusion.Windows.Forms.Grid.GridCellAppearance.Raised;

                    // gridProcessing[iLoop + 1, 8].Text = sdelimStr[3];
                    gridProcessing[iLoop + 1, 9].Text = sdelimStr[3];

                    //Contains Patient ID
                    gridProcessing[iLoop + 1, 10].Text = sdelimStr[6];
                    //Contains Provider ID
                    gridProcessing[iLoop + 1, 11].Text = sdelimStr[0];
                    //Contains Tab ID
                    gridProcessing[iLoop + 1, 12].Text = sdelimStr[8];
                }
                GridModel model = gridProcessing.Model;
                model.RowHeights.ResizeToFit(GridRangeInfo.Rows(0, aProccessList.Count));
                return gridProcessing;
            }
            catch (System.AccessViolationException av)
            {
                Common.Log(av.Message);
                return gridProcessing;
            }
            catch (Exception er)
            {
                Common.Log(er.Message);
                return gridProcessing;
            }
        }
        private void butResend_Click(object sender, EventArgs e)
        {
            try
            {
                txtResend.Update();
                pdfPageView1.Visible = false;
                _fileStream.Close();
                _faxing.RecipientName = "Resend Fax";


                
                string _telephoneNumber = txtResend.Text;
                //Take out 1 and 9 so that it will get put back in
                //if needed
                _telephoneNumber = _telephoneNumber.Replace("1,", "");
                _telephoneNumber = _telephoneNumber.Replace("9,", "");
                _faxing.FaxNumber = _telephoneNumber;
                //edit for sending fax
                try
                {
                    if (_faxing.BrokenRulesCollection.Count > 0)
                    {
                        MessageBox.Show(_faxing.GetBrokenRuleText(),
                          "Missing Information", MessageBoxButtons.OK,
                          MessageBoxIcon.Exclamation);
                        Cursor = Cursors.Default;
                        return;
                    }
                }
                catch (Csla.Validation.ValidationException ex)
                {
                    MessageBox.Show(ex.Message,
                      "Error saving", MessageBoxButtons.OK,
                      MessageBoxIcon.Exclamation);
                    Cursor = Cursors.Default;
                    return;
                }
                //Pdf.ResetAdobe();
                _faxing.FaxNumber = _faxing.FaxNumber.Replace("1,1,", "1,");
                _faxing.InsertMiscFax = false;
                //Send fax
                _faxing.Save();
                //Set variable to null
                _faxing = null;
                //Create new fax object
                InitiateFaxingClass();
                ResetFaxResent();
                pdfPageView1.Visible = false;
                Cursor = Cursors.Default;
                MessageBox.Show("Resent sent successfully",
                  "Success", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
                label6.Visible = false;
                txtResend.Visible = false;
                butResend.Visible = false;
                buttonCancel.Visible = false;   
            }
            catch (Exception er)
            {
                Cursor = Cursors.Default;
                pdfPageView1.Visible = false;
                Common.Log(er.Message);
                MessageBox.Show(er.Message,
                  "Error", MessageBoxButtons.OK,
                  MessageBoxIcon.Exclamation);
                label6.Visible = false;
                txtResend.Visible = false;
                butResend.Visible = false;
                buttonCancel.Visible = false;   
            }
        }
        private void buttonSend_Click(object sender, EventArgs e)
        {

            if (txtTo.Text.Length == 0)
            {
                MessageBox.Show("Please Enter To Field",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (txtFrom.Text.Length == 0)
            {
                MessageBox.Show("Please Enter From Field",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (txtSubject.Text.Length == 0)
            {
                MessageBox.Show("Please Enter Subject Field",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //  The mailman object is used for sending and receiving email.
            Chilkat.MailMan _mailman = new Chilkat.MailMan();

            _success = _mailman.UnlockComponent("SCOTTGMAILQ_ZpcR8r2N6R7a");

            //  Create a new email object
            Chilkat.Email _email = new Chilkat.Email();

            //  Set the SMTP server.
            _mailman.SmtpHost = System.Configuration
                .ConfigurationManager.AppSettings["smtpaddress"];
            _mailman.SmtpUsername = System.Configuration
                .ConfigurationManager.AppSettings["smtpid"];
            _mailman.SmtpPassword = System.Configuration
                .ConfigurationManager.AppSettings["smtppwd"];

            //  You may need a login/password.  In many cases,
            //  authentication is not required when sending local email
            //  (i.e. to email addresses having the same domain as the
            //  SMTP server), but is required when sending to non-local
            //  recipients.
            //mailman.SmtpUsername = "******";
            //mailman.SmtpPassword = "******";

            _email.Subject = txtSubject.Text;
            _email.Body = txtNote.Text;
            _email.From = txtFrom.Text;
            _email.AddTo(txtTo.Text, txtTo.Text);

            //Add any number of attachments:
            string contentType;

            contentType = _email.AddFileAttachment(_attachmentPath);

            _success = _mailman.SendEmail(_email);

            if (_success != true)
            {
                _errorMessage = _mailman.LastErrorText;
            }
            else
            {
                _sendEmail = FaxingServer.NewFaxingServer();
                _sendEmail.FaxNumber = "None";
                _sendEmail.FaxPageCount = 0;
                _sendEmail.FaxPath = _attachmentPath;
                _sendEmail.RecipientName = txtTo.Text;
                _sendEmail.DisplayName = txtFrom.Text;
                _sendEmail.Notes = "Subject: " + txtSubject.Text + " on " 
                        + DateTime.Now.ToShortDateString() 
                        + " at " + DateTime.Now.ToShortTimeString();

                _sendEmail.ShowFax = "N";
                _sendEmail.Save(); 
            }

            this.Close();  
        }