Index: cprs/branches/foia-cprs/CPRS-Chart/fFrame.pas
===================================================================
--- cprs/branches/foia-cprs/CPRS-Chart/fFrame.pas	(revision 459)
+++ cprs/branches/foia-cprs/CPRS-Chart/fFrame.pas	(revision 460)
@@ -5,4 +5,5 @@
 {$OPTIMIZATION OFF}                              // REMOVE AFTER UNIT IS DEBUGGED
 {$WARN SYMBOL_PLATFORM OFF}
+{$DEFINE CCOWBROKER}
 
 {.$define debug}
@@ -120,4 +121,16 @@
     mnuAlertRenew: TMenuItem;
     AppEvents: TApplicationEvents;
+    paVAA: TKeyClickPanel;
+    mnuToolsGraphing: TMenuItem;
+    laVAA2: TButton;
+    laMHV: TButton;
+    lblCIRNAvail: TLabel;
+    mnuViewInformation: TMenuItem;
+    mnuViewVisits: TMenuItem;
+    mnuViewPrimaryCare: TMenuItem;
+    mnuViewMyHealtheVet: TMenuItem;
+    mnuInsurance: TMenuItem;
+    mnuViewFlags: TMenuItem;
+    mnuViewRemoteData: TMenuItem;
     procedure tabPageChange(Sender: TObject);
     procedure FormCreate(Sender: TObject);
@@ -142,5 +155,4 @@
     procedure mnuHelpBrokerClick(Sender: TObject);
     procedure mnuFileEncounterClick(Sender: TObject);
-    procedure mnuViewDemoClick(Sender: TObject);
     procedure mnuViewPostingsClick(Sender: TObject);
     procedure mnuHelpAboutClick(Sender: TObject);
@@ -171,5 +183,4 @@
     function FormHelp(Command: Word; Data: Integer;
       var CallHelp: Boolean): Boolean;
-    procedure mnuViewRemindersClick(Sender: TObject);
     procedure pnlRemindersMouseDown(Sender: TObject; Button: TMouseButton;
       Shift: TShiftState; X, Y: Integer);
@@ -219,5 +230,15 @@
     procedure AppEventsActivate(Sender: TObject);
     procedure ScreenActiveFormChange(Sender: TObject);
+    procedure AppEventsShortCut(var Msg: TWMKey; var Handled: Boolean);
     procedure mnuToolsClick(Sender: TObject);
+    procedure mnuToolsGraphingClick(Sender: TObject);
+    procedure pnlCIRNMouseDown(Sender: TObject; Button: TMouseButton;
+      Shift: TShiftState; X, Y: Integer);
+    procedure pnlCIRNMouseUp(Sender: TObject; Button: TMouseButton;
+      Shift: TShiftState; X, Y: Integer);
+    procedure laMHVClick(Sender: TObject);
+    procedure laVAA2Click(Sender: TObject);
+    procedure ViewInfo(Sender: TObject);
+    procedure mnuViewInformationClick(Sender: TObject);
   private
     FJustEnteredApp : boolean;
@@ -228,4 +249,5 @@
     FCCOWBusy: boolean;
     FCCOWError: boolean;
+    FNoPatientSelected: boolean;
     FRefreshing: boolean;
     FClosing: boolean;
@@ -242,4 +264,5 @@
     FTabChanged: TNotifyEvent;
     FOldActivate: TNotifyEvent;
+    FOldActiveFormChange: TNotifyEvent;
     FECSAuthUser: Boolean;
     FFixedStatusWidth: integer;
@@ -248,5 +271,9 @@
     FFlagList: TStringList;
     FPrevPtID: string;
+    FVitalsDLLActive: boolean;
+    FGraphFloatActive: boolean;
+    FGraphContext: string;
     procedure RefreshFixedStatusWidth;
+    procedure FocusApplicationTopForm;
     procedure AppActivated(Sender: TObject);
     procedure AppDeActivated(Sender: TObject);
@@ -272,5 +299,5 @@
     procedure SetDebugMenu;
     procedure SetupPatient(AFlaggedList : TStringList = nil);
-    procedure SetUpCIRN;
+    //procedure SetUpCIRN;
     procedure RemindersChanged(Sender: TObject);
     procedure ReportsOnlyDisplay;
@@ -283,15 +310,19 @@
     procedure UpdateECSParameter(var CmdParameter: string);
     function  ValidECSUser: boolean;
-    function  AllowCCOWContextChange(NewDFN: string): boolean;
+    procedure StartCCOWContextor;
+    function  AllowCCOWContextChange(var CCOWResponse: UserResponse; NewDFN: string): boolean;
     procedure UpdateCCOWContext;
     procedure CheckHyperlinkResponse(aContextItemCollection: IDispatch; var HyperlinkReason: string);
     procedure CheckForDifferentPatient(aContextItemCollection: IDispatch; var PtChanged: boolean);
+{$IFDEF CCOWBROKER}
+    procedure CheckForDifferentUser(aContextItemCollection: IDispatch; var UserChanged: boolean);
+{$ENDIF}
     procedure HideEverything;
     procedure ShowEverything;
     //function FindBestCCOWDFN(var APatientName: string): string;
-    function  FindBestCCOWDFN: string;
+    function FindBestCCOWDFN: string;
     procedure HandleCCOWError(AMessage: string);
   public
-    EnduringPtSelSplitterPos: integer;
+   EnduringPtSelSplitterPos: integer;
     procedure SetBADxList;
 
@@ -309,4 +340,9 @@
     property Closing:         Boolean read FClosing;
     property OnTabChanged:    TNotifyEvent read FTabChanged write FTabChanged;
+    property VitalsDLLActive: boolean read FVitalsDLLActive write FVitalsDLLActive;
+    property GraphFloatActive: boolean read FGraphFloatActive write FGraphFloatActive;
+    property GraphContext: string read FGraphContext write FGraphContext;
+    procedure ToggleMenuItemChecked(Sender: TObject);
+    procedure SetUpCIRN;
   end;
 
@@ -331,10 +367,18 @@
 uses
   ORNet, rCore, fPtSelMsg, fPtSel, fCover, fProbs, fMeds, fOrders, rOrders, fNotes, fConsults, fDCSumm,
-  rMisc, Clipbrd, fLabs, fReports, fPtDemo, fEncnt, fPtCWAD, uCore, fAbout, fReview, fxBroker,
+  rMisc, Clipbrd, fLabs, fReports, rReports, fPtDemo, fEncnt, fPtCWAD, uCore, fAbout, fReview, fxBroker,
   fxLists, fxServer, ORSystem, fRptBox, fSplash, rODAllergy, uInit, fLabTests, fLabInfo,
   uReminders, fReminderTree, ORClasses, fDeviceSelect, fDrawers, fReminderDialog, ShellAPI, rVitals,
-  fOptions, rTemplates, fSurgery, rSurgery, uEventHooks, uSignItems, fDefaultEvent,rECS,
+  fOptions, fGraphs, rTemplates, fSurgery, rSurgery, uEventHooks, uSignItems, fDefaultEvent,rECS,
   fIconLegend, uOrders, fPtSelOptns, DateUtils, uSpell, uOrPtf, fPatientFlagMulti,
-  fAlertForward, UBAGlobals, fBAOptionsDiagnoses,UBACore,fOrdersSign;
+  fAlertForward, UBAGlobals, fBAOptionsDiagnoses, UBACore, fOrdersSign, uVitals, fOrdersRenew, uFormMonitor
+  {$IFDEF CCOWBROKER}
+  , CCOW_const
+  {$ENDIF}
+  ;
+
+var                                 //  RV 05/11/04
+  IsRunExecuted: Boolean = FALSE;           //  RV 05/11/04
+  GraphFloat: TfrmGraphs;
 
 const
@@ -410,5 +454,8 @@
 procedure TfrmFrame.TimeOutAction;
 begin
-  Close;
+  if frmFrame.VitalsDLLActive then
+    CloseVitalsDLL()
+  else
+    Close;
 end;
 
@@ -460,5 +507,5 @@
   FContextChanging := True;
   Result := True;
-  if COMObjectActive or SpellCheckInProgress then
+  if COMObjectActive or SpellCheckInProgress or VitalsDLLActive then
     begin
       Reason := 'COM_OBJECT_ACTIVE';
@@ -531,4 +578,13 @@
   HasFlag := False;
   HidePatientSelectMessages;
+  if (GraphFloat <> nil) and GraphFloatActive then
+  with GraphFloat do
+  begin
+    Initialize;
+    DisplayData('top');
+    DisplayData('bottom');
+    lstCheck.Items.Clear;
+    Caption := 'CPRS Graphing - Patient: ' + MixedCase(Patient.Name);
+  end;
 end;
 
@@ -564,18 +620,11 @@
   FJustEnteredApp := false;
   SizeHolder := TSizeHolder.Create;
+  FOldActiveFormChange := Screen.OnActiveFormChange;
   Screen.OnActiveFormChange := ScreenActiveFormChange;
   if not (ParamSearch('CCOW')='DISABLE') then
     try
