Index: cprs/trunk/CPRS-Chart/Consults/fODConsult.pas
===================================================================
--- cprs/trunk/CPRS-Chart/Consults/fODConsult.pas	(revision 456)
+++ cprs/trunk/CPRS-Chart/Consults/fODConsult.pas	(revision 829)
@@ -8,5 +8,6 @@
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   fODBase, StdCtrls, ORCtrls, ExtCtrls, ComCtrls, ORfn, uConst, Buttons,
-  Menus, UBAGlobals, rOrders, fBALocalDiagnoses, UBAConst, UBACore, ORNet ;
+  Menus, UBAGlobals, rOrders, fBALocalDiagnoses, UBAConst, UBACore, ORNet,
+  VA508AccessibilityManager ;
 
 type
@@ -18,8 +19,8 @@
     txtAttn: TORComboBox;
     lblService: TLabel;
-    lblUrgency: TStaticText;
-    lblPlace: TStaticText;
-    lblAttn: TStaticText;
-    lblProvDiag: TStaticText;
+    lblUrgency: TLabel;
+    lblPlace: TLabel;
+    lblAttn: TLabel;
+    lblProvDiag: TLabel;
     treService: TORTreeView;
     cboCategory: TORComboBox;
@@ -86,4 +87,6 @@
     procedure cmdQuitClick(Sender: TObject);
     procedure FormClose(Sender: TObject; var Action: TCloseAction);
+    procedure FormResize(Sender: TObject);
+    procedure treServiceEnter(Sender: TObject);
 
   private
@@ -103,4 +106,5 @@
     procedure SaveConsultDxForNurse(pDiagnosis: string);  // save the dx entered by nurese if Master BA switch is ON
     procedure SetUpCopyConsultDiagnoses(pOrderID:string);
+    procedure AdjustMemReasonSize;
   protected
     procedure InitDialog; override;
@@ -125,5 +129,5 @@
 uses
     rODBase, rConsults, uCore, uConsults, rCore, fConsults, fPCELex, rPCE, fPreReq,
-    ORClasses, clipbrd, uTemplates, fFrame, uODBase;
+    ORClasses, clipbrd, uTemplates, fFrame, uODBase, uVA508CPRSCompatibility;
 
 var
@@ -153,4 +157,8 @@
   TX_INACTIVE_CODE_OPTIONAL = 'If another code is not selected, no code will be saved.';
 
+  TX_SVC_HRCHY = 'services/specialties hierarchy';
+  TX_VIEW_SVC_HRCHY = 'View services/specialties hierarchically';
+  TX_CLOSE_SVC_HRCHY = 'Close services/specialties hierarchy tree view';
+
 procedure TfrmODCslt.FormCreate(Sender: TObject);
 begin
@@ -180,5 +188,5 @@
   Responses.Dialog := 'GMRCOR CONSULT';   // loads formatting info
   StatusText('Loading Default Values');
-  Defaults.Assign(ODForConsults);  // ODForConsults returns TStrings with defaults
+  FastAssign(ODForConsults, Defaults);  // ODForConsults returns TStrings with defaults
   CtrlInits.LoadDefaults(Defaults);
   txtAttn.InitLongList('') ;
@@ -229,6 +237,6 @@
   btnServiceTree.Enabled := True;
   pnlServiceTreeButton.Enabled := True;
-  ActiveControl := cboService;
   SetProvDiagPromptingMode;
+  ActiveControl := cboService; // set after call to SetProvDiagPromptingMode
   Changing := False;
   StatusText('');
@@ -240,4 +248,7 @@
                     'Please contact your Clinical Coordinator/IRM staff to fix this order.';
   TX_INACTIVE_SVC_CAP = 'Inactive Service';
+  TX_NO_SVC = 'The order or quick order you have selected does not specify a consult service.' + CRLF +
+              'Please contact your Clinical Coordinator/IRM staff to fix this order.';
+  TC_NO_SVC = 'No service specified';
 var
  i:integer;
@@ -254,5 +265,13 @@
       Changing := True;
       tmpResp := TResponse(FindResponseByName('ORDERABLE',1));
-      SvcIEN := GetServiceIEN(tmpResp.IValue);
+      if tmpResp <> nil then
+        SvcIEN := GetServiceIEN(tmpResp.IValue)
+      else
+        begin
+          InfoBox(TX_NO_SVC, TC_NO_SVC, MB_ICONERROR or MB_OK);
+          AbortOrder := True;
+          Close;
+          Exit;
+        end;
       if SvcIEN = '-1' then
         begin
@@ -284,6 +303,20 @@
       SetProvDiagPromptingMode;
       GetProvDxandValidateCode(Responses);
+      SetTemplateDialogCanceled(FALSE);
       SetControl(memReason,     'COMMENT',   1);
