// ------------------------------------------------------------------------------------------
        // Main Loop
        // ------------------------------------------------------------------------------------------
        private void MQ_bt_Run_Click(object sender, EventArgs e)
        {
            FatalError = false;
            FatalErrorMessage = "";

            MQ_ProgressBar.Value = 0;
            MQ_tb_status.Text = "";

            DateTime dtTimer = DateTime.Now;

            cParser = new Parser(this);
            cEncounters = new MQ_Encounters();
            cJournals = new MQ_Journals();
            cReferrals = new MQ_Referrals();
            cPatients = new MQ_Patients();

            cGlobals.sInPath = MQ_tb_ResponseFolderPath.Text;
            cGlobals.sOutPath = MQ_tb_OutputFolderPath.Text;
            cGlobals.bV1Export = MQ_cb_V1_export.Checked;
            cGlobals.bV1Initial = MQ_rb_V1_initial.Checked;
            cGlobals.bV1Bulk = MQ_rb_V1_bulk.Checked;
            cGlobals.bPseudonymise = MQ_cb_Pseudonymise.Checked;
            cGlobals.bPseudoDateShift = MQ_cb_PseudonymiseDateShift.Checked;
            if (cGlobals.bPseudoDateShift)
                cGlobals.iPseudoDateShift = cUtils.RandomNumber(0, 10);
            else
                cGlobals.iPseudoDateShift = 0;

            if (!cGlobals.sInPath.EndsWith("\\")) cGlobals.sInPath = cGlobals.sInPath + "\\";
            if (!cGlobals.sOutPath.EndsWith("\\")) cGlobals.sOutPath = cGlobals.sOutPath + "\\";

            if (MQ_cb_V1_export.Checked)
            {
                // Create the Sollis A2 folder structure (use todays date)
                string a2path = DateTime.Today.Year + "\\" + DateTime.Today.Month + "\\" + DateTime.Today.Day;
                cGlobals.sOutPath += a2path;
                if (!cGlobals.sOutPath.EndsWith("\\")) cGlobals.sOutPath = cGlobals.sOutPath + "\\";
            }

            if (!Directory.Exists(cGlobals.sInPath))
            {
                FatalErrorMessage = "Response folder not found";
                FatalError = true;
            }
            else if (!cUtils.CheckCreateFolder(cGlobals.sOutPath))
            {
                FatalErrorMessage = "Output folder not found";
                FatalError = true;
            }

            if (!FatalError)
            {
                fLogFile = new StreamWriter(cGlobals.sOutPath + String.Format("{0:yyyyMMdd}", DateTime.Today) + "_log.txt");
                cUtils.LogMessage(fLogFile, "Started");

                // Parse input files
                cUtils.LogMessage(fLogFile, "Parsing files");
                cUtils.LogMessage(fLogFile, "Input Folder: " + cGlobals.sInPath);
                cParser.ProcessResponseFiles(cGlobals.sInPath);

                // Assess Data
                cUtils.LogMessage(fLogFile, "Assessing data");
                cPatients.Assess();
                cJournals.Assess();
                cEncounters.Assess();
                cReferrals.Assess();

                if (cGlobals.bPseudonymise)
                {
                    cGlobals.sPracticeCode = cUtils.Pseudomymise(cGlobals.sPracticeCode, "PRACTICE", cGlobals.iPseudoDateShift);
                    cGlobals.dtQueryDate = Convert.ToDateTime(cUtils.Pseudomymise(cGlobals.dtQueryDate.ToShortDateString(), "DATE", cGlobals.iPseudoDateShift));
                    cEncounters.dtEnd = Convert.ToDateTime(cUtils.Pseudomymise(cEncounters.dtEnd.ToShortDateString(), "DATE", cGlobals.iPseudoDateShift));
                    cEncounters.dtStart = Convert.ToDateTime(cUtils.Pseudomymise(cEncounters.dtStart.ToShortDateString(), "DATE", cGlobals.iPseudoDateShift));
                    cJournals.dtEnd = Convert.ToDateTime(cUtils.Pseudomymise(cJournals.dtEnd.ToShortDateString(), "DATE", cGlobals.iPseudoDateShift));
                    cJournals.dtStart = Convert.ToDateTime(cUtils.Pseudomymise(cJournals.dtStart.ToShortDateString(), "DATE", cGlobals.iPseudoDateShift));
                    cReferrals.dtEnd = Convert.ToDateTime(cUtils.Pseudomymise(cReferrals.dtEnd.ToShortDateString(), "DATE", cGlobals.iPseudoDateShift));
                    cReferrals.dtStart = Convert.ToDateTime(cUtils.Pseudomymise(cReferrals.dtStart.ToShortDateString(), "DATE", cGlobals.iPseudoDateShift));
                }

                // Log some facts
                if (cGlobals.bPseudonymise)
                    cUtils.LogMessage(fLogFile, "Psuedonymising");
                cUtils.LogMessage(fLogFile, "Practice Code: " + cGlobals.sPracticeCode);
                cUtils.LogMessage(fLogFile, "Query Date: " + cGlobals.dtQueryDate.ToShortDateString());
                cUtils.LogMessage(fLogFile, "PATIENTS: " + cPatients.Describe());
                cUtils.LogMessage(fLogFile, "JOURNALS: " + cJournals.Describe());
                cUtils.LogMessage(fLogFile, "ENCOUNTERS: " + cEncounters.Describe());
                cUtils.LogMessage(fLogFile, "REFERRALS: " + cReferrals.Describe());

                // Version 2 Save
                if (MQ_cb_V2_export.Checked)
                {
                    cUtils.LogMessage(fLogFile, "Saving Version 2 Format Files");
                    cUtils.LogMessage(fLogFile, "Output Folder: " + cGlobals.sOutPath);
                    if (!cPatients.Save(cGlobals))
                    {
                        WarningMessage = "Data issues in Patient file";
                        cUtils.LogMessage(fLogFile, WarningMessage);
                    }
                    cUtils.LogMessage(fLogFile, "Written Patient file");

                    if (!cJournals.Save(cGlobals))
                    {
                        WarningMessage = "Data issues in Journal file";
                        cUtils.LogMessage(fLogFile, WarningMessage);
                    }
                    cUtils.LogMessage(fLogFile, "Written Journal file");

                    if (!cEncounters.Save(cGlobals))
                    {
                        WarningMessage = "Data issues in Encounter file";
                        cUtils.LogMessage(fLogFile, WarningMessage);
                    }
                    cUtils.LogMessage(fLogFile, "Written Encounter file");

                    if (!cReferrals.Save(cGlobals))
                    {
                        WarningMessage = "Data issues in Referral file";
                        cUtils.LogMessage(fLogFile, WarningMessage);
                    }
                    cUtils.LogMessage(fLogFile, "Written Referral file");
                }

                // Version 1 Save
                if (cGlobals.bV1Export)
                {
                    cGlobals.CreateV1Header();
                    cUtils.LogMessage(fLogFile, "Saving Version 1 Format Files");
                    cUtils.LogMessage(fLogFile, "Output Folder: " + cGlobals.sOutPath);

                    int iProgInc = (MQ_ProgressBar.Maximum - MQ_ProgressBar.Value) / 3;

                    cUtils.LogMessage(fLogFile, "Writing V1 Output Files");
                    cPatients.WriteV1File(cGlobals);
                    cUtils.LogMessage(fLogFile, "Written Patient file");
                    MQ_ProgressBar.Value = MQ_ProgressBar.Value + iProgInc;
                    cJournals.WriteV1File(cGlobals);
                    cUtils.LogMessage(fLogFile, "Written Journal file");
                    MQ_ProgressBar.Value = MQ_ProgressBar.Value + iProgInc;
                    cEncounters.WriteV1File(cGlobals);
                    cUtils.LogMessage(fLogFile, "Written Encounter file");
                    MQ_ProgressBar.Value = MQ_ProgressBar.Value + iProgInc;
                }

                MQ_ProgressBar.Value = MQ_ProgressBar.Maximum;
                cUtils.LogMessage(fLogFile,"Finished");
                cUtils.LogMessage(fLogFile,"Processed in " + DateTime.Now.Subtract(dtTimer).Seconds + " secs (" + DateTime.Now.Subtract(dtTimer).Milliseconds + " ms)");
                fLogFile.Close();
            }

            if (FatalError)
            {
                MessageBox.Show("ERROR: " + FatalErrorMessage);
                MQ_tb_status.Text = FatalErrorMessage;
            }
            else
            {
                if (WarningMessage != "")
                {
                    MessageBox.Show("WARNING: " + WarningMessage);
                    MQ_tb_status.Text = WarningMessage;
                }
                else
                     MQ_tb_status.Text = "Complete";
            }
        }
        // ------------------------------------------------------------------------------------------
        // Main Loop
        // ------------------------------------------------------------------------------------------
        private void MQ_bt_Run_Click(object sender, EventArgs e)
        {
            MQ_ProgressBar.Value = 0;
            MQ_tb_status.Text = "";

            DateTime dtTimer = DateTime.Now;

            cParser = new Parser(this);

            cGlobals.sInPath = MQ_tb_ResponseFolderPath.Text;
            cGlobals.sOutPath = MQ_tb_OutputFolderPath.Text;

            if (!cGlobals.sInPath.EndsWith("\\")) cGlobals.sInPath = cGlobals.sInPath + "\\";
            if (!cGlobals.sOutPath.EndsWith("\\")) cGlobals.sOutPath = cGlobals.sOutPath + "\\";

            fLogFile = new StreamWriter(cGlobals.sOutPath + String.Format("{0:yyyyMMdd}", DateTime.Today) + "_log.txt");
            cUtils.LogMessage(fLogFile, "Started");

            // Parse input files
            cUtils.LogMessage(fLogFile, "Parsing files");
            cUtils.LogMessage(fLogFile, "Input Folder: " + cGlobals.sInPath);
            cParser.ProcessResponseFiles(cGlobals.sInPath);

            MQ_ProgressBar.Value = MQ_ProgressBar.Maximum;
            cUtils.LogMessage(fLogFile,"Finished");
            cUtils.LogMessage(fLogFile,"Processed in " + DateTime.Now.Subtract(dtTimer).Seconds + " secs (" + DateTime.Now.Subtract(dtTimer).Milliseconds + " ms)");
            fLogFile.Close();
        }