-      ctxContextor := TContextorControl.Create(Self);
-      with ctxContextor do
-        begin
-          OnPending := ctxContextorPending;
-          OnCommitted := ctxContextorCommitted;
-          OnCanceled := ctxContextorCanceled;
-        end;
-      FCCOWBusy := False;
-      FCCOWInstalled := True;
-      FCCOWDrivedChange := False;
+      StartCCOWContextor;
     except
+      IsRunExecuted := False;
       FCCOWInstalled := False;
       pnlCCOW.Visible := False;
@@ -585,4 +634,5 @@
   else
     begin
+      IsRunExecuted := False;
       FCCOWInstalled := False;
       pnlCCOW.Visible := False;
@@ -603,8 +653,29 @@
   FCreateProgress := FCP_SERVER;
 
+{$IFDEF CCOWBROKER}
+  EnsureBroker;
+  if ctxContextor <> nil then
+  begin
+    if ParamSearch('CCOW') = 'PATIENTONLY' then
+      RPCBrokerV.Contextor := nil
+    else
+      RPCBrokerV.Contextor := ctxContextor;
+  end
+  else
+    RPCBrokerV.Contextor := nil;
+{$ENDIF}
+
   if not ConnectToServer(TX_OPTION) then
   begin
+    if Assigned(RPCBrokerV) then
+      InfoBox(RPCBrokerV.RPCBError, 'Error', MB_OK or MB_ICONERROR);
     Close;
     Exit;
+  end;
+
+  if ctxContextor <> nil then
+  begin
+    if not (ParamSearch('CCOW') = 'PATIENTONLY') then
+      ctxContextor.NotificationFilter := ctxContextor.NotificationFilter + ';User';
   end;
 
@@ -616,6 +687,6 @@
   FOldActivate := Application.OnActivate;
   Application.OnActivate := AppActivated;
-  Application.OnDeactivate := AppDeActivated;
-
+  Application.OnDeActivate := AppDeActivated;
+  
   // create initial core objects
   FCreateProgress := FCP_OBJECTS;
@@ -695,5 +766,4 @@
   HasFlag  := False;
   FlagList := TStringList.Create;
-  FPrevPtID := '';
   // set up structures specific to the user
   Caption := TX_IN_USE + MixedCase(User.Name) + '  (' + RPCBrokerV.Server + ')';
@@ -729,5 +799,8 @@
   FCreateProgress := FCP_FINISH;
   pnlReminders.Visible := InteractiveRemindersActive;
+  GraphFloatActive := false;
+  GraphContext := '';
   uRemoteType := '';
+  FPrevPtID := '';
   SetUserTools;
   EnduringPtSelSplitterPos := 0;
@@ -736,5 +809,4 @@
   InitialOrderVariables;
   PostMessage(Handle, UM_INITIATE, 0, 0);    // select patient after main form is created
-
 //  mnuFileOpenClick(Self);
 //  if Patient.DFN = '' then  //*DFN*
@@ -744,53 +816,70 @@
 //  end;
 //  if WindowState = wsMinimized then WindowState := wsNormal;
-end;
-
-procedure TfrmFrame.UMInitiate(var Message: TMessage);
-begin
-  NotifyOtherApps(NAE_OPEN, IntToStr(User.DUZ));
-
-  if FCCOWInstalled then      // Please see me re: changes here - Thanks - RV.
+  SetFormMonitoring(true);
+end;
+
+procedure TfrmFrame.StartCCOWContextor;
+begin
   try
-    // To re-enable the multiple instance functionality, remove the '#' from the following line
-    ctxContextor.Run('CPRSChart', '', TRUE, '*');
-
-    // Uncomment the following line to run in secure binding mode, once the vault is configured to do so.
-    //   Also comment out the 'ctxContextor.Run' line above.
-    //ctxContextor.Run('CPRSChart', PASSCODE, TRUE, '*');
+    ctxContextor := TContextorControl.Create(Self);
+    with ctxContextor do
+      begin
+        OnPending := ctxContextorPending;
+        OnCommitted := ctxContextorCommitted;
+        OnCanceled := ctxContextorCanceled;
+      end;
+    FCCOWBusy := False;
+    FCCOWInstalled := True;
+    FCCOWDrivedChange := False;
+    ctxContextor.Run('CPRSChart', '', TRUE, 'Patient');
+    IsRunExecuted := True;
   except
     on exc : EOleException do
-      begin
-        try
-          // Uncomment the following line to run in secure binding mode, once the vault is configured to do so.
-          ctxContextor.Run('CPRSChart' + '#', '', TRUE, '*');
-          //ctxContextor.Run('CPRSChart' + '#', PASSCODE, TRUE, '*');
-
-          if ParamSearch('CCOW') = 'FORCE' then
+    begin
+      IsRunExecuted := False;
+      FreeAndNil(ctxContextor);
+      try
+        ctxContextor := TContextorControl.Create(Self);
+        with ctxContextor do
           begin
-            mnuFileResumeContext.Enabled := False;
-            mnuFileBreakContext.Visible := True;
-            mnuFileBreakContext.Enabled := True;
-          end
-          else
-          begin
-            ctxContextor.Suspend;
-            mnuFileResumeContext.Visible := True;
-            mnuFileBreakContext.Visible := True;
-            mnuFileBreakContext.Enabled := False;
+            OnPending := ctxContextorPending;
+            OnCommitted := ctxContextorCommitted;
+            OnCanceled := ctxContextorCanceled;
           end;
-        except
-          on exc : EOleException do
-            begin
-              FCCOWInstalled := False;
-              pnlCCOW.Visible := False;
-              mnuFileResumeContext.Visible := False;
-              mnuFileBreakContext.Visible := False;
-            end;
+        FCCOWBusy := False;
+        FCCOWInstalled := True;
+        FCCOWDrivedChange := False;
+        ctxContextor.Run('CPRSChart' + '#', '', TRUE, 'Patient');
+        IsRunExecuted := True;
+        if ParamSearch('CCOW') = 'FORCE' then
+        begin
+          mnuFileResumeContext.Enabled := False;
+          mnuFileBreakContext.Visible := True;
+          mnuFileBreakContext.Enabled := True;
+        end
+        else
+        begin
+          ctxContextor.Suspend;
+          mnuFileResumeContext.Visible := True;
+          mnuFileBreakContext.Visible := True;
+          mnuFileBreakContext.Enabled := False;
         end;
-      end;   // on exc
-  end;  // try/except
-
+      except
+        IsRunExecuted := False;
+        FCCOWInstalled := False;
+        FreeAndNil(ctxContextor);
+        pnlCCOW.Visible := False;
+        mnuFileResumeContext.Visible := False;
+        mnuFileBreakContext.Visible := False;
+      end;
+    end;
+  end
+end;
+
+procedure TfrmFrame.UMInitiate(var Message: TMessage);
+begin
+  NotifyOtherApps(NAE_OPEN, IntToStr(User.DUZ));
   LoadUserPreferences;
-  GetBAStatus(User.DUZ,Encounter.Location);
+  GetBAStatus(User.DUZ,Patient.DFN);
   mnuFileOpenClick(Self);
   Enabled := True;
@@ -803,4 +892,5 @@
 begin
   Application.OnActivate := FOldActivate;
+  Screen.OnActiveFormChange := FOldActiveFormChange;
   FNextButtonBitmap.Free;
   uTabList.Free;
@@ -814,4 +904,5 @@
   User.Free;
   SizeHolder.Free;
+  ctxContextor.Free;
 end;
 
@@ -833,8 +924,4 @@
 
 procedure TfrmFrame.SetUserTools;
-const
-  TX_M_NOT_CONFIG = 'Tools/Options menu on M side not configured properly -'+
-                    ' please notify your IRM support immediately';
-  TC_M_NOT_CONFIG = 'Warning: Check Tools/Options Menu in M';
 var
   ToolItems: TToolItemList;
@@ -885,11 +972,5 @@
     UserTool.Hint := Action;
     UserTool.OnClick := ToolClick;
-    if i < mnuTools.Count then
-      mnuTools.Insert(i, UserTool)
-    else
-    begin
-      mnuTools.Add(UserTool);
-      InfoBox(TX_M_NOT_CONFIG, TC_M_NOT_CONFIG, MB_ICONWARNING or MB_OK)
-    end;
+    mnuTools.Insert(i, UserTool);
   end;
 end;
@@ -965,4 +1046,5 @@
 begin
   FClosing := TRUE;
+  SetFormMonitoring(false);
   if FCreateProgress < FCP_FINISH then FTerminate := True;
 
@@ -970,4 +1052,11 @@
   if FCreateProgress = FCP_FINISH then NotifyOtherApps(NAE_CLOSE, '');
   TerminateOtherAppNotification;
+
+  if GraphFloat <> nil then
+  begin
+    if frmFrame.GraphFloatActive then
+      GraphFloat.Close;
+    GraphFloat.Release;
+  end;
 
   // unhook the timeout hooks
