Index: cprs/trunk/CPRS-Chart/Orders/fOrders.pas
===================================================================
--- cprs/trunk/CPRS-Chart/Orders/fOrders.pas	(revision 456)
+++ cprs/trunk/CPRS-Chart/Orders/fOrders.pas	(revision 829)
@@ -8,5 +8,5 @@
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, fHSplit, StdCtrls,
   ExtCtrls, Menus, ORCtrls, ComCtrls, ORFn, rOrders, fODBase, uConst, uCore, uOrders,UBACore,
-  UBAGlobals;
+  UBAGlobals, VA508AccessibilityManager, fBase508Form;
 
 type
@@ -165,4 +165,5 @@
   private
     { Private declarations }
+    OrderListClickProcessing : Boolean;
     FDfltSort: Integer;
     FCurrentView: TOrderView;
@@ -184,4 +185,5 @@
     FDontCheck: boolean;
     FParentComplexOrderID: string;
+    FHighContrast2Mode: boolean;
     function CanChangeOrderView: Boolean;
     function GetEvtIFN(AnIndex: integer): string;
@@ -205,5 +207,5 @@
     procedure UseDefaultSort;
     procedure SynchListToOrders;
-//    procedure ActivateDeactiveRenew;
+    procedure ActivateDeactiveRenew;
     procedure ValidateSelected(const AnAction, WarningMsg, WarningTitle: string);
     procedure ViewAlertedOrders(OrderIEN: string; Status: integer; DispGrp: string;
@@ -214,7 +216,8 @@
     function MeasureColumnHeight(AnOrder: TOrder; Index: Integer; Column: integer):integer;
     function GetPlainText(AnOrder: TOrder; index: integer):string;
-    function PatientStatusChanged: boolean;    
+    //function PatientStatusChanged: boolean;
     procedure UMEventOccur(var Message: TMessage); message UM_EVENTOCCUR;
     function CheckOrderStatus: boolean;
+    procedure RightClickMessageHandler(var Msg: TMessage; var Handled: Boolean);
   public
     procedure setSectionWidths; //CQ6170
@@ -267,5 +270,6 @@
      fOMNavA, rCore, fOCSession, fOrdersPrint, fOrdersTS, fEffectDate, fODActive, fODChild,
      fOrdersCopy, fOMVerify, fODAuto, rODBase, uODBase, rMeds,fODValidateAction, fMeds, uInit, fBALocalDiagnoses,
-     fODConsult, fClinicWardMeds, fActivateDeactivate;
+     fODConsult, fClinicWardMeds, fActivateDeactivate, VA2006Utils, rodMeds,
+     VA508AccessibilityRouter, VAUtils;
 
 {$R *.DFM}
@@ -467,4 +471,6 @@
   AChildList: TStringlist;
   CplxOrderID: string;
+  DCNewOrder: boolean;
+  DCChangeItem: TChangeItem;
 
   procedure RemoveFromOrderList(ChildOrderID: string);
@@ -512,5 +518,19 @@
                   then CanSign := CH_SIGN_YES
                   else CanSign := CH_SIGN_NA;
-                DCOrder(OrderForList, GetReqReason, ReturnedType);
+                DCNEwOrder := false;
+                if Changes.Orders.Count > 0 then
+                  begin
+                    for j := 0 to Changes.Orders.Count - 1 do
+                      begin
+                        DCChangeItem := TChangeItem(Changes.Orders.Items[j]);
+                        if DCChangeItem.ID = OrderForList.ID then
+                          begin
+                            if (Pos('DC', OrderForList.ActionOn) = 0) then
+                            DCNewOrder := True;
+                            //else DCNewOrder := False;
+                          end;
+                      end;
+                  end;
+                DCOrder(OrderForList, GetReqReason, DCNewOrder, ReturnedType);
                 Changes.Add(CH_ORD, OrderForList.ID, OrderForList.Text, '', CanSign);
                 FCompress := True;
@@ -587,6 +607,7 @@
 begin
   inherited;
+  OrderListClickProcessing := false;
+  FixHeaderControlDelphi2006Bug(hdrOrders);
   PageID             := CT_ORDERS;
-  lstOrders.Color    := ReadOnlyColor;
   uOrderList         := TList.Create;
   uEvtDCList         := TList.Create;
@@ -608,4 +629,7 @@
   FDontCheck := False;
   FParentComplexOrderID := '';
+  // 508 black color scheme that causes problems 
+  FHighContrast2Mode := BlackColorScheme and (ColorToRGB(clInfoBk) <> ColorToRGB(clBlack));
+  AddMessageHandler(lstOrders, RightClickMessageHandler);
 end;
 
@@ -613,4 +637,5 @@
 begin
   inherited;
+  RemoveMessageHandler(lstOrders, RightClickMessageHandler);
   ClearOrders(uOrderList);
   uEvtDCList.Clear;
@@ -684,4 +709,5 @@
     SaveTop := TopIndex;
     Clear;
+    repaint;
     for i := 0 to uOrderList.Count - 1 do
     begin
@@ -881,46 +907,53 @@
   inherited;
   if not CanChangeOrderView then Exit;
-  AnOrderView := TOrderView.Create;
-  AnOrderView.Filter    := STS_ACTIVE;
-  AnOrderView.DGroup    := DGroupAll;
-  AnOrderView.ViewName  := 'All Services, Active';
-  AnOrderView.InvChrono := True;
-  AnOrderView.ByService := True;
-  AnOrderView.CtxtTime  := 0;
-  AnOrderView.TextView  := 0;
-  AnOrderView.EventDelay.EventType := 'C';
-  AnOrderView.EventDelay.Specialty := 0;
-  AnOrderView.EventDelay.Effective := 0;
-  AnOrderView.EventDelay.EventIFN  := 0;
-  AnOrderView.EventDelay.EventName := 'All Services, Active';
-  SelectOrderView(AnOrderView);
-  with AnOrderView do if Changed then
-  begin
-    FCurrentView := AnOrderView;
-    if FCurrentView.Filter in [15,16,17,24] then
-    begin
-      FCompress      := False;
-      mnuActRel.Visible   := True;
-      popOrderRel.Visible := True;
-    end else
-    begin
-      mnuActRel.Visible   := False;
-      popOrderRel.Visible := False;
-    end;
-
-    lstSheets.ItemIndex := -1;
-    lblWrite.Caption := 'Write Orders';
-    lstWrite.Clear;
-    lstWrite.Caption := lblWrite.Caption;
-    LoadWriteOrders(lstWrite.Items);
-    RefreshOrderList(FROM_SERVER);
-
-    if ByService then
-    begin
-      if InvChrono then FDfltSort := OVS_CATINV  else FDfltSort := OVS_CATFWD;
-    end else
-    begin
-      if InvChrono then FDfltSort := OVS_INVERSE else FDfltSort := OVS_FORWARD;
-    end;
+  AnOrderView := TOrderView.Create;              //       - this starts fresh instead, since CPRS v22
+  try
+    AnOrderView.Assign(FCurrentView);              // RV - v27.1 - preload form with current view params
+  (*  AnOrderView.Filter    := STS_ACTIVE;                    - CQ #11261
+    AnOrderView.DGroup    := DGroupAll;
+    AnOrderView.ViewName  := 'All Services, Active';
+    AnOrderView.InvChrono := True;
+    AnOrderView.ByService := True;
+    AnOrderView.CtxtTime  := 0;
+    AnOrderView.TextView  := 0;
+    AnOrderView.EventDelay.EventType := 'C';
+    AnOrderView.EventDelay.Specialty := 0;
+    AnOrderView.EventDelay.Effective := 0;
+    AnOrderView.EventDelay.EventIFN  := 0;
+    AnOrderView.EventDelay.EventName := 'All Services, Active';*)
+    SelectOrderView(AnOrderView);
+    with AnOrderView do if Changed then
+    begin
+      FCurrentView.Assign(AnOrderView);
+      if FCurrentView.Filter in [15,16,17,24] then
+      begin
+        FCompress      := False;
+        mnuActRel.Visible   := True;
+        popOrderRel.Visible := True;
+      end else
+      begin
+        mnuActRel.Visible   := False;
+        popOrderRel.Visible := False;
+      end;
+
+      //lstSheets.ItemIndex := -1;
+      lstSheets.Items[0] := 'C;0^' + FCurrentView.ViewName;   // v27.5 - RV
+
+      lblWrite.Caption := 'Write Orders';
+      lstWrite.Clear;
+      lstWrite.Caption := lblWrite.Caption;
+      LoadWriteOrders(lstWrite.Items);
+      RefreshOrderList(FROM_SERVER);
+
+      if ByService then
+      begin
+        if InvChrono then FDfltSort := OVS_CATINV  else FDfltSort := OVS_CATFWD;
+      end else
+      begin
+        if InvChrono then FDfltSort := OVS_INVERSE else FDfltSort := OVS_FORWARD;
+      end;
+    end;
+  finally
+    AnOrderView.free;
   end;
 end;
@@ -984,11 +1017,11 @@
       AnOrderID := Piece(BigOrderID, ';', 1);
       if StrToIntDef(AnOrderID,0) = 0 then
-        ShowMessage('Detail view is not available for selected order.')
+        ShowMsg('Detail view is not available for selected order.')
       else
         begin
-          tmpList.Assign(DetailOrder(BigOrderID));
+          FastAssign(DetailOrder(BigOrderID), tmpList);
           if ((TOrder(Items.Objects[i]).DGroupName = 'Inpt. Meds') or
               (TOrder(Items.Objects[i]).DGroupName = 'Out. Meds') or
-              (TOrder(Items.Objects[i]).DGroupName = 'Clin. Orders') or
+              (TOrder(Items.Objects[i]).DGroupName = 'Clinic Orders') or
               (TOrder(Items.Objects[i]).DGroupName = 'Infusion')) then
             begin
@@ -996,5 +1029,5 @@
               tmpList.Add(StringOfChar('=', 74));
               tmpList.Add('');
-              tmpList.AddStrings(MedAdminHistory(AnOrderID));
+              FastAddStrings(MedAdminHistory(AnOrderID), tmpList);
             end;
 
@@ -1243,4 +1276,11 @@
 end;
 
+procedure TfrmOrders.RightClickMessageHandler(var Msg: TMessage;
+  var Handled: Boolean);
+begin
+  if Msg.Msg = WM_RBUTTONUP then
+    lstOrders.RightClickSelect := (lstOrders.SelCount < 1);
+end;
+
 function TfrmOrders.GetPlainText(AnOrder: TOrder; index: integer):string;
 var
@@ -1254,5 +1294,5 @@
   else
     FirstColumnDisplayed := 1;
-  for i:= FirstColumnDisplayed to 8 do begin
+  for i:= FirstColumnDisplayed to 9 do begin
     x := GetOrderText(AnOrder, index, i);
     if x <> '' then
@@ -1297,5 +1337,5 @@
     {measure height of start/stop times}
     NewHeight := HigherOf(NewHeight, MeasureColumnHeight(AnOrder, Index, 3));
-    if NewHeight > 255 then NewHeight := 255;
+    if NewHeight > 255 then NewHeight := 255;  // This is maximum allowed by a Windows
     if NewHeight <  13 then NewHeight := 13;
   end;
@@ -1316,4 +1356,7 @@
 
 function TfrmOrders.GetOrderText(AnOrder: TOrder; Index: integer; Column: integer): string;
+var
+  AReason:  TStringlist;
+  i: integer;
 begin
   if AnOrder <> nil then with AnOrder do
@@ -1334,5 +1377,20 @@
         result := Text;
         if Flagged then
-          result := result + ' *Flagged*';
+        begin
+          if Notifications.Active then
+          begin
+            AReason := TStringList.Create;
+            try
+              result := result + crlf;
+              LoadFlagReason(AReason, ID);
+              for i := 0 to AReason.Count - 1 do
+                result :=  result + AReason[i] + CRLF;
+            finally
+              AReason.Free;
+            end;
+          end
+          else
+            result := result + '  *Flagged*';
+        end;
       end;
       3: result := GetStartStopText( StartTime, StopTime);
@@ -1375,5 +1433,5 @@
     end;
     Canvas.FillRect(ARect);
-    Canvas.Pen.Color := clSilver;
+    Canvas.Pen.Color := Get508CompliantColor(clSilver);
     Canvas.MoveTo(ARect.Left, ARect.Bottom - 1);
     Canvas.LineTo(ARect.Right, ARect.Bottom - 1);
@@ -1405,7 +1463,7 @@
         if i = FirstColumnDisplayed then
         begin
-          if Flagged and (ColorToRGB(clWindowText) = ColorToRGB(clBlack)) then
+          if Flagged then
           begin
-            Canvas.Brush.Color := clRed;
+            Canvas.Brush.Color := Get508CompliantColor(clRed);
             Canvas.FillRect(ARect);
           end;
@@ -1417,6 +1475,8 @@
           if not (odSelected in State) and (AnOrder.Signature = OSS_UNSIGNED) then
           begin
-            if ColorToRGB(clWindowText) = ColorToRGB(clBlack) then
-              Canvas.Font.Color := clBlue;
+            if FHighContrast2Mode then
+              Canvas.Font.Color := clBlue
+            else
+              Canvas.Font.Color := Get508CompliantColor(clBlue);
           end;
         end;
@@ -1435,7 +1495,5 @@
   inherited;
   FEvtColWidth := hdrOrders.Sections[0].Width;
-  RedrawSuspend(Self.Handle);
   RedrawOrderList;
-  RedrawActivate(Self.Handle);
   lstOrders.Invalidate;
   pnlRight.Refresh;
@@ -1457,5 +1515,7 @@
   NextIndex: Integer;
 begin
-  if PatientStatusChanged then exit;
+  if OrderListClickProcessing then Exit;
+  OrderListClickProcessing := true;   //Make sure this gets set to false prior to exiting.
+  //if PatientStatusChanged then exit;
   if BILLING_AWARE then //CQ5114
      fODConsult.displayDXCode := ''; //CQ5114
@@ -1466,15 +1526,20 @@
   NextIndex := lstWrite.ItemIndex;
   if (FCurrentView.EventDelay.PtEventIFN>0) and (PtEvtCompleted(FCurrentView.EventDelay.PtEventIFN, FCurrentView.EventDelay.EventName)) then
+  begin
+    OrderListClickProcessing := false;
     Exit;
+  end;
   if not ReadyForNewOrder(FCurrentView.EventDelay) then
   begin
     lstWrite.ItemIndex := RefNumFor(Self);
+    OrderListClickProcessing := false;
     Exit;
   end;
-  
+
   // don't write delayed orders for non-VA meds:
   if (FCurrentView.EventDelay.EventIFN>0) and (Piece(lstWrite.ItemID,';',2) = '145') then
     begin
       InfoBox('Delayed orders cannot be written for Non-VA Medications.', 'Meds, Non-VA', MB_OK);
+      OrderListClickProcessing := false;
       Exit;
     end;
@@ -1488,8 +1553,11 @@
       begin
         lstWrite.ItemIndex := -1;
+        OrderListClickProcessing := false;
         Exit;
       end;
-  if frmFrame.CCOWDrivedChange then
+  if frmFrame.CCOWDrivedChange then begin
+    OrderListClickProcessing := false;
     Exit;
+  end;
   PositionTopOrder(StrToIntDef(Piece(lstWrite.ItemID, ';', 3), 0));  // position Display Group
   case CharAt(Piece(lstWrite.ItemID, ';', 4), 1) of
@@ -1517,4 +1585,5 @@
     lstSheetsClick(Self);
   end;
+  OrderListClickProcessing := false;
   if (FCurrentView <> nil) and (FCurrentView.EventDelay.PtEventIFN>0) and
     (PtEvtCompleted(FCurrentView.EventDelay.PtEventIFN, FCurrentView.EventDelay.EventName)) then
@@ -1560,4 +1629,9 @@
     begin
       AnOrder := TOrder(Items.Objects[i]);
+      if (AnAction = 'RN') and (PassDrugTest(StrtoINT(Piece(AnOrder.ID, ';',1)), 'E', True, True)=True) then
+        begin
+          ShowMsg('Cannot renew Clozapine orders.');
+          Selected[i] := false;
+        end;
       if (AnAction = 'RN') and (AnOrder.Status=6) and (AnOrder.DGroupName = 'Inpt. Meds') and (Patient.inpatient) and (IsClinicLoc(Encounter.Location)) then
          begin
@@ -1570,7 +1644,7 @@
         begin
           if (AnAction = 'RN') then
-            ShowMessage('The order contains invalid schedule and can not be renewed.')
+            ShowMsg('The order contains invalid schedule and can not be renewed.')
           else if (AnAction = 'EV') then
-            ShowMessage('The order contains invalid schedule and can not be changed to event delayed order.');
+            ShowMsg('The order contains invalid schedule and can not be changed to event delayed order.');
 
           Selected[i] := False;
@@ -1720,5 +1794,5 @@
     //if CheckOrderStatus = True then Exit;
     ValidateSelected(OA_DC, TX_NO_DC, TC_NO_DC); // validate DC action on each order
-    //ActivateDeactiveRenew;   AGP 26.53 TURN OFF UNTIL FINAL DECISION CAN BE MADE
+    ActivateDeactiveRenew;   //AGP 26.53 TURN OFF UNTIL FINAL DECISION CAN BE MADE
     MakeSelectedList(SelectedList);                     // build list of orders that remain
     // updating the Changes object happens in ExecuteDCOrders, based on individual order
@@ -1748,5 +1822,5 @@
   if not CanManualRelease then
   begin
-    ShowMessage('You are not authorized to manual release delayed orders.');
+    ShowMsg('You are not authorized to manual release delayed orders.');
     Exit;
   end;
@@ -2085,4 +2159,5 @@
     ChangeIFNList.Free;
   end;
+  if frmFrame.TimedOut then Exit;
   RedrawOrderList;
 end;
@@ -2281,5 +2356,5 @@
   if (User.OrderRole <> 2) and (User.OrderRole <> 3) then
   begin
-    ShowMessage('Sorry, You don''t have the permission to release selected orders manually');
+    ShowMsg('Sorry, You don''t have the permission to release selected orders manually');
     Exit;
   end;
@@ -2995,4 +3070,5 @@
     if i > 0 then
       IsDefaultDlg := False;
+
     ADest.ItemIndex := -1;
     for j := 0 to ADest.Items.Count - 1 do
@@ -3107,5 +3183,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;
+  //if PatientStatusChanged then exit;
   //frmFrame.UpdatePtInfoOnRefresh;
 end;
@@ -3114,5 +3190,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;
+  //if PatientStatusChanged then exit;
   //frmFrame.UpdatePtInfoOnRefresh;
 end;
@@ -3121,5 +3197,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;
+  //if PatientStatusChanged then exit;
   //frmFrame.UpdatePtInfoOnRefresh;
 end;
@@ -3128,5 +3204,5 @@
 begin
   inherited;
-  if PatientStatusChanged then exit;  
+  //if PatientStatusChanged then exit;  
   //frmFrame.UpdatePtInfoOnRefresh;
 end;
@@ -3273,5 +3349,5 @@
 end;
 
-function TfrmOrders.PatientStatusChanged: boolean;
+{function TfrmOrders.PatientStatusChanged: boolean;
 const
 
@@ -3302,5 +3378,5 @@
     Result := True;
   end;
-end;
+end;}
 
 function TfrmOrders.CheckOrderStatus: boolean;
@@ -3308,20 +3384,23 @@
 i: integer;
 AnOrder: TOrder;
+OrderArray: TStringList;
 begin
     Result := False;
+    OrderArray := TStringList.Create;
     with lstOrders do for i := 0 to Items.Count - 1 do if Selected[i] then
     begin
       AnOrder := TOrder(Items.Objects[i]);
-      if AnORder.Status <> GetOrderStatus(AnOrder.ID) then
-         begin
-           MessageDlg('The Order status has changed.' + #13#10#13 + 'CPRS needs to refresh patient information to display the correct order status', mtWarning, [mbOK], 0);
-           frmFrame.mnuFileRefreshClick(Application);
-           Result := True;
-           EXIT;
-         end;
-    end;
-end;
-
-(*procedure TfrmOrders.ActivateDeactiveRenew;
+      OrderArray.Add(AnOrder.ID + U + InttoStr(AnOrder.Status));
+    end;
+    if (OrderArray <> nil) and (not DoesOrderStatusMatch(OrderArray)) then
+      begin
+        MessageDlg('The Order status has changed.' + #13#10#13 + 'CPRS needs to refresh patient information to display the correct order status', mtWarning, [mbOK], 0);
+        frmFrame.mnuFileRefreshClick(Application);
+        Result := True;
+      end;
+    ORderArray.Free;
+end;
+
+procedure TfrmOrders.ActivateDeactiveRenew;
 var
   i: Integer;
@@ -3336,5 +3415,5 @@
     end;
     if tmpArr <> nil then frmActivateDeactive.fActivateDeactive(tmpArr);
-end;  *)
+end;
 
 procedure TfrmOrders.ViewInfo(Sender: TObject);
@@ -3395,4 +3474,7 @@
 end;
 
+initialization
+  SpecifyFormIsNotADialog(TfrmOrders);
+
 end.
 
