private void ExamNotes()
    {
        string cmdText;
        string patientID = Request.QueryString["PatientID"];

        if (patientID == null || patientID.Trim() == "")
        {
            throw new ApplicationException("Patient information is not available");
        }

        string examID = Request.QueryString["ExamID"];

        //checking whether the examid exists
        if (examID == null || examID == "")
            examID = "0";
        //if (examID != "")
        //{
            cmdText = String.Format("SELECT TOP 1 ExamID FROM Exam WHERE ExamID = {0} OR (PatientID = {1} AND ExamDate = '{2}') ORDER BY ExamID DESC"
                                , examID, patientID, DateTime.Today.ToShortDateString());
            object retValue = DBUtil.ExecuteScalar(cmdText);
            if (retValue == null)
                examID = "";
            else
            {
                examID = retValue.ToString();
                hdnExamID.Value = examID;
                notesType = ExamNotesType.Correct;
            }
        //}

        PopulateHeader(patientID);

        if (hdnPatientID.Value == "")
            throw new ApplicationException("Patient information is not available");
        else
        {
            string userDefaultID = Request.QueryString["UserDefaultID"];
            BindExamDefaultDropDown(userDefaultID);

            //getting the last Exam record for the patient
            cmdText = "SELECT TOP 1 ExamID FROM EXAM WHERE PatientID = " + hdnPatientID.Value;
            if (examID != "")
                cmdText += " AND ExamID = " + examID;
            cmdText += " ORDER BY SavedInd DESC, ExamDate DESC, ExamID DESC";
            SqlDataReader drExam = DBUtil.ExecuteReader(cmdText);
            if (drExam.Read())
            {
                if (drExam["ExamID"].ToString() != "")
                {
                    examID = drExam["ExamID"].ToString();
                    cmdText = "SELECT ExamText, ExamDate, SavedInd FROM Exam WHERE ExamID = " + examID;
                    SqlDataReader drLastExam = DBUtil.ExecuteReader(cmdText);

                    drLastExam.Read();

                    if (drLastExam["SavedInd"].ToString() == "1")
                    {
                        hdnExamID.Value = examID;
                        notesType = ExamNotesType.Saved;
                    }

                    SetValues(drLastExam.GetString(0));
                    if(notesType != ExamNotesType.Saved)
                        SetPriorValues();

                    //loading the overriding defaults
                    LoadExamDefaults(userDefaultID, patientID);

                    if (notesType == ExamNotesType.New)
                    {
                        string lastExamDate = drLastExam["ExamDate"].ToString();
                        LastExam.Text = DateTime.Parse(lastExamDate).ToShortDateString();
                    }

                    if (notesType == ExamNotesType.Correct)
                    {
                        string lastExamDate = drLastExam["ExamDate"].ToString();
                        ExamDate.Text = DateTime.Parse(lastExamDate).ToShortDateString();
                    }

                    drLastExam.Close();
                    drLastExam.Dispose();

                }
            }
            else
            {
                //commenting this code out as we are not loading defaults based on age
                //there is no last exam, so getting the defaults
                //hdnDefaultInd.Value = "1";  //setting an indicator that the system is setting defaults.
                //examDefaultID = GetDefaultExamID();

                //supressing the defaults -- for poony
                hdnDefaultInd.Value = "";
                LoadExamDefaults(userDefaultID, patientID);
            }
            drExam.Close();
            drExam.Dispose();
        }

        if(notesType == ExamNotesType.New)
            PopulateHeader(patientID);

        if (notesType == ExamNotesType.New)
        {
            if(previousAge != "")
                Summary.Text = Summary.Text.Replace(previousAge, Age.Value);
            Discussed.Text = "Discussed findings with " + patientName;
            if (HxFrom.Text.ToString() != "" && HxFrom.Text != "patient")
            {
                string displaySex = "";
                string hxFrom = "";
                if (HxFrom.Text.IndexOf("patient and") >= 0)
                {
                    hxFrom = HxFrom.Text.Replace("patient and", "").Trim();
                    if (Sex.Text.ToLower() == female)
                        displaySex += "her";
                    else if (Sex.Text.ToLower() == male)
                        displaySex += "his";
                    Discussed.Text += " and " + displaySex + " " + hxFrom;
                }
                else
                {
                    Discussed.Text += "'s " + HxFrom.Text;
                }

            }

            //setting the Copy To
            CopyTo.Text = Refd.Text;
            if (Refd.Text != RefDoctor.Text)
                CopyTo.Text += ", " + RefDoctor.Text;

        }
        else
        {
            if(notesType == ExamNotesType.Correct)
                ExamDate.IsReadOnly = true;
        }
        if (notesType == ExamNotesType.New)
            SetBackgroundColours(1);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Context.Items["ShowTitle"] = true;

        pageControls = FlattenHierachy(Page);

        //getting the colour types selected by the user
        string[] arr = hdnColourType.Value.Split(',');
        foreach (string str in arr)
        {
            if (str != "" && !ColourType.ContainsKey(str))
                ColourType.Add(str, "");
            hdnColourType.Value = "";
        }

        //setting the doctor as the userName
        if (Request.QueryString["DoctorUserName"] != null)
            doctorUserName = Request.QueryString["DoctorUserName"];

        if (!IsPostBack)
        {
            LastExamElse.Visible = false;

            string cmdText = "SELECT FirstName + ' ' + LastName as DoctorName, UserName FROM [User]";

            SqlDataReader drUser = DBUtil.ExecuteReader(cmdText);

            WebUtil.BindLookupDropDown(User, drUser, "DoctorName", "UserName");

            drUser.Close();
            drUser.Dispose();

            WebUtil.SelectDropDownValue(User, doctorUserName);

            //populating the drop downs
            cmdText = "SELECT ControlName, FieldValue, RTrim(lu.FieldDescription) as FieldDescription, SortOrder";

            cmdText += ", null as DefaultValue FROM ExamLookUp elu inner join LookUp lu on elu.FieldName = lu.FieldName";
            lookUp = DBUtil.ExecuteDataTable(cmdText);

            PopulateDropDowns();

            if (Request.QueryString["ExamDefaultID"] != null)
                ExamDefault();
            else
                ExamNotes();

            hdnNoteType.Value = ((int)notesType).ToString();
            if (Dilate3.SelectedIndex == 0) //for backward compatibility as I removed the Not Dialted checkbox
                Dilate3.SelectedIndex = 1;
        }

        //getting the notes type for this page
        notesType = (ExamNotesType) Convert.ToInt32(hdnNoteType.Value);

        if (notesType == ExamNotesType.Correct)
        {
            HideSaveButtons();
            btnSignOff.Text = "CORRECT";
            btnSignOff1.Text = "CORRECT";
            btnSignOff2.Text = "CORRECT";
            btnSignOff3.Text = "CORRECT";
            btnSignOff4.Text = "CORRECT";
        }

        if (Request.QueryString["Print"] != null)
        {
            ExamPatientTab.HideTab = true;
            pnlComplaint.Visible = false;
            btnEditPatient.Visible = false;
            pnlVisual.Visible = false;
            pnlOcular.Visible = false;
            pnlAnterior.Visible = false;
            pnlLoc.Visible = false;
        }
    }
    private void ExamDefault()
    {
        //for defining defaults
        string examDefaultID  = Request.QueryString["ExamDefaultID"];
        notesType = ExamNotesType.Default;
        Title = "Notes Default";

        pnlHeader.Visible = false;
        pnlDefault.Visible = true;
        HideSaveButtons();
        btnSignOff.Visible = false;
        btnSignOff1.Visible = false;
        btnSignOff2.Visible = false;
        btnSignOff3.Visible = false;
        btnSignOff4.Visible = false;
        btnDefault.Visible = true;

        string cmdText;

        cmdText = "SELECT UserID, FirstName + ' ' + LastName as DoctorName FROM [USER] ORDER BY DoctorName";
        SqlDataReader drDoctor = DBUtil.ExecuteReader(cmdText);

        WebUtil.BindLookupDropDown(DoctorList, drDoctor, "DoctorName", "UserID");
        DoctorList.Items[0].Value = "0"; //setting the blank field value to 0;

        drDoctor.Close();
        drDoctor.Dispose();

        //getting the previous text
        cmdText = "SELECT ExamDefaultID, DefaultName, AgeStart, AgeEnd, PrematureBirth, DoctorUserID, ExamText FROM ExamDefault WHERE ExamDefaultID = " + examDefaultID;
        SqlDataReader drExamDefault = DBUtil.ExecuteReader(cmdText);

        if (drExamDefault.Read())
        {
            hdnExamDefaultID.Value = examDefaultID;
            SetValues(drExamDefault["ExamText"].ToString());
        }
        else
        {
            hdnExamDefaultID.Value = "0";
        }

        drExamDefault.Close();
        drExamDefault.Dispose();
    }