@@ -976,5 +1065,5 @@
   if FCreateProgress = FCP_FINISH then Changes.Clear;
   // clear server side flag global tmp
-  ClearFlag;
+  if FCreateProgress = FCP_FINISH then ClearFlag;
   // save user preferences
   if FCreateProgress = FCP_FINISH then SaveUserPreferences;
@@ -1005,4 +1094,5 @@
   //Application.ProcessMessages;  // so everything finishes closing
   // if < FCP_FINISH we came here from inside FormCreate, so need to call terminate
+  //if GraphFloat <> nil then GraphFloat.Release;
   if FCreateProgress < FCP_FINISH then Application.Terminate;
 end;
@@ -1234,30 +1324,23 @@
       NotifyOtherApps(NAE_NEWPT, SSN + U + FloatToStr(DOB) + U + Name);
     SelectMsg := '';
-    if (not FRefreshing) then
-    begin
-      if not Assigned(AFlaggedList) then ShowFlags
-      else if (AFlaggedList.IndexOf(Patient.DFN) < 0) then ShowFlags;
-    end;
     if MeansTestRequired(Patient.DFN, AMsg) then SelectMsg := AMsg;
     if HasLegacyData(Patient.DFN, AMsg)     then SelectMsg := SelectMsg + CRLF + AMsg;
+
     HasActiveFlg(FlagList, HasFlag, Patient.DFN);
-    if FPrevPtID <> patient.DFN then
-    begin
-      if HasFlag then
-      begin
-        pnlFlag.Enabled := True;
-        lblFlag.Font.Color := clMaroon;
-        lblFlag.Enabled := True;
-        if not FReFreshing then
-          ShowFlags;
-      end else
-      begin
-        pnlFlag.Enabled := False;
-        lblFlag.Font.Color := clBtnFace;
-        lblFlag.Enabled := False;
-      end;
-      FPrevPtID := patient.DFN;
+    if HasFlag then
+    begin
+      pnlFlag.Enabled := True;
+      lblFlag.Font.Color := clMaroon;
+      lblFlag.Enabled := True;
+      if (not FReFreshing) and (TriggerPRFPopUp(Patient.DFN)) then
+        ShowFlags;
+    end else
+    begin
+      pnlFlag.Enabled := False;
+      lblFlag.Font.Color := clBtnFace;
+      lblFlag.Enabled := False;
     end;
-
+    FPrevPtID := patient.DFN;
+    frmCover.UpdateVAAButton; //VAA CQ7525  (moved here in v26.30 (RV))
     ProcessPatientChangeEventHook;
     if Length(SelectMsg) > 0 then ShowPatientSelectMessages(SelectMsg);
@@ -1270,4 +1353,5 @@
   NextIndex: Integer;
   Reason: string;
+  CCOWResponse: UserResponse;
 begin
   SaveDFN := Patient.DFN;
@@ -1293,5 +1377,5 @@
          HasActiveFlg(FlagList, HasFlag, NewDFN);
         end;
-        if (FCCOWInstalled and (ctxContextor.State = csSuspended)) or (AllowCCOWContextChange(Patient.DFN)) then
+        if (FCCOWInstalled and (ctxContextor.State = csSuspended)) or (AllowCCOWContextChange(CCOWResponse, Patient.DFN)) then
           begin
             if Patient.Inpatient then
