public void TestEmailClass()
        {
            Email e = new Email();
            e.patientID = 1;
            e.type = "Personal";
            e.email = "*****@*****.**";

            Assert.IsNotNull(e);
            Assert.AreEqual(1, e.patientID);
            Assert.AreEqual("Personal", e.type);
            Assert.AreEqual("*****@*****.**", e.email);
        }
        private void btnAddRecord_Click(object sender, EventArgs e)
        {
            bool DOB_valid;
            bool exp_valid;
            bool pass=false;

            //check whether Insurance numer exits
            dictionary = new Dictionary<string, string>();
            dictionary.Add("@insuranceNumber", txtInsuranceNumber.Text);
            dc = new DatabaseConnector();
            dtInsurance = dc.getData("CheckInsuranceExists", dictionary);
            int ins_exist = int.Parse(dtInsurance.Rows[0][0].ToString());

            //create new patient
            Patient patient = new Patient();
            patient.firstName = txtFirstName.Text;
            patient.lastName = txtLastName.Text;
            patient.dob = txtDOB.Text;
            patient.gender = cmbxGender.GetItemText(cmbxGender.SelectedItem);

            //create new insurance
            Insurance insurance = new Insurance();
            insurance.timestamp = System.DateTime.Today.ToShortDateString();
            insurance.insNumber = txtInsuranceNumber.Text;
            insurance.versionCode = txtVersionCode.Text;
            insurance.expDate = txtInsuranceExpDate.Text;

            //create new allergy
            Allergy allergy = new Allergy();
            allergy.name = txtAllergyName.Text;
            allergy.category = txtAllergyCatergory.Text;

            //create new allergy-patient
            PatientAllergy ptAllgergy = new PatientAllergy();

            //
            Doctor doctor = new Doctor();
            doctor.billingCode = txtDoctorBillingCode.Text;
            doctor.name = txtReferralDoctor.Text;

            //
            ReferralDoctor refDoctor = new ReferralDoctor();

                    
            //ins number : number only
            //ins vc : character only
            //no blank: name, dob

            Phone phone = new Phone();
            phone.type = cmbxPhoneType.GetItemText(cmbxPhoneType.SelectedItem);
            phone.phoneNumber = txtPhoneNumber.Text;

            Email email = new Email();
            email.type = cmbxEmailType.GetItemText(cmbxEmailType.SelectedItem);
            email.email = txtEmail.Text;

            Address address = new Address();
            address.type = cmbxAddressType.GetItemText(cmbxAddressType.SelectedItem);
            address.address = txtAddress.Text;
            address.city = txtCity.Text;
            address.province = txtProvince.Text;
            address.postalCode = txtPostalCode.Text;

            DOB_valid = DOB_validation();
            exp_valid = exp_validation();

            if (DOB_valid == true && exp_valid == true && ins_exist == 0 && txtInsuranceNumber.TextLength == 10)
            {
                pass = true;
            }

              if (pass == true )
              {
                  insertPatient(patient);

                  patient.patientID = getPtIdfromPatienTbl();
                  insurance.patientID = patient.patientID;
                  phone.patientID = patient.patientID;
                  email.patientID = patient.patientID;
                  address.patientID = patient.patientID;
                  

                  insertInsurance(insurance);

                  if (txtAllergyCatergory.Text != "" && txtAllergyName.Text != "")
                  {

                      if (getAllergyIdfromAllergyTbl1() != 0)
                      {
                          ptAllgergy.patientId = patient.patientID;
                          ptAllgergy.allergyId = getAllergyIdfromAllergyTbl1();
                          insertPatientAllergy(ptAllgergy);
                      }
                      else
                      {
                          insertAllergy(allergy);
                          ptAllgergy.patientId = patient.patientID;
                          ptAllgergy.allergyId = getAllergyIdfromAllergyTbl2();
                          insertPatientAllergy(ptAllgergy);
                      }
                  }
                    
                  if (txtPhoneNumber.Text != "")
                  insertPhone(phone);
                  if (txtAddress.Text != "")
                  insertAdress(address);
                  if (txtEmail.Text != "")
                  insertEmail(email);

                  if (txtReferralDoctor.Text != "")
                  {
                      refDoctor.patientID = patient.patientID;
                      refDoctor.doctorID = getDoctorIdfromDoctorTbl1();
                      insertReferralDoctor(refDoctor);
                  }

               /*   if (txtDoctorBillingCode.Text != "" && txtReferralDoctor.Text != "")
                  {

                      if (getDoctorIdfromDoctorTbl1() != 0)
                      {
                          refDoctor.patientID = patient.patientID;
                          refDoctor.doctorID = getDoctorIdfromDoctorTbl1();
                          insertReferralDoctor(refDoctor);
                      }
                      else
                      {
                          insertDoctor(doctor);
                          refDoctor.patientID = patient.patientID;
                          refDoctor.doctorID = getDoctorIdfromDoctorTbl2();
                          insertReferralDoctor(refDoctor);
                      }
                  }*/


                

                    clearText();
                    MessageBox.Show("Patient was registed");
                    DialogResult result = MessageBox.Show("Go to patient demography?", "Patient registed", MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Question);

                   if (result == DialogResult.OK)
                   {
                         this.Hide();
                        ViewDemography viewDemo = new ViewDemography();
                        viewDemo.patientId = patient.patientID;
                       viewDemo.Show();        
                      
                                         
                      
                       
                   }
                   else if (result == DialogResult.Cancel)
                       this.Close();
              }

              else if (txtInsuranceNumber.TextLength != 10) //number incorrect of left blank
              {
                  MessageBox.Show("Please insert correct insurance number");
              }
              else if (ins_exist != 0)
              {
                  MessageBox.Show("Insurance Number already exists");
              }
              else if (DOB_valid == false)
              {
                  MessageBox.Show("D.O.B not valid");
              }

              else if (exp_valid == false)
              {
                  MessageBox.Show("Insurance Expiry Date not valid");
              }
              
 
       

            

            

           
        }
        private static void insertEmail(Email email)
        {
            myConnection.Open();

            SqlCommand cmd = new SqlCommand("insert into PatientEmail(patientID, type, emailAddress) values(@pt_Id, @type, @email);");
            cmd.Connection = myConnection;

            cmd.Parameters.AddWithValue("@pt_Id", email.patientID);
            cmd.Parameters.AddWithValue("@type", email.type);
            cmd.Parameters.AddWithValue("@email", email.email);


            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();


            myConnection.Close();
        }