Index: cprs/trunk/CPRS-Chart/Templates/fTemplateDialog.pas
===================================================================
--- cprs/trunk/CPRS-Chart/Templates/fTemplateDialog.pas	(revision 456)
+++ cprs/trunk/CPRS-Chart/Templates/fTemplateDialog.pas	(revision 829)
@@ -5,8 +5,9 @@
 uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
-  StdCtrls, ExtCtrls, ORCtrls, ORFn, AppEvnts, uTemplates;
+  StdCtrls, ExtCtrls, ORCtrls, ORFn, AppEvnts, uTemplates, fBase508Form, uConst,
+  VA508AccessibilityManager;
 
 type
-  TfrmTemplateDialog = class(TForm)
+  TfrmTemplateDialog = class(TfrmBase508Form)
     sbMain: TScrollBox;
     pnlBottom: TScrollBox;
@@ -26,5 +27,7 @@
     procedure btnPreviewClick(Sender: TObject);
     procedure FormClose(Sender: TObject; var Action: TCloseAction);
+    procedure FormShow(Sender: TObject);
   private
+    FFirstBuild: boolean;
     SL: TStrings;
     BuildIdx: TStringList;
@@ -57,4 +60,6 @@
     procedure ParentCBEnter(Sender: TObject);
     procedure ParentCBExit(Sender: TObject);
+    procedure UMScreenReaderInit(var Message: TMessage); message UM_MISC;
+    procedure InitScreenReaderSetup;
   public
     property Silent: boolean read FSilent write FSilent ;
@@ -72,5 +77,6 @@
 implementation
 
-uses dShared, uConst, uTemplateFields, fRptBox, uInit, rMisc;
+uses dShared, uTemplateFields, fRptBox, uInit, rMisc, uDlgComponents,
+  VA508AccessibilityRouter, VAUtils;
 
 {$R *.DFM}
@@ -187,4 +193,5 @@
   Result := FALSE;
   CancelDlg := FALSE;
+  SetTemplateDialogCanceled(FALSE);
   frmTemplateDialog := TfrmTemplateDialog.Create(Application);
   try
@@ -272,7 +279,12 @@
   end;
 
-  if not Result then
+  if Result then
+    SetTemplateDialogCanceled(TRUE)
+  else
+  begin
+    SetTemplateDialogCanceled(FALSE);
     CheckBoilerplate4Fields(SL, CaptionText, PreviewMode);
-
+  end;
+  
 end;
 
@@ -289,4 +301,5 @@
       SL.Clear;
   end;
+  StripScreenReaderCodes(SL);
 end;
 
@@ -380,4 +393,25 @@
 end;
 
+procedure TfrmTemplateDialog.InitScreenReaderSetup;
+var
+  ctrl: TWinControl;
+  list: TList;
+begin
+  if ScreenReaderSystemActive then
+  begin
+    list := TList.Create;
+    try
+      sbMain.GetTabOrderList(list);
+      if list.Count > 0 then
+      begin
+        ctrl := TWinControl(list[0]);
+        PostMessage(Handle, UM_MISC, WParam(ctrl), 0);
+      end;
+    finally
+      list.free;
+    end;
+  end;
+end;
+
 function TfrmTemplateDialog.IsAncestor( OldID: string; NewID: string): boolean;
 begin
@@ -398,5 +432,6 @@
   KillCtrl, doHint, dsp, noTextParent: boolean;
   Entry: TTemplateDialogEntry;
-  StringIn, StringOut: string;
+//  StringIn, StringOut: string;
+  cb: TCPRSDialogParentCheckBox;
 
   procedure NextTabCtrl(ACtrl: TControl);
@@ -511,5 +546,10 @@
       Entry := TTemplateDialogEntry(Entries.Objects[idx]);
 
-    pnl := Entry.GetPanel(FMaxPnlWidth, sbMain);
+    if(dsp or OneOnly) then
+      cb := nil
+    else
+      cb := TCPRSDialogParentCheckBox.Create(Self);
+
+    pnl := Entry.GetPanel(FMaxPnlWidth, sbMain, cb);
     pnl.Show;
     if(doHint and (not pnl.ShowHint)) then