@@ -1333,5 +1417,6 @@
     mnuFileNotifRemove.Enabled := Notifications.Followup in [NF_FLAGGED_ORDERS,
                                                              NF_ORDER_REQUIRES_ELEC_SIGNATURE,
-                                                             NF_MEDICATIONS_EXPIRING,
+                                                             NF_MEDICATIONS_EXPIRING_INPT,
+                                                             NF_MEDICATIONS_EXPIRING_OUTPT,
                                                              NF_UNVERIFIED_MEDICATION_ORDER,
                                                              NF_UNVERIFIED_ORDER,
@@ -1361,5 +1446,6 @@
         NextIndex := PageIDToTab(CT_REPORTS);
     NF_DNR_EXPIRING                  : NextIndex := PageIDToTab(CT_ORDERS);
-    NF_MEDICATIONS_EXPIRING          : NextIndex := PageIDToTab(CT_ORDERS);
+    NF_MEDICATIONS_EXPIRING_INPT     : NextIndex := PageIDToTab(CT_ORDERS);
+    NF_MEDICATIONS_EXPIRING_OUTPT    : NextIndex := PageIDToTab(CT_ORDERS);
     NF_UNVERIFIED_MEDICATION_ORDER   : NextIndex := PageIDToTab(CT_ORDERS);
     NF_NEW_ORDER                     : NextIndex := PageIDToTab(CT_ORDERS);
@@ -1431,7 +1517,9 @@
   SaveDFN, Reason: string;
   //NextTab: Integer;     // moved up for visibility - v23.4  rV
-  ok, OldRemindersStarted: boolean;
+  ok, OldRemindersStarted, PtSelCancelled: boolean;
   //i: smallint;
-begin
+  CCOWResponse: UserResponse;
+begin
+  PtSelCancelled := FALSE;
   DetermineNextTab;
 (*  if (FRefreshing or User.UseLastTab) and (not FFirstLoad) then
@@ -1471,5 +1559,9 @@
   end;
 
-  SaveDFN := Patient.DFN;
+  if FNoPatientSelected then
+    SaveDFN := ''
+  else
+    SaveDFN := Patient.DFN;
+    
   OldRemindersStarted := RemindersStarted;
   RemindersStarted := FALSE;
@@ -1502,5 +1594,6 @@
           end;
         if (Patient.DFN = '') or (Sender = mnuFileOpen) or (Sender = mnuFileNext) or (Sender = mnuViewDemo) then
-          SelectPatient(SHOW_NOTIFICATIONS, Font.Size);
+          SelectPatient(SHOW_NOTIFICATIONS, Font.Size, PtSelCancelled);
+        if PtSelCancelled then exit;
         ShowEverything;
         DisplayEncounterText;
@@ -1536,7 +1629,8 @@
       begin
         Notifications.Clear;
-        SelectPatient(False, Font.Size); // Call Pt. Sel. w/o notifications.
+        SelectPatient(False, Font.Size, PtSelCancelled); // Call Pt. Sel. w/o notifications.
+        if PtSelCancelled then exit;
         ShowEverything;
-        DisplayEncounterText;        
+        DisplayEncounterText;
         FPrevInPatient := Patient.Inpatient;
         ok := TRUE;
@@ -1547,5 +1641,5 @@
       if FCCOWInstalled and (ctxContextor.State = csParticipating) and (not FRefreshing) then
         begin
-          if (AllowCCOWContextChange(Patient.DFN)) then
+          if (AllowCCOWContextChange(CCOWResponse, Patient.DFN)) then
             begin
               SetupPatient;
@@ -1555,8 +1649,15 @@
           else
             begin
-              Patient.DFN := SaveDFN;
-              SetupPatient;
-              tabPage.TabIndex := PageIDToTab(NextTab);
-              tabPageChange(tabPage);
+              case CCOWResponse of
+                urCancel: UpdateCCOWContext;
+                urBreak:
+                  begin
+                    // do not revert to old DFN if context was manually broken by user - v26 (RV)
+                    if (ctxContextor.State = csParticipating) then Patient.DFN := SaveDFN;
+                    SetupPatient;
+                    tabPage.TabIndex := PageIDToTab(NextTab);
+                    tabPageChange(tabPage);
+                  end;
+              end;
             end;
         end
@@ -1576,5 +1677,5 @@
   if  BILLING_AWARE then frmFrame.SetBADxList; //end IsBillingAware
  {End BillingAware}
-
+ //frmCover.UpdateVAAButton; //VAA CQ7525   CQ#7933 - moved to SetupPatient, before event hook execution (RV)
 end;
 
@@ -1669,21 +1770,6 @@
 { View Menu Events ------------------------------------------------------------------------- }
 
-procedure TfrmFrame.mnuViewDemoClick(Sender: TObject);
-{ displays patient inquiry report (which optionally allows new patient to be selected) }
-var
-  SelectNew: Boolean;
-begin
-  StatusText(TX_PTINQ);
-  PatientInquiry(SelectNew);
-  if Assigned(FLastPage) then
-    FLastPage.FocusFirstControl;
-  StatusText('');
-  if SelectNew then mnuFileOpenClick(mnuViewDemo);
-end;
-
 procedure TfrmFrame.mnuViewPostingsClick(Sender: TObject);
-{ displays the window that shows crisis notes, warnings, allergies, & advance directives }
-begin
-  ShowCWAD;
+begin
 end;
 
@@ -1933,4 +2019,5 @@
 { emulate a button press in the patient identification panel }
 begin
+  if pnlPatient.BevelOuter = bvLowered then exit;
   pnlPatient.BevelOuter := bvLowered;
   with lblPtName do SetBounds(Left+2, Top+2, Width, Height);
@@ -1943,4 +2030,5 @@
 { emulate the button raising in the patient identification panel & call Patient Inquiry }
 begin
+  if pnlPatient.BevelOuter = bvRaised then exit;
   pnlPatient.BevelOuter := bvRaised;
   with lblPtName do SetBounds(Left-2, Top-2, Width, Height);
@@ -2034,4 +2122,5 @@
   SetUserBounds2(RemDlgSplitters, RemDlgSpltr1, RemDlgSpltr2, Dummy ,Dummy);
   SetUserBounds2(DrawerSplitters,s1, s2, s3, Dummy);
+  if Assigned(frmSurgery) then frmSurgery.Drawers.LastOpenSize := Dummy; //CQ7315
   frmNotes.Drawers.LastOpenSize := s1;
   frmConsults.Drawers.LastOpenSize := s2;
@@ -2045,7 +2134,9 @@
      frmMeds.pnlBottom.Height := panelBottom;
      frmMeds.pnlMedIn.Height := panelMedIn;
+     //Meds Tab Non-VA meds columns
+     SetUserColumns(TControl(hdrMedsNonVA)); //CQ7314
      end;
 
-  frmCover.DisableAlign;
+     frmCover.DisableAlign;
   try
     SetUserBounds2(CoverSplitters1, s1, s2, s3, s4);
@@ -2076,4 +2167,5 @@
 var
   SizeList: TStringList;
+  SurgTempHt: integer;
 begin
   SaveUserFontSize(MainFontSize);
@@ -2098,7 +2190,12 @@
       Add(StrUserBounds2(RemDlgName, RemDlgLeft, RemDlgTop, RemDlgWidth, RemDlgHeight));
       Add(StrUserBounds2(RemDlgSplitters, RemDlgSpltr1, RemDlgSpltr2, 0 ,0));
+
+      //v26.47 - RV - access violation if Surgery Tab not enabled.  Set to designer height as default.
+      if Assigned(frmSurgery) then SurgTempHt := frmSurgery.Drawers.pnlTemplates.Height else SurgTempHt := 85;
       Add(StrUserBounds2(DrawerSplitters, frmNotes.Drawers.LastOpenSize,
                                              frmConsults.Drawers.LastOpenSize,
-                                             frmDCSumm.Drawers.LastOpenSize,0));
+                                             frmDCSumm.Drawers.LastOpenSize,
+                                             SurgTempHt)); // last parameter = CQ7315
+
       Add(StrUserBounds2(CoverSplitters1,
         frmCover.pnl_1.Width,
@@ -2114,4 +2211,10 @@
       //Meds Tab Splitters
       Add(StrUserBounds2(frmMeds.Name+'Split',frmMeds.pnlBottom.Height,frmMeds.pnlMedIn.Height,0,0));
+
+      //Meds Tab Non-VA meds columns
+      Add(StrUserColumns(fMeds.frmMeds.hdrMedsNonVA)); //CQ7314
+
+      //Orders Tab columns
+      Add(StrUserColumns(fOrders.frmOrders.hdrOrders)); //CQ6328
 
       if EnduringPtSelSplitterPos <> 0 then
@@ -2185,4 +2288,6 @@
       with lstCIRNLocations do Font.Size := NewFontSize;
       with tabPage       do Font.Size := NewFontSize;
+      with laMHV         do Font.Size := NewFontSize; //VAA
+      with laVAA2        do Font.Size := NewFontSize; //VAA
 
       tabPage.Height := MainFontHeight + TAB_VOFFSET;   // resize tab selector
@@ -2192,4 +2297,17 @@
       stsArea.Panels[0].Width := ResizeWidth( OldFont, Font, stsArea.Panels[0].Width);
       stsArea.Panels[2].Width := ResizeWidth( OldFont, Font, stsArea.Panels[2].Width);
+
+      //VAA CQ8271
+      if ((fCover.PtIsVAA and fCover.PtIsMHV)) then
+        begin
+         laMHV.Height := (pnlToolBar.Height div 2) -1;
+         with laVAA2 do
+           begin
+           Top := laMHV.Top + laMHV.Height;
+           Height := (pnlToolBar.Height div 2) -1;
+           end;
+         end;
+      //end VAA
+
       RefreshFixedStatusWidth;
       FormResize( self );
@@ -2206,4 +2324,13 @@
     lblPtCWAD.Font.Color := clWindowText;
     lblFlag.Font.Color   := clWindowText;
+  end;
+
+  case (NewFontSize) of
+   8: mnu8pt.Checked := true;
+  10: mnu10pt1.Checked := true;
+  12: mnu12pt1.Checked := true;
+  14: mnu14pt1.Checked := true;
+  18: mnu18pt1.Checked := true;
+  24: mnu24pt1.Checked := true;
   end;
 
@@ -2223,4 +2350,5 @@
   if Assigned(frmRemDlg) then frmRemDlg.SetFontSize;
   if Assigned(frmReminderTree) then frmReminderTree.SetFontSize(NewFontSize);
+  if GraphFloat <> nil then ResizeAnchoredFormToFont(GraphFloat);
 end;
 
@@ -2231,6 +2359,7 @@
   VISIT_WIDTH   = 36;
   POSTING_WIDTH = 11.5;
-  FLAG_WIDTH    =5;
+  FLAG_WIDTH    = 5;
   CIRN_WIDTH    = 7;
+  MHV_WIDTH     = 6;
   LINES_HIGH    = 2;
   M_HORIZ       = 4;
@@ -2239,6 +2368,6 @@
   M_WVERT       = 6;
   TINY_MARGIN   = 2;
-var
-  WidthNeeded: integer;
+//var
+  //WidthNeeded: integer;
 begin
   pnlToolbar.Height  := (LINES_HIGH * lblPtName.Height) + M_HORIZ + M_MIDDLE + M_HORIZ;
@@ -2257,5 +2386,6 @@
   pnlFlag.Width      := Round(FLAG_WIDTH * MainFontWidth);
   pnlFlag.Width      := Round(FLAG_WIDTH * MainFontWidth);
-  pnlCIRN.Width  := Round(CIRN_WIDTH * MainFontWidth) + M_WVERT;
+  pnlCIRN.Width      := Round(CIRN_WIDTH * MainFontWidth) + M_WVERT;
+  paVAA.Width        := Round(MHV_WIDTH * MainFontWidth) + M_WVERT;
   with lblPtPostings do
     SetBounds(M_WVERT, M_HORIZ, pnlPostings.Width-M_WVERT-M_WVERT, lblPtName.Height);
@@ -2274,5 +2404,5 @@
   end;
   //If that is not enough, add scroll bars to form
-  if pnlPrimaryCare.Width < HigherOf( lblPtCare.Left + lblPtCare.Width, lblPtAttending.Left + lblPtAttending.Width) + TINY_MARGIN then
+  {if pnlPrimaryCare.Width < HigherOf( lblPtCare.Left + lblPtCare.Width, lblPtAttending.Left + lblPtAttending.Width) + TINY_MARGIN then
   begin
     WidthNeeded := HigherOf( lblPtCare.Left + lblPtCare.Width, lblPtAttending.Left + lblPtAttending.Width) + TINY_MARGIN - pnlPrimaryCare.Width;
@@ -2280,9 +2410,20 @@
     Width := Width + WidthNeeded;
   end
-  else
+  else }   // commented out - BA
     HorzScrollBar.Range := 0;
 end;
 
 { Temporary Calls -------------------------------------------------------------------------- }
+
+procedure TfrmFrame.ToggleMenuItemChecked(Sender: TObject);
+begin
+  with (Sender as TMenuItem) do
+     begin
+     if not Checked then
+        Checked := true
+     else
+        Checked := false;
+     end;
+end;
 
 procedure TfrmFrame.mnuFontSizeClick(Sender: TObject);
@@ -2290,8 +2431,16 @@
   if (frmRemDlg <> nil) then
     ShowMessage('Please close the reminder dialog before changing font sizes.')
-  else if (dlgProbs <> nil) then
+  else
+     if (dlgProbs <> nil) then
     ShowMessage('Font size cannot be changed while adding or editing a problem.')
   else
-    with Sender as TMenuItem do ChangeFont(Tag);
+     begin
+     with (Sender as TMenuItem) do
+        begin
+        ToggleMenuItemChecked(Sender);
+        fMeds.oldFont := MainFontSize; //CQ9182
+        ChangeFont(Tag);
+        end;
+     end;
 end;
 
@@ -2413,9 +2562,22 @@
     inherited;
   end;
-end;
-
-procedure TfrmFrame.mnuViewRemindersClick(Sender: TObject);
-begin
-  ViewReminderTree;
+  if Message.WParam = SC_MAXIMIZE then
+  begin
+    // form becomes maximized;
+    frmOrders.mnuOptimizeFieldsClick(self);
+    frmProblems.mnuOptimizeFieldsClick(self);
+    frmMeds.mnuOptimizeFieldsClick(self);
+  end
+  else if Message.WParam = SC_MINIMIZE then
+  begin
+    // form becomes maximized;
+  end
+  else if Message.WParam = SC_RESTORE then
+  begin
+    // form is restored (from maximized);
+    frmOrders.mnuOptimizeFieldsClick(self);
+    frmProblems.mnuOptimizeFieldsClick(self);
+    frmMeds.mnuOptimizeFieldsClick(self);
+  end;
 end;
 
@@ -2423,5 +2585,4 @@
 var
   ImgName: string;
-
 begin
   pnlReminders.tag := HAVE_REMINDERS;
@@ -2497,5 +2658,5 @@
   pnlReminders.BevelOuter := bvRaised;
   if(pnlReminders.tag = HAVE_REMINDERS) then
-    mnuViewRemindersClick(Self);
+    ViewInfo(mnuViewReminders);
 end;
 
@@ -2505,13 +2666,27 @@
 var
   i: integer;
+  aAutoQuery: string;
   ASite: TRemoteSite;
 begin
   with RemoteSites do
+  if UseVistaWeb then
     begin
       ChangePatient(Patient.DFN);
+      lblCIRN.Caption := 'Remote';      //VistaWeb On
+      lblCIRNData.Caption := 'Data*';
+      pnlCIRN.Caption := 'Remote Data';
+      lblCIRN.Width := 43;
+      lblCIRNData.Width := 43;
+      lblCIRNData.Alignment := taCenter;
+      lblCIRN.Alignment := taCenter;
+      lblCIRN.Enabled     := True;
+      lblCIRNData.Enabled := True;
+      lblCIRNAvail.Enabled := True;
+      pnlCIRN.TabStop     := True;
       if RemoteDataExists and (RemoteSites.Count > 0) then
         begin
           lblCIRN.Enabled     := True;
           lblCIRNData.Enabled := True;
+          lblCIRNAvail.Enabled := True;
           pnlCIRN.TabStop     := True;
           if ColorToRGB(clWindowText) = ColorToRGB(clBlack) then
@@ -2519,5 +2694,10 @@
               lblCIRN.Font.Color  := clBlue;
               lblCIRNData.Font.Color  := clBlue;
+              lblCIRNAvail.Font.Color := clBlue;
               lstCIRNLocations.Font.Color  := clBlue;
+              lblCIRN.Caption := 'Remote';
+              lblCIRNData.Caption := 'Data*';
+              lblCIRNAvail.Caption := 'Available';
+              pnlCIRN.Caption := 'Remote Data Available';
             end
           else
@@ -2525,7 +2705,59 @@
               lblCIRN.Font.Color  := clWindowText;
               lblCIRNData.Font.Color  := clWindowText;
+              lblCIRNAvail.Font.Color := clWindowText;
               lstCIRNLocations.Font.Color  := clWindowText;
             end;
+        end
+      else
+        begin
+          lblCIRN.Font.Color  := clWindowText;
+          lblCIRNData.Font.Color  := clWindowText;
+          lblCIRNAvail.Font.Color := clWindowText;
+          lblCIRN.Enabled     := False;
+          lblCIRNData.Enabled := False;
+          lblCIRNAvail.Enabled := False;
+          pnlCIRN.TabStop     := False;
+          pnlCIRN.Hint := NoDataReason;
+        end;
+      pnlCIRN.Hint := 'Click to open VistaWeb';
+    end
+  else
+    begin
+      ChangePatient(Patient.DFN);
+      lblCIRN.Caption := ' Remote';
+      lblCIRNData.Caption := 'Data';
+      pnlCIRN.Caption := 'Remote Data';
+      lblCIRNAvail.Caption := '';
+      lblCIRN.Width := 43;
+      lblCIRNData.Width := 43;
+      lblCIRNData.Alignment := taCenter;
+      lblCIRN.Alignment := taCenter;
+      if RemoteDataExists and (RemoteSites.Count > 0) then
+        begin
+          lblCIRN.Enabled     := True;
+          lblCIRNData.Enabled := True;
+          lblCIRNAvail.Enabled := True;
+          pnlCIRN.TabStop     := True;
+          if ColorToRGB(clWindowText) = ColorToRGB(clBlack) then
+            begin
+              lblCIRN.Font.Color  := clBlue;
+              lblCIRNData.Font.Color  := clBlue;
+              lblCIRNAvail.Font.Color := clBlue;
+              lstCIRNLocations.Font.Color  := clBlue;
+              lblCIRN.Caption := 'Remote';
+              lblCIRNData.Caption := 'Data';
+              lblCIRNAvail.Caption := 'Available';
+              pnlCIRN.Caption := 'Remote Data Available';
+            end
+          else
+            begin
+              lblCIRN.Font.Color  := clWindowText;
+              lblCIRNData.Font.Color  := clWindowText;
+              lblCIRNAvail.Font.Color := clWindowText;
+              lstCIRNLocations.Font.Color  := clWindowText;
+              lblCIRNAvail.Color := clWindowText;
+            end;
           pnlCIRN.Hint := 'Click to display other facilities having data for this patient.';
+          lstCIRNLocations.Items.Add('-1' + U + 'Use VistaWeb from now on');
           if RemoteSites.Count > 0 then
             lstCIRNLocations.Items.Add('0' + U + 'All Available Sites');
@@ -2541,14 +2773,39 @@
           lblCIRN.Font.Color  := clWindowText;
           lblCIRNData.Font.Color  := clWindowText;
+          lblCIRNAvail.Font.Color := clWindowText;
           lblCIRN.Enabled     := False;
           lblCIRNData.Enabled := False;
+          lblCIRNAvail.Enabled := False;
           pnlCIRN.TabStop     := False;
           pnlCIRN.Hint := NoDataReason;
         end;
+    aAutoQuery := AutoRDV;        //Check to see if Remote Queries should be used for all available sites
+    if (aAutoQuery = '1') and (lstCIRNLocations.Count > 0) then
+      begin
+        lstCIRNLocations.ItemIndex := 1;
+        lstCIRNLocations.Checked[1] := true;
+        lstCIRNLocationsClick(self);
+      end;
+  end;
+end;
+
+procedure TfrmFrame.pnlCIRNClick(Sender: TObject);
+//var
+//  aAddress: string;
+begin
+  {if UseVistaWeb then
+    begin
+      pnlCIRN.BevelOuter := bvRaised;
+      pnlCIRN.Hint := 'Click to open VistaWeb';
+      lblCIRN.Width := 43;
+      lblCIRNData.Width := 43;
+      lblCIRNData.Alignment := taCenter;
+      lblCIRN.Alignment := taCenter;
+      lstCIRNLocations.Visible := false;
+      lstCIRNLocations.SendToBack;
+      aAddress := GetVistaWebAddress(Patient.DFN);
+      ShellExecute(Handle, 'open', PChar(aAddress), PChar(''), '', SW_NORMAL);
+      Exit;
     end;
-end;
-
-procedure TfrmFrame.pnlCIRNClick(Sender: TObject);
-begin
   if not RemoteSites.RemoteDataExists then Exit;
   if (not lstCIRNLocations.Visible) then
@@ -2566,5 +2823,6 @@
       lstCIRNLocations.SendToBack;
       pnlCIRN.Hint := 'Click to display other facilities having data for this patient.';
-    end
+    end }
+  ViewInfo(mnuViewRemoteData);
 end;
 
@@ -2581,16 +2839,73 @@
   AccessStatus: integer;
 begin
-  iAll := 0;
+  iAll := 1;
   AccessStatus := 0;
+  iIndex := lstCIRNLocations.ItemIndex;
+  if iIndex = 0 then
+    if (piece(lstCIRNLocations.Items[0],'^',1) = '-1') and (lstCIRNLocations.Checked[iIndex] = true) then
+      begin
+        if MessageDlg('Are you sure you want to make VistaWeb your default for viewing Remote Data?',
+          mtConfirmation, [mbYes, mbNo], 0) = mrYes then
+          begin
+            ChangeVistaWebParam('1');
+            lblCIRN.Caption := 'Remote';          //VistaWeb On
+            lblCIRNData.Caption := 'Data*';
+            pnlCIRN.Caption := 'Remote Data';
+            lblCIRNAvail.Caption := '';
+            lblCIRN.Width := 43;
+            lblCIRNData.Width := 43;
+            lblCIRNData.Alignment := taCenter;
+            lblCIRN.Alignment := taCenter;
+            with RemoteSites do if RemoteDataExists and (RemoteSites.Count > 0) then
+              begin
+                lblCIRN.Enabled     := True;
+                lblCIRNData.Enabled := True;
+                pnlCIRN.TabStop     := True;
+                if ColorToRGB(clWindowText) = ColorToRGB(clBlack) then
+                  begin
+                    lblCIRN.Font.Color  := clBlue;
+                    lblCIRNData.Font.Color  := clBlue;
+                    lstCIRNLocations.Font.Color  := clBlue;
+                    lblCIRN.Caption := 'Remote';
+                    lblCIRNData.Caption := 'Data*';
+                    lblCIRNAvail.Caption := 'Available';
+                    pnlCIRN.Caption := 'Remote Data Available';
+                  end
+                else
+                  begin
+                    lblCIRN.Font.Color  := clWindowText;
+                    lblCIRNData.Font.Color  := clWindowText;
+                    lstCIRNLocations.Font.Color  := clWindowText;
+                    lblCIRNAvail.Font.Color := clWindowText;
+                  end;
+              end;
+            pnlCIRNClick(self);
+            Exit;
+          end
+        else
+          lstCIRNLocations.Checked[iIndex] := false;
+      end
+    else
+      begin
+        ChangeVistaWebParam('0');
+        lblCIRN.Caption := 'Remote';
+        lblCIRNData.Caption := 'Data';
+        pnlCIRN.Caption := 'Remote Data';
+        lblCIRN.Width := 43;
+        lblCIRNData.Width := 43;
+        lblCIRNData.Alignment := taCenter;
+        lblCIRN.Alignment := taCenter;
+        pnlCIRNClick(self);
+        Exit;
+      end;
   if not CheckHL7TCPLink then
     begin
       InfoBox('Local HL7 TCP Link is down.' + CRLF + 'Unable to retrieve remote data.', TC_DGSR_ERR, MB_OK);
-      lstCIRNLocations.Checked[lstCIRNLocations.ItemIndex] := false;
+      lstCIRNLocations.Checked[iIndex] := false;
       Exit;
     end;
-  if lstCIRNLocations.Items.Count > 0 then
-    if piece(lstCIRNLocations.Items[0],'^',1) = '0' then
-      iAll := 1;
-  iIndex := lstCIRNLocations.ItemIndex;
+  if lstCIRNLocations.Items.Count > 1 then
+    if piece(lstCIRNLocations.Items[1],'^',1) = '0' then
+      iAll := 2;
   with frmReports do
     if piece(uRemoteType,'^',2) = 'V' then
@@ -2612,7 +2927,7 @@
         with RemoteSites.SiteList do
         for j := 0 to Count - 1 do
-          if lstCIRNLocations.Checked[j+1] = true then
+          if lstCIRNLocations.Checked[j+2] = true then
             begin
-              lstCIRNLocations.Checked[j+1] := false;
+              lstCIRNLocations.Checked[j+2] := false;
               TRemoteSite(RemoteSites.SiteList[j]).Selected := false;
               TRemoteSite(RemoteSites.SiteList[j]).ReportClear;
@@ -2630,11 +2945,11 @@
               Screen.Cursor := crDefault;
               aMsg := aMsg + ' at site: ' + TRemoteSite(Items[j]).SiteName;
-              s := lstCIRNLocations.Items[j+1];
-              lstCIRNLocations.Items[j+1] := pieces(s, '^', 1, 3);
+              s := lstCIRNLocations.Items[j+2];
+              lstCIRNLocations.Items[j+2] := pieces(s, '^', 1, 3);
               case AccessStatus of
               DGSR_FAIL: begin
                            if piece(aMsg,':',1) = 'RPC name not found at site' then //Allow for backward compatibility
                              begin
-                               lstCIRNLocations.Checked[j+1] := true;
+                               lstCIRNLocations.Checked[j+2] := true;
                                TRemoteSite(RemoteSites.SiteList[j]).ReportClear;
                                TRemoteSite(RemoteSites.SiteList[j]).LabClear;
@@ -2644,6 +2959,6 @@
                              begin
                                InfoBox(aMsg, TC_DGSR_ERR, MB_OK);
-                               lstCIRNLocations.Checked[j+1] := false;
-                               lstCIRNLocations.Items[j+1] := pieces(s, '^', 1, 3) + '^' + TC_DGSR_ERR;
+                               lstCIRNLocations.Checked[j+2] := false;
+                               lstCIRNLocations.Items[j+2] := pieces(s, '^', 1, 3) + '^' + TC_DGSR_ERR;
                                TRemoteSite(Items[j]).Selected := false;
                                Continue;
@@ -2651,5 +2966,5 @@
                          end;
               DGSR_NONE: begin
-                           lstCIRNLocations.Checked[j+1] := true;
+                           lstCIRNLocations.Checked[j+2] := true;
                            TRemoteSite(RemoteSites.SiteList[j]).ReportClear;
                            TRemoteSite(RemoteSites.SiteList[j]).LabClear;
@@ -2658,5 +2973,5 @@
               DGSR_SHOW: begin
                            InfoBox(AMsg, TC_DGSR_SHOW, MB_OK);
-                           lstCIRNLocations.Checked[j+1] := true;
+                           lstCIRNLocations.Checked[j+2] := true;
                            TRemoteSite(RemoteSites.SiteList[j]).ReportClear;
                            TRemoteSite(RemoteSites.SiteList[j]).LabClear;
@@ -2666,5 +2981,5 @@
                            MB_DEFBUTTON2) = IDYES then
                            begin
-                             lstCIRNLocations.Checked[j+1] := true;
+                             lstCIRNLocations.Checked[j+2] := true;
                              TRemoteSite(RemoteSites.SiteList[j]).ReportClear;
                              TRemoteSite(RemoteSites.SiteList[j]).LabClear;
@@ -2673,6 +2988,6 @@
                            else
                              begin
-                               lstCIRNLocations.Checked[j+1] := false;
-                               lstCIRNLocations.Items[j+1] := pieces(s, '^', 1, 3) + '^' + TC_DGSR_SHOW;
+                               lstCIRNLocations.Checked[j+2] := false;
+                               lstCIRNLocations.Items[j+2] := pieces(s, '^', 1, 3) + '^' + TC_DGSR_SHOW;
                                TRemoteSite(Items[j]).Selected := false;
                                Continue;
@@ -2680,6 +2995,6 @@
               else       begin
                            InfoBox(AMsg, TC_DGSR_DENY, MB_OK);
-                           lstCIRNLocations.Checked[j+1] := false;
-                           lstCIRNLocations.Items[j+1] := pieces(s, '^', 1, 3) + '^' + TC_DGSR_DENY;
+                           lstCIRNLocations.Checked[j+2] := false;
+                           lstCIRNLocations.Items[j+2] := pieces(s, '^', 1, 3) + '^' + TC_DGSR_DENY;
                            TRemoteSite(Items[j]).Selected := false;
                            Continue;
@@ -2690,5 +3005,5 @@
   else
     begin
-      if iIndex > -1 then
+      if iIndex > 0 then
         begin
           iCur := iIndex - iAll;
@@ -2779,6 +3094,8 @@
             TRemoteSite(Items[j]));
         end;
