protected void RebindNotes(object source, Telerik.WebControls.AjaxRequestEventArgs e)
        {
            // Get all medic records from data.
            List <TrinityClassLibrary_BL.Medic> allMedics = TrinityClassLibrary_DAL.MedicProvider.FetchList();

            // Get all notes for this encounter that are not marked as deleted.
            List <TrinityClassLibrary_BL.AdditionalPatientNote> notes = TrinityClassLibrary_DAL.AdditionalPatientNoteProvider.FetchList(CurrentPatientEncounter.PatientID, false);

            // Get the user's medic information.
            TrinityClassLibrary_BL.Medic currentUser = allMedics.Find(delegate(TrinityClassLibrary_BL.Medic currentMedic)
            {
                if (currentMedic.MedicID == Convert.ToInt32(Session["MedicPK"]))
                {
                    return(true);
                }
                return(false);
            });
            //ViewState["CurrentMedicSecurity"] = currentUser.SecurityLevel.Value.ToString();

            // Filters out notes the user does not have rights to view.
            notes = FilterNotes(notes, currentUser);

            // Displays the additional notes at the bottom of the screen.
            DisplayNotes(allMedics, notes, currentUser);
        }
        }         // Done... Tested

        #region DisplayNotes
        /// <summary>
        /// Displays the addiitonal notes to the page.
        /// </summary>
        /// <param name="allMedics">
        /// A list of all medics.
        /// </param>
        /// <param name="notes">
        /// A list of additional patient notes for this patient encounter.
        /// </param>
        /// <param name="userMedic">
        /// Medic who is using this program.
        /// </param>
        private void DisplayNotes(List <TrinityClassLibrary_BL.Medic> allMedics, List <TrinityClassLibrary_BL.AdditionalPatientNote> notes, TrinityClassLibrary_BL.Medic userMedic)
        {
            List <AdditionalNoteDisplay> notesForDisplay = new List <AdditionalNoteDisplay>();

            foreach (TrinityClassLibrary_BL.AdditionalPatientNote note in notes)
            {
                // Get the medic who created the note.
                TrinityClassLibrary_BL.Medic medic = allMedics.Find(delegate(TrinityClassLibrary_BL.Medic currentMedic)
                {
                    if (currentMedic.MedicID == note.CreatedByUserID)
                    {
                        return(true);
                    }
                    return(false);
                });

                // Build the link text for the note.
                string link = note.DateCreated.ToShortDateString() + " " + note.DateCreated.ToShortTimeString() + " CT:";

                // Build the remainder of the note text.
                string noteText = " (" + CurrentMedicSecurityLevel.ToString() + ": " + medic.FirstName + " " + medic.LastName + ") " + note.Notes;

                // Create a new note to display.
                AdditionalNoteDisplay newDisplayNote = new AdditionalNoteDisplay(note.NoteID.ToString(), link, noteText, note.SecurityLevel, CurrentMedicSecurityLevel);
                notesForDisplay.Add(newDisplayNote);
            }

            rptNotes.DataSource = notesForDisplay;
            rptNotes.DataBind();

            if (CurrentMedicSecurityLevel == MedicSecurityLevel.Customer || CurrentMedicSecurityLevel == MedicSecurityLevel.NONE)
            {
                btnAddNote.Visible = false;
            }
        }
 /// <summary>
 /// Removes patient encounters the medic does not have rights to see. A medic can only see patient encounters where they were the medic
 /// and encounters on their current rig.
 /// </summary>
 /// <param name="medic">
 /// The medic's information.
 /// </param>
 /// <param name="patientEncounters">
 /// A list of patient encounters that have returned in the medic's search.
 /// </param>
 /// <returns>
 /// A list of patient encounters.
 /// </returns>
 private List <TrinityClassLibrary_BL.Patient> FilterPatientEncounters(TrinityClassLibrary_BL.Medic medic, List <TrinityClassLibrary_BL.Patient> patientEncounters)
 {
     // If the user is a medic the results must be narrowed down to on patient encounters on their current rig or where they
     // are the medic for the encounter.
     if (CurrentMedicSecurityLevel == MedicSecurityLevel.Medic)
     {
         patientEncounters = patientEncounters.FindAll(delegate(TrinityClassLibrary_BL.Patient patient)
         {
             if ((patient.RigID == medic.RigID) || (patient.MedicID == medic.MedicID))
             {
                 return(true);
             }
             return(false);
         });
     }
     return(patientEncounters);
 }
        /// <summary>
        /// Fills the encounter results datagrid with the search results.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void rgEncounterResults_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            // Get the medic information from session or data.
            TrinityClassLibrary_BL.Medic medic = GetCurrentMedic();

            // Get all encounter status records from viewstate or data.
            List <TrinityClassLibrary_BL.EncounterStatus> allEncounterStatus = GetAllEncounterStatus();

            // Get all rigs from viewstate or data.
            List <TrinityClassLibrary_BL.Rig> allRigs = GetAllRigs();

            // Get all of the patient encounters that match the search criteria entered by the user.
            List <TrinityClassLibrary_BL.Patient> patientEncounters = TrinityClassLibrary_DAL.PatientProvider.Search(null, txtFName.Text, txtLName.Text, null, null, null, null, null, null, null, null, null, null, null, null, null, txtSSN.Text);

            // Remove patient encounters that medics are not allowed to view.
            patientEncounters = FilterPatientEncounters(medic, patientEncounters);

            // Add the information to a list to be used as the datasource for gird.
            List <EncounterResults> encounters = new List <EncounterResults>();

            foreach (TrinityClassLibrary_BL.Patient patient in patientEncounters)
            {
                // Get the encounter status description.
                string currentEncounterStatus = GetPatientEncounterStatus(allEncounterStatus, patient);

                // Get the name of the patient's rig.
                string currentRig = GetRig(allRigs, patient.RigID);

                string name = BuildName(patient.FirstName, patient.LastName);

                string address = BuildPatientAddress(patient);

                // Create the entry for the list.
                EncounterResults currentEncounter = new EncounterResults(patient.PatientID, name, Convert.ToDateTime(patient.EncounterDate),
                                                                         patient.SSN, address, patient.HomePhone, currentRig, currentEncounterStatus);

                encounters.Add(currentEncounter);
            }

            ViewState["EncountersCount"] = encounters.Count;

            rgEncounterResults.DataSource = encounters;
        }
        /// <summary>
        /// Filter out additional notes that the user does not have rights to view.
        /// </summary>
        /// <param name="notes">
        /// All notes for the patient encounter.
        /// </param>
        /// <param name="currentUser">
        /// The current user of the patient encounter program.
        /// </param>
        /// <returns>
        /// A list of notes with the notes the user does not have rights to view removed.
        /// </returns>
        private List <TrinityClassLibrary_BL.AdditionalPatientNote> FilterNotes(List <TrinityClassLibrary_BL.AdditionalPatientNote> notes, TrinityClassLibrary_BL.Medic currentUser)
        {
            // Hide notes the user does not have rights to view.
            // No hiding needs to be done for administrators, because they have rights to view everything.
            switch (CurrentMedicSecurityLevel)
            {
            // Medics can only view notes that are marked for "All Users".
            case MedicSecurityLevel.Medic:
                notes = notes.FindAll(delegate(TrinityClassLibrary_BL.AdditionalPatientNote currentNote)
                {
                    if (currentNote.SecurityLevel != TrinityClassLibrary_BL.AdditionalPatientNote.SecurityLevels.AllUsers)
                    {
                        return(false);
                    }
                    return(true);
                });
                break;

            // Physicians can view all notes not makred as Admin Only.
            case MedicSecurityLevel.Physician:
                notes = notes.FindAll(delegate(TrinityClassLibrary_BL.AdditionalPatientNote currentNote)
                {
                    if (currentNote.SecurityLevel == TrinityClassLibrary_BL.AdditionalPatientNote.SecurityLevels.AdminOnly)
                    {
                        return(false);
                    }
                    return(true);
                });
                break;

            // Customers can only view notes that are marked for "All Users".
            case MedicSecurityLevel.Customer:
                notes = notes.FindAll(delegate(TrinityClassLibrary_BL.AdditionalPatientNote currentNote)
                {
                    if (currentNote.SecurityLevel != TrinityClassLibrary_BL.AdditionalPatientNote.SecurityLevels.AllUsers)
                    {
                        return(false);
                    }
                    return(true);
                });
                break;

            // Handles users that may not have the role securable.
            case MedicSecurityLevel.NONE:
                notes = notes.FindAll(delegate(TrinityClassLibrary_BL.AdditionalPatientNote currentNote)
                {
                    if (currentNote.SecurityLevel != TrinityClassLibrary_BL.AdditionalPatientNote.SecurityLevels.AllUsers)
                    {
                        return(false);
                    }
                    return(true);
                });
                break;
            }
            return(notes);
        }
        protected override void Page_Load(object sender, System.EventArgs e)
        {
            base.Page_Load(sender, e);

            Session["SearchComplete"] = true;

            if (Session["mode"] == null)
            {
                if (Session["PatientID"] == null)
                {
                    Response.Redirect("SessionTimedOut.aspx");
                }
            }

            if (!IsPostBack)
            {
                LoadTips();

                // Get the patient information from session.
                TrinityClassLibrary_BL.Patient patient = (TrinityClassLibrary_BL.Patient)Session["Patient"];

                // Get the patient's rig information from session.
                TrinityClassLibrary_BL.Rig rig = null;
                if (patient.RigID.HasValue)
                {
                    rig = (TrinityClassLibrary_BL.Rig)Session["PatientRig"];
                }

                // Display the patient information to the screen.
                if (rig != null)
                {
                    litPatientData.Text = rig.Name + ": ";
                }
                litPatientData.Text += patient.LastName + ", " + patient.FirstName + " (" + Convert.ToDateTime(patient.EncounterDate).ToShortDateString() + ")";

                // Get all medic records from data.
                List <TrinityClassLibrary_BL.Medic> allMedics = TrinityClassLibrary_DAL.MedicProvider.FetchList();

                // Get all notes for this encounter that are not marked as deleted.
                List <TrinityClassLibrary_BL.AdditionalPatientNote> notes = TrinityClassLibrary_DAL.AdditionalPatientNoteProvider.FetchList(patient.PatientID, false);

                // Get the user's medic information.
                TrinityClassLibrary_BL.Medic currentUser = allMedics.Find(delegate(TrinityClassLibrary_BL.Medic currentMedic)
                {
                    if (currentMedic.MedicID == Convert.ToInt32(Session["MedicPK"]))
                    {
                        return(true);
                    }
                    return(false);
                });
                //ViewState["CurrentMedicSecurity"] = currentUser.SecurityLevel.Value.ToString();

                // Filters out notes the user does not have rights to view.
                notes = FilterNotes(notes, currentUser);

                // Displays the additional notes at the bottom of the screen.
                DisplayNotes(allMedics, notes, currentUser);
            }
            //DataUtility clsData = new DataUtility();

            //StringBuilder strSQL = new StringBuilder();

            if (Session["mode"] != null)
            {
                if (Session["mode"].ToString() == "view")
                {
                    btnContinue.Visible = false;
                    btnReset.Visible    = false;

                    DataTool  dbTool         = new DataTool();
                    DataTable dtPatientNotes = dbTool.getPatientNotes(Session["PatientID"].ToString());
                    // add the javascript onload event that will initialize the span tables
                    bodyMain.Attributes.Add("onLoad", "initSpan()");

                    // disable the input boxes and show the data from the database
                    enterData();

                    //clsData.doIncompletes(strSQL.ToString(), Page);
                    //Filling Spans with datatable
                    if (dtPatientNotes.Rows.Count > 0)
                    {
                        if (dtPatientNotes.Rows[0][1] != null)
                        {
                            txtImpressionNotes.Text = dtPatientNotes.Rows[0][1].ToString();
                        }
                        if (dtPatientNotes.Rows[0][3] != null)
                        {
                            txtInstructionNotes.Text = dtPatientNotes.Rows[0][3].ToString();
                        }
                        if (dtPatientNotes.Rows[0][0] != null)
                        {
                            txtPhysicalNotes.Text = dtPatientNotes.Rows[0][0].ToString();
                        }
                        if (dtPatientNotes.Rows[0][2] != null)
                        {
                            txtRecommendationNotes.Text = dtPatientNotes.Rows[0][2].ToString();
                        }
                        if (dtPatientNotes.Rows[0][1] != null)
                        {
                            spantxtImpressionNotes.InnerHtml = dtPatientNotes.Rows[0][1].ToString();
                        }
                        if (dtPatientNotes.Rows[0][3] != null)
                        {
                            spantxtInstructionNotes.InnerHtml = dtPatientNotes.Rows[0][3].ToString();
                        }
                        if (dtPatientNotes.Rows[0][0] != null)
                        {
                            spantxtPhysicalNotes.InnerHtml = dtPatientNotes.Rows[0][0].ToString();
                        }
                        if (dtPatientNotes.Rows[0][2] != null)
                        {
                            spantxtRecommendationNotes.InnerHtml = dtPatientNotes.Rows[0][2].ToString();
                        }
                    }

                    // Set the visibility of the header panels.
                    pnlAddEditHeader.Visible = false;
                    pnlViewHeader.Visible    = true;
                }
                else if (Session["mode"].ToString() == "edit")
                {
                    DataTool  dbTool         = new DataTool();
                    DataTable dtPatientNotes = dbTool.getPatientNotes(Session["PatientID"].ToString());

                    if (!Page.IsPostBack)
                    {
                        if (dtPatientNotes.Rows.Count > 0)
                        {
                            //Filling Textboxes with datatable
                            txtImpressionNotes.Text              = dtPatientNotes.Rows[0][1].ToString();
                            txtInstructionNotes.Text             = dtPatientNotes.Rows[0][3].ToString();
                            txtPhysicalNotes.Text                = dtPatientNotes.Rows[0][0].ToString();
                            txtRecommendationNotes.Text          = dtPatientNotes.Rows[0][2].ToString();
                            spantxtImpressionNotes.InnerHtml     = dtPatientNotes.Rows[0][1].ToString();
                            spantxtInstructionNotes.InnerHtml    = dtPatientNotes.Rows[0][3].ToString();
                            spantxtPhysicalNotes.InnerHtml       = dtPatientNotes.Rows[0][0].ToString();
                            spantxtRecommendationNotes.InnerHtml = dtPatientNotes.Rows[0][2].ToString();
                        }
                    }
                    // enter the data into the fields

                    // Set the visibility of the header panels.
                    pnlAddEditHeader.Visible = true;
                    pnlViewHeader.Visible    = false;
                }
                else
                {
                }
                ShowHideEditButtons();
            }
        }         // Done... Tested