Index: cprs/trunk/CPRS-Chart/fMeds.pas
===================================================================
--- cprs/trunk/CPRS-Chart/fMeds.pas	(revision 456)
+++ cprs/trunk/CPRS-Chart/fMeds.pas	(revision 830)
@@ -8,5 +8,5 @@
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   fPage, StdCtrls, Menus, ORCtrls, ORFn, ExtCtrls, ComCtrls, rOrders, uConst,
-  rMeds, ORNet;
+  rMeds, ORNet, fBase508Form, VA508AccessibilityManager;
 
 type
@@ -83,4 +83,10 @@
     mnuViewPostings: TMenuItem;
     mnuOptimizeFields: TMenuItem;
+    SortbyStatusthenLocation1: TMenuItem;
+    SortbyClinicOrderthenStatusthenStopDate1: TMenuItem;
+    SortbyDrugalphabeticallystatusactivestatusrecentexpired1: TMenuItem;
+    N3: TMenuItem;
+    pnlView: TPanel;
+    txtView: TVA508StaticText;
     procedure mnuChartTabClick(Sender: TObject);
     procedure FormCreate(Sender: TObject);
@@ -136,5 +142,5 @@
     procedure hdrMedsInMouseUp(Sender: TObject; Button: TMouseButton;
       Shift: TShiftState; X, Y: Integer);
-    //procedure ActivateDeactiveRenew(AListBox: TListBox);
+    procedure ActivateDeactiveRenew(AListBox: TListBox);
     procedure ViewInfo(Sender: TObject);
     procedure mnuViewInformationClick(Sender: TObject);
@@ -146,4 +152,9 @@
     procedure hdrMedsInSectionClick(HeaderControl: THeaderControl;
       Section: THeaderSection);
+    procedure SortbyStatusthenLocation1Click(Sender: TObject);
+    procedure SortbyClinicOrderthenStatusthenStopDate1Click(
+      Sender: TObject);
+    procedure SortbyDrugalphabeticallystatusactivestatusrecentexpired1Click(
+      Sender: TObject);
   private
     FIterating: Boolean;
@@ -159,4 +170,5 @@
     uNonVAOrdersOut:  TStringList;
     ChildODList: TStringList;
+    FSortView: integer;
     function ListSelected(const ErrMsg: string): TListBox;
     procedure ValidateSelected(AListBox: TListBox; const AnAction, WarningMsg, WarningTitle: string);
@@ -170,6 +182,10 @@
     function GetMedList(Control: TWinControl): TList;
     function GetPharmacyOrders(Control: TWinControl): TStringList;
-    function PatientStatusChanged: boolean;
+    //function PatientStatusChanged: boolean;
     procedure ClearChildODList;
+    procedure SetViewCaption(Caption : String);
+    procedure lstMedsInRightClickHandler(var Msg: TMessage; var Handled: Boolean);
+    procedure lstMedsNonVARightClickHandler(var Msg: TMessage; var Handled: Boolean);
+    procedure lstMedsOutRightClickHandler(var Msg: TMessage; var Handled: Boolean);
   public
     procedure RefreshMedLists;
@@ -184,4 +200,5 @@
     function GetTotalSectionsWidth(Sender: TObject) : integer;
     function CheckMedStatus(ActiveList: TListBox): boolean;
+    property SortView: integer read FSortView write FSortView;
   end;
 
@@ -223,5 +240,6 @@
 uses uCore, rCore, fFrame, fRptBox, uOrders, fODBase, fOrdersDC, fOrdersHold,
      fOrdersRenew, fOMNavA, fOrdersRefill, fMedCopy, fOrders, fODChild, rODBase, 
-     StrUtils, fActivateDeactivate;
+     StrUtils, fActivateDeactivate, VA2006Utils, VA508AccessibilityRouter,
+  VAUtils;
 
 {$R *.DFM}
