Index: cprs/branches/foia-cprs/CPRS-Chart/fNotes.pas
===================================================================
--- cprs/branches/foia-cprs/CPRS-Chart/fNotes.pas	(revision 459)
+++ cprs/branches/foia-cprs/CPRS-Chart/fNotes.pas	(revision 460)
@@ -1,13 +1,4 @@
 unit fNotes;
 {$O-}
-{
-NOTES:
-    TAB ORDER:
-      Becuase of the Dynamic ways of this form, the tab order must be
-      coded into the onExit events of the controls. If a new control is
-      added that needs access with tab & shift tab you must add the
-      code. Right clicking on the form and selecting tab order will not
-      be sufficient. See TfrmNotes.memPCEShowExit procedure for example.
-}
 
 interface
@@ -16,5 +7,6 @@
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   fHSplit, StdCtrls, ExtCtrls, Menus, ComCtrls, ORCtrls, ORFn, uConst, ORDtTm,
-  uPCE, ORClasses, fDrawers, ImgList, rTIU, uTIU, uDocTree, fRptBox, fPrintList;
+  uPCE, ORClasses, fDrawers, ImgList, rTIU, uTIU, uDocTree, fRptBox, fPrintList,
+  fNoteST, ORNet, fNoteSTStop;
 
 type
@@ -116,4 +108,5 @@
     N3: TMenuItem;
     mnuEditDialgFields: TMenuItem;
+    tvNotes: TORTreeView;
     lvNotes: TCaptionListView;
     sptList: TSplitter;
@@ -143,5 +136,16 @@
     popNoteMemoInsTemplate: TMenuItem;
     popNoteMemoEncounter: TMenuItem;
-    tvNotes: TORTreeView;
+    mnuSearchForText: TMenuItem;
+    popSearchForText: TMenuItem;
+    mnuViewInformation: TMenuItem;
+    mnuViewDemo: TMenuItem;
+    mnuViewVisits: TMenuItem;
+    mnuViewPrimaryCare: TMenuItem;
+    mnuViewMyHealtheVet: TMenuItem;
+    mnuInsurance: TMenuItem;
+    mnuViewFlags: TMenuItem;
+    mnuViewReminders: TMenuItem;
+    mnuViewRemoteData: TMenuItem;
+    mnuViewPostings: TMenuItem;
     procedure mnuChartTabClick(Sender: TObject);
     procedure lstNotesClick(Sender: TObject);
@@ -214,6 +218,5 @@
     procedure dlgReplaceTextFind(Sender: TObject);
     procedure mnuActAttachtoIDParentClick(Sender: TObject);
-    procedure memNewNoteKeyDown(Sender: TObject; var Key: Word;
-      Shift: TShiftState);
+    procedure memNewNoteKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
     procedure sptHorzCanResize(Sender: TObject; var NewSize: Integer; var Accept: Boolean);
     procedure popNoteMemoInsTemplateClick(Sender: TObject);
@@ -222,7 +225,10 @@
     procedure tvNotesDeletion(Sender: TObject; Node: TTreeNode);
     procedure tvNotesExit(Sender: TObject);
+    procedure pnlReadExit(Sender: TObject);
     procedure cmdNewNoteExit(Sender: TObject);
     procedure FormHide(Sender: TObject);
     procedure FormShow(Sender: TObject);