@@ -521,9 +561,9 @@
       Entry.OnChange := FieldChanged;
     end;
-    if(dsp or OneOnly) then
+    if not assigned(cb) then
       ctrl := pnl
     else
     begin
-      ctrl := TORCheckBox.Create(Self);
+      ctrl := cb;
       ctrl.Parent := sbMain;
 
@@ -539,11 +579,13 @@
       TORCheckBox(ctrl).AutoSize := false;
       TORCheckBox(ctrl).Associate := pnl;
+      pnl.Tag := Integer(ctrl);
       tmpID := copy(ID, 1, (pos('.', ID) - 1)); {copy the ID without the decimal place}
-      if Templates.IndexOf(tmpID) > -1 then
-        StringIn := 'Sub-Template: ' + TTemplate(Templates.Objects[Templates.IndexOf(tmpID)]).PrintName
-      else
-        StringIn := 'Sub-Template:';
-      StringOut := StringReplace(StringIn, '&', '&&', [rfReplaceAll]);
-      TORCheckBox(ctrl).Caption := StringOut;
+//      if Templates.IndexOf(tmpID) > -1 then
+//        StringIn := 'Sub-Template: ' + TTemplate(Templates.Objects[Templates.IndexOf(tmpID)]).PrintName
+//      else
+//        StringIn := 'Sub-Template:';
+//      StringOut := StringReplace(StringIn, '&', '&&', [rfReplaceAll]);
+//      TORCheckBox(ctrl).Caption := StringOut;
+      UpdateColorsFor508Compliance(ctrl);
 
     end;
@@ -615,4 +657,9 @@
     for i := 1 to Count do
       BuildCB(i, Y, FirstTime);
+    if ScreenReaderSystemActive then
+    begin
+      amgrMain.RefreshComponents;
+      Application.ProcessMessages;
+    end;
   finally
     FBuilding := FALSE;
@@ -626,4 +673,15 @@
     RepaintBuild := FALSE;
     BuildAllControls;
+    InitScreenReaderSetup;
+  end;
+end;
+
+procedure TfrmTemplateDialog.FormShow(Sender: TObject);
+begin
+  inherited;
+  if FFirstBuild then
+  begin
+    FFirstBuild := FALSE;
+    InitScreenReaderSetup;
   end;
 end;
@@ -631,4 +689,5 @@
 procedure TfrmTemplateDialog.FormCreate(Sender: TObject);
 begin
+  FFirstBuild := TRUE;
   BuildIdx := TStringList.Create;
   Entries := TStringList.Create;
@@ -700,5 +759,5 @@
       if not CanClose then
       begin
-        ShowMessage(MissingFieldsTxt);
+        ShowMsg(MissingFieldsTxt);
         break;
       end;
@@ -719,6 +778,7 @@
   TmpSL := TStringList.Create;
   try
-    TmpSL.Assign(SL);
+    FastAssign(SL, TmpSL);
     GetText(TmpSL, FALSE);  {FALSE = Do not include embedded fields}
+    StripScreenReaderCodes(TmpSL);
     ReportBox(TmpSL, 'Dialog Preview', FALSE);
   finally
@@ -755,4 +815,21 @@
 end;
 
+procedure TfrmTemplateDialog.UMScreenReaderInit(var Message: TMessage);
+var
+  ctrl: TWinControl;
+  item: TVA508AccessibilityItem;
+begin
+  ctrl := TWinControl(Message.WParam);
+  // Refresh the accessibility manager entry -
+  // fixes bug where first focusable check boxes weren't working correctly  
+  if ctrl is TCPRSDialogParentCheckBox then
+  begin
+    item := amgrMain.AccessData.FindItem(ctrl, FALSE);
+    if assigned(item) then
+      item.free;
+    amgrMain.AccessData.EnsureItemExists(ctrl);
+  end;
+end;
+
 end.
 