-  frmLabs.TabControl1.OnChange(nil);
-  frmReports.TabControl1.OnChange(nil);
+  //frmLabs.TabControl1.OnChange(nil);
+  //frmReports.TabControl1.OnChange(nil);
+  if frmReports.tvReports.SelectionCount > 0 then frmReports.tvReportsClick(self);
+  if frmLabs.lstReports.ItemIndex > -1 then frmLabs.ExtlstReportsClick(self, true);
   StatusText('');
 end;
@@ -2817,7 +3134,7 @@
 procedure TfrmFrame.lstCIRNLocationsChange(Sender: TObject);
 begin
-  if lstCIRNLocations.ItemIndex > -1 then
+  if lstCIRNLocations.ItemIndex > 0 then
     if (lstCIRNLocations.Selected[lstCIRNLocations.ItemIndex] = true) and (uUpdateStat = false) then
-      if not (piece(lstCIRNLocations.Items[0],'^',1) = '0') then
+      if not (piece(lstCIRNLocations.Items[1],'^',1) = '0') then
         lstCIRNLocations.OnClick(nil);
 end;
@@ -2852,5 +3169,6 @@
   if not Notifications.Active then Exit;
   case Notifications.Followup of
-    NF_MEDICATIONS_EXPIRING         : AlertType := 'Expiring Medications';
+    NF_MEDICATIONS_EXPIRING_INPT    : AlertType := 'Expiring Medications';
+    NF_MEDICATIONS_EXPIRING_OUTPT   : AlertType := 'Expiring Medications';
     NF_ORDER_REQUIRES_ELEC_SIGNATURE: AlertType := 'Unsigned Orders';
     NF_FLAGGED_ORDERS               : AlertType := 'Flagged Orders (for clarification)';