+    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
+      Y: Integer);
     procedure memNewNoteKeyPress(Sender: TObject; var Key: Char);
     procedure memNewNoteKeyUp(Sender: TObject; var Key: Word;
@@ -231,6 +237,6 @@
     procedure cmdChangeExit(Sender: TObject);
     procedure cmdPCEExit(Sender: TObject);
-    procedure memNoteExit(Sender: TObject);
-    procedure lvNotesExit(Sender: TObject);
+    procedure ViewInfo(Sender: TObject);
+    procedure mnuViewInformationClick(Sender: TObject);
   private
     FNavigatingTab : Boolean; //Currently Using tab to navigate
@@ -255,4 +261,5 @@
     FOldDrawerPnlEncounterButtonExit: TNotifyEvent;
     FOldDrawerEdtSearchExit: TNotifyEvent;
+    FMousing: TDateTime;
     FStarting: boolean;
     procedure frmFramePnlPatientExit(Sender: TObject);
@@ -261,5 +268,5 @@
     procedure frmDrawerEdtSearchExit(Sender: TObject);
     procedure ClearEditControls;
-    procedure DoAutoSave;
+    procedure DoAutoSave(Suppress: integer = 1);
     function GetTitleText(AnIndex: Integer): string;
     procedure InsertAddendum;
@@ -310,4 +317,5 @@
 var
   frmNotes: TfrmNotes;
+  SearchTextStopFlag: Boolean;   // Text Search CQ: HDS00002856
 
 implementation
@@ -389,5 +397,11 @@
                       ' but will remain indefinitely discoverable to HIMS.' +CRLF +CRLF;
   TX_AUTH_SIGNED    = 'Author has not signed, are you SURE you want to sign.' +CRLF;
-
+{
+type
+  //CQ8300
+  ClipboardData = record
+     Text: array[0..255] of char;
+  end;
+}
 var
   uPCEShow, uPCEEdit:  TPCEData;
@@ -399,4 +413,5 @@
   uChanging: Boolean;
   uIDNotesActive: Boolean;
+
 
 { TPage common methods --------------------------------------------------------------------- }
@@ -631,8 +646,10 @@
     {LastCosigner & LastCosignerName aren't cleared because they're used as default for next note.}
     Lines        := nil;
+    PRF_IEN := 0;
+    ActionIEN := '';
   end;
   // clear the editing controls (also clear the new labels?)
   txtSubject.Text := '';
-  memNewNote.Clear;
+  if memNewNote <> nil then memNewNote.Clear; //CQ7012 Added test for nil
   timAutoSave.Enabled := False;
   // clear the PCE object for editing
@@ -754,4 +771,5 @@
     if IsConsultTitle(Title) and (PkgIEN = 0) then Result := True;
     if IsSurgeryTitle(Title) and (PkgIEN = 0) then Result := True;
+    if IsPRFTitle(Title) and (PRF_IEN = 0) and (not DocType = TYP_ADDENDUM) then Result := True;
     if (DocType = TYP_ADDENDUM) then
     begin
@@ -760,5 +778,5 @@
     begin
       if Title > 0 then CurTitle := Title else CurTitle := DocType;
-      if AskCosignerForTitle(CurTitle, Author) and (Cosigner <= 0) then Result := True;
+      if AskCosignerForTitle(CurTitle, Author, DateTime) and (Cosigner <= 0) then Result := True;
     end;
   end;
@@ -897,4 +915,6 @@
   x, WhyNot, DocInfo: string;
 begin
+  if frmFrame.Timedout then Exit;
+
   FNewIDChild := IsIDChild;
   EnableAutosave := FALSE;
@@ -932,5 +952,6 @@
     end;
     // check to see if interaction necessary to get required fields
-    if LacksRequiredForCreate or VerifyNoteTitle or UnresolvedConsultsExist
+    GetUnresolvedConsultsInfo;
+    if LacksRequiredForCreate or VerifyNoteTitle or uUnresolvedConsults.UnresolvedConsultsExist
       then HaveRequired := ExecuteNoteProperties(FEditNote, CT_NOTES, IsIDChild, FNewIDChild, '', 0)
       else HaveRequired := True;
@@ -953,9 +974,13 @@
         //7348^Note Title^3000913^NERD, YOURA  (N0165)^1329;Rich Vertigan;VERTIGAN,RICH^8E REHAB MED^complete^Adm: 11/05/98;2981105.095547^        ;^^0^^^2
         with FEditNote do
-          begin
-            x := IntToStr(CreatedNote.IEN) + U + TitleName + U + FloatToStr(FEditNote.DateTime) + U +
-                 Patient.Name + U + IntToStr(Author) + ';' + AuthorName + U + LocationName + U + 'new' + U +
-                 U + U + U + U + U + U + U;
-          end;
+        begin
+          x := IntToStr(CreatedNote.IEN) + U + TitleName + U + FloatToStr(FEditNote.DateTime) + U +
+               Patient.Name + U + IntToStr(Author) + ';' + AuthorName + U + LocationName + U + 'new' + U +
+               U + U + U + U + U + U + U;
+          //Link Note to PRF Action
+          if PRF_IEN <> 0 then
+            if sCallV('TIU LINK TO FLAG', [CreatedNote.IEN,PRF_IEN,ActionIEN,Patient.DFN]) = '0' then
+              ShowMessage('TIU LINK TO FLAG: FAILED');
+        end;
 
         lstNotes.Items.Insert(0, x);
@@ -995,5 +1020,5 @@
         FChanged := False;
         cmdChangeClick(Self); // will set captions, sign state for Changes
-        lstNotesClick(Self);  // will makepnlWrite visible
+        lstNotesClick(Self);  // will make pnlWrite visible
         if timAutoSave.Interval <> 0 then EnableAutosave := TRUE;
         if txtSubject.Visible then txtSubject.SetFocus else memNewNote.SetFocus;
@@ -1065,5 +1090,7 @@
     else HaveRequired := True;
   // lock the consult request if there is a consult
-  with FEditNote do if (PkgIEN > 0) and (PkgPtr = PKG_CONSULTS) then HaveRequired := LockConsultRequest(PkgIEN);
+  if HaveRequired then
+    with FEditNote do
+      if (PkgIEN > 0) and (PkgPtr = PKG_CONSULTS) then HaveRequired := LockConsultRequest(PkgIEN);
   if HaveRequired then
   begin
@@ -1285,6 +1312,11 @@
   inherited;
   LimitEditWidth(memNewNote, MAX_PROGRESSNOTE_WIDTH - 1);
-  memNewNote.Constraints.MinWidth := TextWidthByFont(memNewNote.Font.Handle, StringOfChar('X', MAX_PROGRESSNOTE_WIDTH)) + (LEFT_MARGIN * 2) + ScrollBarWidth;
-  pnlLeft.Width := self.ClientWidth - pnlWrite.Width - sptHorz.Width;
+
+  //CQ7012 Added test for nil
+  if memNewNote <> nil then
+     memNewNote.Constraints.MinWidth := TextWidthByFont(memNewNote.Font.Handle, StringOfChar('X', MAX_PROGRESSNOTE_WIDTH)) + (LEFT_MARGIN * 2) + ScrollBarWidth;
+  //CQ7012 Added test for nil
+   if (Self <> nil) and (pnlLeft <> nil) and (pnlWrite <> nil) and (sptHorz <> nil) then
+     pnlLeft.Width := self.ClientWidth - pnlWrite.Width - sptHorz.Width;
 end;
 
@@ -1395,5 +1427,6 @@
     // no other note being edited, so just proceed as before.
     UpdateEncounterInfo;
-  cmdPCE.Enabled := TRUE;
+  if cmdPCE <> nil then
+    cmdPCE.Enabled := TRUE
 end;
 
@@ -1466,4 +1499,5 @@
   OKPressed, IsIDChild: Boolean;
   x: string;
+  DisAssoText : String;
 begin
   inherited;
@@ -1471,4 +1505,14 @@
   LastTitle   := FEditNote.Title;
   FEditNote.IsNewNote := False;
+  DisAssoText := '';
+  if (FEditNote.PkgPtr = PKG_CONSULTS) then
+    DisAssoText := 'Consults';
+  if (FEditNote.PkgPtr = PKG_PRF) then
+    DisAssoText := 'Patient Record Flags';
+  if (DisAssoText <> '') and (Sender <> Self) then
+    if InfoBox('If this title is changed, Any '+DisAssoText+' will be disassociated'+
+               ' with this note',
+               'Disassociate '+DisAssoText+'?',MB_OKCANCEL) = IDCANCEL	 then
+      exit;
   if FEditNote.PkgPtr = PKG_CONSULTS then LastConsult := FEditNote.PkgIEN else LastConsult := 0;;
   if Sender <> Self then OKPressed := ExecuteNoteProperties(FEditNote, CT_NOTES, IsIDChild, FNewIDChild, '', 0)
@@ -1500,5 +1544,7 @@
   lstNotes.Items[EditingIndex] := x;
   Changes.ReplaceText(CH_DOC, lstNotes.ItemID, GetTitleText(EditingIndex));
-  with FEditNote do if (PkgPtr = PKG_CONSULTS) and (LastConsult <> PkgIEN) then
+  with FEditNote do
+  begin
+  if (PkgPtr = PKG_CONSULTS) and (LastConsult <> PkgIEN) then
   begin
     // try to lock the new consult, reset to previous if unable
@@ -1514,4 +1560,10 @@
     end;
   end;
+  //Link Note to PRF Action
+  if PRF_IEN <> 0 then
+    if sCallV('TIU LINK TO FLAG', [lstNotes.ItemIEN,PRF_IEN,ActionIEN,Patient.DFN]) = '0' then
+      ShowMessage('TIU LINK TO FLAG: FAILED');
+  end;
+
   if LastTitle <> FEditNote.Title then mnuActLoadBoilerClick(Self);
 end;
@@ -1532,5 +1584,5 @@
 end;
 
-procedure TfrmNotes.DoAutoSave;
+procedure TfrmNotes.DoAutoSave(Suppress: integer = 1);
 var
   ErrMsg: string;
@@ -1542,5 +1594,5 @@
     timAutoSave.Enabled := False;
     try
-      SetText(ErrMsg, memNewNote.Lines, lstNotes.GetIEN(EditingIndex),1);
+      SetText(ErrMsg, memNewNote.Lines, lstNotes.GetIEN(EditingIndex), Suppress);
     finally
       timAutoSave.Enabled := True;
@@ -1566,4 +1618,5 @@
 var
   AuthCtxt: TAuthorContext;
+  SearchCtxt: TSearchContext; // Text Search CQ: HDS00002856
   DateRange: TNoteDateRange;
   Saved: Boolean;
@@ -1602,4 +1655,20 @@
                    LoadNotes;
                  end;
+  // Text Search CQ: HDS00002856 --------------------
+  NC_SEARCHTEXT: begin;
+                   SearchTextStopFlag := False;
+                   SelectSearchText(Font.Size, FCurrentContext.SearchString, SearchCtxt );
+                   with SearchCtxt do if Changed then
+                   begin
+                     //FCurrentContext.Status := IntToStr(ViewContext);
+                     frmSearchStop.Show;
+                     lblNotes.Caption := 'Search: '+ SearchString;
+                     frmSearchStop.lblSearchStatus.Caption := lblNotes.Caption;
+                     FCurrentContext.SearchString := SearchString;
+                     LoadNotes;
+                   end;
+                   // Only do LoadNotes if something changed 
+                 end;
+  // Text Search CQ: HDS00002856 --------------------
   NC_UNCOSIGNED: begin
                    FillChar(FCurrentContext, SizeOf(FCurrentContext), 0);
@@ -1667,4 +1736,12 @@
   end; {case}
   lblNotes.Caption := SetNoteTreeLabel(FCurrentContext);
+  // Text Search CQ: HDS00002856 --------------------
+  If FCurrentContext.SearchString <> '' then
+    lblNotes.Caption := lblNotes.Caption+', containing "'+FCurrentContext.SearchString+'"';
+  If SearchTextStopFlag=True then begin;
+    lblNotes.Caption := 'Search for "'+FCurrentContext.SearchString+'" was stopped!';
+  end;
+  frmSearchStop.Hide;
+  // Text Search CQ: HDS00002856 --------------------
   lblNotes.hint := lblNotes.Caption;
   tvNotes.Caption := lblNotes.Caption;
@@ -2128,11 +2205,14 @@
 procedure TfrmNotes.SaveSignItem(const ItemID, ESCode: string);
 { saves and optionally signs a progress note or addendum }
+const
+  SIG_COSIGN = 'COSIGNATURE';
+  SIG_SIGN   = 'SIGNATURE';
 var
   AnIndex, IEN, i: Integer;
   Saved, ContinueSign: Boolean;  {*RAB* 8/26/99}
-  SignSts: TActionRec;
+  ActionSts, SignSts: TActionRec;
   APCEObject: TPCEData;
   OK: boolean;
-
+  ActionType, SignTitle: string;
 begin
   AnIndex := -1;
@@ -2159,5 +2239,20 @@
   if Length(ESCode) > 0 then
   begin
-    if not NoteHasText(IEN) then
+    if CosignDocument(IEN) then
+    begin
+      SignTitle := TX_COSIGN;
+      ActionType := SIG_COSIGN;
+    end else
+    begin
+      SignTitle := TX_SIGN;
+      ActionType := SIG_SIGN;
+    end;
+    ActOnDocument(ActionSts, IEN, ActionType);
+    if not ActionSts.Success then
+      begin
+        InfoBox(ActionSts.Reason, TX_IN_AUTH, MB_OK);
+        ContinueSign := False;
+      end
+    else if not NoteHasText(IEN) then
       begin
         InfoBox(TX_EMPTY_NOTE1, TC_EMPTY_NOTE, MB_OK or MB_ICONERROR);
@@ -2276,4 +2371,7 @@
 
 procedure TfrmNotes.popNoteMemoFindClick(Sender: TObject);
+//var
+  //hData: THandle;  //CQ8300
+  //pData: ^ClipboardData; //CQ8300
 begin
   inherited;
@@ -2284,4 +2382,14 @@
       FindText := '';
       Options := [frDown, frHideUpDown];
+{
+      //CQ8300
+      OpenClipboard(dlgFindText.Handle);
+      hData := GetClipboardData(CF_TEXT);
+      pData := GlobalLock(hData);
+      FindText := pData^.Text;
+      GlobalUnlock(hData);
+      CloseClipboard;
+      //end CQ8300
+}
       Execute;
     end;
@@ -2322,9 +2430,11 @@
 begin
   inherited;
-  DoAutoSave;
+  DoAutoSave(0);
   timAutoSave.Enabled := False;
   try
     SpellCheckForControl(memNewNote);
   finally
+    FChanged := True;
+    DoAutoSave(0);
     timAutoSave.Enabled := True;
   end;
@@ -2334,9 +2444,11 @@
 begin
   inherited;
-  DoAutoSave;
+  DoAutoSave(0);
   timAutoSave.Enabled := False;
   try
     GrammarCheckForControl(memNewNote);
   finally
+    FChanged := True;
+    DoAutoSave(0);
     timAutoSave.Enabled := True;
   end;
@@ -2399,4 +2511,5 @@
   SigAction: integer;
   SavedDocID: string;
+  ARefDate: TFMDateTime;
 begin
   inherited;
@@ -2432,5 +2545,6 @@
   if not LockConsultRequestAndNote(lstNotes.ItemIEN) then Exit;
   Exclusions := GetCurrentSigners(lstNotes.ItemIEN);
-  SelectAdditionalSigners(Font.Size, lstNotes.ItemIEN, SigAction, Exclusions, SignerList, CT_NOTES);
+  ARefDate := StrToFloat(Piece(lstNotes.Items[lstNotes.ItemIndex], U, 3));
+  SelectAdditionalSigners(Font.Size, lstNotes.ItemIEN, SigAction, Exclusions, SignerList, CT_NOTES, ARefDate);
   with SignerList do
     begin
@@ -2713,7 +2827,14 @@
 //===================  Added for sort/search enhancements ======================
 procedure TfrmNotes.LoadNotes;
+const
+  INVALID_ID = -1;
+  INFO_ID = 1;
 var
   tmpList: TStringList;
   ANode: TORTreeNode;
+  x,xx,noteId: integer;   // Text Search CQ: HDS00002856
+  Dest: TStrings;  // Text Search CQ: HDS00002856
+  KeepFlag: Boolean;  // Text Search CQ: HDS00002856
+  NoteCount, NoteMatches: integer;  // Text Search CQ: HDS00002856
 begin
   tmpList := TStringList.Create;
@@ -2759,4 +2880,50 @@
         ListNotesForTree(tmpList, StrToIntDef(Status, 0), FMBeginDate, FMEndDate, Author, MaxDocs, TreeAscending);
         CreateListItemsforDocumentTree(FDocList, tmpList, StrToIntDef(Status, 0), GroupBy, TreeAscending, CT_NOTES);
+
+        // Text Search CQ: HDS00002856 ---------------------------------------
+        if FCurrentContext.SearchString<>''  then   // Text Search CQ: HDS00002856
+          begin
+            NoteMatches := 0;
+            Dest:=TStringList.Create;
+            NoteCount:=FDocList.Count-1;
+            if FDocList.Count>0 then
+              for x := FDocList.Count-1 downto 1 do begin;  // Don't do 0, because it's informational
+                KeepFlag:=False;
+                lblNotes.Caption:='Scanning '+IntToStr(NoteCount-x+1)+' of '+IntToStr(NoteCount)+', '+IntToStr(NoteMatches);
+                If NoteMatches=1 then lblNotes.Caption:=lblNotes.Caption+' match' else
+                                      lblNotes.Caption:=lblNotes.Caption+' matches';
+                frmSearchStop.lblSearchStatus.Caption := lblNotes.Caption;
+                frmSearchStop.lblSearchStatus.Repaint;
+                lblNotes.Repaint;
+                // Free up some ticks so they can click the "Stop" button
+                application.processmessages;
+                application.processmessages;
+                application.processmessages;
+                If SearchTextStopFlag = False then begin
+                  noteId := StrToIntDef(Piece(FDocList.Strings[x],'^',1),-1);
+                  if (noteId = INVALID_ID) or (noteId = INFO_ID) then
+                    Continue;
+                  CallV('TIU GET RECORD TEXT', [Piece(FDocList.Strings[x],'^',1)]);
+                  Dest.Assign(RPCBrokerV.Results);
+                  If Dest.Count > 0 then
+                     for xx := 0 to Dest.Count-1 do
+                     begin
+                      //Dest.Strings[xx] := StringReplace(Dest.Strings[xx],'#13',' ',[rfReplaceAll, rfIgnoreCase]);
+                      if Pos(Uppercase(FCurrentContext.SearchString),Uppercase(Dest.Strings[xx]))>0 then
+                        keepflag:=true;
+                     end;
+                  If KeepFlag=False then begin;
+                    if FDocList.Count >= x then
+                      FDocList.Delete(x);
+                    if (tmpList.Count >= x) and (x > 0) then
+                      tmpList.Delete(x-1);
+                  end else
+                    Inc(NoteMatches);
+                end;
+              end;
+            Dest.Free;
+          end;
+        // Text Search CQ: HDS00002856 ---------------------------------------
+
         UpdateTreeView(FDocList, tvNotes);
       end;
@@ -3096,4 +3263,5 @@
 var
   WhyNot: string;
+  //Saved: boolean;
 begin
   if EditingIndex <> -1 then
@@ -3213,4 +3381,6 @@
 procedure TfrmNotes.ShowPCEButtons(Editing: boolean);
 begin
+  if frmFrame.Timedout then Exit;
+
   FEditingNotePCEObj := Editing;
   if Editing or AnytimeEncounters then
@@ -3424,5 +3594,5 @@
     SetPiece(x, U, 5, NewAuth + ';' + AuthNameCheck);
     lstNotes.Items[EditingIndex] := x;
-    if AskCosignerForTitle(Title, Author) then
+    if AskCosignerForTitle(Title, Author, DateTime) then
     begin
       InfoBox(UpperCase(AuthNameCheck) + TX_COSIGNER_REQD, TC_COSIGNER_REQD, MB_OK);
@@ -3495,5 +3665,5 @@
 procedure TfrmNotes.tvNotesExit(Sender: TObject);
 begin
-  if Boolean(Hi(GetKeyState(VK_TAB))) and (not Boolean(Hi(GetKeyState(VK_SHIFT))))then
+  if IncSecond(FMousing,1) < Now then
   begin
     if (Screen.ActiveControl = frmDrawers.pnlTemplatesButton) or
@@ -3503,4 +3673,22 @@
       FindNextControl( cmdPCE, True, True, False).SetFocus;
   end;
+  FMousing := 0;
+end;
+
+procedure TfrmNotes.pnlReadExit(Sender: TObject);
+begin
+  inherited;
+  if IncSecond(FMousing,1) < Now then
+  begin
+    if (Screen.ActiveControl = frmFrame.pnlPatient) then
+      FindNextControl( tvNotes, True, True, False).SetFocus
+    else
+    if (Screen.ActiveControl = frmDrawers.pnlTemplatesButton) or
+        (Screen.ActiveControl = frmDrawers.pnlEncounterButton) or
+        (Screen.ActiveControl = cmdNewNote) or
+        (Screen.ActiveControl = cmdPCE) then
+      FindNextControl( frmDrawers.pnlTemplatesButton, False, True, False).SetFocus;
+  end;
+  FMousing := 0;
 end;
 
@@ -3508,11 +3696,14 @@
 begin
   inherited;
-  if Boolean(Hi(GetKeyState(VK_TAB))) and
-     (not frmDrawers.pnlEncounterButton.CanFocus) and
-     (not Boolean(Hi(GetKeyState(VK_SHIFT)))) then
-    if (memPCEShow.CanFocus) then
-      memPCEShow.SetFocus
+  if IncSecond(FMousing,1) < Now then
+  begin
+    if (Screen.ActiveControl = lvNotes) or
+        (Screen.ActiveControl = memNote) then
+      frmFrame.pnlPatient.SetFocus
     else
-      frmFrame.pnlPatient.SetFocus;
+    if (Screen.ActiveControl = tvNotes) then
+      FindNextControl( frmFrame.pnlPatient, False, True, False).SetFocus;
+  end;
+  FMousing := 0;
 end;
 
@@ -3520,5 +3711,5 @@
 begin
   FOldFramePnlPatientExit(Sender);
-  if Boolean(Hi(GetKeyState(VK_TAB))) then
+  if IncSecond(FMousing,1) < Now then
   begin
     if (Screen.ActiveControl = lvNotes) or
@@ -3526,4 +3717,5 @@
       FindNextControl( lvNotes, False, True, False).SetFocus;
   end;
+  FMousing := 0;
 end;
 
@@ -3559,9 +3751,9 @@
 begin
   FOldDrawerPnlTemplatesButtonExit(Sender);
-  if Boolean(Hi(GetKeyState(VK_TAB))) and Boolean(Hi(GetKeyState(VK_SHIFT))) then
-    if memNewNote.CanFocus then
-      memNewNote.SetFocus
-    else if memNote.CanFocus then
-      memNote.SetFocus;
+  if Boolean(Hi(GetKeyState(VK_TAB))) and  (memNewNote.CanFocus) and
+     Boolean(Hi(GetKeyState(VK_SHIFT))) then
+    memNewNote.SetFocus
+  else
+    cmdNewNoteExit(Sender);
 end;
 
@@ -3570,4 +3762,11 @@
   FOldDrawerPnlEncounterButtonExit(Sender);
   cmdNewNoteExit(Sender);
+end;
+
+procedure TfrmNotes.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
+  Y: Integer);
+begin
+  inherited;
+  FMousing := Now;
 end;
 