+      if WasTemplateDialogCanceled then
+      begin
+        AbortOrder := True;
+        Close;
+        Exit;
+      end;
+      SetTemplateDialogCanceled(FALSE);
       SetupReasonForRequest(OrderAction);
+      if WasTemplateDialogCanceled then
+      begin
+        AbortOrder := True;
+        Close;
+        Exit;
+      end;
       Changing := False;
       ControlChange(Self);
@@ -292,6 +325,6 @@
     begin
       if QuickList.Count > 0 then BuildQuickTree(QuickList, '0', nil) ;
-      SvcList.Assign(LoadServiceListWithSynonyms(CN_SVC_LIST_ORD));           {RV}
-      AList.Assign(SvcList);
+      FastAssign(LoadServiceListWithSynonyms(CN_SVC_LIST_ORD), SvcList);           {RV}
+      FastAssign(SvcList, AList);
       SortByPiece(AList, U, 2);
       BuildServiceTree(treService, SvcList, '0', nil) ;
@@ -308,5 +341,5 @@
       if QuickList.Count > 0 then with cboService do
         begin
-          Items.Assign(QuickList);
+          FastAssign(QuickList, cboService.Items);
           Items.Add(LLS_LINE);
           Items.Add(LLS_SPACE);
@@ -338,5 +371,5 @@
               Exit;
             end;
-          memReason.Lines.Assign(DefaultReasonForRequest(cboService.ItemID, True));
+          QuickCopy(DefaultReasonForRequest(cboService.ItemID, True), memReason);
         end;
       PreserveControl(treService);
@@ -555,7 +588,14 @@
       SetControl(cboPlace,      'PLACE',     1);
       SetControl(txtAttn,       'PROVIDER',  1);
+      SetTemplateDialogCanceled(FALSE);
       SetControl(memReason,     'COMMENT',   1);
+      if WasTemplateDialogCanceled and OrderContainsObjects then
+      begin
+        AbortOrder := TRUE;
+        Close;
+        Exit;
+      end;
       if ((cboService.ItemIEN > 0) and (Length(memReason.Text) = 0)) then
-        memReason.Lines.Assign(DefaultReasonForRequest(cboService.ItemID, True));
+        QuickCopy(DefaultReasonForRequest(cboService.ItemID, True), memReason);
       SetupReasonForRequest(ORDER_QUICK);
       GetProvDxandValidateCode(Responses);
@@ -571,5 +611,5 @@
               Exit;
             end;
-          memReason.Lines.Assign(DefaultReasonForRequest(cboService.ItemID, True));
+          QuickCopy(DefaultReasonForRequest(cboService.ItemID, True), memReason);
           SetupReasonForRequest(ORDER_NEW);
         end;
@@ -620,7 +660,14 @@
 end;
 
+procedure TfrmODCslt.treServiceEnter(Sender: TObject);
+begin
+  inherited;
+  cmdQuit.Cancel := FALSE;
+end;
+
 procedure TfrmODCslt.treServiceExit(Sender: TObject);
 begin
   inherited;
+  cmdQuit.Cancel := TRUE;
   with cboService do
   begin
@@ -688,7 +735,9 @@
   AStringList := TStringList.Create;
   try
-    AStringList.Assign(memReason.Lines);
+    //QuickCopy(memReason, AStringList);
+    AStringList.Text := memReason.Text;
     LimitStringLength(AStringList, 74);
-    memReason.Lines.Assign(AstringList);
+    //QuickCopy(AstringList, memReason);
+    memReason.Text := AStringList.Text;
     ControlChange(Self);
   finally
@@ -762,7 +811,14 @@
               SetControl(cboPlace,      'PLACE',     1);
               SetControl(txtAttn,       'PROVIDER',  1);
+              SetTemplateDialogCanceled(FALSE);
               SetControl(memReason,     'COMMENT',   1);
+              if WasTemplateDialogCanceled and OrderContainsObjects then
+              begin
+                AbortOrder := TRUE;
+                Close;
+                Exit;
+              end;
 //              if ((cboService.ItemIEN > 0) and (Length(memReason.Text) = 0)) then
-//                memReason.Lines.Assign(DefaultReasonForRequest(cboService.ItemID, True));
+//                QuickCopy(DefaultReasonForRequest(cboService.ItemID, True), memReason);
               SetupReasonForRequest(ORDER_QUICK);
               GetProvDxandValidateCode(Responses);
@@ -779,5 +835,5 @@
                       Exit;
                     end;
-                  memReason.Lines.Assign(DefaultReasonForRequest(cboService.ItemID, True));
+                  QuickCopy(DefaultReasonForRequest(cboService.ItemID, True), memReason);
                   SetupReasonForRequest(ORDER_NEW);
                   Changing := False;
@@ -824,4 +880,8 @@
   if treService.Visible then
   begin
