private void CompleteConsultation()
        {
            // check if any existing entry is selected in the list
            if (this.SelectedSR != null)
            {
                // check if the content of the report has been change, if so save first
                if (this.SelectedSR.Content != SRSelectedContent)
                {
                    MessageBox.Show("The consultation report has been changed. Please save the changes before completing the report.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                // check to make sure the content is definitely a consultation
                // the first line must have ---Consultation from: [Consulting Site Abbreviation]---
                string searchHeader = "---Consultation from: " + UserContext.LocalSite.SiteName + "---";
                if (!this.SRSelectedContent.Contains(searchHeader))
                {
                    MessageBox.Show("Consultation report must have the following line at the beginning." + Environment.NewLine + searchHeader,
                                    "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                // check for esig if needed
                bool CanComplete = true;

                // ask for E-signature if it's enabled
                if (this.LocalEsigStatus.Status == ESigNeedType.authorized_needs_signature)
                {
                    if (!this.LocalEsigStatus.IsSigned)
                    {
                        EverifyView eview = new EverifyView(DataSource, UserContext.LocalSite.PrimarySiteStationNUmber);
                        eview.ShowDialog();
                        CanComplete = eview.Success;
                        if (CanComplete)
                            this.LocalEsigStatus.IsSigned = true;
                    }
                }

                if (!CanComplete)
                {
                    MessageBox.Show("You cannot complete consultation without proper e-signature verification.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                // update the consultation status
                try
                {
                    DataSource.UpdateConsultationStatus(this.ConsultationID, "completed");
                    var consult = this.ConsultationList.ConsultationList.Where(con => con.ConsultationID == this.ConsultationID).FirstOrDefault();
                    consult.Status = "COMPLETED";
                    Log.Info(string.Format("Completed consultation for {0} at {1}.", this.AccessionNumber, this.SiteID));
                }
                catch (MagVixFailureException vfe)
                {
                    Log.Error("Failed to update consultation status.", vfe);
                    MessageBox.Show("Couldn't update the consultation status.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                // create a local duplicate of the case before proceeding
                // only proceed if the case is created successfully
                string copyCaseAccession = DataSource.CreateCopyCase(UserContext.LocalSite.PrimarySiteStationNUmber, this.CaseURN);
                string completionDateTime = string.Empty;
                if (!string.IsNullOrWhiteSpace(copyCaseAccession))
                {
                    // try tro retrieve the server time for verifying
                    string[] accPieces = copyCaseAccession.Split('^');
                    if ((accPieces != null) && (accPieces.Length == 2))
                    {
                        completionDateTime = DataSource.GetReportFieldData(".11", accPieces[1]);

                        MessageBox.Show("A reference record has been created for this consultation.\r\nThe reference case is: " + UserContext.LocalSite.SiteAbbreviation + " " + accPieces[0],
                                        "Information", MessageBoxButton.OK, MessageBoxImage.Information);

                        Log.Info(string.Format("Local reference report {0} created at site {1}",
                                                       accPieces[0], UserContext.LocalSite.PrimarySiteStationNUmber));
                    }
                    else
                    {
                        MessageBox.Show("A reference record has been created for this consultation.\r\nThe reference case is: " + UserContext.LocalSite.SiteAbbreviation + " " + copyCaseAccession,
                                        "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    }

                    Log.Info(string.Format("Reference report created for {0} at {1}.", this.AccessionNumber, this.SiteID));
                }
                else
                {
                    MessageBox.Show("A reference report couldn't be generated. Please note down the information for this case and contact help.", "Information",
                                    MessageBoxButton.OK, MessageBoxImage.Information);
                }

                // store either new released datetime or current time
                completionDateTime = string.IsNullOrWhiteSpace(completionDateTime) ? DateTime.Now.ToString("dd-MM-yyyy HH:mm") : completionDateTime;

                // add a boiler plate footer to the report
                string footer = string.Empty;
                footer += Environment.NewLine + "---Completed by: " + UserContext.UserCredentials.Fullname + "---" + Environment.NewLine;
                footer += "---On: " + completionDateTime + "---" + Environment.NewLine;
                footer += "---At: " + UserContext.LocalSite.SiteName + "---";
                this.SelectedSR.Content += footer;

                // try to save the report with footer to database
                try
                {
                    DataSource.SaveSupReport(this.CaseURN, SRSelectedDate.Value.ToString("yyyyMMddHHmm"), false, this.SelectedSR.Content);
                }
                catch (MagVixFailureException vfe)
                {
                    // if unable to save the report, remove the footer
                    Log.Error("Failed to save supplementary report.", vfe);
                    this.SelectedSR.Content.Replace(footer, string.Empty);
                    MessageBox.Show("Couldn't update the consultation report.\r\nPlease retry at a later time.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                ClearSR();
            }
            else
            {
                MessageBox.Show("Please selected a valid consultation to complete.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
        private void VerifySR()
        {
            // check if an existing entry is selected in the list
            if (this.SelectedSR != null)
            {
                // check if the content of the report has been change, if so save first
                if (this.SelectedSR.Content != SRSelectedContent)
                {
                    MessageBox.Show("The supplementary report has been changed. Please save the changes before verifying the report.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                bool CanVerify = true;

                // ask for E-signature if it's enabled
                if (this.EsigStatus.Status == ESigNeedType.authorized_needs_signature)
                {
                    if (!this.EsigStatus.IsSigned)
                    {
                        EverifyView eview = new EverifyView(DataSource, this.SiteID);
                        eview.ShowDialog();
                        CanVerify = eview.Success;
                        if (CanVerify)
                            this.EsigStatus.IsSigned = true;
                    }
                }

                if (!CanVerify)
                {
                    MessageBox.Show("You cannot verify supplementary report without proper e-signature verification.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                // only the interpreting site can actually verify the report
                try
                {
                    DataSource.SaveSupReport(this.CaseURN, SRSelectedDate.Value.ToString("yyyyMMddHHmm"), true, this.SelectedSR.Content);
                    this.SelectedSR.Verified = "Yes";
                    this.SelectedSR.VerifiedBy = UserContext.UserCredentials.Fullname;

                    MessageBoxResult result = MessageBox.Show("An alert has been sent to " + Practitioner + ".\r\nDo you want to send to additional recipient or mailgroups?",
                                                              "Infomation", MessageBoxButton.YesNo, MessageBoxImage.Information, MessageBoxResult.No);
                    if (result == MessageBoxResult.Yes)
                    {
                        try
                            {
                                string subject = SRSelectedDate.GetValueOrDefault().ToShortDateString() + " supplementary report for " + AccessionNumber + " has been verified.";
                                Process.Start("mailto:?subject=" + subject);
                            }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Email client could not be initiated.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                            Log.Error("Failed to start email client process.", ex);
                        }
                    }

                    ClearSR();

                    Log.Info(string.Format("A supplementary report has been verified and released for {0} at {1}.", this.AccessionNumber, this.SiteID));
                }
                catch (MagVixFailureException vfe)
                {
                    Log.Error("Failed to verify the supplementary report.", vfe);
                    MessageBox.Show("Couldn't verify the supplementary report.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
            else
            {
                MessageBox.Show("Please select a valid item in the supplementary report list to verify", "Information", MessageBoxButton.OK,MessageBoxImage.Information);
            }
        }
        public void VerifyReport()
        {
            // check for illegal characters
            if (ContainsIllegalCharacters())
            {
                MessageBox.Show("Please remove ^ from your inputs.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            // check for required fields
            if (!AreRequiredFieldsCompleted())
            {
                MessageBox.Show("Required fields (*) must not be left empty.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            // check for changes
            if (this.IsModified)
            {
                MessageBox.Show("Main report contains unsaved changes. Please save the changes first.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            // All consultations must not be pending
            if (this.ConsultationList != null)
            {
                if (this.ConsultationList.ConsultationList != null)
                {
                    // go through the consultation list and check if there are any pending consultation entry
                    foreach (CaseConsultation con in this.ConsultationList.ConsultationList)
                    {
                        if (con.Type == "CONSULTATION")
                        {
                            if (con.Status == "PENDING")
                            {
                                MessageBox.Show("There are pending consultations for this case." + Environment.NewLine +
                                                "Report cannot be verified before all consultations has been completed.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                                return;
                            }
                        }
                    }
                }
            }

            // All supplemetary at this time must be verified first
            if (SRViewModel != null)
            {
                if (!SRViewModel.IsAllSRVerified)
                {
                    MessageBox.Show("All supplementary reports must be verified before verifying the main report.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
            }

            // can only verify if the report is pending
            if (this.State == ReportState.Pending)
            {
                bool CanVerify = true;

                // ask for E-signature if it's enabled
                if ((CanVerify) && (this.EsigStatus.Status == ESigNeedType.authorized_needs_signature))
                {
                    if (!this.EsigStatus.IsSigned)
                    {
                        EverifyView eview = new EverifyView(DataSource, this.SiteCode);
                        eview.ShowDialog();
                        CanVerify = eview.Success;
                        if (CanVerify)
                        {
                            this.EsigStatus.IsSigned = true;
                        }
                    }
                }

                if (CanVerify)
                {
                    // set the verifying date time and the verifier
                    DateTime verifyDate = DateTime.Now;
                    string format = "MM/dd/yy@HH:mm";
                    this.ReleasedDate = verifyDate.ToString(format);
                    this.ReleasedBy = UserContext.UserCredentials.Fullname;

                    UpdateChangeList(".11", this.ReleasedDate);
                    UpdateChangeList(".13", this.ReleasedBy);

                    try
                    {
                        // try to save the verification info to the DB
                        SaveReportToDatabase();
                        if ((this.saveResult != null) && (this.saveResult.Released))
                        {
                            report.ChangeState(ReportState.Verified);

                            this.ReleasedDate = DataSource.GetReportFieldData(".11", this.CaseURN);
                            RaisePropertyChanged("ReleasedDate");
                            RaisePropertyChanged("ReportTitle");

                            Log.Info(string.Format("Verified and released the report of {1} at {0}.", this.SiteCode, this.AccessionNumber));
                        }
                        else
                            throw new Exception("Failed to verify main report.");
                    }
                    catch (Exception)
                    {
                        // clear the change list because we dont want unusable date time
                        ChangeList.Fields.Clear();
                        string message = "Failed to verify the main report.";
                        //Log.Error(message, ex);
                        MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    // if the site is primary read but not a local site then create a duplicate
                    if ((this.CurrentSiteType == ReadingSiteType.interpretation) &&
                        (UserContext.LocalSite.PrimarySiteStationNUmber != this.SiteCode))
                    {
                        // ask for E-signature if it's enabled
                        bool canMakeCopy = false;
                        if (this.LocalEsigStatus.Status == ESigNeedType.authorized_needs_signature)
                        {
                            EverifyView localEView = new EverifyView(DataSource, UserContext.LocalSite.PrimarySiteStationNUmber);
                            localEView.ShowDialog();
                            canMakeCopy = localEView.Success;
                        }

                        if (!canMakeCopy)
                        {
                            string message = "You failed to enter your electronic signature for " + UserContext.LocalSite.SiteAbbreviation + "." + Environment.NewLine +
                                             "Without a correct e-signature, a reference report cannot be generated.";
                            MessageBox.Show(message, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        else
                        {
                            string copyCaseAccession = DataSource.CreateCopyCase(UserContext.LocalSite.PrimarySiteStationNUmber, this.CaseURN);
                            if (!string.IsNullOrWhiteSpace(copyCaseAccession))
                            {
                                string[] accPieces = copyCaseAccession.Split('^');
                                if ((accPieces != null) && (accPieces.Length == 2))
                                {
                                    MessageBox.Show("A reference record has been created for this report.\r\nThe reference case is: " + UserContext.LocalSite.SiteAbbreviation + " " + accPieces[0],
                                                    "Information", MessageBoxButton.OK, MessageBoxImage.Information);

                                    Log.Info(string.Format("Local reference report {0} created at site {1}",
                                                           accPieces[0], UserContext.LocalSite.PrimarySiteStationNUmber));
                                }
                            }
                            else
                            {
                                MessageBox.Show("A reference report couldn't be generated. Please note down the information for this case and contact help.", "Information",
                                                MessageBoxButton.OK, MessageBoxImage.Information);
                            }
                        }
                    }

                    //string msg = this.saveResult.Message + ".\nDo you want to send to additional recipients or mailgroups?";
                    string msg = "An alert has been sent to " + Practitioner + ".\nDo you want to send to additional recipients or mailgroups?";
                    MessageBoxResult result = MessageBox.Show(msg, "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                    if (result == MessageBoxResult.Yes)
                    {
                        try
                        {
                            string subject = "The report for " + AccessionNumber + " has been verified.";
                            Process.Start("mailto:?subject=" + subject);
                        }
                        catch (Exception ex1)
                        {
                            MessageBox.Show("Email client could not be initiated.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                            Log.Error("Failed to start email client process.", ex1);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("You can only verify the main report if it's pending for verification.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }