protected void SetBooking()
    {
        string booking_patient_id = Request.QueryString["bookingpatient"];
        string booking_id         = Request.QueryString["booking"];

        if (booking_patient_id != null)
        {
            if (!Regex.IsMatch(booking_patient_id, @"^\d+$"))
            {
                throw new CustomMessageException();
            }

            BookingPatient bookingPatient = BookingPatientDB.GetByID(Convert.ToInt32(booking_patient_id));
            if (bookingPatient == null)
            {
                throw new CustomMessageException();
            }
            if (bookingPatient.Booking.Organisation == null)
            {
                throw new CustomMessageException();
            }

            btnOtherEmail.OnClientClick = "javascript: get_referrer_additional_emails(" + bookingPatient.Patient.PatientID + ");return false;";

            // get selected id's
            ArrayList selectedIDs = new ArrayList();
            foreach (RepeaterItem item in lstNotes.Items)
            {
                CheckBox chkUseNote = (CheckBox)item.FindControl("chkUseNote");
                Label    lblNoteID  = (Label)item.FindControl("lblNoteID");
                if (chkUseNote.Checked)
                {
                    selectedIDs.Add(lblNoteID.Text);
                }
            }

            txtUpdatePatientID.Text      = bookingPatient.Patient.PatientID.ToString();
            txtUpdatePatientName.Text    = bookingPatient.Patient.Person.FullnameWithoutMiddlename;
            txtUpdatePatientName.Visible = false;
            lblUpdatePatientName.Text    = "<a href=\"#=\" onclick=\"open_new_window('PatientDetailV2.aspx?type=view&id=" + bookingPatient.Patient.PatientID + "'); return false;\">" + bookingPatient.Patient.Person.FullnameWithoutMiddlename + "</a>";
            lblUpdatePatientName.Visible = true;

            txtUpdateOrganisationID.Text      = bookingPatient.Booking.Organisation.OrganisationID.ToString();
            txtUpdateOrganisationName.Text    = bookingPatient.Booking.Organisation.Name;
            txtUpdateOrganisationName.Visible = false;
            lblUpdateOrganisationName.Text    = "<a href=\"#=\" onclick=\"open_new_window('OrganisationDetailV2.aspx?type=view&id=" + bookingPatient.Booking.Organisation.OrganisationID + "'); return false;\">" + bookingPatient.Booking.Organisation.Name + "</a>";
            lblUpdateOrganisationName.Visible = true;



            // show booking info

            lnkBookingSheetForPatient.Text        = "Booking sheet for " + bookingPatient.Patient.Person.FullnameWithoutMiddlename;
            lnkBookingSheetForPatient.PostBackUrl = String.Format("~/BookingsV2.aspx?type=patient&patient={0}&org={1}&staff={2}&offering={3}&date={4}", bookingPatient.Patient.PatientID, bookingPatient.Booking.Organisation.OrganisationID, bookingPatient.Booking.Provider.StaffID, bookingPatient.Offering.OfferingID, bookingPatient.Booking.DateStart.ToString("yyyy_MM_dd"));

            lnkBookingListForPatient.Text        = "Booking list for " + bookingPatient.Patient.Person.FullnameWithoutMiddlename;
            lnkBookingListForPatient.PostBackUrl = String.Format("~/BookingsListV2.aspx?patient={0}", bookingPatient.Patient.PatientID);

            lblBooking_Provider.Text      = bookingPatient.Booking.Provider.Person.FullnameWithoutMiddlename;
            lblBooking_Offering.Text      = bookingPatient.Offering.Name;
            lblBooking_BookingStatus.Text = bookingPatient.Booking.BookingStatus.Descr.ToString();
            lblBooking_Time.Text          = bookingPatient.Booking.DateStart.Date.ToString("dd MMM yyyy") + " - " + bookingPatient.Booking.DateStart.ToString("hh:mm") + "-" + bookingPatient.Booking.DateEnd.ToString("hh:mm");
            lblBooking_Notes.Text         = Note.GetPopupLinkTextV2(15, bookingPatient.EntityID, bookingPatient.NoteCount > 0, true, 1050, 530, "images/notes-bw-24.jpg", "images/notes-24.png");


            // display list of notes in repeater
            DataTable notes = NoteDB.GetDataTable_ByEntityID(bookingPatient.EntityID);
            lstNotes.DataSource = notes;
            lstNotes.DataBind();

            // check id's that were previously checked
            foreach (RepeaterItem item in lstNotes.Items)
            {
                CheckBox chkUseNote      = (CheckBox)item.FindControl("chkUseNote");
                Label    lblNoteID       = (Label)item.FindControl("lblNoteID");
                Label    lblOriginalText = (Label)item.FindControl("lblOriginalText");
                chkUseNote.Checked = selectedIDs.Contains(lblNoteID.Text);
            }

            // hide if got from url ... no need to change it
            btnPatientListPopup.Visible      = false;
            btnClearPatient.Visible          = false;
            btnOrganisationListPopup.Visible = false;
            btnClearOrganisation.Visible     = false;
        }
        else if (booking_id != null)
        {
            if (!Regex.IsMatch(booking_id, @"^\d+$"))
            {
                throw new CustomMessageException();
            }

            Booking booking = BookingDB.GetByID(Convert.ToInt32(booking_id));
            if (booking == null)
            {
                throw new CustomMessageException();
            }

            if (booking.Patient != null)
            {
                btnOtherEmail.OnClientClick = "javascript: get_referrer_additional_emails(" + booking.Patient.PatientID + ");return false;";
            }

            if (booking.Patient == null)
            {
                DataTable dt = BookingPatientDB.GetDataTable_ByBookingID(booking.BookingID);
                lstBookingPatients.DataSource = dt;
                lstBookingPatients.DataBind();

                main_table.Visible = false;
                select_booking_patient_table.Visible = true;
                return;

                //throw new CustomMessageException();
            }
            if (booking.Organisation == null)
            {
                throw new CustomMessageException();
            }

            // get selected id's
            ArrayList selectedIDs = new ArrayList();
            foreach (RepeaterItem item in lstNotes.Items)
            {
                CheckBox chkUseNote = (CheckBox)item.FindControl("chkUseNote");
                Label    lblNoteID  = (Label)item.FindControl("lblNoteID");
                if (chkUseNote.Checked)
                {
                    selectedIDs.Add(lblNoteID.Text);
                }
            }

            txtUpdatePatientID.Text      = booking.Patient.PatientID.ToString();
            txtUpdatePatientName.Text    = booking.Patient.Person.FullnameWithoutMiddlename;
            txtUpdatePatientName.Visible = false;
            lblUpdatePatientName.Text    = "<a href=\"#=\" onclick=\"open_new_window('PatientDetailV2.aspx?type=view&id=" + booking.Patient.PatientID + "'); return false;\">" + booking.Patient.Person.FullnameWithoutMiddlename + "</a>";
            lblUpdatePatientName.Visible = true;

            txtUpdateOrganisationID.Text      = booking.Organisation.OrganisationID.ToString();
            txtUpdateOrganisationName.Text    = booking.Organisation.Name;
            txtUpdateOrganisationName.Visible = false;
            lblUpdateOrganisationName.Text    = "<a href=\"#=\" onclick=\"open_new_window('OrganisationDetailV2.aspx?type=view&id=" + booking.Organisation.OrganisationID + "'); return false;\">" + booking.Organisation.Name + "</a>";
            lblUpdateOrganisationName.Visible = true;



            // show booking info

            lnkBookingSheetForPatient.Text        = "Booking sheet for " + booking.Patient.Person.FullnameWithoutMiddlename;
            lnkBookingSheetForPatient.PostBackUrl = String.Format("~/BookingsV2.aspx?type=patient&patient={0}&org={1}&staff={2}&offering={3}&date={4}", booking.Patient.PatientID, booking.Organisation.OrganisationID, booking.Provider.StaffID, booking.Offering.OfferingID, booking.DateStart.ToString("yyyy_MM_dd"));

            lnkBookingListForPatient.Text        = "Booking list for " + booking.Patient.Person.FullnameWithoutMiddlename;
            lnkBookingListForPatient.PostBackUrl = String.Format("~/BookingsListV2.aspx?patient={0}", booking.Patient.PatientID);

            lblBooking_Provider.Text      = booking.Provider.Person.FullnameWithoutMiddlename;
            lblBooking_Offering.Text      = booking.Offering.Name;
            lblBooking_BookingStatus.Text = booking.BookingStatus.Descr.ToString();
            lblBooking_Time.Text          = booking.DateStart.Date.ToString("dd MMM yyyy") + " - " + booking.DateStart.ToString("hh:mm") + "-" + booking.DateEnd.ToString("hh:mm");
            lblBooking_Notes.Text         = Note.GetPopupLinkTextV2(15, booking.EntityID, booking.NoteCount > 0, true, 1050, 530, "images/notes-bw-24.jpg", "images/notes-24.png");


            // display list of notes in repeater
            DataTable notes = NoteDB.GetDataTable_ByEntityID(booking.EntityID);
            lstNotes.DataSource = notes;
            lstNotes.DataBind();

            // check id's that were previously checked
            foreach (RepeaterItem item in lstNotes.Items)
            {
                CheckBox chkUseNote      = (CheckBox)item.FindControl("chkUseNote");
                Label    lblNoteID       = (Label)item.FindControl("lblNoteID");
                Label    lblOriginalText = (Label)item.FindControl("lblOriginalText");
                chkUseNote.Checked = selectedIDs.Contains(lblNoteID.Text);
            }

            // hide if got from url ... no need to change it
            btnPatientListPopup.Visible      = false;
            btnClearPatient.Visible          = false;
            btnOrganisationListPopup.Visible = false;
            btnClearOrganisation.Visible     = false;
        }
    }
    protected void btnPrint_Click(object sender, EventArgs e)
    {
        try
        {
            //ScriptManager.RegisterClientScriptBlock(this, GetType(), "fancyBox", "alert('a');", true);

            int letterPrintHistorySendMethodID = 1; // send by mail


            // make sure org and patient selected
            if (txtUpdatePatientID.Text.Length == 0)
            {
                throw new CustomMessageException("Please select a patient.");
            }
            //if (txtUpdateOrganisationID.Text.Length == 0)    //--- checking in javascript .. cuz can be blank and use site info in place of org info
            //    throw new CustomMessageException("Please select an organisation.");


            if (lstLetters.GetSelectedIndices().Length == 0)
            {
                throw new CustomMessageException("Please select a letter.");
            }

            // get letter and make sure it exists
            Letter letter             = LetterDB.GetByID(Convert.ToInt32(lstLetters.SelectedValue));
            string sourchTemplatePath = letter.GetFullPath(Convert.ToInt32(Session["SiteID"]));
            if (!File.Exists(sourchTemplatePath))
            {
                throw new CustomMessageException("File doesn't exist.");
            }

            // get list of selected notes!
            ArrayList list = new ArrayList();
            foreach (RepeaterItem item in lstNotes.Items)
            {
                if (((CheckBox)item.FindControl("chkUseNote")).Checked)
                {
                    Label lblNoteID = (Label)item.FindControl("lblNoteID");
                    Note  note      = NoteDB.GetByID(Convert.ToInt32(lblNoteID.Text));
                    list.Add(Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + "Treatment Note (" + note.DateAdded.ToString("dd-MM-yyyy") + "):" + Environment.NewLine + Environment.NewLine + ((Label)item.FindControl("lblOriginalText")).Text);
                }
            }
            string[] notes = (string[])list.ToArray(typeof(string));

            int bookingID = -1;
            if (Request.QueryString["booking"] != null)
            {
                bookingID = Convert.ToInt32(Request.QueryString["booking"]);
            }
            if (Request.QueryString["bookingpatient"] != null)
            {
                BookingPatient bp = BookingPatientDB.GetByID(Convert.ToInt32(Request.QueryString["bookingpatient"]));
                bookingID = bp.Booking.BookingID;
            }

            Letter.SendLetter(Response,
                              Letter.FileFormat.Word, // .pdf
                              SiteDB.GetByID(Convert.ToInt32(Session["SiteID"])),
                              letter.LetterID,
                              txtUpdateOrganisationID.Text == "" ? 0 : Convert.ToInt32(txtUpdateOrganisationID.Text),
                              Convert.ToInt32(txtUpdatePatientID.Text),
                              Convert.ToInt32(Session["StaffID"]),
                              bookingID,
                              -1,
                              1,
                              notes,
                              true,
                              letterPrintHistorySendMethodID);
        }
        catch (CustomMessageException cmEx)
        {
            SetErrorMessage(cmEx.Message);
            return;
        }
    }