private void OpenFile()
        {
            EmailAttach emailAttach       = _listEmailAttachDisplayed[gridAttachments.SelectedIndices[0]];
            string      strFilePathAttach = FileAtoZ.CombinePaths(EmailAttaches.GetAttachPath(), emailAttach.ActualFileName);

            try {
                if (EhrCCD.IsCcdEmailAttachment(emailAttach))
                {
                    string strTextXml = FileAtoZ.ReadAllText(strFilePathAttach);
                    if (EhrCCD.IsCCD(strTextXml))
                    {
                        Patient patEmail = null;                      //Will be null for most email messages.
                        if (_emailMessage.SentOrReceived == EmailSentOrReceived.ReadDirect || _emailMessage.SentOrReceived == EmailSentOrReceived.ReceivedDirect)
                        {
                            patEmail = _patCur;                          //Only allow reconcile if received via Direct.
                        }
                        string strAlterateFilPathXslCCD = "";
                        //Try to find a corresponding stylesheet. This will only be used in the event that the default stylesheet cannot be loaded from the EHR dll.
                        for (int i = 0; i < _listEmailAttachDisplayed.Count; i++)
                        {
                            if (Path.GetExtension(_listEmailAttachDisplayed[i].ActualFileName).ToLower() == ".xsl")
                            {
                                strAlterateFilPathXslCCD = FileAtoZ.CombinePaths(EmailAttaches.GetAttachPath(), _listEmailAttachDisplayed[i].ActualFileName);
                                break;
                            }
                        }
                        FormEhrSummaryOfCare.DisplayCCD(strTextXml, patEmail, strAlterateFilPathXslCCD);
                        return;
                    }
                }
                else if (IsORU_R01message(strFilePathAttach))
                {
                    if (DataConnection.DBtype == DatabaseType.Oracle)
                    {
                        MsgBox.Show(this, "Labs not supported with Oracle.  Opening raw file instead.");
                    }
                    else
                    {
                        FormEhrLabOrderImport FormELOI = new FormEhrLabOrderImport();
                        FormELOI.Hl7LabMessage = FileAtoZ.ReadAllText(strFilePathAttach);
                        FormELOI.ShowDialog();
                        return;
                    }
                }
                FileAtoZ.OpenFile(FileAtoZ.CombinePaths(EmailAttaches.GetAttachPath(), emailAttach.ActualFileName), emailAttach.DisplayedFileName);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
		private void butImport_Click(object sender,EventArgs e) {
			MsgBoxCopyPaste MBCP = new MsgBoxCopyPaste("Paste HL7 Lab Message Text Here.");
			MBCP.textMain.SelectAll();
			MBCP.ShowDialog();
			if(MBCP.DialogResult!=DialogResult.OK) {
				return;
			}
			List<EhrLab> listEhrLabs;
			try {
				listEhrLabs=EhrLabs.ProcessHl7Message(MBCP.textMain.Text);//Not a typical use of the msg box copy paste
				if(listEhrLabs[0].PatNum==PatCur.PatNum) {//only need to check the first lab.
					//nothing to do here. Imported lab matches the current patient.
				}
				else{//does not match current patient, redirect to import form which displays patient information and is build for importing.
					FormEhrLabOrderImport FormLOI=new FormEhrLabOrderImport();
					FormLOI.PatCur=PatCur;
					FormLOI.Hl7LabMessage=MBCP.textMain.Text;
					FormLOI.ShowDialog();
					FillGrid();
					return;
				}
				//else if(listEhrLabs[0].PatNum==0) {
				//	if(MessageBox.Show("Lab patient does not match current patient. Lab patient name is "
				//		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]+" "//first name
				//		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]+" "//last name
				//		+"\r\nWould you like to import lab for the current patient?","",MessageBoxButtons.OKCancel)!=DialogResult.OK) 
				//	{
				//		return;
				//	}
				//	//User agreed to import current lab(s) for current patient.
				//	for(int i=0;i<listEhrLabs.Count;i++) {
				//		listEhrLabs[i].PatNum=PatCur.PatNum;
				//		//TODO: Import external OIDs and PatIDs so that we can identify this patient next time.
				//	}
				//}
				//else {//Patnum is already associated with another patient.
				//	MessageBox.Show("This lab contains patient information for a different patient. Lab patient name is "
				//		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]+" "//first name
				//		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]);
				//	return;
				//}
			}
			catch (Exception Ex){
				MessageBox.Show(this,"Unable to import lab.\r\n"+Ex.Message);
				return;
			}
			for(int i=0;i<listEhrLabs.Count;i++) {
				EhrLab tempLab=null;//lab from DB if it exists.
				tempLab=EhrLabs.GetByGUID(listEhrLabs[i].PlacerOrderUniversalID,listEhrLabs[i].PlacerOrderNum);
				if(tempLab==null){
					tempLab=EhrLabs.GetByGUID(listEhrLabs[i].FillerOrderUniversalID,listEhrLabs[i].FillerOrderNum);
				}
				if(tempLab!=null) {
					//Date validation.
					//if(tempLab.ResultDateTime.CompareTo(listEhrLabs[i].ResultDateTime)<=0) {//string compare dates will return 1+ if tempLab Date is greater.
					//	MsgBox.Show(this,"This lab already exists in the database and has a more recent timestamp.");
					//	continue;
					//}
					//TODO: The code above works, but ignores more recent lab results. Although the lab order my be unchanged there may be updated lab results.
					//It would be better to check for updated results, unfortunately results have no unique identifiers.
				}
				if(Security.CurUser.ProvNum!=0 && Providers.GetProv(Security.CurUser.ProvNum).EhrKey!="") {//The user who is currently logged in is a provider and has a valid EHR key.
					ListEhrLabs[i].IsCpoe=true;
				}
				listEhrLabs[i]=EhrLabs.SaveToDB(listEhrLabs[i]);//SAVE
				for(int j=0;j<listEhrLabs[i].ListEhrLabResults.Count;j++) {//EHR TRIGGER
					if(CDSPermissions.GetForUser(Security.CurUser.UserNum).ShowCDS && CDSPermissions.GetForUser(Security.CurUser.UserNum).LabTestCDS) {
						FormCDSIntervention FormCDSI=new FormCDSIntervention();
						FormCDSI.ListCDSI=EhrTriggers.TriggerMatch(listEhrLabs[i].ListEhrLabResults[j],PatCur);
						FormCDSI.ShowIfRequired(false);
					}
				}
			}
			FillGrid();
		}
        private void butImport_Click(object sender, EventArgs e)
        {
            MsgBoxCopyPaste MBCP = new MsgBoxCopyPaste("Paste HL7 Lab Message Text Here.");

            MBCP.textMain.SelectAll();
            MBCP.ShowDialog();
            if (MBCP.DialogResult != DialogResult.OK)
            {
                return;
            }
            List <EhrLab> listEhrLabs;

            try {
                listEhrLabs = EhrLabs.ProcessHl7Message(MBCP.textMain.Text); //Not a typical use of the msg box copy paste
                if (listEhrLabs[0].PatNum == PatCur.PatNum)                  //only need to check the first lab.
                //nothing to do here. Imported lab matches the current patient.
                {
                }
                else                 //does not match current patient, redirect to import form which displays patient information and is build for importing.
                {
                    FormEhrLabOrderImport FormLOI = new FormEhrLabOrderImport();
                    FormLOI.PatCur        = PatCur;
                    FormLOI.Hl7LabMessage = MBCP.textMain.Text;
                    FormLOI.ShowDialog();
                    FillGrid();
                    return;
                }
                //else if(listEhrLabs[0].PatNum==0) {
                //	if(MessageBox.Show("Lab patient does not match current patient. Lab patient name is "
                //		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]+" "//first name
                //		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]+" "//last name
                //		+"\r\nWould you like to import lab for the current patient?","",MessageBoxButtons.OKCancel)!=DialogResult.OK)
                //	{
                //		return;
                //	}
                //	//User agreed to import current lab(s) for current patient.
                //	for(int i=0;i<listEhrLabs.Count;i++) {
                //		listEhrLabs[i].PatNum=PatCur.PatNum;
                //		//TODO: Import external OIDs and PatIDs so that we can identify this patient next time.
                //	}
                //}
                //else {//Patnum is already associated with another patient.
                //	MessageBox.Show("This lab contains patient information for a different patient. Lab patient name is "
                //		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]+" "//first name
                //		+MBCP.textMain.Text.Split(new string[] { "\r\n" },StringSplitOptions.RemoveEmptyEntries)[1].Split('|')[5].Split('~')[0].Split('^')[1]);
                //	return;
                //}
            }
            catch (Exception Ex) {
                MessageBox.Show(this, "Unable to import lab.\r\n" + Ex.Message);
                return;
            }
            for (int i = 0; i < listEhrLabs.Count; i++)
            {
                EhrLab tempLab = null;              //lab from DB if it exists.
                tempLab = EhrLabs.GetByGUID(listEhrLabs[i].PlacerOrderUniversalID, listEhrLabs[i].PlacerOrderNum);
                if (tempLab == null)
                {
                    tempLab = EhrLabs.GetByGUID(listEhrLabs[i].FillerOrderUniversalID, listEhrLabs[i].FillerOrderNum);
                }
                if (tempLab != null)
                {
                    //Date validation.
                    //if(tempLab.ResultDateTime.CompareTo(listEhrLabs[i].ResultDateTime)<=0) {//string compare dates will return 1+ if tempLab Date is greater.
                    //	MsgBox.Show(this,"This lab already exists in the database and has a more recent timestamp.");
                    //	continue;
                    //}
                    //TODO: The code above works, but ignores more recent lab results. Although the lab order my be unchanged there may be updated lab results.
                    //It would be better to check for updated results, unfortunately results have no unique identifiers.
                }
                Provider prov = Providers.GetProv(Security.CurUser.ProvNum);
                if (Security.CurUser.ProvNum != 0 && EhrProvKeys.GetKeysByFLName(prov.LName, prov.FName).Count > 0)            //The user who is currently logged in is a provider and has a valid EHR key.
                {
                    ListEhrLabs[i].IsCpoe = true;
                }
                listEhrLabs[i] = EhrLabs.SaveToDB(listEhrLabs[i]);               //SAVE
                for (int j = 0; j < listEhrLabs[i].ListEhrLabResults.Count; j++) //EHR TRIGGER
                {
                    if (CDSPermissions.GetForUser(Security.CurUser.UserNum).ShowCDS&& CDSPermissions.GetForUser(Security.CurUser.UserNum).LabTestCDS)
                    {
                        FormCDSIntervention FormCDSI = new FormCDSIntervention();
                        FormCDSI.ListCDSI = EhrTriggers.TriggerMatch(listEhrLabs[i].ListEhrLabResults[j], PatCur);
                        FormCDSI.ShowIfRequired(false);
                    }
                }
            }
            FillGrid();
        }
		private void OpenFile(){
			string strFilePathAttach=ODFileUtils.CombinePaths(EmailMessages.GetEmailAttachPath(),MessageCur.Attachments[listAttachments.SelectedIndex].ActualFileName);
			try{
				if(EhrCCD.IsCcdEmailAttachment(MessageCur.Attachments[listAttachments.SelectedIndex])) {
					string strTextXml=File.ReadAllText(strFilePathAttach);
					if(EhrCCD.IsCCD(strTextXml)) {
						Patient patEmail=null;//Will be null for most email messages.
						if(MessageCur.SentOrReceived==EmailSentOrReceived.ReadDirect || MessageCur.SentOrReceived==EmailSentOrReceived.ReceivedDirect) {
							patEmail=_patCur;//Only allow reconcile if received via Direct.
						}
						string strAlterateFilPathXslCCD="";
						//Try to find a corresponding stylesheet. This will only be used in the event that the default stylesheet cannot be loaded from the EHR dll.
						for(int i=0;i<MessageCur.Attachments.Count;i++) {
							if(Path.GetExtension(MessageCur.Attachments[i].ActualFileName).ToLower()==".xsl") { 
								strAlterateFilPathXslCCD=ODFileUtils.CombinePaths(EmailMessages.GetEmailAttachPath(),MessageCur.Attachments[i].ActualFileName);
								break;
							}
						}
						FormEhrSummaryOfCare.DisplayCCD(strTextXml,patEmail,strAlterateFilPathXslCCD);
						return;
					}	
				}
				else if(detectORU_R01Message(strFilePathAttach)) {
					FormEhrLabOrderImport FormELOI =new FormEhrLabOrderImport();
					FormELOI.Hl7LabMessage=File.ReadAllText(strFilePathAttach);
					FormELOI.ShowDialog();
					return;
				}
				//We have to create a copy of the file because the name is different.
				//There is also a high probability that the attachment no longer exists if
				//the A to Z folders are disabled, since the file will have originally been
				//placed in the temporary directory.
				string tempFile=ODFileUtils.CombinePaths(Path.GetTempPath(),MessageCur.Attachments[listAttachments.SelectedIndex].DisplayedFileName);
				File.Copy(strFilePathAttach,tempFile,true);
				Process.Start(tempFile);
			}
			catch(Exception ex){
				MessageBox.Show(ex.Message);
			}
		}