@@ -3606,8 +3805,5 @@
   if Boolean(Hi(GetKeyState(VK_TAB))) then
     if Boolean(Hi(GetKeyState(VK_SHIFT))) then
-      if cmdPCE.CanFocus then
-        cmdPCE.SetFocus
-      else
-        cmdNewNote.SetFocus
+      cmdPCE.SetFocus
     else
       frmFrame.pnlPatient.SetFocus;
@@ -3629,30 +3825,26 @@
   if Boolean(Hi(GetKeyState(VK_TAB))) and
      Not Boolean(Hi(GetKeyState(VK_SHIFT))) then
-    memPCEShow.SetFocus;
-end;
-
-procedure TfrmNotes.memNoteExit(Sender: TObject);
-begin
-  inherited;
-  //Fix the Tab Order  Make Drawers Buttons Accessible
-  if Boolean(Hi(GetKeyState(VK_TAB))) then
-    if Boolean(Hi(GetKeyState(VK_SHIFT))) then
-    begin
-      if lvNotes.CanFocus then
-        lvNotes.SetFocus
-      else if tvNotes.CanFocus then
-        tvNotes.SetFocus;
-    end
-    else if frmDrawers.pnlTemplatesButton.CanFocus then
-      frmDrawers.pnlTemplatesButton.SetFocus
-
-end;
-
-procedure TfrmNotes.lvNotesExit(Sender: TObject);
-begin
-  inherited;
-  if Boolean(Hi(GetKeyState(VK_TAB))) and Boolean(Hi(GetKeyState(VK_SHIFT))) then
-    if tvNotes.CanFocus then
-      tvNotes.SetFocus
+        if memPCEShow.Visible then //CQ7120
+           memPCEShow.SetFocus; //CQ7120
+end;
+
+procedure TfrmNotes.ViewInfo(Sender: TObject);
+begin
+  inherited;
+  frmFrame.ViewInfo(Sender);
+end;
+
+procedure TfrmNotes.mnuViewInformationClick(Sender: TObject);
+begin
+  inherited;
+  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;
 
@@ -3662,6 +3854,6 @@
 
 finalization
-  uPCEEdit.Free;
-  uPCEShow.Free;
+  if (uPCEEdit <> nil) then uPCEEdit.Free; //CQ7012 Added test for nil
+  if (uPCEShow <> nil) then uPCEShow.Free; //CQ7012 Added test for nil
 
 end.