@@ -2935,5 +3253,9 @@
      FOSTFhintWindow.ReleaseHandle;
      FOSTFHintWndActive := False ;
-
+  end;
+  if FHintWinActive then   // graphing - hints on values
+  begin
+    FHintWin.ReleaseHandle;
+    FHintWinActive := false;
   end;
 end;
@@ -3080,4 +3402,6 @@
   Patient.Clear;
   Patient.DFN := tmpDFN;
+  uCore.TempEncounterLoc := 0;  //hds7591  Clinic/Ward movement.
+  uCore.TempEncounterLocName := ''; //hds7591  Clinic/Ward movement.
 
   if (FPrevInPatient and Patient.Inpatient) then                //transfering inside hospital
@@ -3092,4 +3416,6 @@
   begin
     Encounter.Inpatient := True;
+    uCore.TempEncounterLoc := Encounter.Location;  //hds7591  Clinic/Ward movement.
+    uCore.TempEncounterLocName := Encounter.LocationName; //hds7591  Clinic/Ward movement.
     Encounter.Location := Patient.Location;
     Encounter.DateTime := Patient.AdmitTime;
@@ -3101,6 +3427,5 @@
 end;
 
-procedure TfrmFrame.FormKeyDown(Sender: TObject; var Key: Word;
-  Shift: TShiftState);
+procedure TfrmFrame.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
 var
   NewTabIndex: integer;
