/// <summary>
        /// Reviews a session
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonReviewSession_Click(object sender, EventArgs e)
        {
            if (_patient.Sessions[GetSelectedSessionID()].MotionCaptureDataRecorded && _patient.Sessions[GetSelectedSessionID()].SubSessions.Count != 0)
            {
                //Todo instead of calling Start and stop here, open up Run_Review_Session_Form, set the task and then let it control things
                if (_remote_DataManager.PlaybackStatus)
                {
                    StopPlayback();
                }
                else
                {
                    _remote_DataManager.ClientOpenSessionForPlayback(_selectedPatientIndex, GetSelectedSessionID(), 0);
                    _remote_DataManager.ClientRequestUpdatedPatientList();
                    _patient = _remote_DataManager.PatientList[_selectedPatientIndex];
                    if (_patient.Sessions[GetSelectedSessionID()].MotionCaptureDataRecorded == false)
                        FillInFields();
                    else
                    {
                        OnHideForm(true);
                        Run_Review_Session_Form form = new Run_Review_Session_Form();
                        form.CurrentSession = _patient.Sessions[_selectedSessionIndex];
                        if (form.ShowDialog() == DialogResult.Cancel) // showdialog rather than show so that we can get data back from it using form.Something
                        {
                            StopPlayback();
                        }
                        form.Dispose();
                        OnHideForm(false);
                    }
                    
                }
            }

        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            string errorString = "You must fill in this field";
            errorProvider.Clear();

            //create the patient
            _patient = new Patient();
            
            

            //validate and add fields
                      

            

           
            if (comboBoxTitle.SelectedIndex != -1)
            {
                _patient.Title = (Patient.TitleType)comboBoxTitle.SelectedIndex;
            }
            else
            {
                errorProvider.SetError(comboBoxTitle, errorString);
                return;
            }
            if (textBoxLastName.Text.Trim() == "")
            {
                errorProvider.SetError(textBoxLastName, errorString);
                return;
            }
            else
                _patient.LastName = textBoxLastName.Text;
            if (textBoxFirstName.Text.Trim() == "")
            {
                errorProvider.SetError(textBoxFirstName, errorString);
                return;
            }
            else
                _patient.FirstName = textBoxFirstName.Text;

            if (textBoxID.Text.Trim() == "")
            {
                errorProvider.SetError(textBoxID, errorString);
                return;
            }
            else
                _patient.PatientID = textBoxID.Text;

            
            if (comboBoxGender.SelectedIndex != -1)
            {
                _patient.Gender = (Patient.GenderType)comboBoxGender.SelectedIndex;
            }
            else
            {
                errorProvider.SetError(comboBoxGender, errorString);
                return;
            }

            if (dateTimePicker.Value.Date == DateTime.Today)
            {
                errorProvider.SetError(dateTimePicker, errorString);
                return;
            }

            _patient.DOB = dateTimePicker.Value;

            
           

            Postcode validatedPostcode;
            bool postcodeResult = Postcode.TryParse(textBoxPostcode.Text, out validatedPostcode, true);
            if (postcodeResult == false)
            {
                errorProvider.SetError(textBoxPostcode, "Invalid Post Code");
                return;
            }
            else
                _patient.Address.Postcode = validatedPostcode;

            if (!_patient.ContactNumber.TryParse(textBoxContactNumber.Text))
            {
                errorProvider.SetError(textBoxContactNumber, "Invalid Number");
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
            
        }
        /// <summary>
        /// Runs a session
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonRunSession_Click(object sender, EventArgs e)
        {
            if (CurrentClinician.RunSessions)
            {
                _selectedSessionIndex = GetSelectedSessionID();
                if (_patient.Sessions[_selectedSessionIndex].Task.FileName != "")
                {
                    //Todo instead of calling Start and stop here, open up Run_Review_Session_Form, set the task and then let it control things
                    if (_remote_DataManager.RecordingStatus)
                    {

                        StopRecording();
                    }
                    else
                    {
                        _patient.Sessions[_selectedSessionIndex].Clinician = CurrentClinician.NameString;
                        _remote_DataManager.ClientUpdatePatient(_patient, _selectedPatientIndex);
                        _remote_DataManager.ClientPrepareToRecordNewSession(_selectedPatientIndex, _selectedSessionIndex);

                        //Hide the main form, load the run_review_session form and wait for it to return
                        OnHideForm(true);
                        Run_Review_Session_Form form = new Run_Review_Session_Form();
                        form.CurrentSession = _patient.Sessions[_selectedSessionIndex];
                        if (form.ShowDialog() == DialogResult.Cancel) // showdialog rather than show so that we can get data back from it using form.Something
                        {
                            StopRecording();
                        }
                        else
                        {

                            _remote_DataManager.ClientRequestUpdatedPatientList();
                            _patient = _remote_DataManager.PatientList[_selectedPatientIndex];
                            FillInFields();
                        }
                        form.Dispose();
                        OnHideForm(false);
                    }
                }
                else
                    MessageBox.Show("You must select a Task before you can run the session.\nSelect the session you want to run, click Edit, and then select a task", "StroMoHab Client", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                MessageBox.Show(StroMoHab_Client.Properties.Settings.Default.InvalidPermissionsString, "StroMoHab Client", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
 private void StopRecording()
 {
     _remote_DataManager.ClientStopRecord();
     _remote_DataManager.ClientRequestUpdatedPatientList();
     _patient = _remote_DataManager.PatientList[_selectedPatientIndex];
     FillInFields();
 }
 /// <summary>
 /// Saves a newly created patient to the server
 /// </summary>
 /// <param name="patient">The new patient</param>
 public void ClientSaveNewPatient(Patient patient)
 {
     if (NewPatientSaveRequestedByClient != null)
         NewPatientSaveRequestedByClient(patient);
 }
        /// <summary>
        /// Generates a ListViewItem for listViewSessions
        /// </summary>
        /// <param name="patient"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        private ListViewItem GenerateListViewSessionItem(Patient patient, int index)
        {
            if (patient.Sessions[index].ScheduledSession && patient.Sessions[index].Task.FileName != "")
                return new ListViewItem(new string[] {
                    "Scheduled",
                    "",
                    "",
                    patient.Sessions[index].Task.FileName + " - " + Task.GetEnumDescription(patient.Sessions[index].Task.TaskType),
                    index.ToString()}, listViewGroup_Scheduled);
            else if (patient.Sessions[index].ScheduledSession && patient.Sessions[index].Task.FileName == "")
                return new ListViewItem(new string[] {
                    "Scheduled",
                    "",
                    "",
                    "Not Selected",
                    index.ToString()}, listViewGroup_Scheduled);
            else
                return new ListViewItem(new string[] {
                    patient.Sessions[index].SessionStartTime.ToShortDateString(),
                    patient.Sessions[index].SessionStartTime.ToShortTimeString(),
                    patient.Sessions[index].Duration.Minutes + ":" + patient.Sessions[index].Duration.Seconds,
                    patient.Sessions[index].Task.FileName + " - " + Task.GetEnumDescription(patient.Sessions[index].Task.TaskType),
                    index.ToString()
            }, listViewGroup_Completed);

        }
 /// <summary>
 /// Updates the details for the specified patient
 /// </summary>
 /// <param name="patient">The updated patient</param>
 /// <param name="patientIndex">Its current location in the patient list</param>
 public void ClientUpdatePatient(Patient patient, int patientIndex)
 {
     if (UpdatePatientRequestedByClient != null)
         UpdatePatientRequestedByClient(patient, patientIndex);
 }
        /// <summary>
        /// Converts patient data into a form suitable for a ListView
        /// </summary>
        /// <param name="patient">The Patient</param>
        /// <param name="ID">An ID number to trace the item back to the patient</param>
        /// <returns></returns>
        private ListViewItem GenerateListViewPatientItem(Patient patient, int ID)
        {
            return new ListViewItem(new string[] {
                patient.PatientID,
                patient.Title.ToString(),
                patient.LastName,
                patient.FirstName,
                patient.Gender.ToString(),
                patient.DOB.ToShortDateString(),
                patient.ContactNumber.Number,
                patient.Address.Postcode.ToString(),
                ID.ToString() // Hide the id number after the main data so that the item can be traced back
            },listViewGroup_Patients);

        }
 /// <summary>
 /// Saves a patient file
 /// </summary>
 /// <param name="patient"></param>
 /// <returns></returns>
 private static bool SavePatientFile(Patient patient)
 {
     bool saveSucceeded = false;
     string filePath = GetFullFileName(patient.FileName);
     try
     {
         FileStream fs = new FileStream(filePath, FileMode.Create);
         BinaryFormatter bFormatter = new BinaryFormatter();
         bFormatter.Serialize(fs, patient);
         fs.Close();
         saveSucceeded = true;
     }
     catch (Exception ex)
     {
         System.Windows.Forms.MessageBox.Show("Save Unsuccesful. System Message: " + ex.Message);
     }
     finally
     {
         if (saveSucceeded)
         {
             //  System.Windows.Forms.MessageBox.Show("File Saved", "Save Successful");
         }
     }
     return saveSucceeded;
 }
 /// <summary>
 /// Saves a new patient
 /// </summary>
 /// <param name="patient">The new patient</param>
 static void Patient_Remote_DataManager_NewPatientSaveRequestedByClient(Patient patient)
 {
     SavePatientFile(patient);
     LoadInPatientData();
 }
        /// <summary>
        /// Updates a previously saved patient and deletes the old one if the file name has changed
        /// </summary>
        /// <param name="patient">The patient data</param>
        /// <param name="patientIndex">The patients current location</param>
        static void Patient_Remote_DataManager_UpdatePatientRequestedByClient(Patient patient, int patientIndex)
        {
            if (patient.FileName == _patients[patientIndex].FileName) //If they have the same filename then save over
                SavePatientFile(patient);
            else //otherwise delete the old one and save the new one
            {
                System.IO.File.Delete(GetFullFileName(_patients[patientIndex].FileName));
                SavePatientFile(patient);
            }
            //now update the lists
            Patient_Remote_DataManager_UpdatedPatientListRequestedByClient();

        }