+  // for some reason screen reader is reading caption when tree view is not visible
+    treService.Caption := TX_SVC_HRCHY;
+    pnlServiceTreeButton.Caption := TX_CLOSE_SVC_HRCHY;
+    btnServiceTree.Hint := TX_CLOSE_SVC_HRCHY;
     treService.SetFocus;
     with treService do for i := 0 to Items.Count-1 do
@@ -834,4 +894,11 @@
         end;
     end;
+  end
+  else
+  begin
+    treService.Caption := '';
+    pnlServiceTreeButton.Caption := TX_VIEW_SVC_HRCHY;
+    btnServiceTree.Hint := TX_VIEW_SVC_HRCHY;
+    pnlServiceTreeButton.SetFocus;
   end;
   Changing := False;
@@ -863,5 +930,4 @@
       txtProvDiag.Enabled    := False;
       txtProvDiag.Font.Color := clGrayText;
-      lblProvDiag.Enabled    := False;
       txtProvDiag.ReadOnly   := True;
       txtProvDiag.Color      := clBtnFace;
@@ -940,5 +1006,4 @@
   txtProvDiag.Color      := clBtnFace;
   txtProvDiag.Font.Color := clBtnText;
-  lblProvDiag.Enabled    := False;
   txtProvDiag.Hint       := '';
   if cboService.ItemIEN = 0 then Exit;
@@ -962,5 +1027,4 @@
       txtProvDiag.Color      := clBtnFace;
       txtProvDiag.Font.Color := clBtnText;
-      lblProvDiag.Enabled    := False;
     end
   else
@@ -972,5 +1036,4 @@
               txtProvDiag.Color      := clWindow;
               txtProvDiag.Font.Color := clWindowText;
-              lblProvDiag.Enabled    := True;
             end;
       'L':  begin
@@ -989,5 +1052,4 @@
               txtProvDiag.Color      := clInfoBk;
               txtProvDiag.Font.Color := clInfoText;
-              lblProvDiag.Enabled    := True;
            end;
     end;
@@ -1037,5 +1099,5 @@
 begin
   if ((OrderAction = ORDER_QUICK) and (cboService.ItemID <> '') and (Length(memReason.Text) = 0)) then
-    memReason.Lines.Assign(DefaultReasonForRequest(cboService.ItemID, True));
+    QuickCopy(DefaultReasonForRequest(cboService.ItemID, True), memReason);
   EditReason := GMRCREAF;
   if EditReason = '' then EditReason := ReasonForRequestEditable(cboService.ItemID + CSLT_PTR);
@@ -1065,5 +1127,5 @@
       if ItemIEN > 0 then
         begin
-          Alist.Assign(GetServicePrerequisites(ItemID + CSLT_PTR));
+          FastAssign(GetServicePrerequisites(ItemID + CSLT_PTR), Alist);
           if AList.Count > 0 then
             begin
@@ -1154,5 +1216,5 @@
   try
     Result := GetDefaultReasonForRequest(Service + CSLT_PTR, Resolve);
-    TmpSL.Assign(Result);
+    FastAssign(Result, TmpSL);
     x := TmpSL.Text;
     ExpandOrderObjects(x, HasObjects);
@@ -1160,6 +1222,14 @@
     Responses.OrderContainsObjects := HasObjects;
     ExecuteTemplateOrBoilerPlate(TmpSL, cboService.ItemIEN , ltConsult, nil, 'Reason for Request: ' + cboService.DisplayText[cboService.ItemIndex], DocInfo);
-    if TmpSL.Text <> x then Responses.OrderContainsObjects := False;
-    Result.Assign(TmpSL);
+    AbortOrder := WasTemplateDialogCanceled;
+    Responses.OrderContainsObjects := HasObjects or TemplateBPHasObjects;
+    if AbortOrder then
+    begin
+      Result.Text := '';
+      Close;
+      Exit;
+    end
+    else
+      FastAssignWith508Msg(TmpSL, Result);
   finally
     TmpSL.Free;
@@ -1190,4 +1260,9 @@
       treServiceChange(Sender, treService.Selected);
     end;
+  VK_ESCAPE:
+    begin
+      key := 0;
+      btnServiceTreeClick(Self);
+    end    
   else
     FKeyBoarding := True;
@@ -1406,4 +1481,18 @@
 end;
 
+procedure TfrmODCslt.FormResize(Sender: TObject);
+begin
+  inherited;
+  AdjustMemReasonSize();
+end;
+
+procedure TfrmODCslt.AdjustMemReasonSize;
+const
+  PIXEL_SPACE = 3;
+begin
+  pnlReason.Top := cboService.Top + cboService.Height + PIXEL_SPACE;
+  pnlReason.Height := memOrder.Top - pnlReason.Top - PIXEL_SPACE;
+end;
+
 end.
 