@@ -3108,8 +3433,6 @@
   //CQ2844: Toggle Remote Data button using Alt+R
    case Key of
-     82,114:  begin
-              if (ssAlt in Shift) then
+     82,114:  if (ssAlt in Shift) then
                  frmFrame.pnlCIRNClick(Sender);
-              end;
      end;
 
@@ -3173,16 +3496,18 @@
 procedure TfrmFrame.pnlPatientClick(Sender: TObject);
 begin
-  mnuViewDemoClick(Self);
+  ViewInfo(mnuViewDemo);
 end;
 
 procedure TfrmFrame.pnlVisitClick(Sender: TObject);
 begin
-  if (not User.IsReportsOnly) then // Reports Only tab.
-    mnuFileEncounterClick(Self);
+ //if (not User.IsReportsOnly) then // Reports Only tab.
+ //  mnuFileEncounterClick(Self);
+  ViewInfo(mnuViewVisits);
 end;
 
 procedure TfrmFrame.pnlPrimaryCareClick(Sender: TObject);
 begin
-  ReportBox(DetailPrimaryCare(Patient.DFN), 'Primary Care', True);
+  //ReportBox(DetailPrimaryCare(Patient.DFN), 'Primary Care', True);
+  ViewInfo(mnuViewPrimaryCare);
 end;
 
@@ -3190,10 +3515,11 @@
 begin
   if(pnlReminders.tag = HAVE_REMINDERS) then
-    mnuViewRemindersClick(Self);
+      ViewInfo(mnuViewReminders);
+
 end;
 
 procedure TfrmFrame.pnlPostingsClick(Sender: TObject);
 begin
-  mnuViewPostingsClick(Self);
+  ViewInfo(mnuViewPostings);
 end;
 
@@ -3216,5 +3542,5 @@
 end;
 
-function TfrmFrame.AllowCCOWContextChange(NewDFN: string): boolean;
+function TfrmFrame.AllowCCOWContextChange(var CCOWResponse: UserResponse; NewDFN: string): boolean;
 var
   PtData : IContextItemCollection;
@@ -3248,4 +3574,8 @@
           IsProdAcct := User.IsProductionAccount;
 
+          {$IFDEF CCOWBROKER}
+          //IsProdAcct := RPCBrokerV.Login.IsProduction;  //not yet
+          {$ENDIF}
+
           PtDataItem2 := CoContextItem.Create();
           PtDataItem2.Set_Name('Patient.co.PatientName');                // Patient.Name
@@ -3293,4 +3623,5 @@
       end;
 
+    CCOWResponse := response;
     if (response = UrCommit) then
     begin
@@ -3355,4 +3686,7 @@
   Reason, HyperLinkReason: string;
   PtChanged: boolean;
+{$IFDEF CCOWBROKER}
+  UserChanged: boolean;
+{$ENDIF}
 begin
   // If the app would lose data, or have other problems changing context at
@@ -3370,4 +3704,7 @@
   try
     CheckForDifferentPatient(aContextItemCollection, PtChanged);
+{$IFDEF CCOWBROKER}
+    CheckForDifferentUser(aContextItemCollection, UserChanged);
+{$ENDIF}
   except
     on E: Exception do HandleCCOWError(E.Message);
@@ -3379,5 +3716,9 @@
   end;
 
+{$IFDEF CCOWBROKER}
+  if PtChanged or UserChanged then
+{$ELSE}
   if PtChanged then
+{$ENDIF}
     begin
       FCCOWContextChanging := True;
@@ -3404,5 +3745,16 @@
   FCCOWError := False;
   try
-  CheckForDifferentPatient(ctxContextor.CurrentContext, PtChanged);
+  {$IFDEF CCOWBROKER}
+    with RPCBrokerV do if (WasUserDefined and IsUserCleared and (ctxContextor.CurrentContext.Present(CCOW_USER_NAME) = nil)) then    // RV 05/11/04
+    begin
+      Reason := 'COMMIT';
+      if AllowContextChangeAll(Reason) then
+      begin
+        Close;
+        Exit;
+      end;
+    end;
+  {$ENDIF}
+    CheckForDifferentPatient(ctxContextor.CurrentContext, PtChanged);
   except
     on E: Exception do HandleCCOWError(E.Message);
@@ -3457,4 +3809,7 @@
   end;
   IsProdAcct := User.IsProductionAccount;
+  {$IFDEF CCOWBROKER}
+  //IsProdAcct := RPCBrokerV.Login.IsProduction;  //not yet
+  {$ENDIF}
   // Get an item collection of the current context
   FCCOWError := False;
@@ -3495,5 +3850,5 @@
   //PtDFN := FindBestCCOWDFN(PtName);
   PtDFN := FindBestCCOWDFN;
-  if PtDFN <> '' then
+  if StrToInt64Def(PtDFN, 0) > 0 then
     begin
       // Select new patient based on context value
@@ -3517,5 +3872,6 @@
       end;
       if User.IsProvider then Encounter.Provider := User.DUZ;
-      SetupPatient;
+      if not FFirstLoad then SetupPatient;
+      frmCover.UpdateVAAButton; //VAA
       DetermineNextTab;
       tabPage.TabIndex := PageIDToTab(NextTab);
@@ -3571,5 +3927,10 @@
 
 procedure TfrmFrame.mnuFileResumeContextSetClick(Sender: TObject);
-begin
+var
+  CCOWResponse: UserResponse;
+  Reason: string;
+begin
+  Reason := '';
+  if not AllowContextChangeAll(Reason) then exit;
   FCCOWIconName := 'BMP_CCOW_CHANGING';
   pnlCCOW.Hint := TX_CCOW_CHANGING;
@@ -3582,5 +3943,5 @@
   end;
   if FCCOWError then exit;
-  if (AllowCCOWContextChange(Patient.DFN)) then
+  if (AllowCCOWContextChange(CCOWResponse, Patient.DFN)) then
     begin
       mnuFileResumeContext.Enabled := False;
@@ -3598,4 +3959,9 @@
       pnlCCOW.Hint := TX_CCOW_BROKEN;
       imgCCOW.Picture.BitMap.LoadFromResourceName(hInstance, FCCOWIconName);
+      try
+        if ctxContextor.State in [csParticipating] then ctxContextor.Suspend;
+      except
+        on E: Exception do HandleCCOWError(E.Message);
+      end;
    end;
   SetupPatient;
@@ -3607,5 +3973,5 @@
 var
   data : IContextItemCollection;
-  anItem : IContextItem;
+  anItem: IContextItem;
   PtDFN, PtName: string;
 begin
@@ -3623,4 +3989,19 @@
   PtChanged := not ((PtDFN = Patient.DFN) and (PtName = Piece(Patient.Name, ',', 1) + U + Piece(Patient.Name, ',', 2) + '^^^^'));
 end;
+
+{$IFDEF CCOWBROKER}
+procedure TfrmFrame.CheckForDifferentUser(aContextItemCollection: IDispatch; var UserChanged: boolean);
+var
+  data : IContextItemCollection;
+begin
+  if uCore.User = nil then
+  begin
+    UserChanged := False;
+    Exit;
+  end;
+  data := IContextItemCollection(aContextItemCollection) ;
+  UserChanged := RPCBrokerV.IsUserContextPending(data);
+end;
+{$ENDIF}
 
 procedure TfrmFrame.CheckHyperlinkResponse(aContextItemCollection: IDispatch; var HyperlinkReason: string);
@@ -3651,4 +4032,5 @@
 procedure TfrmFrame.HideEverything;
 begin
+  FNoPatientSelected := TRUE;
   pnlNoPatientSelected.Visible := True;
   pnlNoPatientSelected.BringToFront;
@@ -3671,4 +4053,5 @@
 procedure TfrmFrame.ShowEverything;
 begin
+  FNoPatientSelected := FALSE;
   pnlNoPatientSelected.Visible := False;
   pnlNoPatientSelected.SendToBack;
@@ -3711,5 +4094,6 @@
 procedure TfrmFrame.pnlFlagClick(Sender: TObject);
 begin
-  ShowFlags;
+  //ShowFlags;
+  ViewInfo(mnuViewFlags);
 end;
 
@@ -3812,20 +4196,224 @@
 
 procedure TfrmFrame.ScreenActiveFormChange(Sender: TObject);
