Index: cprs/trunk/CPRS-Chart/fMHTest.pas
===================================================================
--- cprs/trunk/CPRS-Chart/fMHTest.pas	(revision 830)
+++ cprs/trunk/CPRS-Chart/fMHTest.pas	(revision 1679)
@@ -1,3 +1,5 @@
 unit fMHTest;
+
+{$DEFINE CCOWBROKER}
 
 interface
@@ -6,8 +8,10 @@
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   StdCtrls, ExtCtrls, ORCtrls, ORFn, uConst, fBase508Form, uDlgComponents,
-  VA508AccessibilityManager, uCore, orNet, TRPCB, StrUtils, rCore, VAUtils;
+  VA508AccessibilityManager, uCore, orNet, TRPCB, StrUtils, rCore, VAUtils
+  ;
 
 type
-TShowProc = procedure(Broker: TRPCBroker;
+TShowProc = procedure(
+   RPCBrokerV: TRPCBroker;
   InstrumentName,
   PatientDFN,
@@ -21,5 +25,6 @@
   var ProgressNote: string); stdcall;
 
-TSaveProc = procedure(Broker: TRPCBroker;
+TSaveProc = procedure(
+   RPCBrokerV: TRPCBroker;
   InstrumentName,
   PatientDFN,
@@ -30,5 +35,6 @@
   var Status: string); stdcall;
 
-TRemoveTempFile = procedure(
+TRemoveTempVistaFile = procedure(
+   RPCBrokerV: TRPCBroker;
   InstrumentName,
   PatientDFN: string); stdcall;
@@ -85,5 +91,5 @@
 var
   MHDLLHandle: THandle = 0;
-  
+
 implementation
 
@@ -107,6 +113,6 @@
   ShowProc                    : TShowProc = nil;
   SaveProc                    : TSaveProc = nil;
-  RemoveTempFile              : TRemoveTempFile = nil;
-  CloseProc                  : TCloseProc = nil;
+  RemoveTempVistaFile         : TRemoveTempVistaFile = nil;
+  CloseProc                   : TCloseProc = nil;
   SHARE_DIR = '\VISTA\Common Files\';
 var
@@ -115,5 +121,4 @@
   FYPos: TList;
   UsedMHDll: TUsedMHDll;
-  //DLLForceClose: boolean = false;
 
 type
@@ -147,4 +152,28 @@
   end;
 
+const
+  MHDLLName = 'YS_MHA_A.DLL';
+
+procedure LoadMHDLL;
+var
+  MHPath: string;
+
+begin
+  if MHDLLHandle = 0 then
+  begin
+    MHPath := GetProgramFilesPath + SHARE_DIR + MHDLLName;
+    MHDLLHandle := LoadLibrary(PChar(MHPath));
+  end;
+end;
+
+procedure UnloadMHDLL;
+begin
+  if MHDLLHandle <> 0 then
+  begin
+    FreeLibrary(MHDLLHandle);
+    MHDLLHandle := 0;
+  end;
+end;
+
 procedure ProcessMsg;
 var
@@ -224,14 +253,11 @@
 function SaveMHTest(TestName, date, Loc: string): boolean;
 var
-  MHPath, save: string;
-  
-begin
-  MHPath := GetProgramFilesPath + SHARE_DIR + 'YS_MHA';
-  if MHDLLHandle = 0 then
-    MHDLLHandle := LoadLibrary(PChar(MHPath));
+  save: string;
+begin
+  LoadMHDLL;
   Result := true;
   if MHDLLHandle = 0 then
     begin
-      InfoBox('YS_MHA.DLL not available', 'Error', MB_OK);
+      InfoBox(MHDLLName + ' not available', 'Error', MB_OK);
       Exit;
     end
@@ -244,5 +270,5 @@
           begin
           // function not found.. misspelled?
-            infoBox('Save Instrument Function not found within YS_MHA.DLL.', 'Error', MB_OK);
+            infoBox('Save Instrument Function not found within ' + MHDLLName + '.', 'Error', MB_OK);
             Exit;
           end;
@@ -250,5 +276,4 @@
         if Assigned(SaveProc) then
          begin
-//          fFrame.frmFrame.DLLActive := True;
           try
             SaveProc(RPCBrokerV,
@@ -258,8 +283,7 @@
             InttoStr(User.duz), //AdministeredByDUZ
             date,
-            Loc, //LocationIEN
+            Loc + 'V', //LocationIEN
             save);
           finally
-//            fFrame.frmFrame.DLLActive := false;
             if RPCBrokerV.CurrentContext <> 'OR CPRS GUI CHART' then
                begin
@@ -270,9 +294,5 @@
          end;
       finally
-        if MHDLLHandle <> 0 then
-        begin
-          FreeLibrary(MHDLLHandle);
-          MHDLLHandle := 0;
-        end;
+        UnloadMHDLL;
       end; {try..finally}
   end;
@@ -280,13 +300,9 @@
 
 procedure RemoveMHTest(TestName: string);
-var
-  MHPath: string;
-begin
-  MHPath := GetProgramFilesPath + SHARE_DIR + 'YS_MHA';
+begin
+  LoadMHDLL;
   if MHDLLHandle = 0 then
-    MHDLLHandle := LoadLibrary(PChar(MHPath));
-  if MHDLLHandle = 0 then
-    begin
-      InfoBox('YS_MHA.DLL not available', 'Error', MB_OK);
+    begin
+      InfoBox(MHDLLName + ' not available', 'Error', MB_OK);
       Exit;
     end
@@ -294,21 +310,20 @@
     begin
       try
-        @RemoveTempFile := GetProcAddress(MHDLLHandle, 'RemoveTempFile');
-
-        if @RemoveTempFile = nil then
+        @RemoveTempVistaFile := GetProcAddress(MHDLLHandle, 'RemoveTempVistaFile');
+
+        if @RemoveTempVistaFile = nil then
           begin
           // function not found.. misspelled?
-            InfoBox('Remove Temp File function not found within YS_MHA.DLL.', 'Error', MB_OK);
+            InfoBox('Remove Temp File function not found within ' + MHDLLName + '.', 'Error', MB_OK);
             Exit;
           end;
 
-        if Assigned(RemoveTempFile) then
+        if Assigned(RemoveTempVistaFile) then
          begin
-//          fFrame.frmFrame.DLLActive := True;
           try
-            RemoveTempFile(UpperCase(TestName), //InstrumentName
+            RemoveTempVistaFile(RPCBrokerV,
+            UpperCase(TestName), //InstrumentName
             Patient.DFN);
           finally
-//            fFrame.frmFrame.DLLActive := False;
             if RPCBrokerV.CurrentContext <> 'OR CPRS GUI CHART' then
                begin
@@ -319,9 +334,5 @@
          end;
       finally
-        if MHDLLHandle <> 0 then
-        begin
-          FreeLibrary(MHDLLHandle);
-          MHDLLHandle := 0;
-        end;
+        UnloadMHDLL;
       end; {try..finally}
   end;
@@ -329,6 +340,4 @@
 
 function CheckforMHDll: boolean;
-var
-  MHPath: string;
 begin
   Result := True;
@@ -346,13 +355,9 @@
   if MHDLLHandle = 0 then // if not 0 the DLL already loaded - result = true
   begin
-    MHPath := GetProgramFilesPath + SHARE_DIR + 'YS_MHA';
-    MHDLLHandle := LoadLibrary(PChar(MHPath));
+    LoadMHDLL;
     if MHDLLHandle = 0 then
       Result := false
     else
-    begin
-      FreeLibrary(MHDLLHandle);
-      MHDLLHandle := 0;
-    end;
+      UnloadMHDLL;
   end;
 end;
@@ -368,9 +373,5 @@
     end;
   finally
-    if MHDLLHandle <> 0 then
-    begin
-      FreeLibrary(MHDLLHandle);
-      MHDLLHandle := 0;
-    end;
+    UnloadMHDLL;
   end; {try..finally}
 end;
@@ -693,6 +694,5 @@
 function TfrmMHTest.CallMHDLL(TestName: string; Required: boolean): String;
 var                               
-//  dllHandle                   : THandle;
-  ProgressNote, MHPath                : string;
+  ProgressNote : string;
 begin
   ProgressNote := '';
@@ -704,12 +704,12 @@
       if UsedMHDll.Display = false then exit;
     end;
-  MHPath := GetProgramFilesPath + SHARE_DIR + 'YS_MHA';
-  if MHDLLHandle = 0 then
-    MHDLLHandle := LoadLibrary(PChar(MHPath));
+  LoadMHDLL;
   Result := '';
   if MHDLLHandle = 0 then
     begin
-      InfoBox('YS_MHA.dll not available.' + CRLF + 'CPRS will continue processing the MH test using the previous format.' +
-                  CRLF + CRLF + 'Contact IRM to install the YS_MHA.dll file on this machine.', 'Warning', MB_OK);
+      InfoBox(MHDLLName + ' not available.' + CRLF +
+                          'CPRS will continue processing the MH test using the previous format.' +
+                  CRLF + CRLF + 'Contact IRM to install the ' + MHDLLName +
+                                ' file on this machine.', 'Warning', MB_OK);
       Exit;
     end
@@ -722,5 +722,6 @@
           begin
           // function not found.. misspelled?
-            InfoBox('Function ShowInstrument not found within YS_MHA.DLL not available', 'Error', MB_OK);
+            InfoBox('Function ShowInstrument not found within ' + MHDLLName +
+                    ' not available', 'Error', MB_OK);
             Exit;
           end;
@@ -728,7 +729,5 @@
         if Assigned(ShowProc) then
            begin
-//             MHDLLHandle := dllHandle;
              Result := '';
-//             fFrame.frmFrame.DLLActive := True;
              try
                ShowProc(RPCBrokerV,
@@ -740,12 +739,10 @@
                InttoStr(User.duz), //AdministeredByDUZ
                Encounter.LocationName, //Location
-               InttoStr(Encounter.Location), //LocationIEN
+               InttoStr(Encounter.Location) + 'V', //LocationIEN
                Required,
                ProgressNote);
                Result := ProgressNote;
            finally
-//             MHDllHandle := 0;
-//             fFrame.frmFrame.DLLActive := false;
-             if RPCBrokerV.CurrentContext <> 'OR CPRS GUI CHART' then
+//           if RPCBrokerV.CurrentContext <> 'OR CPRS GUI CHART' then
                begin
                  if RPCBrokerV.CreateContext('OR CPRS GUI CHART') = false then
@@ -755,9 +752,5 @@
             end;
       finally
-        if MHDLLHandle <> 0 then
-        begin
-          FreeLibrary(MHDLLHandle);
-          MHDllHandle := 0;
-        end;
+        UnloadMHDLL;
       end; {try..finally}
       //Result := ProgressNote;