@@ -532,7 +550,8 @@
 begin
   inherited;
+  FixHeaderControlDelphi2006Bug(hdrMedsIn);
+  FixHeaderControlDelphi2006Bug(hdrMedsOut);
+  FixHeaderControlDelphi2006Bug(hdrMedsNonVA);
   PageID := CT_MEDS;
-  lstMedsIn.Color := ReadOnlyColor;
-  lstMedsOut.Color := ReadOnlyColor;
   uMedListIn  := TList.Create;
   uMedListOut := TList.Create;
@@ -598,4 +617,7 @@
      end;
   //end CQ9622
+  AddMessageHandler(lstMedsIn, lstMedsInRightClickHandler);
+  AddMessageHandler(lstMedsNonVA, lstMedsNonVARightClickHandler);
+  AddMessageHandler(lstMedsOut, lstMedsOutRightClickHandler);
 end;
 
@@ -603,4 +625,7 @@
 begin
   inherited;
+  RemoveMessageHandler(lstMedsOut, lstMedsOutRightClickHandler);
+  RemoveMessageHandler(lstMedsNonVA, lstMedsNonVARightClickHandler);
+  RemoveMessageHandler(lstMedsIn, lstMedsInRightClickHandler);
   ClearChildODList;
   ClearMedList(uMedListIn);
@@ -641,5 +666,5 @@
       if AnID <> '' then
         begin
-          tmpList.Assign(DetailMedLM(AnID));
+          FastAssign(DetailMedLM(AnID), tmpList);
         end;
       AnOrder := Piece(Strings[i], U, 2);
@@ -649,5 +674,5 @@
           tmpList.Add(StringOfChar('=', 74));
           tmpList.Add('');
-          tmpList.AddStrings(MedAdminHistory(AnOrder));
+          FastAddStrings(MedAdminHistory(AnOrder), tmpList);
         end;
       if CheckOrderGroup(AnOrder)=1 then  // if it's UD group
@@ -665,5 +690,5 @@
       end;
       if tmpList.Count > 0 then ReportBox(tmpList, ATitle, True);
-      if frmFrame.CCOWDrivedChange then   
+      if (frmFrame.TimedOut) or (frmFrame.CCOWDrivedChange) then Exit; //code added to correct access violation on timeout
         Exit;
     end;
@@ -704,5 +729,5 @@
 procedure TfrmMeds.RefreshMedLists;
 var
-  i: Integer;
+  i,view: Integer;
   AMed: TMedListRec;
 begin
@@ -711,5 +736,32 @@
   lstMedsNonVA.Clear;
   StatusText('Retrieving active medications...');
-  LoadActiveMedLists(uMedListIn, uMedListOut, uMedListNonVA);
+  view := self.FSortView;
+  //AGP Fix for CQ 10410 added view arguement to control Meds Tab sort criteria
+  LoadActiveMedLists(uMedListIn, uMedListOut, uMedListNonVA, view);
+  if view <> self.FSortView then
+    begin
+      self.FSortView := view;
+      if view = 1 then
+        begin
+          self.SortbyStatusthenLocation1.Checked := True;
+          SetViewCaption(SortbyStatusthenLocation1.Caption);
+          self.SortbyClinicOrderthenStatusthenStopDate1.Checked := False;
+          self.SortbyDrugalphabeticallystatusactivestatusrecentexpired1.checked := false;
+        end
+      else if view = 2 then
+        begin
+          self.SortbyStatusthenLocation1.Checked := False;
+          self.SortbyClinicOrderthenStatusthenStopDate1.Checked := True;
+          SetViewCaption(SortbyClinicOrderthenStatusthenStopDate1.Caption);
+          self.SortbyDrugalphabeticallystatusactivestatusrecentexpired1.Checked := false;
+        end
+      else if view = 3 then
+        begin
+          self.SortbyStatusthenLocation1.Checked := False;
+          self.SortbyClinicOrderthenStatusthenStopDate1.Checked := false;
+          self.SortbyDrugalphabeticallystatusactivestatusrecentexpired1.Checked := true;
+          SetViewCaption(SortbyDrugalphabeticallystatusactivestatusrecentexpired1.Caption);
+        end
+    end;
   uPharmacyOrdersIn.Clear;
   uPharmacyOrdersOut.Clear;