+begin
+  if(assigned(FOldActiveFormChange)) then
+    FOldActiveFormChange(Sender);
+  //Focus the Form that Stays on Top after the Application Regains focus.
+  if FJustEnteredApp then
+    FocusApplicationTopForm;
+  FJustEnteredApp := false;
+end;
+
+procedure TfrmFrame.FocusApplicationTopForm;
 var
   I : integer;
 begin
-  //Locate the Form that Stays on Top after the Application Regains focus.
-  if FJustEnteredApp then
-  begin
-    for I := (Screen.FormCount-1) downto 0 do //Set the last one opened last
-    begin
-      with Screen.Forms[I] do
-        if (FormStyle = fsStayOnTop) and (Enabled) and (Visible) then
-          SetFocus;
+  for I := (Screen.FormCount-1) downto 0 do //Set the last one opened last
+  begin
+    with Screen.Forms[I] do
+      if (FormStyle = fsStayOnTop) and (Enabled) and (Visible) then
+        SetFocus;
+  end;
+end;
+
+procedure TfrmFrame.AppEventsShortCut(var Msg: TWMKey;
+  var Handled: Boolean);
+begin
+  if ((Boolean(Hi(GetKeyState(VK_MENU{ALT})))) and (Msg.CharCode = VK_F1)) then
+  begin
+    FocusApplicationTopForm;
+    Handled := True;
+  end;
+end;
+
+procedure TfrmFrame.mnuToolsGraphingClick(Sender: TObject);
+begin
+  Screen.Cursor := crHourGlass;
+  if GraphFloat = nil then              // new graph
+  begin
+    GraphFloat := TfrmGraphs.Create(self);
+    try
+      with GraphFloat do
+      begin
+        if btnClose.Tag = 1 then
+          Exit;
+        Initialize;
+        Caption := 'CPRS Graphing - Patient: ' + MixedCase(Patient.Name);
+        BorderIcons := [biSystemMenu, biMaximize, biMinimize];
+        BorderStyle := bsSizeable;
+        BorderWidth := 1;
+        // context sensitive       type (tabPage.TabIndex)  & [item]
+        ResizeAnchoredFormToFont(GraphFloat);
+        Show;
+      end;
+    finally
+      if GraphFloat.btnClose.Tag = 1 then
+      begin
+        GraphFloatActive := false;
+        GraphFloat.Free;
+        GraphFloat := nil;
+      end
+      else
+        GraphFloatActive := true;
     end;
-    FJustEnteredApp := false;
-  end;
-end;
-
+  end
+  else if GraphFloat.btnClose.Tag = 1 then
+    Exit
+  else if GraphFloatActive and (GraphFloat.lstTypes.Hint = Patient.DFN) then
+    GraphFloat.BringToFront             // graph is active, same patient
+  else if GraphFloat.lstTypes.Hint = Patient.DFN then
+  begin                                 // graph is not active, same patient
+    // context sensitive
+    GraphFloat.Show;
+    GraphFloatActive := true;
+  end
+  else
+  //with GraphFloat do                    // new patient
+  begin
+    GraphFloat.InitialRetain;
+    GraphFloatActive := false;
+    GraphFloat.Free;
+    GraphFloat := nil;
+    mnuToolsGraphingClick(self);          // delete and recurse
+    {//FormCreate(self);   //****************
+    Initialize;
+    DisplayData('top');
+    DisplayData('bottom');
+    lstCheck.Items.Clear;
+    Caption := 'CPRS Graphing - Patient: ' + MixedCase(Patient.Name);
+    // context sensitive
+    Show;
+    GraphFloatActive := true;}
+  end;
+  Screen.Cursor := crDefault;
+end;
+
+procedure TfrmFrame.pnlCIRNMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
+begin
+  pnlCIRN.BevelOuter := bvLowered;
+end;
+
+procedure TfrmFrame.pnlCIRNMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
+begin
+  pnlCIRN.BevelOuter := bvRaised;
+end;
+
+procedure TfrmFrame.laMHVClick(Sender: TObject);
+begin
+  //if laMHV.Caption = 'MHV' then
+  //  ShellExecute(Handle, 'open', PChar('http://www.myhealth.va.gov/'), '', '', SW_NORMAL);
+  ViewInfo(mnuViewMyHealtheVet);
+end;
+
+procedure TfrmFrame.laVAA2Click(Sender: TObject);
+{var
+  InsuranceSubscriberName: string;
+  ReportString: TStringList; //CQ7782 }
+begin
+  {if fCover.VAAFlag[0] <> '0' then //'0' means subscriber not found
+     begin
+     InsuranceSubscriberName := fCover.VAAFlag[12];
+     //CQ7782
+     //ReportString := TStringList.Create;
+     ReportString := VAAFlag;
+     ReportString[0] := '';
+     ReportBox(ReportString, InsuranceSubscriberName, True);
+     //end CQ7782
+     end;}
+  ViewInfo(mnuInsurance);
+end;
+
+procedure TfrmFrame.ViewInfo(Sender: TObject);
+var
+  SelectNew: Boolean;
+  InsuranceSubscriberName: string;
+  ReportString: TStringList;
+  aAddress: string;
+begin
+  case (Sender as TMenuItem).Tag of
+    1:begin { displays patient inquiry report (which optionally allows new patient to be selected) }
+        StatusText(TX_PTINQ);
+        PatientInquiry(SelectNew);
+        if Assigned(FLastPage) then
+          FLastPage.FocusFirstControl;
+        StatusText('');
+        if SelectNew then mnuFileOpenClick(mnuViewDemo);
+      end;
+    2:begin
+        if (not User.IsReportsOnly) then // Reports Only tab.
+          mnuFileEncounterClick(Self);
+      end;
+    3:begin
+        ReportBox(DetailPrimaryCare(Patient.DFN), 'Primary Care', True);
+      end;
+    4:begin
+        if laMHV.Caption = 'MHV' then
+          ShellExecute(laMHV.Handle, 'open', PChar('http://www.myhealth.va.gov/'), '', '', SW_NORMAL);
+      end;
+    5:begin
+        if fCover.VAAFlag[0] <> '0' then //'0' means subscriber not found
+        begin
+          InsuranceSubscriberName := fCover.VAAFlag[12];
+          ReportString := VAAFlag;
+          ReportString[0] := '';
+          ReportBox(ReportString, InsuranceSubscriberName, True);
+        end;
+      end;
+    6:begin
+        ShowFlags;
+      end;
+    7:begin
+        if UseVistaWeb then
+          begin
+            pnlCIRN.BevelOuter := bvRaised;
+            pnlCIRN.Hint := 'Click to open VistaWeb';
+            lblCIRN.Width := 43;
+            lblCIRNData.Width := 43;
+            lblCIRNData.Alignment := taCenter;
+            lblCIRN.Alignment := taCenter;
+            lstCIRNLocations.Visible := false;
+            lstCIRNLocations.SendToBack;
+            aAddress := GetVistaWebAddress(Patient.DFN);
+            ShellExecute(pnlCirn.Handle, 'open', PChar(aAddress), PChar(''), '', SW_NORMAL);
+            Exit;
+          end;
+        if not RemoteSites.RemoteDataExists then Exit;
+        if (not lstCIRNLocations.Visible) then
+          begin
+            pnlCIRN.BevelOuter := bvLowered;
+            lstCIRNLocations.Visible := True;
+            lstCIRNLocations.BringToFront;
+            lstCIRNLocations.SetFocus;
+            pnlCIRN.Hint := 'Click to close list.';
+          end
+        else
+          begin
+            pnlCIRN.BevelOuter := bvRaised;
+            lstCIRNLocations.Visible := False;
+            lstCIRNLocations.SendToBack;
+            pnlCIRN.Hint := 'Click to display other facilities having data for this patient.';
+          end;
+      end;
+    8:begin
+        ViewReminderTree;
+      end;
+    9:begin { displays the window that shows crisis notes, warnings, allergies, & advance directives }
+        ShowCWAD;
+      end;
+  end;
+end;
+
+procedure TfrmFrame.mnuViewInformationClick(Sender: TObject);
+begin
+  mnuViewDemo.Enabled := frmFrame.pnlPatient.Enabled;
+  mnuViewVisits.Enabled := frmFrame.pnlVisit.Enabled;
+  mnuViewPrimaryCare.Enabled := frmFrame.pnlPrimaryCare.Enabled;
+  mnuViewMyHealtheVet.Enabled := not (Copy(frmFrame.laMHV.Hint, 1, 2) = 'No');
+  mnuInsurance.Enabled := not (Copy(frmFrame.laVAA2.Hint, 1, 2) = 'No');
+  mnuViewFlags.Enabled := frmFrame.lblFlag.Enabled;
+  mnuViewRemoteData.Enabled := frmFrame.lblCirn.Enabled;
+  mnuViewReminders.Enabled := frmFrame.pnlReminders.Enabled;
+  mnuViewPostings.Enabled := frmFrame.pnlPostings.Enabled;
+end;
 
 initialization
@@ -3833,4 +4421,6 @@
 finalization
 
+
 end.
 
+