@@ -735,5 +787,5 @@
     lstMedsOut.Items.AddObject(GetPlainText(lstMedsOut, i), AMed);
   end;
- 
+
         StatusText('');
 end;
@@ -769,10 +821,10 @@
   // replace pharmacy text with order text if this is a change
   if CharAt(AnAction, 1) = 'X' then result := Piece(AnAction, U, 3);
-  if AMed.IVFluid then Indent := Pos(#10 + 'in ', result) else Indent := Pos(#13, result);
+  if AMed.IVFluid then Indent := Pos(CRLF + 'in ', result) else Indent := Pos(#13, result);
   if Indent > 0 then
   begin
     if AMed.IVFluid then
     begin
-      Detail := Copy(result, Indent + 1, Length(result));
+      Detail := Copy(result, Indent + Length(CRLF), Length(result));
       result := Copy(result, 1, Indent - 1);
     end else
@@ -928,8 +980,8 @@
               Canvas.Font.Color := clHighlightText;
               //Canvas.FillRect(ARect);
-              Canvas.Font.Color := clWhite;
+              Canvas.Font.Color := Get508CompliantColor(clWhite);
            end;
-        if (ColorToRGB(clWindowText) = ColorToRGB(clBlack)) and (Canvas.Font.Color <> clWhite) then
-          Canvas.Font.Color := clBlue;
+        if (Canvas.Font.Color <> Get508CompliantColor(clWhite)) then
+          Canvas.Font.Color := Get508CompliantColor(clBlue);
         if (Length(Piece(AnAction,'^',4)) > 0) then
           AMed.Location := Piece(AnAction,'^',4);
@@ -981,5 +1033,5 @@
   inherited;
   
-  if PatientStatusChanged then exit;
+  //if PatientStatusChanged then exit;
   with lstMedsOut do for i := 0 to Items.Count -1 do
     Selected[i] := false;
@@ -988,4 +1040,14 @@
 end;
 
+procedure TfrmMeds.lstMedsInRightClickHandler(var Msg: TMessage;
+  var Handled: Boolean);
+begin
+  if Msg.Msg = WM_RBUTTONUP then
+  begin
+    lstMedsIn.RightClickSelect := (lstMedsIn.SelCount < 1);
+    lstMedsInClick(lstMedsIn);
+  end;
+end;
+
 procedure TfrmMeds.lstMedsOutClick(Sender: TObject);
 var
@@ -994,5 +1056,5 @@
   inherited;
  
-  if PatientStatusChanged then exit;
+  //if PatientStatusChanged then exit;
   with lstMedsIn do for i := 0 to Items.Count -1 do
     Selected[i] := false;
@@ -1001,4 +1063,14 @@
 end;
 
+procedure TfrmMeds.lstMedsOutRightClickHandler(var Msg: TMessage;
+  var Handled: Boolean);
+begin
+  if Msg.Msg = WM_RBUTTONUP then
+  begin
+    lstMedsOut.RightClickSelect := (lstMedsOut.SelCount < 1);
+    lstMedsOutClick(lstMedsOut);
+  end;
+end;
+
 procedure TfrmMeds.hdrMedsOutSectionResize(HeaderControl: THeaderControl; Section: THeaderSection);
 begin
@@ -1035,5 +1107,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;
+  //if PatientStatusChanged then exit;
   if lstMedsOut.SelCount > 0 then
   begin
@@ -1071,8 +1143,9 @@
     begin
       ValidateSelected(ActiveList, OA_DC, TX_NO_DC, TC_NO_DC);
-      //ActivateDeactiveRenew(ActiveList); AGP 26.53 TURN OFF UNTIL FINAL DECISION CAN BE MADE
+      ActivateDeactiveRenew(ActiveList); //AGP 26.53 TURN OFF UNTIL FINAL DECISION CAN BE MADE
       MakeSelectedList(ActiveList, SelectedList);
       if ExecuteDCOrders(SelectedList,DelEvt) then
       begin
+        if frmFrame.TimedOut = true then  exit;
         ResetSelectedForList(ActiveList);
         SynchListToOrders(ActiveList, SelectedList);
@@ -1108,4 +1181,5 @@
       if ExecuteHoldOrders(SelectedList) then
       begin
+        if frmFrame.TimedOut = true then  exit;
         AddSelectedToChanges(SelectedList);
         ResetSelectedForList(ActiveList);
@@ -1114,5 +1188,5 @@
     end;
   finally
-    ActiveList.SetFocus;
+    if frmFrame.TimedOut = false then ActiveList.SetFocus;
     FIterating := False;
     SelectedList.Free;
@@ -1152,4 +1226,5 @@
       if ExecuteRenewOrders(SelectedList) then
       begin
+        if frmFrame.TimedOut = true then  exit;
         AddSelectedToChanges(SelectedList);
         ResetSelectedForList(ActiveList);
@@ -1158,5 +1233,5 @@
    end;
   finally
-    ActiveList.SetFocus;
+    if frmFrame.TimedOut = false then ActiveList.SetFocus;
     FIterating := False;
     SelectedList.Free;
@@ -1193,4 +1268,5 @@
     if not ShowMsgOn(ChangeIFNList.Count = 0, TX_NOSEL, TC_NOSEL)
       then ChangeOrders(ChangeIFNList, DelayEvent);
+    if frmFrame.TimedOut = true then  exit;
     SynchListToOrders(ActiveList, SelectedList);    // rehighlights
     Activelist.SetFocus;
@@ -1225,5 +1301,5 @@
   if (Length(AuthErr)>0) then
   begin
-    ShowMessage(AuthErr);
+    ShowMsg(AuthErr);
     if not EncounterPresent then Exit;
   end;
@@ -1241,4 +1317,5 @@
   DoesDestEvtOccur := False;
   ActiveList := ListSelected(TX_NOSEL);
+  if not assigned(ActiveList) then exit;  
   if CheckMedStatus(ActiveList) = True then Exit;
   if ActiveList = nil then Exit;
@@ -1343,4 +1420,5 @@
       end;
     end;
+    if frmFrame.TimedOut = true then  exit;
     SynchListToOrders(ActiveList, SelectedList);    // rehighlights
     if IsTransferAction then
@@ -1352,5 +1430,5 @@
     frmOrders.PtEvtCompleted(TempEvent.PtEventIFN,TempEvent.EventName,True);
   finally
-    ActiveList.SetFocus;
+    if frmFrame.TimedOut = false then  ActiveList.SetFocus;
     FActionOnMedsTab := False;
     uAutoAC := False;
@@ -1411,4 +1489,5 @@
       if ExecuteRefillOrders(SelectedList) then
       begin
+        if frmFrame.TimedOut = true then  exit;
         ResetSelectedForList(ActiveList);
         SynchListToOrders(ActiveList, SelectedList);
@@ -1416,5 +1495,5 @@
     end;
   finally
-    ActiveList.SetFocus;
+    if frmFrame.TimedOut = false then ActiveList.SetFocus;
     FIterating := False;
     SelectedList.Free;
@@ -1539,5 +1618,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;
+  //if PatientStatusChanged then exit;
 end;
 
@@ -1545,5 +1624,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;  
+  //if PatientStatusChanged then exit;
 end;
 
@@ -1553,5 +1632,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;  
+  //if PatientStatusChanged then exit;
   with lstMedsIn do for i := 0 to Items.Count -1 do
      Selected[i] := false;
@@ -1570,4 +1649,14 @@
   inherited;
    if not FIterating then ResetSelectedForList(TListBox(Sender));
+end;
+
+procedure TfrmMeds.lstMedsNonVARightClickHandler(var Msg: TMessage;
+  var Handled: Boolean);
+begin
+  if Msg.Msg = WM_RBUTTONUP then
+  begin
+    lstMedsNonVA.RightClickSelect := (lstMedsNonVA.SelCount < 1);
+    lstMedsNonVAClick(lstMedsNonVA);
+  end;
 end;
 
@@ -1725,5 +1814,5 @@
 end;
 
-function TfrmMeds.PatientStatusChanged: boolean;
+{function TfrmMeds.PatientStatusChanged: boolean;
 const
 
@@ -1752,5 +1841,5 @@
     Result := True;
   end;
-end;
+end;}
 
 function TfrmMeds.GetTotalSectionsWidth(Sender: TObject) : integer;
@@ -1792,4 +1881,9 @@
      for i := 0 to hdrMedsNonVA.Sections.Count - 1 do
         OrigNonVASecWidths[i] := hdrMedsNonVA.Sections[i].Width;
+end;
+
+procedure TfrmMeds.SetViewCaption(Caption: String);
+begin
+  txtView.Caption := StringReplace(Caption,'&','',[rfReplaceAll]);
 end;
 
@@ -1946,5 +2040,5 @@
 end;
 
-(*procedure TfrmMeds.ActivateDeactiveRenew(AListBox: TListBox);
+procedure TfrmMeds.ActivateDeactiveRenew(AListBox: TListBox);
 var
   i: Integer;
@@ -1963,5 +2057,5 @@
       end;
     if tmpArr <> nil then frmActivateDeactive.fActivateDeactive(tmpArr, AListBox);
-end; *)
+end;
 
 procedure TfrmMeds.ViewInfo(Sender: TObject);
@@ -2052,4 +2146,42 @@
 end;
 
+procedure TfrmMeds.SortbyStatusthenLocation1Click(Sender: TObject);
+begin
+  inherited;
+  self.FSortView := 1;
+  self.SortbyStatusthenLocation1.Checked := True;
+  SetViewCaption(SortbyStatusthenLocation1.Caption);
+  self.SortbyClinicOrderthenStatusthenStopDate1.Checked := False;
+  self.SortbyDrugalphabeticallystatusactivestatusrecentexpired1.Checked := false;
+  self.RefreshMedLists;
+end;
+
+procedure TfrmMeds.SortbyClinicOrderthenStatusthenStopDate1Click(
+  Sender: TObject);
+begin
+  inherited;
+  self.FSortView := 2;
+  self.SortbyStatusthenLocation1.Checked := False;
+  self.SortbyClinicOrderthenStatusthenStopDate1.Checked := True;
+  SetViewCaption(SortbyClinicOrderthenStatusthenStopDate1.Caption);
+  self.SortbyDrugalphabeticallystatusactivestatusrecentexpired1.Checked := false;
+  self.RefreshMedLists;
+end;
+
+procedure TfrmMeds.SortbyDrugalphabeticallystatusactivestatusrecentexpired1Click(
+  Sender: TObject);
+begin
+  inherited;
+  self.FSortView := 3;
+  self.SortbyStatusthenLocation1.Checked := False;
+  self.SortbyClinicOrderthenStatusthenStopDate1.Checked := false;
+  self.SortbyDrugalphabeticallystatusactivestatusrecentexpired1.Checked := true;
+  SetViewCaption(SortbyDrugalphabeticallystatusactivestatusrecentexpired1.Caption);
+  self.RefreshMedLists;
+end;
+
+initialization
+  SpecifyFormIsNotADialog(TfrmMeds);
+
 end.
 
