Index: cprs/branches/HealthSevak-CPRS/CPRS-Chart/Templates/fTemplateEditor.pas
===================================================================
--- cprs/branches/HealthSevak-CPRS/CPRS-Chart/Templates/fTemplateEditor.pas	(revision 1676)
+++ cprs/branches/HealthSevak-CPRS/CPRS-Chart/Templates/fTemplateEditor.pas	(revision 1693)
@@ -31,6 +31,4 @@
     btnCancel: TButton;
     btnOK: TButton;
-    pnlBoilerplate: TPanel;
-    reBoil: TRichEdit;
     pnlTop: TPanel;
     pnlRightTop: TPanel;
@@ -66,9 +64,4 @@
     mnuBPErrorCheck: TMenuItem;
     mnuBPSpellCheck: TMenuItem;
-    pnlGroupBP: TPanel;
-    reGroupBP: TRichEdit;
-    lblGroupBP: TLabel;
-    splBoil: TSplitter;
-    pnlGroupBPGap: TPanel;
     tmrAutoScroll: TTimer;
     popGroup: TPopupMenu;
@@ -154,8 +147,4 @@
     mnuAutoGen: TMenuItem;
     mnuNodeAutoGen: TMenuItem;
-    pnlNotes: TPanel;
-    reNotes: TRichEdit;
-    splNotes: TSplitter;
-    lblNotes: TLabel;
     popNotes: TPopupMenu;
     mnuNotesUndo: TMenuItem;
@@ -193,13 +182,6 @@
     N17: TMenuItem;
     mnuTemplateIconLegend: TMenuItem;
-    pnlBP: TPanel;
-    lblBoilerplate: TLabel;
-    cbLongLines: TCheckBox;
     cbLock: TORCheckBox;
     mnuRefresh: TMenuItem;
-    lblBoilRow: TLabel;
-    lblGroupRow: TLabel;
-    lblBoilCol: TLabel;
-    lblGroupCol: TLabel;
     pnlCOM: TPanel;
     lblCOMParam: TLabel;
@@ -211,8 +193,27 @@
     lblLink: TLabel;
     imgLblTemplates: TVA508ImageListLabeler;
+    Panel1: TPanel;
+    pnlBoilerplate: TPanel;
+    splBoil: TSplitter;
+    splNotes: TSplitter;
+    reBoil: TRichEdit;
+    pnlGroupBP: TPanel;
+    lblGroupBP: TLabel;
+    lblGroupRow: TLabel;
+    lblGroupCol: TLabel;
+    reGroupBP: TRichEdit;
+    pnlGroupBPGap: TPanel;
+    pnlBP: TPanel;
+    lblBoilerplate: TLabel;
+    lblBoilRow: TLabel;
+    lblBoilCol: TLabel;
+    cbLongLines: TCheckBox;
+    pnlNotes: TPanel;
+    lblNotes: TLabel;
+    reNotes: TRichEdit;
     procedure btnNewClick(Sender: TObject);
     procedure btnApplyClick(Sender: TObject);
     procedure FormCreate(Sender: TObject);
-    procedure cboOwnerNeedData(Sender: TObject; const StartFrom: String;
+    procedure cboOwnerNeedData(Sender: TObject; const StartFrom: string;
       Direction, InsertAt: Integer);
     procedure cboOwnerChange(Sender: TObject);
@@ -232,5 +233,5 @@
     procedure tvTreeEnter(Sender: TObject);
     procedure tvTreeNodeEdited(Sender: TObject; Node: TTreeNode;
-      var S: String);
+      var S: string);
     procedure cbShHideClick(Sender: TObject);
     procedure cbPerHideClick(Sender: TObject);
@@ -338,7 +339,7 @@
     procedure cbxCOMObjChange(Sender: TObject);
     procedure edtCOMParamChange(Sender: TObject);
-    procedure cbxLinkNeedData(Sender: TObject; const StartFrom: String;
+    procedure cbxLinkNeedData(Sender: TObject; const StartFrom: string;
       Direction, InsertAt: Integer);
-    procedure cbxLinkChange(Sender: TObject);
+    procedure cbxLinkExit(Sender: TObject);
     procedure reBoilKeyUp(Sender: TObject; var Key: Word;
       Shift: TShiftState);
@@ -347,4 +348,6 @@
       Shift: TShiftState);
     procedure FormClose(Sender: TObject; var Action: TCloseAction);
+    procedure splMainCanResize(Sender: TObject; var NewSize: Integer;
+      var Accept: Boolean);
   private
     FLastRect: TRect;
@@ -366,5 +369,5 @@
     FCurTree: TTreeView;
     FTreeControl: array[TTemplateTreeType, TTemplateTreeControl] of TControl;
-    FInternalHiddenExpand :boolean;
+    FInternalHiddenExpand: boolean;
     FFindShOn: boolean;
     FFindShNext: boolean;
@@ -451,24 +454,24 @@
   PropText = ' Template Properties ';
 //  GroupTag = 5;
-  BPDisplayOnlyFld  = 0;
-  BPFirstLineFld    = 1;
-  BPOneItemOnlyFld  = 2;
+  BPDisplayOnlyFld = 0;
+  BPFirstLineFld = 1;
+  BPOneItemOnlyFld = 2;
   BPHideDlgItemsFld = 3;
-  BPHideItemsFld    = 4;
-  BPIndentFld       = 5;
-  BPLockFld         = 6;
+  BPHideItemsFld = 4;
+  BPIndentFld = 5;
+  BPLockFld = 6;
   NoIE5 = 'You must have Internet Explorer 5 or better installed to %s Templates';
   NoIE5Header = 'Need Internet Explorer 5';
-  VK_A              = Ord('A');
-  VK_C              = Ord('C');
-  VK_E              = Ord('E');
-  VK_F              = Ord('F');
-  VK_G              = Ord('G');
-  VK_I              = Ord('I');
-  VK_S              = Ord('S');
-  VK_T              = Ord('T');
-  VK_V              = Ord('V');
-  VK_X              = Ord('X');
-  VK_Z              = Ord('Z');
+  VK_A = Ord('A');
+  VK_C = Ord('C');
+  VK_E = Ord('E');
+  VK_F = Ord('F');
+  VK_G = Ord('G');
+  VK_I = Ord('I');
+  VK_S = Ord('S');
+  VK_T = Ord('T');
+  VK_V = Ord('V');
+  VK_X = Ord('X');
+  VK_Z = Ord('Z');
 
 type
@@ -481,10 +484,10 @@
 
   TypeTag: array[TTypeIndex] of TTemplateType = (ttDoc, ttClass, ttGroup, ttDialog, ttDoc, ttDoc);
-  ForcedIdx: array[boolean, TTypeIndex] of integer = ((0,1,2,3,4,5),(-1,0,1,2,-1,-1));
+  ForcedIdx: array[boolean, TTypeIndex] of integer = ((0, 1, 2, 3, 4, 5), (-1, 0, 1, 2, -1, -1));
   IdxForced: array[boolean, 0..5] of TTypeIndex = ((tiTemplate, tiFolder, tiGroup, tiDialog, tiRemDlg, tiCOMObj),
-                                                   (tiFolder, tiGroup, tiDialog, tiNone, tiNone, tiNone));
+    (tiFolder, tiGroup, tiDialog, tiNone, tiNone, tiNone));
   iMessage = 'This template has one or more new fields, and you are not authorized to create new fields.  ' +
-             'If you continue, the program will import the new template without the new fields.  Do you wish ' +
-             'to do this?';
+    'If you continue, the program will import the new template without the new fields.  Do you wish ' +
+    'to do this?';
   iMessage2 = 'The imported template fields had XML errors.  ';
   iMessage3 = 'No Fields were imported.';
@@ -495,5 +498,5 @@
 
 procedure EditTemplates(Form: TForm; NewTemplate: boolean = FALSE;
-                        CopiedText: string = ''; Shared: boolean = FALSE);
+  CopiedText: string = ''; Shared: boolean = FALSE);
 var
   frmTemplateEditor: TfrmTemplateEditor;
@@ -504,16 +507,16 @@
 
 begin
-  if(UserTemplateAccessLevel in [taReadOnly, taNone]) then exit;
+  if (UserTemplateAccessLevel in [taReadOnly, taNone]) then exit;
 
   ExpandStr := '';
   SelectStr := '';
   Drawers := nil;
-  if(not NewTemplate) and (CopiedText = '') then
+  if (not NewTemplate) and (CopiedText = '') then
   begin
     if Form is TfrmDrawers then
       Drawers := TFrmDrawers(Form)
     else
-    if IsPublishedProp(Form, DrawersProperty) then
-      Drawers := TFrmDrawers(GetOrdProp(Form, DrawersProperty));
+      if IsPublishedProp(Form, DrawersProperty) then
+        Drawers := TFrmDrawers(GetOrdProp(Form, DrawersProperty));
   end;
 
@@ -521,5 +524,5 @@
   begin
     ExpandStr := Drawers.tvTemplates.GetExpandedIDStr(1, ';');
-    SelectStr := Drawers.tvTemplates.GetNodeID(TORTreeNode(Drawers.tvTemplates.Selected),1,';');
+    SelectStr := Drawers.tvTemplates.GetNodeID(TORTreeNode(Drawers.tvTemplates.Selected), 1, ';');
   end;
 
@@ -533,14 +536,14 @@
       reNotes.Font.Size := Form.Font.Size;
       dmodShared.ExpandTree(tvShared, ExpandStr, FSharedEmptyNodeCount);
-      SelNode := tvShared.FindPieceNode(SelectStr,1,';');
+      SelNode := tvShared.FindPieceNode(SelectStr, 1, ';');
       SelShared := assigned(SelNode);
       dmodShared.ExpandTree(tvPersonal, ExpandStr, FPersonalEmptyNodeCount);
       if not SelShared then
-        SelNode := tvPersonal.FindPieceNode(SelectStr,1,';');
-
-      if(SelShared and (not Shared)) then
+        SelNode := tvPersonal.FindPieceNode(SelectStr, 1, ';');
+
+      if (SelShared and (not Shared)) then
         Shared := TRUE;
 
-      if(Shared and (UserTemplateAccessLevel = taEditor)) then
+      if (Shared and (UserTemplateAccessLevel = taEditor)) then
       begin
         cbEditShared.Checked := TRUE;
@@ -552,10 +555,10 @@
       end
       else
-      if(not SelShared) and (assigned(SelNode)) then
-        tvPersonal.Selected := SelNode;
-      if(NewTemplate) then
+        if (not SelShared) and (assigned(SelNode)) then
+          tvPersonal.Selected := SelNode;
+      if (NewTemplate) then
       begin
         btnNewClick(frmTemplateEditor);
-        if(CopiedText <> '') then
+        if (CopiedText <> '') then
         begin
           TTemplate(FBtnNewNode.Data).Boilerplate := CopiedText;
@@ -580,18 +583,18 @@
 
 begin
-  if((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
-  begin
-    if(FCurTree = tvShared) and (FCanEditShared) then
+  if ((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  begin
+    if (FCurTree = tvShared) and (FCanEditShared) then
       ok := TRUE
     else
-    if(FCurTree = tvPersonal) and (FCanEditPersonal) then
-      ok := TRUE
-    else
-      ok := FALSE;
-    if(ok) then
+      if (FCurTree = tvPersonal) and (FCanEditPersonal) then
+        ok := TRUE
+      else
+        ok := FALSE;
+    if (ok) then
     begin
       Node := FCurTree.Selected;
       PNode := Node;
-      if(TTemplate(Node.Data).RealType = ttDoc) then
+      if (TTemplate(Node.Data).RealType = ttDoc) then
         PNode := Node.Parent;
       if CanClone(PNode) then
@@ -605,14 +608,14 @@
           else
             idx := Owner.Items.IndexOf(Node.Data) + 1;
-          if(FCurTree = tvShared) then
-            begin
-              ownr := '';
-              ACheckBox := cbShHide;
-            end
+          if (FCurTree = tvShared) then
+          begin
+            ownr := '';
+            ACheckBox := cbShHide;
+          end
           else
-            begin
-              ownr := IntToStr(User.DUZ);
-              ACheckBox := cbPerHide;
-            end;
+          begin
+            ownr := IntToStr(User.DUZ);
+            ACheckBox := cbPerHide;
+          end;
           if FImportingFromXML then
           begin
@@ -622,10 +625,10 @@
           else
           begin
-            Tmp := TTemplate.Create('0^T^A^'+NewTemplateName+'^^^'+ownr);
+            Tmp := TTemplate.Create('0^T^A^' + NewTemplateName + '^^^' + ownr);
             Tmp.BackupItems;
             Templates.AddObject(Tmp.ID, Tmp);
           end;
           btnApply.Enabled := TRUE;
-          if(idx >= Owner.Items.Count) then
+          if (idx >= Owner.Items.Count) then
             Owner.Items.Add(Tmp)
           else
@@ -633,7 +636,7 @@
           Resync([Owner]);
           Node := FCurTree.Selected;
-          if(Node.Data <> Tmp) then
+          if (Node.Data <> Tmp) then
           begin
-            if(TTemplate(Node.Data).RealType = ttDoc) then
+            if (TTemplate(Node.Data).RealType = ttDoc) then
               Node := Node.GetNextSibling
             else
@@ -645,5 +648,5 @@
           end;
           FBtnNewNode := Node;
-          if(FFirstShow) then
+          if (FFirstShow) then
             FFocusName := TRUE
           else
@@ -660,5 +663,5 @@
 procedure TfrmTemplateEditor.btnApplyClick(Sender: TObject);
 begin
-  if(ScanNames) then
+  if (ScanNames) then
   begin
     SaveAllTemplates;
@@ -671,4 +674,5 @@
 procedure TfrmTemplateEditor.FormCreate(Sender: TObject);
 begin
+  SetFormPosition(Self);
   ResizeAnchoredFormToFont(self);
   //Now fix everything the resize messed up
@@ -681,5 +685,5 @@
 
   FSavePause := Application.HintHidePause;
-  Application.HintHidePause := FSavePause*2;
+  Application.HintHidePause := FSavePause * 2;
   if InteractiveRemindersActive then
   begin
@@ -696,13 +700,13 @@
   FFirstShow := TRUE;
 
-  FTreeControl[ttShared,   tcDel]  := sbShDelete;
-  FTreeControl[ttShared,   tcUp]   := sbShUp;
-  FTreeControl[ttShared,   tcDown] := sbShDown;
-  FTreeControl[ttShared,   tcLbl]  := lblCopy;
-  FTreeControl[ttShared,   tcCopy] := sbCopyRight;
-  FTreeControl[ttPersonal, tcDel]  := sbPerDelete;
-  FTreeControl[ttPersonal, tcUp]   := sbPerUp;
+  FTreeControl[ttShared, tcDel] := sbShDelete;
+  FTreeControl[ttShared, tcUp] := sbShUp;
+  FTreeControl[ttShared, tcDown] := sbShDown;
+  FTreeControl[ttShared, tcLbl] := lblCopy;
+  FTreeControl[ttShared, tcCopy] := sbCopyRight;
+  FTreeControl[ttPersonal, tcDel] := sbPerDelete;
+  FTreeControl[ttPersonal, tcUp] := sbPerUp;
   FTreeControl[ttPersonal, tcDown] := sbPerDown;
-  FTreeControl[ttPersonal, tcLbl]  := lblCopy;
+  FTreeControl[ttPersonal, tcLbl] := lblCopy;
   FTreeControl[ttPersonal, tcCopy] := sbCopyLeft;
   dmodShared.InEditor := TRUE;
@@ -739,5 +743,5 @@
 
   BtnApply.Enabled := BackupDiffers;
-  SetFormPosition(Self);
+  //SetFormPosition(Self);
 end;
 
@@ -745,5 +749,5 @@
 begin
   sbCopyRight.Visible := FCanEditPersonal;
-  if(not FCanEditPersonal) then
+  if (not FCanEditPersonal) then
     cbPerHide.Checked := TRUE;
   cbPerHide.Visible := FCanEditPersonal;
@@ -756,5 +760,5 @@
 
 procedure TfrmTemplateEditor.cboOwnerNeedData(Sender: TObject;
-  const StartFrom: String; Direction, InsertAt: Integer);
+  const StartFrom: string; Direction, InsertAt: Integer);
 begin
 //  cboOwner.ForDataUse(SubSetOfTemplateOwners(StartFrom, Direction));
@@ -773,5 +777,5 @@
   FCurrentPersonalUser := UsrIEN;
   NewEdit := (FCurrentPersonalUser = User.DUZ);
-  if(FCanEditPersonal <> NewEdit) then
+  if (FCanEditPersonal <> NewEdit) then
   begin
     FCanEditPersonal := NewEdit;
@@ -784,6 +788,6 @@
 begin
   AllowExpansion := dmodShared.ExpandNode(tvPersonal, Node,
-                    FPersonalEmptyNodeCount, not cbPerHide.Checked);
-  if(AllowExpansion and FInternalHiddenExpand) then
+    FPersonalEmptyNodeCount, not cbPerHide.Checked);
+  if (AllowExpansion and FInternalHiddenExpand) then
     AllowExpansion := FALSE;
 end;
@@ -793,6 +797,6 @@
 begin
   AllowExpansion := dmodShared.ExpandNode(tvShared, Node,
-                    FSharedEmptyNodeCount, not cbShHide.Checked);
-  if(AllowExpansion and FInternalHiddenExpand) then
+    FSharedEmptyNodeCount, not cbShHide.Checked);
+  if (AllowExpansion and FInternalHiddenExpand) then
     AllowExpansion := FALSE;
 end;
@@ -813,5 +817,5 @@
 var
   Template: TTemplate;
-  
+
 begin
   Result := FALSE;
@@ -822,16 +826,16 @@
       Result := TRUE
     else
-    if (Template.PersonalOwner = 0) then
-    begin
-      if RootTemplate.IsLocked then
-        Result := TRUE
-      else
-      begin
-        Result := TTemplate(Node.Data).IsLocked;
-        if (not Result) and assigned(Node.Parent) and
-           (TTemplate(Node.Parent).PersonalOwner = 0) then
-          Result := IsTemplateLocked(Node.Parent);
+      if (Template.PersonalOwner = 0) then
+      begin
+        if RootTemplate.IsLocked then
+          Result := TRUE
+        else
+        begin
+          Result := TTemplate(Node.Data).IsLocked;
+          if (not Result) and assigned(Node.Parent) and
+            (TTemplate(Node.Parent).PersonalOwner = 0) then
+            Result := IsTemplateLocked(Node.Parent);
+        end;
       end;
-    end;
   end;
 end;
@@ -852,5 +856,5 @@
     if Something then
     begin
-      if(Sender = tvPersonal) then
+      if (Sender = tvPersonal) then
       begin
         ok := FCanEditPersonal;
@@ -875,5 +879,5 @@
 begin
   cbLock.Enabled := ok and (FCurTree = tvShared);
-  if(ok and Root) then
+  if (ok and Root) then
   begin
     ok := FALSE;
@@ -925,19 +929,30 @@
 begin
   tmpHeight := tvShared.Height;
-  dec(tmpHeight,lblCopy.Height);
-  if(sbCopyLeft.Visible) then
-    dec(tmpHeight, sbCopyLeft.Height+5);
-  if(sbCopyRight.Visible) then
-    dec(tmpHeight, sbCopyRight.Height+5);
+  dec(tmpHeight, lblCopy.Height);
+  if (sbCopyLeft.Visible) then
+    dec(tmpHeight, sbCopyLeft.Height + 5);
+  if (sbCopyRight.Visible) then
+    dec(tmpHeight, sbCopyRight.Height + 5);
   tmpHeight := (tmpHeight div 2) + tvShared.Top;
   lblCopy.Top := tmpHeight;
-  inc(tmpHeight,lblCopy.height+5);
-  if(sbCopyLeft.Visible) then
+  inc(tmpHeight, lblCopy.height + 5);
+  if (sbCopyLeft.Visible) then
   begin
     sbCopyLeft.Top := tmpHeight;
-    inc(tmpHeight, sbCopyLeft.Height+5);
-  end;
-  if(sbCopyRight.Visible) then
+    inc(tmpHeight, sbCopyLeft.Height + 5);
+  end;
+  if (sbCopyRight.Visible) then
     sbCopyRight.Top := tmpHeight;
+end;
+
+procedure TfrmTemplateEditor.splMainCanResize(Sender: TObject;
+  var NewSize: Integer; var Accept: Boolean);
+begin
+  inherited;
+  if (pnlGroupBP.Height - (NewSize - splMain.Top) <= pnlGroupBP.Constraints.MinHeight) and (NewSize > splMain.Top) then begin
+    //Moving Down
+    Accept := False;
+    NewSize := splMain.Top;
+  end else Accept := True; //Moving Up
 end;
 
@@ -962,4 +977,5 @@
     pnlGroupBP.Align := alBottom;
     reBoil.Align := alClient;
+    pnlNotes.Constraints.MaxHeight := 0;
   end;
 end;
@@ -977,5 +993,5 @@
   FUpdating := TRUE;
   try
-    if(assigned(Node)) then
+    if (assigned(Node)) then
     begin
       FShowingTemplate := TTemplate(Node.Data);
@@ -987,5 +1003,5 @@
         ShowTemplateType(TTemplate(Node.Data));
         lt := GetLinkType(Node);
-        if(lt = ltNone) or (IsReminderDialog and (not (lt in [ltNone, ltTitle]))) then
+        if (lt = ltNone) or (IsReminderDialog and (not (lt in [ltNone, ltTitle]))) then
           pnlLink.Visible := FALSE
         else
@@ -994,8 +1010,8 @@
           pnlLink.Tag := ord(lt);
           case lt of
-            ltTitle:     lts := 'Title';
-            ltConsult:   lts := 'Consult Service';
+            ltTitle: lts := 'Title';
+            ltConsult: lts := 'Consult Service';
             ltProcedure: lts := 'Procedure';
-            else         lts := '';
+          else lts := '';
           end;
           cbxLink.Clear;
@@ -1024,5 +1040,5 @@
         edtName.Text := PrintName;
         reNotes.Lines.Text := Description;
-        if(PersonalOwner = 0) and (FCurTree = tvShared) and (cbEditShared.Checked) then
+        if (PersonalOwner = 0) and (FCurTree = tvShared) and (cbEditShared.Checked) then
         begin
           cbLock.Checked := IsLocked;
@@ -1036,5 +1052,5 @@
         end;
         CanDoCom := FCanDoCOMObjects and (PersonalOwner = 0);
-        if(RealType in AllTemplateRootTypes) then
+        if (RealType in AllTemplateRootTypes) then
         begin
           ClearRB := TRUE;
@@ -1045,20 +1061,20 @@
           case RealType of
             ttDoc: begin
-                     if IsReminderDialog then
-                       Idx := tiRemDlg
-                     else
-                     if IsCOMObject then
-                       Idx := tiCOMObj
-                     else
-                       Idx := tiTemplate;
-                   end;
+                if IsReminderDialog then
+                  Idx := tiRemDlg
+                else
+                  if IsCOMObject then
+                    Idx := tiCOMObj
+                  else
+                    Idx := tiTemplate;
+              end;
             ttGroup: begin
-                       if(Dialog) then
-                         Idx := tiDialog
-                       else
-                         Idx := tiGroup;
-                     end;
+                if (Dialog) then
+                  Idx := tiDialog
+                else
+                  Idx := tiGroup;
+              end;
             ttClass: Idx := tiFolder;
-            else Idx := tiNone;
+          else Idx := tiNone;
           end;
           FForceContainer := ((RealType in [ttGroup, ttClass]) and (Children <> tcNone));
@@ -1071,11 +1087,11 @@
           if (not FForceContainer) then
           begin
-            if(FCanDoReminders or CanDoCOM) then
+            if (FCanDoReminders or CanDoCOM) then
               cbxType.Items.Add('Reminder Dialog');
-            if(CanDoCOM) then
+            if (CanDoCOM) then
               cbxType.Items.Add('COM Object');
           end;
           cbxType.ItemIndex := ForcedIdx[FForceContainer, Idx];
-          if(Idx = tiRemDlg) and FCanDoReminders then
+          if (Idx = tiRemDlg) and FCanDoReminders then
             cbxRemDlgs.SelectByID(ReminderDialogIEN)
           else
@@ -1098,5 +1114,5 @@
           end;
           cbActive.Checked := Active;
-          if(RealType in [ttClass, ttGroup]) then
+          if (RealType in [ttClass, ttGroup]) then
             cbHideItems.Checked := HideItems
           else
@@ -1105,7 +1121,7 @@
             cbHideItems.Enabled := FALSE;
           end;
-          if((RealType in [ttDoc, ttGroup]) and (assigned(Node.Parent)) and
-             (TTemplate(Node.Parent.Data).RealType = ttGroup) and
-             (not IsReminderDialog) and (not IsCOMObject)) then
+          if ((RealType in [ttDoc, ttGroup]) and (assigned(Node.Parent)) and
+            (TTemplate(Node.Parent.Data).RealType = ttGroup) and
+            (not IsReminderDialog) and (not IsCOMObject)) then
             cbExclude.Checked := Exclude
           else
@@ -1126,10 +1142,10 @@
             cbFirstLine.Enabled := FALSE;
           end;
-          if(RealType in [ttGroup, ttClass]) and (Children <> tcNone) and
+          if (RealType in [ttGroup, ttClass]) and (Children <> tcNone) and
             (dmodShared.InDialog(Node)) then
           begin
             cbOneItemOnly.Checked := OneItemOnly;
             cbIndent.Checked := IndentItems;
-            if(RealType = ttGroup) and (Boilerplate <> '') then
+            if (RealType = ttGroup) and (Boilerplate <> '') then
             begin
               cbHideDlgItems.Checked := HideDlgItems;
@@ -1150,5 +1166,5 @@
             cbIndent.Enabled := FALSE;
           end;
-          if(RealType = ttGroup) then
+          if (RealType = ttGroup) then
             edtGap.Text := IntToStr(Gap)
           else
@@ -1171,14 +1187,14 @@
       gbProperties.Caption := PropText;
     end;
-    if(ClearName) then
+    if (ClearName) then
     begin
       edtName.Text := '';
       reNotes.Clear;
     end;
-    if(ClearRB) then
+    if (ClearRB) then
     begin
       cbxType.ItemIndex := Ord(tiNone);
     end;
-    if(ClearAll) then
+    if (ClearAll) then
     begin
       cbActive.Checked := FALSE;
@@ -1197,9 +1213,9 @@
       pnlLink.Visible := FALSE;
     end;
-    if cbDisplayOnly.Enabled  or
-       cbFirstLine.Enabled    or
-       cbIndent.Enabled       or
-       cbOneItemOnly.Enabled  or
-       cbHideDlgItems.Enabled then
+    if cbDisplayOnly.Enabled or
+      cbFirstLine.Enabled or
+      cbIndent.Enabled or
+      cbOneItemOnly.Enabled or
+      cbHideDlgItems.Enabled then
       gbDialogProps.Font.Color := clWindowText
     else
@@ -1215,12 +1231,12 @@
 
 begin
-  if(pnlGroupBP.Visible) and (pnlGroupBP.Height > (pnlBoilerplate.Height-29)) then
-  begin
-    pnlGroupBP.Height := pnlBoilerplate.Height-29;
+  if (pnlGroupBP.Visible) and (pnlGroupBP.Height > (pnlBoilerplate.Height - 29)) then
+  begin
+    pnlGroupBP.Height := pnlBoilerplate.Height - 29;
   end;
   if cbLongLines.checked then
     Max := 240
   else
-    Max := MAX_ENTRY_WIDTH;
+    Max := MAX_ENTRY_WIDTH - 1;
   LimitEditWidth(reBoil, Max);
   LimitEditWidth(reNotes, MAX_ENTRY_WIDTH);
@@ -1234,5 +1250,5 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     if CanClone(FCurTree.Selected) then
@@ -1244,7 +1260,7 @@
         Template.PrintName := edtName.Text;
         UpdateApply(Template);
-        for i := 0 to Template.Nodes.Count-1 do
+        for i := 0 to Template.Nodes.Count - 1 do
           TTreeNode(Template.Nodes.Objects[i]).Text := Template.PrintName;
-        if(DoRefresh) then
+        if (DoRefresh) then
         begin
           tvShared.Invalidate;
@@ -1263,5 +1279,5 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     if CanClone(FCurTree.Selected) then
@@ -1273,10 +1289,10 @@
         Template.Active := cbActive.Checked;
         UpdateApply(Template);
-        for i := 0 to Template.Nodes.Count-1 do
+        for i := 0 to Template.Nodes.Count - 1 do
         begin
           Node := TTreeNode(Template.Nodes.Objects[i]);
           Node.Cut := not Template.Active;
         end;
-        if(FCurTree = tvShared) then
+        if (FCurTree = tvShared) then
         begin
           cbPerHideClick(Sender);
@@ -1304,5 +1320,5 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     if CanClone(FCurTree.Selected) then
@@ -1314,5 +1330,5 @@
         Template.Exclude := cbExclude.Checked;
         UpdateApply(Template);
-        for i := 0 to Template.Nodes.Count-1 do
+        for i := 0 to Template.Nodes.Count - 1 do
         begin
           Node := TTreeNode(Template.Nodes.Objects[i]);
@@ -1333,5 +1349,5 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     if CanClone(FCurTree.Selected) then
@@ -1344,5 +1360,5 @@
         UpdateApply(Template);
         DisplayBoilerPlate(FCurTree.Selected);
-        if(DoRefresh) then
+        if (DoRefresh) then
         begin
           tvShared.Invalidate;
@@ -1362,8 +1378,8 @@
   tvShared.HideSelection := TRUE;
   tvPersonal.HideSelection := TRUE;
-  if(NewTree <> FCurTree) then
+  if (NewTree <> FCurTree) then
   begin
     Result := TRUE;
-    if(assigned(FCurTree)) then
+    if (assigned(FCurTree)) then
     begin
       for i := low(TTemplateTreeControl) to high(TTemplateTreeControl) do
@@ -1372,12 +1388,12 @@
     FCurTree := NewTree;
   end;
-  if(assigned(FCurTree)) then
+  if (assigned(FCurTree)) then
   begin
     FCurTree.HideSelection := FALSE;
-    if(FCurTree = tvPersonal) and (Screen.ActiveControl = tvShared) then
+    if (FCurTree = tvPersonal) and (Screen.ActiveControl = tvShared) then
       tvPersonal.SetFocus
     else
-    if(FCurTree = tvShared) and (Screen.ActiveControl = tvPersonal) then
-      tvShared.SetFocus;
+      if (FCurTree = tvShared) and (Screen.ActiveControl = tvPersonal) then
+        tvShared.SetFocus;
   end;
 end;
@@ -1385,10 +1401,10 @@
 procedure TfrmTemplateEditor.tvTreeEnter(Sender: TObject);
 begin
-  if((Sender is TTreeView) and (ChangeTree(TTreeView(Sender)))) then
+  if ((Sender is TTreeView) and (ChangeTree(TTreeView(Sender)))) then
     tvTreeChange(Sender, TTreeView(Sender).Selected);
 end;
 
 procedure TfrmTemplateEditor.tvTreeNodeEdited(Sender: TObject;
-  Node: TTreeNode; var S: String);
+  Node: TTreeNode; var S: string);
 begin
   FUpdating := TRUE;
@@ -1419,5 +1435,5 @@
 begin
   dmodShared.Resync(tvPersonal.Items.GetFirstNode, not cbPerHide.Checked,
-                                                          FPersonalEmptyNodeCount);
+    FPersonalEmptyNodeCount);
   tvTreeChange(tvPersonal, tvPersonal.Selected);
   EnableNavControls;
@@ -1440,9 +1456,9 @@
     with Node, TTemplate(Node.Data) do
     begin
-      if(RealType in [ttDoc, ttGroup]) then
+      if (RealType in [ttDoc, ttGroup]) then
       begin
         TmpSL := TStringList.Create;
         try
-          if(RealType = ttGroup) and (not reBoil.ReadOnly) then
+          if (RealType = ttGroup) and (not reBoil.ReadOnly) then
           begin
             ItemOK := TRUE;
@@ -1454,5 +1470,5 @@
             TmpSL.Text := FullBoilerplate;
           LongLines := FALSE;
-          for i := 0 to TmpSL.Count-1 do
+          for i := 0 to TmpSL.Count - 1 do
           begin
             if length(TmpSL[i]) > MAX_ENTRY_WIDTH then
@@ -1475,5 +1491,5 @@
       end;
       ShowGroupBoilerplate(ItemOK);
-      if(not ItemOK) and (IsReminderDialog or IsCOMObject) then
+      if (not ItemOK) and (IsReminderDialog or IsCOMObject) then
         BPOK := FALSE;
       pnlBoilerplateResize(Self);
@@ -1491,10 +1507,10 @@
   KillObj(@FConsultServices);
   Application.HintHidePause := FSavePause;
-  if(assigned(frmTemplateObjects)) then
+  if (assigned(frmTemplateObjects)) then
   begin
     frmTemplateObjects.Free;
     frmTemplateObjects := nil;
   end;
-  if(assigned(frmTemplateFields)) then
+  if (assigned(frmTemplateFields)) then
   begin
     frmTemplateFields.Free;
@@ -1527,6 +1543,6 @@
 
 begin
-  if((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
-                              (assigned(FCurTree.Selected.Parent))) then
+  if ((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
+    (assigned(FCurTree.Selected.Parent))) then
   begin
     Node := FCurTree.Selected;
@@ -1536,12 +1552,12 @@
     idx := Template.Items.IndexOf(NodeTemplate);
     ChangeLevel := (idx < 1);
-    if(not ChangeLevel) then
-    begin
-      if(TTemplateTreeType(TBitBtn(Sender).Tag) = ttShared) then
+    if (not ChangeLevel) then
+    begin
+      if (TTemplateTreeType(TBitBtn(Sender).Tag) = ttShared) then
         Hide := cbShHide.Checked
       else
         Hide := cbPerHide.Checked;
       First := TRUE;
-      while(idx > 0) do
+      while (idx > 0) do
       begin
         if First then
@@ -1551,5 +1567,5 @@
           if CanClone(ParentNode) then
           begin
-            if(Clone(ParentNode)) then
+            if (Clone(ParentNode)) then
               Template := TTemplate(ParentNode.Data);
             if Template.CanModify then
@@ -1561,6 +1577,6 @@
         if ok then
         begin
-          Template.Items.Exchange(idx-1, idx);
-          if(Hide and (not TTemplate(Template.Items[idx]).Active)) then
+          Template.Items.Exchange(idx - 1, idx);
+          if (Hide and (not TTemplate(Template.Items[idx]).Active)) then
           begin
             dec(idx);
@@ -1574,11 +1590,11 @@
       end;
     end;
-    if(ChangeLevel) then
+    if (ChangeLevel) then
     begin
       ParentsParent := ParentNode.Parent;
-      if(assigned(ParentsParent)) then
+      if (assigned(ParentsParent)) then
       begin
         ParentTemplate := TTemplate(ParentsParent.Data);
-        if(ParentTemplate.Items.IndexOf(NodeTemplate) >= 0) then
+        if (ParentTemplate.Items.IndexOf(NodeTemplate) >= 0) then
           InfoBox(ParentsParent.Text + ' already contains the ' +
             NodeTemplate.PrintName + ' template.',
@@ -1588,9 +1604,9 @@
           if CanClone(ParentNode) then
           begin
-            if(Clone(ParentNode)) then
+            if (Clone(ParentNode)) then
               Template := TTemplate(ParentNode.Data);
             if Template.CanModify and CanClone(ParentsParent) then
             begin
-              if(Clone(ParentsParent)) then
+              if (Clone(ParentsParent)) then
                 ParentTemplate := TTemplate(ParentsParent.Data);
               if ParentTemplate.CanModify then
@@ -1598,5 +1614,5 @@
                 Template.Items.Delete(idx);
                 idx := ParentTemplate.Items.IndexOf(Template);
-                if(idx >= 0) then
+                if (idx >= 0) then
                 begin
                   ParentTemplate.Items.Insert(idx, NodeTemplate);
@@ -1627,6 +1643,6 @@
 
 begin
-  if((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
-                              (assigned(FCurTree.Selected.Parent))) then
+  if ((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
+    (assigned(FCurTree.Selected.Parent))) then
   begin
     Node := FCurTree.Selected;
@@ -1635,14 +1651,14 @@
     Template := TTemplate(ParentNode.Data);
     idx := Template.Items.IndexOf(NodeTemplate);
-    max := Template.Items.Count-1;
+    max := Template.Items.Count - 1;
     ChangeLevel := (idx >= max);
-    if(not ChangeLevel) then
-    begin
-      if(TTemplateTreeType(TBitBtn(Sender).Tag) = ttShared) then
+    if (not ChangeLevel) then
+    begin
+      if (TTemplateTreeType(TBitBtn(Sender).Tag) = ttShared) then
         Hide := cbShHide.Checked
       else
         Hide := cbPerHide.Checked;
       First := TRUE;
-      while(idx < max) do
+      while (idx < max) do
       begin
         if First then
@@ -1652,5 +1668,5 @@
           if CanClone(ParentNode) then
           begin
-            if(Clone(ParentNode)) then
+            if (Clone(ParentNode)) then
               Template := TTemplate(ParentNode.Data);
             if Template.CanModify then
@@ -1662,6 +1678,6 @@
         if ok then
         begin
-          Template.Items.Exchange(idx, idx+1);
-          if(Hide and (not TTemplate(Template.Items[idx]).Active)) then
+          Template.Items.Exchange(idx, idx + 1);
+          if (Hide and (not TTemplate(Template.Items[idx]).Active)) then
           begin
             inc(idx);
@@ -1675,11 +1691,11 @@
       end;
     end;
-    if(ChangeLevel) then
+    if (ChangeLevel) then
     begin
       ParentsParent := ParentNode.Parent;
-      if(assigned(ParentsParent)) then
+      if (assigned(ParentsParent)) then
       begin
         ParentTemplate := TTemplate(ParentsParent.Data);
-        if(ParentTemplate.Items.IndexOf(NodeTemplate) >= 0) then
+        if (ParentTemplate.Items.IndexOf(NodeTemplate) >= 0) then
           InfoBox(ParentsParent.Text + ' already contains the ' +
             NodeTemplate.PrintName + ' template.',
@@ -1689,9 +1705,9 @@
           if CanClone(ParentNode) then
           begin
-            if(Clone(ParentNode)) then
+            if (Clone(ParentNode)) then
               Template := TTemplate(ParentNode.Data);
             if Template.CanModify and CanClone(ParentsParent) then
             begin
-              if(Clone(ParentsParent)) then
+              if (Clone(ParentsParent)) then
                 ParentTemplate := TTemplate(ParentsParent.Data);
               if ParentTemplate.CanModify then
@@ -1699,10 +1715,10 @@
                 Template.Items.Delete(idx);
                 idx := ParentTemplate.Items.IndexOf(Template);
-                if(idx >= 0) then
+                if (idx >= 0) then
                 begin
-                  if(idx = (ParentTemplate.Items.Count-1)) then
+                  if (idx = (ParentTemplate.Items.Count - 1)) then
                     ParentTemplate.Items.Add(NodeTemplate)
                   else
-                    ParentTemplate.Items.Insert(idx+1, NodeTemplate);
+                    ParentTemplate.Items.Insert(idx + 1, NodeTemplate);
                   Resync([ParentTemplate, Template]);
                   btnApply.Enabled := TRUE;
@@ -1730,6 +1746,6 @@
 
 begin
-  if((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
-                              (assigned(FCurTree.Selected.Parent))) then
+  if ((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
+    (assigned(FCurTree.Selected.Parent))) then
   begin
     Node := FCurTree.Selected;
@@ -1737,27 +1753,27 @@
     PNode := Node.Parent;
     Parent := TTemplate(PNode.Data);
-    if(AutoDel(Template)) then
+    if (AutoDel(Template)) then
       DoIt := TRUE
     else
-    if(Template.Active) and (cbActive.Checked) then
-    begin
-      DoIt := FALSE;
-      Answer := MessageDlg('Once you delete a template you may not be able to retrieve it.' + CRLF +
-                           'Rather than deleting, you may want to inactivate a template instead.' + CRLF +
-                           'You may inactivate this template by pressing the Ignore button now.' + CRLF +
-                           'Are you sure you want to delete the "' + Node.Text +
-                           '" Template?', mtConfirmation, [mbYes, mbNo, mbIgnore], 0);
-      if(Answer = mrYes) then
-        DoIt := TRUE
+      if (Template.Active) and (cbActive.Checked) then
+      begin
+        DoIt := FALSE;
+        Answer := MessageDlg('Once you delete a template you may not be able to retrieve it.' + CRLF +
+          'Rather than deleting, you may want to inactivate a template instead.' + CRLF +
+          'You may inactivate this template by pressing the Ignore button now.' + CRLF +
+          'Are you sure you want to delete the "' + Node.Text +
+          '" Template?', mtConfirmation, [mbYes, mbNo, mbIgnore], 0);
+        if (Answer = mrYes) then
+          DoIt := TRUE
+        else
+          if (Answer = mrIgnore) then
+            cbActive.Checked := FALSE;
+      end
       else
-      if(Answer = mrIgnore) then
-        cbActive.Checked := FALSE;
-    end
-    else
-      DoIt := InfoBox('Are you sure you want to delete the "' + Node.Text +
-              '" Template?', 'Confirmation', MB_YESNO or MB_ICONQUESTION) = IDYES;
-    if(DoIt and CanClone(PNode)) then
-    begin
-      if(Clone(PNode)) then
+        DoIt := InfoBox('Are you sure you want to delete the "' + Node.Text +
+          '" Template?', 'Confirmation', MB_YESNO or MB_ICONQUESTION) = IDYES;
+    if (DoIt and CanClone(PNode)) then
+    begin
+      if (Clone(PNode)) then
         Parent := TTemplate(PNode.Data);
       if assigned(Parent) and Parent.CanModify then
@@ -1782,18 +1798,18 @@
   OldActiveControl: TControl;
 begin
-  if(Assigned(FCurTree)) then
+  if (Assigned(FCurTree)) then
   begin
     Tree := TTemplateTreeType(FCurTree.Tag);
     Node := FCurTree.Selected;
-    if(Assigned(Node)) then
+    if (Assigned(Node)) then
       Curok := (TTemplate(Node.Data).RealType in [ttDoc, ttGroup, ttClass])
     else
       Curok := FALSE;
-    if(Curok) then
+    if (Curok) then
     begin
       OldActiveControl := ActiveControl;
       FTreeControl[Tree, tcDel].Enabled := TRUE;
       AllowSet := FALSE;
-      if(Node.Index > 0) then
+      if (Node.Index > 0) then
         AllowUp := TRUE
       else
@@ -1804,9 +1820,9 @@
       FTreeControl[Tree, tcUp].Enabled := AllowUp;
       AllowDown := AllowUp;
-      if(Node.Index < (Node.Parent.Count-1)) then
+      if (Node.Index < (Node.Parent.Count - 1)) then
         AllowDown := TRUE
       else
       begin
-        if(not AllowSet) then
+        if (not AllowSet) then
           AllowDown := AllowMove(Node.Parent.Parent, Node);
       end;
@@ -1816,14 +1832,14 @@
       if not AllowDown and (OldActiveControl = FTreeControl[Tree, tcDown]) then
         (FTreeControl[Tree, tcUp] as TWinControl).SetFocus;
-      FTreeControl[Tree, tcCopy].Enabled := FTreeControl[TTemplateTreeType(1-ord(Tree)), tcDel].Visible;
-      if(FTreeControl[Tree, tcCopy].Enabled) then
-      begin
-        if(Tree = ttShared) then
+      FTreeControl[Tree, tcCopy].Enabled := FTreeControl[TTemplateTreeType(1 - ord(Tree)), tcDel].Visible;
+      if (FTreeControl[Tree, tcCopy].Enabled) then
+      begin
+        if (Tree = ttShared) then
           Node := tvPersonal.Selected
         else
           Node := tvShared.Selected;
-        if(assigned(Node)) then
+        if (assigned(Node)) then
         begin
-          if(TTemplate(Node.Data).RealType = ttDoc) then
+          if (TTemplate(Node.Data).RealType = ttDoc) then
             Node := Node.Parent;
           FTreeControl[Tree, tcCopy].Enabled := AllowMove(Node, FCurTree.Selected);
@@ -1839,11 +1855,11 @@
         FTreeControl[Tree, i].Enabled := FALSE;
     end;
-    if(FCurTree = tvShared) and (FCanEditShared) then
+    if (FCurTree = tvShared) and (FCanEditShared) then
       btnNew.Enabled := TRUE
     else
-    if(FCurTree = tvPersonal) and (FCanEditPersonal) then
-      btnNew.Enabled := TRUE
-    else
-      btnNew.Enabled := FALSE;
+      if (FCurTree = tvPersonal) and (FCanEditPersonal) then
+        btnNew.Enabled := TRUE
+      else
+        btnNew.Enabled := FALSE;
   end
   else
@@ -1856,5 +1872,5 @@
 begin
   CanDrag := (TTemplate(Node.Data).RealType in [ttDoc, ttGroup, ttClass]);
-  if(CanDrag) then
+  if (CanDrag) then
     FDragNode := Node
   else
@@ -1871,15 +1887,15 @@
   FDropNode := nil;
   Accept := FALSE;
-  if(Source is TTreeView) and (assigned(FDragNode)) then
+  if (Source is TTreeView) and (assigned(FDragNode)) then
   begin
     Tree := TTreeView(Sender);
-    FDropNode := Tree.GetNodeAt(X,Y);
-    if(((Tree = tvShared)   and (FCanEditShared)) or
-       ((Tree = tvPersonal) and (FCanEditPersonal))) then
-    begin
-      if(assigned(FDropNode)) then
+    FDropNode := Tree.GetNodeAt(X, Y);
+    if (((Tree = tvShared) and (FCanEditShared)) or
+      ((Tree = tvPersonal) and (FCanEditPersonal))) then
+    begin
+      if (assigned(FDropNode)) then
       begin
         FDropInto := (TTemplate(FDropNode.Data).RealType in AllTemplateFolderTypes);
-        if(FDropInto) then
+        if (FDropInto) then
           TmpNode := FDropNode
         else
@@ -1899,8 +1915,8 @@
 
 begin
-  if(assigned(FDragNode)) and (assigned(FDropNode)) and (FDragNode <> FDropNode) then
+  if (assigned(FDragNode)) and (assigned(FDropNode)) and (FDragNode <> FDropNode) then
   begin
     Item := TTemplate(FDragNode.Data);
-    if(FDropInto) then
+    if (FDropInto) then
     begin
       TmpNode := FDropNode;
@@ -1912,8 +1928,8 @@
       idx := TTemplate(FDropNode.Parent.Data).Items.IndexOf(FDropNode.Data);
     end;
-    if(AllowMove(TmpNode, FDragNode) and (idx >= 0)) then
+    if (AllowMove(TmpNode, FDragNode) and (idx >= 0)) then
     begin
       Template := TTemplate(TmpNode.Data);
-      if(Template <> FDragNode.Parent.Data) and
+      if (Template <> FDragNode.Parent.Data) and
         (Template.Items.IndexOf(Item) >= 0) then
         InfoBox(Template.PrintName + ' already contains the ' +
@@ -1926,19 +1942,19 @@
         if CanClone(TmpNode) then
         begin
-          if(Clone(TmpNode)) then
+          if (Clone(TmpNode)) then
             Template := TTemplate(TmpNode.Data);
           if assigned(Template) and Template.CanModify then
           begin
-            if(Sidx >= 0) and (FDragNode.TreeView = FDropNode.TreeView) and
+            if (Sidx >= 0) and (FDragNode.TreeView = FDropNode.TreeView) and
               (not FCopying) then // if same tree delete source
             begin
               if CanClone(FDragNode.Parent) then
               begin
-                if(Clone(FDragNode.Parent)) then
+                if (Clone(FDragNode.Parent)) then
                   Src := TTemplate(FDragNode.Parent.Data);
                 if assigned(Src) and Src.CanModify then
                 begin
                   Src.Items.Delete(Sidx);
-                  if(Template = Src) then
+                  if (Template = Src) then
                     Src := nil;
                 end
@@ -1951,8 +1967,8 @@
             else
               Src := nil;
-            if(idx > 0) then
+            if (idx > 0) then
               idx := TTemplate(FDropNode.Parent.Data).Items.IndexOf(FDropNode.Data);
             Template.Items.Insert(idx, Item);
-            if(TTreeView(FDropNode.TreeView) = tvShared) then
+            if (TTreeView(FDropNode.TreeView) = tvShared) then
             begin
               Item.PersonalOwner := 0;
@@ -1987,15 +2003,15 @@
       begin
         tmpl := Templates[i];
-        if(assigned(tmpl)) then
+        if (assigned(tmpl)) then
         begin
-          for j := 0 to tmpl.Nodes.Count-1 do
+          for j := 0 to tmpl.Nodes.Count - 1 do
           begin
             Node := TTreeNode(tmpl.Nodes.Objects[j]);
-            if(NodeList.IndexOfObject(Node) < 0) then
+            if (NodeList.IndexOfObject(Node) < 0) then
             begin
               NodeID := IntToStr(Node.Level);
-              NodeID := copy('000',1,4-length(NodeID))+NodeID+U+tmpl.Nodes[j];
-              TemplateList.AddObject(NodeID,tmpl);
-              NodeList.AddObject(NodeId,Node);
+              NodeID := copy('000', 1, 4 - length(NodeID)) + NodeID + U + tmpl.Nodes[j];
+              TemplateList.AddObject(NodeID, tmpl);
+              NodeList.AddObject(NodeId, Node);
             end;
           end;
@@ -2008,20 +2024,20 @@
       NodeList.Sort;
 
-      for i := 0 to NodeList.Count-1 do
+      for i := 0 to NodeList.Count - 1 do
       begin
         NodeID := NodeList[i];
         Node := TTreeNode(NodeList.Objects[i]);
         j := TemplateList.IndexOf(NodeID);
-        if(j >= 0) then
+        if (j >= 0) then
         begin
           tmpl := TTemplate(TemplateList.Objects[j]);
-          NodeID := Piece(NodeID,U,2);
-          if(tmpl.Nodes.IndexOf(NodeID) >= 0) then
+          NodeID := Piece(NodeID, U, 2);
+          if (tmpl.Nodes.IndexOf(NodeID) >= 0) then
           begin
-            if(Node.TreeView = tvShared) then
+            if (Node.TreeView = tvShared) then
               dmodShared.Resync(Node, not cbShHide.Checked, FSharedEmptyNodeCount)
             else
-            if(Node.TreeView = tvPersonal) then
-              dmodShared.Resync(Node, not cbPerHide.Checked, FPersonalEmptyNodeCount);
+              if (Node.TreeView = tvPersonal) then
+                dmodShared.Resync(Node, not cbPerHide.Checked, FPersonalEmptyNodeCount);
           end;
         end;
@@ -2034,5 +2050,5 @@
   end;
   EnableNavControls;
-  if((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
     tvTreeChange(FCurTree, FCurTree.Selected)
   else
@@ -2043,12 +2059,12 @@
 procedure TfrmTemplateEditor.sbCopyLeftClick(Sender: TObject);
 begin
-  if(assigned(tvPersonal.Selected)) then
-  begin
-    if(not assigned(tvShared.Selected)) then
+  if (assigned(tvPersonal.Selected)) then
+  begin
+    if (not assigned(tvShared.Selected)) then
       tvShared.Selected := tvShared.Items.GetFirstNode;
     FDragNode := tvPersonal.Selected;
     FDropNode := tvShared.Selected;
     FDropInto := (TTemplate(FDropNode.Data).RealType in AllTemplateFolderTypes);
-    tvTreeDragDrop(tvPersonal, tvShared, 0,0);
+    tvTreeDragDrop(tvPersonal, tvShared, 0, 0);
   end;
 end;
@@ -2056,12 +2072,12 @@
 procedure TfrmTemplateEditor.sbCopyRightClick(Sender: TObject);
 begin
-  if(assigned(tvShared.Selected)) then
-  begin
-    if(not assigned(tvPersonal.Selected)) then
+  if (assigned(tvShared.Selected)) then
+  begin
+    if (not assigned(tvPersonal.Selected)) then
       tvPersonal.Selected := tvPersonal.Items.GetFirstNode;
     FDragNode := tvShared.Selected;
     FDropNode := tvPersonal.Selected;
     FDropInto := (TTemplate(FDropNode.Data).RealType in AllTemplateFolderTypes);
-    tvTreeDragDrop(tvShared, tvPersonal, 0,0);
+    tvTreeDragDrop(tvShared, tvPersonal, 0, 0);
   end;
 end;
@@ -2102,5 +2118,5 @@
 
 begin
-  if(assigned(ADropNode) and assigned(ADragNode)) then
+  if (assigned(ADropNode) and assigned(ADragNode)) then
   begin
     DropTemplate := TTemplate(ADropNode.Data);
@@ -2110,29 +2126,29 @@
     else
       Result := (DragTemplate.RealType in [ttDoc, ttGroup, ttClass]);
-    if(Result) then
-    begin
-      if(FCopying) then
-      begin
-        if(DropTemplate.Items.IndexOf(DragTemplate) >= 0) then
+    if (Result) then
+    begin
+      if (FCopying) then
+      begin
+        if (DropTemplate.Items.IndexOf(DragTemplate) >= 0) then
           Result := FALSE;
       end
       else
-      if((assigned(ADragNode.Parent)) and (ADropNode <> ADragNode.Parent) and
-         (DropTemplate.Items.IndexOf(DragTemplate) >= 0)) then
-        Result := FALSE;
-    end;
-    if(Result) then
-    begin
-      for i := 0 to DropTemplate.Nodes.Count-1 do
+        if ((assigned(ADragNode.Parent)) and (ADropNode <> ADragNode.Parent) and
+          (DropTemplate.Items.IndexOf(DragTemplate) >= 0)) then
+          Result := FALSE;
+    end;
+    if (Result) then
+    begin
+      for i := 0 to DropTemplate.Nodes.Count - 1 do
       begin
         TmpNode := TTreeNode(DropTemplate.Nodes.Objects[i]);
         while (Result and (assigned(TmpNode.Parent))) do
         begin
-          if(TmpNode.Data = DragTemplate) then
+          if (TmpNode.Data = DragTemplate) then
             Result := FALSE
           else
             TmpNode := TmpNode.Parent;
         end;
-        if(not Result) then break;
+        if (not Result) then break;
       end;
     end;
@@ -2151,8 +2167,8 @@
 begin
   Result := FALSE;
-  if((assigned(Node)) and (TTreeView(Node.TreeView) = tvPersonal)) then
+  if ((assigned(Node)) and (TTreeView(Node.TreeView) = tvPersonal)) then
   begin
     OldT := TTemplate(Node.Data);
-    if(OldT.PersonalOwner <> User.DUZ) then
+    if (OldT.PersonalOwner <> User.DUZ) then
     begin
       PNode := Node.Parent;
@@ -2179,8 +2195,8 @@
         Node.Data := NewT;
         NewT.AddNode(Node);
-        if(assigned(Prnt)) then
+        if (assigned(Prnt)) then
         begin
           idx := Prnt.Items.IndexOf(OldT);
-          if(idx >= 0) then
+          if (idx >= 0) then
             Prnt.Items[idx] := NewT;
         end;
@@ -2200,32 +2216,32 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     Template := TTemplate(FCurTree.Selected.Data);
     TmpBPlate := reBoil.Lines.Text;
-    if(Template.Boilerplate <> TmpBPlate) then
+    if (Template.Boilerplate <> TmpBPlate) then
     begin
       if CanClone(FCurTree.Selected) then
       begin
         DoRefresh := Clone(FCurTree.Selected);
-        if(DoRefresh) then
+        if (DoRefresh) then
           Template := TTemplate(FCurTree.Selected.Data);
         if assigned(Template) and Template.CanModify then
         begin
           DoInfo := FALSE;
-          if(Template.Boilerplate = '') or (TmpBPlate = '') then
+          if (Template.Boilerplate = '') or (TmpBPlate = '') then
             DoInfo := TRUE;
           Template.Boilerplate := TmpBPlate;
           TTemplate(FCurTree.Selected.Data).Gap := StrToIntDef(edtGap.Text, 0);
-          if(Template.RealType = ttGroup) then
+          if (Template.RealType = ttGroup) then
           begin
             reGroupBP.Text := Template.FullBoilerplate;
           end;
-          if(DoRefresh) then
+          if (DoRefresh) then
           begin
             tvShared.Invalidate;
             tvPersonal.Invalidate;
           end;
-          if(DoInfo) then
+          if (DoInfo) then
           begin
             x := reBoil.SelStart;
@@ -2251,5 +2267,5 @@
 {$ENDIF}
   sbCopyLeft.Visible := FCanEditShared;
-  if(not FCanEditShared) then
+  if (not FCanEditShared) then
     cbShHide.Checked := TRUE;
   cbShHide.Visible := FCanEditShared;
@@ -2283,5 +2299,5 @@
   Tree.Selected := Node; // This line prevents selected from changing after menu closes
   mnuCollapseTree.Enabled := dmodShared.NeedsCollapsing(Tree);
-  if(Tree = tvShared) then
+  if (Tree = tvShared) then
   begin
     Txt := 'Shared';
@@ -2296,13 +2312,13 @@
   end;
   mnuFindTemplates.Checked := FindOn;
-  mnuCollapseTree.Caption := 'Collapse '+Txt+' &Tree';
-  mnuFindTemplates.Caption := '&Find '+Txt+' Templates';
-
-  if(assigned(Tree) and assigned(Tree.Selected) and assigned(Tree.Selected.Data)) then
+  mnuCollapseTree.Caption := 'Collapse ' + Txt + ' &Tree';
+  mnuFindTemplates.Caption := '&Find ' + Txt + ' Templates';
+
+  if (assigned(Tree) and assigned(Tree.Selected) and assigned(Tree.Selected.Data)) then
   begin
     mnuNodeCopy.Enabled := (TTemplate(Tree.Selected.Data).RealType in [ttDoc, ttGroup, ttClass]);
     mnuNodeSort.Enabled := (TTemplate(Tree.Selected.Data).RealType in AllTemplateFolderTypes) and
-                           (Tree.Selected.HasChildren) and
-                           (Tree.Selected.GetFirstChild.GetNextSibling <> nil);
+      (Tree.Selected.HasChildren) and
+      (Tree.Selected.GetFirstChild.GetNextSibling <> nil);
   end
   else
@@ -2319,5 +2335,5 @@
 procedure TfrmTemplateEditor.mnuCollapseTreeClick(Sender: TObject);
 begin
-  if(GetTree = tvShared) then
+  if (GetTree = tvShared) then
   begin
     tvShared.Selected := tvShared.Items.GetFirstNode;
@@ -2337,9 +2353,9 @@
 begin
   Tree := GetTree;
-  if(Tree = tvShared) then
+  if (Tree = tvShared) then
   begin
     FFindShOn := not FFindShOn;
     pnlShSearch.Visible := FFindShOn;
-    if(FFindShOn) then
+    if (FFindShOn) then
     begin
       edtShSearch.SetFocus;
@@ -2351,5 +2367,5 @@
     FFindPerOn := not FFindPerOn;
     pnlPerSearch.Visible := FFindPerOn;
-    if(FFindPerOn) then
+    if (FFindPerOn) then
     begin
       edtPerSearch.SetFocus;
@@ -2362,5 +2378,5 @@
 procedure TfrmTemplateEditor.ShowTemplateType(Template: TTemplate);
 begin
-  if(Template.PersonalOwner > 0) then
+  if (Template.PersonalOwner > 0) then
     gbProperties.Caption := 'Personal'
   else
@@ -2371,9 +2387,9 @@
 function TfrmTemplateEditor.GetTree: TTreeView;
 begin
-  if(FFromMainMenu) then
+  if (FFromMainMenu) then
     Result := FMainMenuTree
   else
   begin
-    if(TTemplateTreeType(PopupComponent(popTemplates, popTemplates).Tag) = ttShared) then
+    if (TTemplateTreeType(PopupComponent(popTemplates, popTemplates).Tag) = ttShared) then
       Result := tvShared
     else
@@ -2395,5 +2411,5 @@
 
 begin
-  if(TTemplateTreeType(TButton(Sender).Tag) = ttShared) then
+  if (TTemplateTreeType(TButton(Sender).Tag) = ttShared) then
   begin
     Tree := tvShared;
@@ -2413,9 +2429,9 @@
     LastFoundNode := FLastFoundPerNode;
   end;
-  if(edtSearch.text <> '') then
-  begin
-    IsNext := ((FindNext) and assigned (LastFoundNode));
+  if (edtSearch.text <> '') then
+  begin
+    IsNext := ((FindNext) and assigned(LastFoundNode));
     if IsNext then
-    
+
       TmpNode := LastFoundNode
     else
@@ -2424,5 +2440,5 @@
     try
       Found := FindTemplate(edtSearch.Text, Tree, Self, TmpNode,
-                            IsNext, not FindCase, FindWholeWords);
+        IsNext, not FindCase, FindWholeWords);
     finally
       FInternalHiddenExpand := FALSE;
@@ -2431,5 +2447,5 @@
     begin
       Tree.Selected := Found;
-      if(Tree = tvShared) then
+      if (Tree = tvShared) then
         FLastFoundShNode := Found
       else
@@ -2443,5 +2459,5 @@
 procedure TfrmTemplateEditor.edtSearchChange(Sender: TObject);
 begin
-  if(TTemplateTreeType(TEdit(Sender).Tag) = ttShared) then
+  if (TTemplateTreeType(TEdit(Sender).Tag) = ttShared) then
   begin
     btnShFind.Enabled := (edtShSearch.Text <> '');
@@ -2457,10 +2473,10 @@
 procedure TfrmTemplateEditor.SetFindNext(const Tree: TTreeView; const Value: boolean);
 begin
-  if(Tree = tvShared) then
-  begin
-    if(FFindShNext <> Value) then
+  if (Tree = tvShared) then
+  begin
+    if (FFindShNext <> Value) then
     begin
       FFindShNext := Value;
-      if(FFindShNext) then btnShFind.Caption := 'Find Next'
+      if (FFindShNext) then btnShFind.Caption := 'Find Next'
       else btnShFind.Caption := 'Find';
     end;
@@ -2468,8 +2484,8 @@
   else
   begin
-    if(FFindPerNext <> Value) then
+    if (FFindPerNext <> Value) then
     begin
       FFindPerNext := Value;
-      if(FFindPerNext) then btnPerFind.Caption := 'Find Next'
+      if (FFindPerNext) then btnPerFind.Caption := 'Find Next'
       else btnPerFind.Caption := 'Find';
     end;
@@ -2499,7 +2515,7 @@
 procedure TfrmTemplateEditor.btnOKClick(Sender: TObject);
 begin
-  if(ScanNames) then
-  begin
-    if(SaveAllTemplates) then
+  if (ScanNames) then
+  begin
+    if (SaveAllTemplates) then
     begin
       FOK2Close := TRUE;
@@ -2513,9 +2529,9 @@
 procedure TfrmTemplateEditor.FormShow(Sender: TObject);
 begin
-  if(FFirstShow) then
+  if (FFirstShow) then
   begin
     FUpdating := FALSE;
     FFirstShow := FALSE;
-    if(FFocusName) then
+    if (FFocusName) then
     begin
       edtName.SetFocus;
@@ -2534,5 +2550,5 @@
 
 begin
-  if(not assigned(frmTemplateObjects)) then
+  if (not assigned(frmTemplateObjects)) then
   begin
     dmodShared.LoadTIUObjects;
@@ -2542,9 +2558,9 @@
     begin
       UpdatePersonalObjects;
-      if uPersonalObjects.Count > 0 then                                                  // -------- CQ #8665 - RV ------------
+      if uPersonalObjects.Count > 0 then // -------- CQ #8665 - RV ------------
       begin
         DoIt := FALSE;
-        for i := 0 to dmodShared.TIUObjects.Count-1 do
-          if uPersonalObjects.IndexOf(Piece(dmodShared.TIUObjects[i],U,2)) >= 0 then      // -------- CQ #8665 - RV ------------
+        for i := 0 to dmodShared.TIUObjects.Count - 1 do
+          if uPersonalObjects.IndexOf(Piece(dmodShared.TIUObjects[i], U, 2)) >= 0 then // -------- CQ #8665 - RV ------------
             frmTemplateObjects.cboObjects.Items.Add(dmodShared.TIUObjects[i]);
       end;
@@ -2562,16 +2578,16 @@
 begin
   FBPOK := FALSE;
-  if(reBoil.Lines.Count > 0) then
-  begin
-    if(dmodShared.TemplateOK(FCurTree.Selected.Data,'OK')) then
+  if (reBoil.Lines.Count > 0) then
+  begin
+    if (dmodShared.TemplateOK(FCurTree.Selected.Data, 'OK')) then
     begin
       TestBoilerplate(reBoil.Lines);
-      if(RPCBrokerV.Results.Count > 0) then
-        InfoBox('Boilerplate Contains Errors:'+CRLF+CRLF+
+      if (RPCBrokerV.Results.Count > 0) then
+        InfoBox('Boilerplate Contains Errors:' + CRLF + CRLF +
           RPCBrokerV.Results.Text, 'Error', MB_OK or MB_ICONERROR)
       else
       begin
         FBPOK := TRUE;
-        if(assigned(Sender)) then
+        if (assigned(Sender)) then
           InfoBox('No Errors Found in Boilerplate.', 'Information', MB_OK or MB_ICONINFORMATION);
       end;
@@ -2590,5 +2606,5 @@
 
   mnuBPPaste.Enabled := (ok and Clipboard.HasFormat(CF_TEXT));
-  if(ok) then
+  if (ok) then
     ok := (reBoil.Lines.Count > 0);
   tryOK := (reBoil.Lines.Count > 0) or ((pnlGroupBP.Visible) and (reGroupBP.Lines.Count > 0));
@@ -2616,7 +2632,7 @@
     while (assigned(Node)) do
     begin
-      if(Node.Text <> EmptyNodeText) and (assigned(Node.Data)) then
-      begin
-        if(BadTemplateName(Node.Text)) then
+      if (Node.Text <> EmptyNodeText) and (assigned(Node.Data)) then
+      begin
+        if (BadTemplateName(Node.Text)) then
           Errors.Add(Node);
       end;
@@ -2630,14 +2646,14 @@
     ScanTree(tvShared);
     ScanTree(tvPersonal);
-    if(Errors.Count > 0) then
-    begin
-      if(Errors.Count > 1) then
+    if (Errors.Count > 0) then
+    begin
+      if (Errors.Count > 1) then
         msg := IntToStr(Errors.Count) + ' Templates have invalid names'
       else
         msg := 'Template has an invalid name';
       msg := msg + ': ';
-      for i := 0 to Errors.Count-1 do
-      begin
-        if(i > 0) then msg := msg + ', ';
+      for i := 0 to Errors.Count - 1 do
+      begin
+        if (i > 0) then msg := msg + ', ';
         Node := TTreeNode(Errors[i]);
         msg := msg + Node.Text;
@@ -2671,21 +2687,21 @@
 
 begin
-  if(not FOK2Close) and (BackupDiffers) then
+  if (not FOK2Close) and (BackupDiffers) then
   begin
     ans := InfoBox('Save Changes?', 'Confirmation', MB_YESNOCANCEL or MB_ICONQUESTION);
-    if(ans = IDCANCEL) then
+    if (ans = IDCANCEL) then
       CanClose := FALSE
     else
-    if(ans = IDYES) then
-    begin
-      CanClose := FALSE;
-      if(ScanNames) then
-      begin
-        if(SaveAllTemplates) then
-          CanClose := TRUE
-        else
-          BtnApply.Enabled := BackupDiffers;
+      if (ans = IDYES) then
+      begin
+        CanClose := FALSE;
+        if (ScanNames) then
+        begin
+          if (SaveAllTemplates) then
+            CanClose := TRUE
+          else
+            BtnApply.Enabled := BackupDiffers;
+        end;
       end;
-    end;
   end;
 end;
@@ -2703,4 +2719,8 @@
     tmplEditorSplitterNotes := pnlNotes.Height;
   pnlBoilerplateResize(Self);
+  if not PnlNotes.visible and (PnlNotes.Top <= pnlGroupBP.Top - pnlGroupBP.Constraints.MinHeight) then
+    tmplEditorSplitterNotes := (pnlGroupBP.Height - pnlGroupBP.Constraints.MinHeight) - splNotes.Height - 10;
+  if not pnlGroupBP.visible and (pnlGroupBP.Top >= pnlNotes.Top + pnlGroupBP.Constraints.MinHeight) then
+    tmplEditorSplitterBoil := reBoil.Height - pnlGroupBP.Constraints.MinHeight - 10;
 end;
 
@@ -2708,5 +2728,5 @@
   var Key: Char);
 begin
-  if(not (Key in ['0','1','2','3'])) then Key := #0;
+  if (not (Key in ['0', '1', '2', '3'])) then Key := #0;
 end;
 
@@ -2717,10 +2737,10 @@
 begin
   Warn := (ActiveControl <> btnCancel) and (BadTemplateName(edtName.Text));
-  if(Warn and ((ActiveControl = sbShDelete) or (ActiveControl = sbPerDelete))) then
-  begin
-    if((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if (Warn and ((ActiveControl = sbShDelete) or (ActiveControl = sbPerDelete))) then
+  begin
+    if ((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
       Warn := not AutoDel(TTemplate(FCurTree.Selected.Data));
   end;
-  if(Warn) then
+  if (Warn) then
   begin
     InfoBox('Template has an invalid name: ' + edtName.Text + '.' + BadNameText, 'Error', MB_OK or MB_ICONERROR);
@@ -2741,10 +2761,10 @@
 
 begin
-  if(assigned(FDropNode)) then
+  if (assigned(FDropNode)) then
   begin
     TopNode := FDropNode.TreeView.TopItem;
     Redraw := FALSE;
     TmpPt := FDropNode.TreeView.ScreenToClient(Mouse.CursorPos);
-    if(TopNode = FDropNode) and (TopNode <> TTreeView(FDropNode.TreeView).Items.GetFirstNode) then
+    if (TopNode = FDropNode) and (TopNode <> TTreeView(FDropNode.TreeView).Items.GetFirstNode) then
     begin
       FDropNode.TreeView.TopItem := TopNode.GetPrevVisible;
@@ -2754,18 +2774,18 @@
     begin
       RMax := FDropNode.TreeView.ClientHeight - EdgeScroll;
-      if((TmpPt.Y > RMax) and (FDropNode.GetNextVisible <> nil)) then
+      if ((TmpPt.Y > RMax) and (FDropNode.GetNextVisible <> nil)) then
       begin
         TORTreeView(FDropNode.TreeView).VertScrollPos :=
-        TORTreeView(FDropNode.TreeView).VertScrollPos + 1;
+          TORTreeView(FDropNode.TreeView).VertScrollPos + 1;
         Redraw := TRUE;
       end;
     end;
-    if(FLastDropNode <> FDropNode) then
-    begin
-      if((assigned(FDropNode)) and (FDropNode.GetNext = nil)) then
+    if (FLastDropNode <> FDropNode) then
+    begin
+      if ((assigned(FDropNode)) and (FDropNode.GetNext = nil)) then
         Redraw := TRUE
       else
-      if((assigned(FLastDropNode)) and (FLastDropNode.GetNext = nil)) then
-        Redraw := TRUE;
+        if ((assigned(FLastDropNode)) and (FLastDropNode.GetNext = nil)) then
+          Redraw := TRUE;
       FLastDropNode := FDropNode;
       FDragOverCount := 0;
@@ -2773,11 +2793,11 @@
     else
     begin
-      if(FDropNode.HasChildren) and (not FDropNode.Expanded) then
+      if (FDropNode.HasChildren) and (not FDropNode.Expanded) then
       begin
         ht := FDropNode.TreeView.GetHitTestInfoAt(TmpPt.X, TmpPt.Y);
-        if(htOnButton in ht) then
+        if (htOnButton in ht) then
         begin
           inc(FDragOverCount);
-          if(FDragOverCount > 4) then
+          if (FDragOverCount > 4) then
           begin
             TopNode := FDropNode.TreeView.TopItem;
@@ -2791,11 +2811,11 @@
           FDragOverCount := 0;
       end;
-      if(not Redraw) then
+      if (not Redraw) then
       begin
         HPos := TORTreeView(FDropNode.TreeView).HorzScrollPos;
-        if(HPos > 0) and (TmpPt.X < EdgeScroll) then
+        if (HPos > 0) and (TmpPt.X < EdgeScroll) then
         begin
           TORTreeView(FDropNode.TreeView).HorzScrollPos :=
-          TORTreeView(FDropNode.TreeView).HorzScrollPos - EdgeScroll;
+            TORTreeView(FDropNode.TreeView).HorzScrollPos - EdgeScroll;
           Redraw := TRUE;
         end
@@ -2803,8 +2823,8 @@
         begin
           RMax := FDropNode.TreeView.ClientWidth - EdgeScroll;
-          if(TmpPt.X > RMax) then
+          if (TmpPt.X > RMax) then
           begin
             TORTreeView(FDropNode.TreeView).HorzScrollPos :=
-            TORTreeView(FDropNode.TreeView).HorzScrollPos + EdgeScroll;
+              TORTreeView(FDropNode.TreeView).HorzScrollPos + EdgeScroll;
             Redraw := TRUE;
           end;
@@ -2812,5 +2832,5 @@
       end;
     end;
-    if(Redraw) then
+    if (Redraw) then
     begin
       TmpPt := Mouse.CursorPos; // Wiggling the mouse causes needed windows messages to fire
@@ -2877,14 +2897,14 @@
 procedure TfrmTemplateEditor.mnuNodeDeleteClick(Sender: TObject);
 begin
-  if(FCurTree = tvShared) and (sbShDelete.Visible) and (sbShDelete.Enabled) then
+  if (FCurTree = tvShared) and (sbShDelete.Visible) and (sbShDelete.Enabled) then
     sbDeleteClick(sbShDelete)
   else
-  if(FCurTree = tvPersonal) and (sbPerDelete.Visible) and (sbPerDelete.Enabled) then
-    sbDeleteClick(sbPerDelete);
+    if (FCurTree = tvPersonal) and (sbPerDelete.Visible) and (sbPerDelete.Enabled) then
+      sbDeleteClick(sbPerDelete);
 end;
 
 procedure TfrmTemplateEditor.mnuNodeCopyClick(Sender: TObject);
 begin
-  if(assigned(FCurTree)) then
+  if (assigned(FCurTree)) then
     FCopyNode := FCurTree.Selected
   else
@@ -2894,5 +2914,5 @@
 procedure TfrmTemplateEditor.mnuNodePasteClick(Sender: TObject);
 begin
-  if(PasteOK) then
+  if (PasteOK) then
   begin
     FDragNode := FCopyNode;
@@ -2916,7 +2936,7 @@
 begin
   Result := assigned(FCopyNode) and assigned(FPasteNode);
-  if(Result) then
+  if (Result) then
     Result := (FTreeControl[TTemplateTreeType(FPasteNode.TreeView.Tag), tcDel].Visible);
-  if(Result) then
+  if (Result) then
   begin
     OldCopying := FCopying;
@@ -2924,5 +2944,5 @@
     try
       Node := FPasteNode;
-      if(TTemplate(Node.Data).RealType = ttDoc) then
+      if (TTemplate(Node.Data).RealType = ttDoc) then
         Node := Node.Parent;
       Result := AllowMove(Node, FCopyNode);
@@ -2942,14 +2962,14 @@
 begin
 
-  if(Key = VK_DELETE) then
-  begin
-    if(Sender = tvShared) then
-    begin
-      if(sbShDelete.Visible and sbShDelete.Enabled) then
+  if (Key = VK_DELETE) then
+  begin
+    if (Sender = tvShared) then
+    begin
+      if (sbShDelete.Visible and sbShDelete.Enabled) then
         sbDeleteClick(sbShDelete);
     end
     else
     begin
-      if(sbPerDelete.Visible and sbPerDelete.Enabled) then
+      if (sbPerDelete.Visible and sbPerDelete.Enabled) then
         sbDeleteClick(sbPerDelete);
     end;
@@ -2957,35 +2977,35 @@
    //Code Added to provide CTRL Key access for 508 compliance  GRE 3/03
   if (ssCtrl in Shift) and (Key = VK_A) then
-      reBoil.SelectAll
+    reBoil.SelectAll
   else
-  if (ssCtrl in Shift) and (Key = VK_C) then
+    if (ssCtrl in Shift) and (Key = VK_C) then
       reBoil.CopyToClipboard
-  else
-  if (ssCtrl in Shift) and (Key = VK_E) then
-      mnuBPErrorCheckClick(Self)
-  else
-  if (ssCtrl in Shift) and (Key = VK_F) then
-      mnuBPInsertFieldClick(Self)
-  else
-  if (ssCtrl in Shift) and (Key = VK_G) then
-      GrammarCheckForControl(reBoil)
-  else
-  if (ssCtrl in Shift) and (Key = VK_I) then
-      mnuBPInsertObjectClick(Self)
-  else
-  if (ssCtrl in Shift) and (Key = VK_S) then
-      SpellCheckForControl(reBoil)
-  else
-  if (ssCtrl in Shift) and (Key = VK_T) then
-      mnuBPTryClick(Self)
-  else
-  if (ssCtrl in Shift) and (Key = VK_V) then
-      reBoil.SelText := Clipboard.AsText
-  else
-  if (ssCtrl in Shift) and (Key = VK_X) then
-      reBoil.CutToClipboard
-  else
-  if (ssCtrl in Shift) and (Key = VK_Z) then
-      reBoil.Perform(EM_UNDO, 0, 0);
+    else
+      if (ssCtrl in Shift) and (Key = VK_E) then
+        mnuBPErrorCheckClick(Self)
+      else
+        if (ssCtrl in Shift) and (Key = VK_F) then
+          mnuBPInsertFieldClick(Self)
+        else
+          if (ssCtrl in Shift) and (Key = VK_G) then
+            GrammarCheckForControl(reBoil)
+          else
+            if (ssCtrl in Shift) and (Key = VK_I) then
+              mnuBPInsertObjectClick(Self)
+            else
+              if (ssCtrl in Shift) and (Key = VK_S) then
+                SpellCheckForControl(reBoil)
+              else
+                if (ssCtrl in Shift) and (Key = VK_T) then
+                  mnuBPTryClick(Self)
+                else
+                  if (ssCtrl in Shift) and (Key = VK_V) then
+                    reBoil.SelText := Clipboard.AsText
+                  else
+                    if (ssCtrl in Shift) and (Key = VK_X) then
+                      reBoil.CutToClipboard
+                    else
+                      if (ssCtrl in Shift) and (Key = VK_Z) then
+                        reBoil.Perform(EM_UNDO, 0, 0);
   //End of ---- Code Added to provide CTRL Key access for 508 compliance  GRE 3/03
 end;
@@ -3002,5 +3022,5 @@
     mnuInsertField.Enabled := ok;
     mnuPaste.Enabled := (ok and Clipboard.HasFormat(CF_TEXT));
-    if(ok) then
+    if (ok) then
       ok := (reBoil.Lines.Count > 0);
     tryOK := (reBoil.Lines.Count > 0) or ((pnlGroupBP.Visible) and (reGroupBP.Lines.Count > 0));
@@ -3018,15 +3038,15 @@
   else
   begin
-    mnuInsertObject.Enabled     := FALSE; 
-    mnuInsertField.Enabled      := FALSE;
-    mnuPaste.Enabled            := FALSE;
-    mnuErrorCheck.Enabled       := FALSE;
-    mnuTry.Enabled              := FALSE;
-    mnuSpellCheck.Enabled       := FALSE;
-    mnuCheckGrammar.Enabled     := FALSE;
-    mnuCopy.Enabled             := FALSE;
-    mnuCut.Enabled              := FALSE;
-    mnuSelectAll.Enabled        := FALSE;
-    mnuUndo.Enabled             := FALSE;
+    mnuInsertObject.Enabled := FALSE;
+    mnuInsertField.Enabled := FALSE;
+    mnuPaste.Enabled := FALSE;
+    mnuErrorCheck.Enabled := FALSE;
+    mnuTry.Enabled := FALSE;
+    mnuSpellCheck.Enabled := FALSE;
+    mnuCheckGrammar.Enabled := FALSE;
+    mnuCopy.Enabled := FALSE;
+    mnuCut.Enabled := FALSE;
+    mnuSelectAll.Enabled := FALSE;
+    mnuUndo.Enabled := FALSE;
     mnuGroupBoilerplate.Enabled := FALSE;
   end;
@@ -3042,5 +3062,5 @@
 begin
   SetFindNext(tvShared, FALSE);
-  if(pnlShSearch.Visible) then edtShSearch.SetFocus;
+  if (pnlShSearch.Visible) then edtShSearch.SetFocus;
 end;
 
@@ -3048,5 +3068,5 @@
 begin
   SetFindNext(tvPersonal, FALSE);
-  if(pnlPerSearch.Visible) then edtPerSearch.SetFocus;
+  if (pnlPerSearch.Visible) then edtPerSearch.SetFocus;
 end;
 
@@ -3058,16 +3078,16 @@
   FFromMainMenu := TRUE;
   Tree := FCurTree;
-  if(assigned(Tree) and assigned(Tree.Selected)) then
-  begin
-    if(Tree = tvShared) then
+  if (assigned(Tree) and assigned(Tree.Selected)) then
+  begin
+    if (Tree = tvShared) then
       mnuTDelete.Enabled := ((sbShDelete.Visible) and (sbShDelete.Enabled))
     else
       mnuTDelete.Enabled := ((sbPerDelete.Visible) and (sbPerDelete.Enabled));
-    if(assigned(Tree) and assigned(Tree.Selected) and assigned(Tree.Selected.Data)) then
+    if (assigned(Tree) and assigned(Tree.Selected) and assigned(Tree.Selected.Data)) then
     begin
       mnuTCopy.Enabled := (TTemplate(Tree.Selected.Data).RealType in [ttDoc, ttGroup, ttClass]);
       mnuSort.Enabled := (TTemplate(Tree.Selected.Data).RealType in AllTemplateFolderTypes) and
-                         (Tree.Selected.HasChildren) and
-                         (Tree.Selected.GetFirstChild.GetNextSibling <> nil);
+        (Tree.Selected.HasChildren) and
+        (Tree.Selected.GetFirstChild.GetNextSibling <> nil);
     end
     else
@@ -3120,5 +3140,5 @@
 procedure TfrmTemplateEditor.pnlShSearchResize(Sender: TObject);
 begin
-  if((cbShMatchCase.Width + cbShWholeWords.Width) > pnlShSearch.Width) then
+  if ((cbShMatchCase.Width + cbShWholeWords.Width) > pnlShSearch.Width) then
     cbShWholeWords.Left := cbShMatchCase.Width
   else
@@ -3128,5 +3148,5 @@
 procedure TfrmTemplateEditor.pnlPerSearchResize(Sender: TObject);
 begin
-  if((cbPerMatchCase.Width + cbPerWholeWords.Width) > pnlPerSearch.Width) then
+  if ((cbPerMatchCase.Width + cbPerWholeWords.Width) > pnlPerSearch.Width) then
     cbPerWholeWords.Left := cbPerMatchCase.Width
   else
@@ -3156,5 +3176,5 @@
 begin
   Tree := FCurTree;
-  if(assigned(Tree) and assigned(Tree.Selected) and Tree.Selected.HasChildren) then
+  if (assigned(Tree) and assigned(Tree.Selected) and Tree.Selected.HasChildren) then
   begin
     TTemplate(Tree.Selected.Data).SortChildren;
@@ -3167,15 +3187,15 @@
   var NewWidth, NewHeight: Integer; var Resize: Boolean);
 begin
-  if(NewHeight < 40) then Resize := FALSE;
+  if (NewHeight < 40) then Resize := FALSE;
 end;
 
 function TfrmTemplateEditor.AutoDel(Template: TTemplate): boolean;
 begin
-  if(assigned(Template)) then
+  if (assigned(Template)) then
     Result := (((Template.ID = '0') or (Template.ID = '')) and
-                (Template.PrintName = NewTemplateName) and
-                (Template.Boilerplate = ''))
+      (Template.PrintName = NewTemplateName) and
+      (Template.Boilerplate = ''))
   else
-    Result := FALSE;            
+    Result := FALSE;
 end;
 
@@ -3185,12 +3205,12 @@
   Move: boolean;
   tmpl: TTemplate;
-  txt: String;
+  txt: string;
 
 begin
   mnuBPErrorCheckClick(nil);
-  if(FBPOK) or (reBoil.Lines.Count = 0) then
+  if (FBPOK) or (reBoil.Lines.Count = 0) then
   begin
     Move := assigned(frmTemplateView);
-    if(Move) then
+    if (Move) then
     begin
       R := frmTemplateView.BoundsRect;
@@ -3201,7 +3221,7 @@
     tmpl.TemplatePreviewMode := TRUE; // Prevents "Are you sure?" dialog when canceling
     txt := tmpl.Text;
-    if(not tmpl.DialogAborted) then
-      ShowTemplateData(Self, tmpl.PrintName ,txt);
-    if(Move) then
+    if (not tmpl.DialogAborted) then
+      ShowTemplateData(Self, tmpl.PrintName, txt);
+    if (Move) then
       frmTemplateView.BoundsRect := R;
     tmpl.TemplatePreviewMode := FALSE;
@@ -3216,6 +3236,6 @@
   dmodShared.LoadTIUObjects;
   UpdatePersonalObjects;
-  GetAutoGenText(AName, AText, uPersonalObjects);   // -------- CQ #8665 - RV ------------
-  if(AName <> '') and (AText <> '') then
+  GetAutoGenText(AName, AText, uPersonalObjects); // -------- CQ #8665 - RV ------------
+  if (AName <> '') and (AText <> '') then
   begin
     btnNewClick(Self);
@@ -3233,5 +3253,5 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     if CanClone(FCurTree.Selected) then
@@ -3243,5 +3263,5 @@
         Template.Description := reNotes.Lines.Text;
         UpdateApply(Template);
-        if(DoRefresh) then
+        if (DoRefresh) then
         begin
           tvShared.Invalidate;
@@ -3297,5 +3317,5 @@
   ok := not reNotes.ReadOnly;
   mnuNotesPaste.Enabled := (ok and Clipboard.HasFormat(CF_TEXT));
-  if(ok) then
+  if (ok) then
     ok := (reNotes.Lines.Count > 0);
   mnuNotesSpelling.Enabled := ok and SpellCheckAvailable;
@@ -3308,4 +3328,7 @@
 
 procedure TfrmTemplateEditor.cbNotesClick(Sender: TObject);
+var
+  ScreenPoint: TPoint;
+  NewHeight: Integer;
 begin
   pnlNotes.Visible := cbNotes.Checked;
@@ -3315,5 +3338,24 @@
     pnlNotes.Height := tmplEditorSplitterNotes;
     pnlNotes.Top := pnlBottom.Top - pnlNotes.Height;
-    splNotes.Top := pnlNotes.Top-3;
+    splNotes.Top := pnlNotes.Top - 3;
+    if pnlGroupBP.Height <= pnlGroupBP.Constraints.MinHeight then begin
+      //Clear the points
+      ScreenPoint.Y := 0;
+      ScreenPoint.X := 0;
+      if pnlNotes.ClientToScreen(ScreenPoint).Y + 30 > pnlBottom.Top then begin
+        if pnlNotes.ClientToScreen(ScreenPoint).Y > pnlBottom.Top then
+          NewHeight := pnlNotes.Height + (pnlNotes.ClientToScreen(ScreenPoint).Y - pnlBottom.Top)
+        else
+          NewHeight := pnlNotes.Height - (pnlNotes.ClientToScreen(ScreenPoint).Y - pnlBottom.Top);
+        Reboil.Height := Reboil.Height - (NewHeight + 10);
+        PnlNotes.Constraints.MinHeight := 30;
+        pnlNotes.Height := PnlNotes.Constraints.MinHeight;
+        if reboil.Height = reboil.Constraints.MinHeight then
+          PnlTop.height := PnlTop.height - 100;
+      end;
+    end;
+  end else begin
+    pnlNotes.Constraints.MinHeight := 1;
+    pnlNotes.Height := 1;
   end;
   pnlBoilerplateResize(Self);
@@ -3348,7 +3390,7 @@
 var
   Template: TTemplate;
-  
-begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+
+begin
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     if CanClone(FCurTree.Selected) then
@@ -3359,11 +3401,11 @@
       begin
         case Index of
-          BPDisplayOnlyFld:   Template.DisplayOnly   := Sender.Checked;
-          BPFirstLineFld:     Template.FirstLine     := Sender.Checked;
-          BPOneItemOnlyFld:   Template.OneItemOnly   := Sender.Checked;
-          BPHideDlgItemsFld:  Template.HideDlgItems  := Sender.Checked;
-          BPHideItemsFld:     Template.HideItems     := Sender.Checked;
-          BPIndentFld:        Template.IndentItems   := Sender.Checked;
-          BPLockFld:          Template.Lock          := Sender.Checked;
+          BPDisplayOnlyFld: Template.DisplayOnly := Sender.Checked;
+          BPFirstLineFld: Template.FirstLine := Sender.Checked;
+          BPOneItemOnlyFld: Template.OneItemOnly := Sender.Checked;
+          BPHideDlgItemsFld: Template.HideDlgItems := Sender.Checked;
+          BPHideItemsFld: Template.HideItems := Sender.Checked;
+          BPIndentFld: Template.IndentItems := Sender.Checked;
+          BPLockFld: Template.Lock := Sender.Checked;
         end;
         UpdateApply(Template);
@@ -3383,5 +3425,5 @@
   mnuImportTemplate.Enabled := btnNew.Enabled;
   mnuExportTemplate.Enabled := (assigned(FCurTree) and assigned(FCurTree.Selected) and
-                                assigned(FCurTree.Selected.Data));
+    assigned(FCurTree.Selected.Data));
 end;
 
@@ -3398,5 +3440,5 @@
 procedure TfrmTemplateEditor.mnuBPInsertFieldClick(Sender: TObject);
 begin
-  if(not assigned(frmTemplateFields)) then
+  if (not assigned(frmTemplateFields)) then
   begin
     frmTemplateFields := TfrmTemplateFields.Create(Self);
@@ -3425,5 +3467,5 @@
 begin
   err := FALSE;
-  if(assigned(FCurTree) and assigned(FCurTree.Selected) and assigned(FCurTree.Selected.Data)) then
+  if (assigned(FCurTree) and assigned(FCurTree.Selected) and assigned(FCurTree.Selected.Data)) then
   begin
     dlgExport.FileName := ValidFileName(TTemplate(FCurTree.Selected.Data).PrintName);
@@ -3434,5 +3476,5 @@
         Flds := TStringList.Create;
         try
-          Tmpl.Add('<'+XMLHeader+'>');
+          Tmpl.Add('<' + XMLHeader + '>');
           if TTemplate(FCurTree.Selected.Data).CanExportXML(Tmpl, Flds, 2) then
           begin
@@ -3440,9 +3482,9 @@
               ExpandEmbeddedFields(Flds);
               FastAssign(ExportTemplateFields(Flds), Flds);
-              for i := 0 to Flds.Count-1 do
+              for i := 0 to Flds.Count - 1 do
                 Flds[i] := '  ' + Flds[i];
               FastAddStrings(Flds, Tmpl);
             end; {if}
-            Tmpl.Add('</'+XMLHeader+'>');
+            Tmpl.Add('</' + XMLHeader + '>');
             try
               XMLDoc := CoDOMDocument.Create;
@@ -3460,5 +3502,5 @@
             if not err then
               InfoBox('Template ' + TTemplate(FCurTree.Selected.Data).PrintName +
-                      ' Exported.', 'Template Exported', MB_OK);
+                ' Exported.', 'Template Exported', MB_OK);
           end;
         finally
@@ -3483,5 +3525,5 @@
   ImportedTemplate: TTemplate;
   AppData, Flds, ResultSet: TStringList;
-  tmp,j,p3: string;
+  tmp, j, p3: string;
   err, ok, changes, xmlerr: boolean;
   i: integer;
@@ -3504,6 +3546,6 @@
   begin
     tmp := ExtractFileExt(dlgImport.FileName);
-    if(WordImportActive and ((CompareText(tmp,'.doc') = 0) or
-                             (CompareText(tmp,'.dot') = 0))) then
+    if (WordImportActive and ((CompareText(tmp, '.doc') = 0) or
+      (CompareText(tmp, '.dot') = 0))) then
       AppData := TStringList.Create
     else
@@ -3539,5 +3581,5 @@
             XMLImportError(0);
           try
-            if(RootElement.tagName <> XMLHeader)then
+            if (RootElement.tagName <> XMLHeader) then
               XMLImportError(0)
             else
@@ -3548,5 +3590,5 @@
               begin
                 FXMLFieldElement := FindXMLElement(RootElement, XMLTemplateFieldsTag);
-                if(assigned(FXMLFieldElement)) then
+                if (assigned(FXMLFieldElement)) then
                 begin
                   Flds := TStringList.Create;
@@ -3557,6 +3599,6 @@
                     changes := FALSE;
                     Application.ProcessMessages;
-                    if not BuildTemplateFields(Flds) then  //Calls RPC to transfer all field XML
-                      choice := IDCANCEL;                  //for processing
+                    if not BuildTemplateFields(Flds) then //Calls RPC to transfer all field XML
+                      choice := IDCANCEL; //for processing
                     Flds.Text := '';
                     Application.ProcessMessages;
@@ -3564,35 +3606,35 @@
                       CheckTemplateFields(Flds);
                     if Flds.Count > 0 then
+                    begin
+                      for i := 1 to Flds.Count do
                       begin
-                        for i := 1 to Flds.Count do
-                          begin
-                          j := piece(Flds[i-1],U,2);
-                          if (j = '0') or (j = '2') then
-                            begin
-                              p3 := piece(Flds[i-1],U,3);
-                              if p3 = 'XML FORMAT ERROR' then
-                                choice := IDCANCEL;
-                              changes := TRUE;
-                              if j = '2' then begin
-                                j := Flds[i-1];
-                                SetPiece(j,U,2,'1');
-                                Flds[i-1] := j
-                              end;
-                            end;
+                        j := piece(Flds[i - 1], U, 2);
+                        if (j = '0') or (j = '2') then
+                        begin
+                          p3 := piece(Flds[i - 1], U, 3);
+                          if p3 = 'XML FORMAT ERROR' then
+                            choice := IDCANCEL;
+                          changes := TRUE;
+                          if j = '2' then begin
+                            j := Flds[i - 1];
+                            SetPiece(j, U, 2, '1');
+                            Flds[i - 1] := j
                           end;
-                      end
+                        end;
+                      end;
+                    end
                     else
                       choice := IDCANCEL;
                     if choice <> IDOK then
-                      InfoBox(iMessage2+iMessage3, 'Error', MB_OK or MB_ICONERROR)
+                      InfoBox(iMessage2 + iMessage3, 'Error', MB_OK or MB_ICONERROR)
                     else
-                      if (not CanEditTemplateFields) AND
-                         changes {(there is at least one new field)} then
-                        begin
-                          choice := InfoBox(iMessage, 'Warning', MB_OKCANCEL or MB_ICONWARNING);
-                          Flds.Text := '';
-                        end;
+                      if (not CanEditTemplateFields) and
+                        changes {(there is at least one new field)} then
+                      begin
+                        choice := InfoBox(iMessage, 'Warning', MB_OKCANCEL or MB_ICONWARNING);
+                        Flds.Text := '';
+                      end;
                     if choice <> IDCANCEL then
-                      begin
+                    begin
                       FImportingFromXML := TRUE;
                       try
@@ -3605,11 +3647,11 @@
                       if assigned(ImportedTemplate) and (Flds.Count > 0) then
                         if not ImportLoadedFields(ResultSet) then begin
-                          InfoBox(iMessage2+iMessage3, 'Error', MB_OK or MB_ICONERROR);
+                          InfoBox(iMessage2 + iMessage3, 'Error', MB_OK or MB_ICONERROR);
                           ClearFields;
                           choice := IDCANCEL;
-                        end;//if
+                        end; //if
                       if Flds.Count = 0 then
                         choice := IDCANCEL;
-                      end {if choice <> mrCancel}
+                    end {if choice <> mrCancel}
                     else
                       ClearFields;
@@ -3617,9 +3659,9 @@
                     xmlerr := FALSE;
                     if (Flds.Count > 0) and
-                       (ResultSet.Count > 0) and
-                       (Flds.Count = ResultSet.Count) then
-                      for i := 0 to Flds.Count-1 do begin
-                        if piece(ResultSet[i],U,2) = '0' then begin
-                          j := piece(Flds[i],U,1) + U + '0' + U + piece(ResultSet[i],U,3);
+                      (ResultSet.Count > 0) and
+                      (Flds.Count = ResultSet.Count) then
+                      for i := 0 to Flds.Count - 1 do begin
+                        if piece(ResultSet[i], U, 2) = '0' then begin
+                          j := piece(Flds[i], U, 1) + U + '0' + U + piece(ResultSet[i], U, 3);
                           Flds[i] := j;
                         end
@@ -3639,6 +3681,6 @@
                       else
                         inc(i);
-                    end;//while
-                    if(Flds.Count > 0) then
+                    end; //while
+                    if (Flds.Count > 0) then
                     begin
                       if assigned(frmTemplateFields) then
@@ -3647,15 +3689,15 @@
                       if not assigned(AppData) then
                       begin
-                        for i := 0 to Flds.Count-1 do
-                          Flds[i] := '  Field "' + Piece(Flds[i],U,1) + '" has been renamed to "'+
-                                                   Piece(Flds[i],U,3) + '"';
+                        for i := 0 to Flds.Count - 1 do
+                          Flds[i] := '  Field "' + Piece(Flds[i], U, 1) + '" has been renamed to "' +
+                            Piece(Flds[i], U, 3) + '"';
                         if Flds.Count = 1 then
                           tmp := 'A template field has'
                         else
                           tmp := IntToStr(Flds.Count) + ' template fields have';
-                        Flds.Insert(0,tmp + ' been imported with the same name as');
-                        Flds.Insert(1,'existing template fields, but with different field definitions.');
-                        Flds.Insert(2,'These imported template fields have been renamed as follows:');
-                        Flds.Insert(3,'');
+                        Flds.Insert(0, tmp + ' been imported with the same name as');
+                        Flds.Insert(1, 'existing template fields, but with different field definitions.');
+                        Flds.Insert(2, 'These imported template fields have been renamed as follows:');
+                        Flds.Insert(3, '');
                         InfoBox(Flds.Text, 'Information', MB_OK or MB_ICONINFORMATION);
                       end;
@@ -3692,5 +3734,5 @@
         AppData.Free;
         if err then
-          InfoBox('An error occured while Importing Word Document.  Make sure Word is closed and try again.','Import Error', MB_OK);
+          InfoBox('An error occured while Importing Word Document.  Make sure Word is closed and try again.', 'Import Error', MB_OK);
       end;
     end;
@@ -3707,21 +3749,21 @@
   case IdxForced[FForceContainer, Index] of
     tiTemplate: ImgIdx := 4;
-    tiFolder:   ImgIdx := 3;
-    tiGroup:    ImgIdx := 5;
-    tiDialog:   ImgIdx := 23;
-    tiRemDlg:   ImgIdx := 27;
-    tiCOMObj:   ImgIdx := 28;
-    else
-      ImgIdx := ord(tiNone);
+    tiFolder: ImgIdx := 3;
+    tiGroup: ImgIdx := 5;
+    tiDialog: ImgIdx := 23;
+    tiRemDlg: ImgIdx := 27;
+    tiCOMObj: ImgIdx := 28;
+  else
+    ImgIdx := ord(tiNone);
   end;
   if ImgIdx >= 0 then
-    dmodShared.imgTemplates.Draw(cbxType.Canvas, Rect.Left+1, Rect.Top+1, ImgIdx);
+    dmodShared.imgTemplates.Draw(cbxType.Canvas, Rect.Left + 1, Rect.Top + 1, ImgIdx);
   if Index >= 0 then
-    cbxType.Canvas.TextOut(Rect.Left+21, Rect.Top+2, cbxType.Items[Index]);
+    cbxType.Canvas.TextOut(Rect.Left + 21, Rect.Top + 2, cbxType.Items[Index]);
 end;
 
 procedure TfrmTemplateEditor.cbxTypeChange(Sender: TObject);
 var
-  i,tg: integer;
+  i, tg: integer;
   Template: TTemplate;
   ttyp: TTemplateType;
@@ -3730,5 +3772,5 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
   begin
     tg := cbxType.ItemIndex;
@@ -3738,5 +3780,5 @@
       begin
         idx := IdxForced[FForceContainer, tg];
-        if(idx = tiRemDlg) and (not (GetLinkType(FCurTree.Selected) in [ltNone, ltTitle])) then
+        if (idx = tiRemDlg) and (not (GetLinkType(FCurTree.Selected) in [ltNone, ltTitle])) then
         begin
           FUpdating := TRUE;
@@ -3755,7 +3797,7 @@
           begin
             ttyp := TypeTag[idx];
-            if(not FForceContainer) or (not (idx in [tiTemplate, tiRemDlg])) then
+            if (not FForceContainer) or (not (idx in [tiTemplate, tiRemDlg])) then
             begin
-              if(ttyp = ttDialog) then
+              if (ttyp = ttDialog) then
               begin
                 Template.Dialog := TRUE;
@@ -3765,9 +3807,9 @@
                 Template.Dialog := FALSE;
               Template.RealType := ttyp;
-              if(Template.RealType = ttDoc) and (idx = tiRemDlg) then
+              if (Template.RealType = ttDoc) and (idx = tiRemDlg) then
                 Template.IsReminderDialog := TRUE
               else
                 Template.IsReminderDialog := FALSE;
-              if(Template.RealType = ttDoc) and (idx = tiCOMObj) then
+              if (Template.RealType = ttDoc) and (idx = tiCOMObj) then
                 Template.IsCOMObject := TRUE
               else
@@ -3775,5 +3817,5 @@
               UpdateApply(Template);
             end;
-            for i := 0 to Template.Nodes.Count-1 do
+            for i := 0 to Template.Nodes.Count - 1 do
             begin
               Node := TTreeNode(Template.Nodes.Objects[i]);
@@ -3797,6 +3839,6 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
-     FCanDoReminders) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
+    FCanDoReminders) then
   begin
     if CanClone(FCurTree.Selected) then
@@ -3879,4 +3921,5 @@
 { Returns TRUE if Cloning is not needed or if Cloning is needed and
   the top personal Node in the tree is locked. }
+
 function TfrmTemplateEditor.CanClone(const Node: TTreeNode): boolean;
 var
@@ -3891,5 +3934,5 @@
 
 begin
-  if(assigned(Node)) and assigned(Node.Data) then
+  if (assigned(Node)) and assigned(Node.Data) then
   begin
     if (TTreeView(Node.TreeView) = tvPersonal) then
@@ -3910,5 +3953,5 @@
 procedure TfrmTemplateEditor.UpdateApply(Template: TTemplate);
 begin
-  if(not btnApply.Enabled) then
+  if (not btnApply.Enabled) then
     btnApply.Enabled := Template.Changed;
 end;
@@ -3930,5 +3973,5 @@
   begin
     if InfoBox('All changes must be saved before you can Refresh.  Save Changes?',
-        'Confirmation', MB_YESNO or MB_ICONQUESTION) <> IDYES then
+      'Confirmation', MB_YESNO or MB_ICONQUESTION) <> IDYES then
       exit;
   end;
@@ -3949,8 +3992,8 @@
   exp1 := tvShared.GetExpandedIDStr(1, ';');
   exp2 := tvPersonal.GetExpandedIDStr(1, ';');
-  s1 := tvShared.GetNodeID(TORTreeNode(tvShared.Selected),1,';');
-  s2 := tvPersonal.GetNodeID(TORTreeNode(tvPersonal.Selected),1,';');
-  t1 := tvShared.GetNodeID(TORTreeNode(tvShared.TopItem),1,';');
-  t2 := tvPersonal.GetNodeID(TORTreeNode(tvPersonal.TopItem),1,';');
+  s1 := tvShared.GetNodeID(TORTreeNode(tvShared.Selected), 1, ';');
+  s2 := tvPersonal.GetNodeID(TORTreeNode(tvPersonal.Selected), 1, ';');
+  t1 := tvShared.GetNodeID(TORTreeNode(tvShared.TopItem), 1, ';');
+  t2 := tvPersonal.GetNodeID(TORTreeNode(tvPersonal.TopItem), 1, ';');
   tvPersonal.Items.BeginUpdate;
   try
@@ -3962,9 +4005,9 @@
       InitTrees;
       tvShared.SetExpandedIDStr(1, ';', exp1);
-      tvShared.TopItem := tvShared.FindPieceNode(t1,1,';');
-      tvShared.Selected := tvShared.FindPieceNode(s1,1,';');
+      tvShared.TopItem := tvShared.FindPieceNode(t1, 1, ';');
+      tvShared.Selected := tvShared.FindPieceNode(s1, 1, ';');
       tvPersonal.SetExpandedIDStr(1, ';', exp2);
-      tvPersonal.TopItem := tvPersonal.FindPieceNode(t2,1,';');
-      tvPersonal.Selected := tvPersonal.FindPieceNode(s2,1,';');
+      tvPersonal.TopItem := tvPersonal.FindPieceNode(t2, 1, ';');
+      tvPersonal.Selected := tvPersonal.FindPieceNode(s2, 1, ';');
     finally
       tvShared.Items.EndUpdate;
@@ -3979,8 +4022,8 @@
 begin
   LoadTemplateData;
-  if(not assigned(RootTemplate)) then
-    SaveTemplate(AddTemplate('0^R^A^Shared Templates'),-1);
-  if(not assigned(MyTemplate)) then
-    AddTemplate('0^P^A^My Templates^^^'+IntToStr(User.DUZ));
+  if (not assigned(RootTemplate)) then
+    SaveTemplate(AddTemplate('0^R^A^Shared Templates'), -1);
+  if (not assigned(MyTemplate)) then
+    AddTemplate('0^P^A^My Templates^^^' + IntToStr(User.DUZ));
   dmodShared.AddTemplateNode(tvPersonal, FPersonalEmptyNodeCount, MyTemplate);
   dmodShared.AddTemplateNode(tvShared, FSharedEmptyNodeCount, RootTemplate);
@@ -4004,7 +4047,7 @@
   R := TRichEdit(Sender).ClientRect;
   if (FLastRect.Right <> R.Right) or
-     (FLastRect.Bottom <> R.Bottom) or
-     (FLastRect.Left <> R.Left) or
-     (FLastRect.Top <> R.Top) then
+    (FLastRect.Bottom <> R.Bottom) or
+    (FLastRect.Left <> R.Left) or
+    (FLastRect.Top <> R.Top) then
   begin
     FLastRect := R;
@@ -4038,6 +4081,6 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
-     FCanDoCOMObjects and (FCurTree = tvShared)) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
+    FCanDoCOMObjects and (FCurTree = tvShared)) then
   begin
     Template := TTemplate(FCurTree.Selected.Data);
@@ -4058,6 +4101,6 @@
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
-     FCanDoCOMObjects and (FCurTree = tvShared)) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
+    FCanDoCOMObjects and (FCurTree = tvShared)) then
   begin
     Template := TTemplate(FCurTree.Selected.Data);
@@ -4078,5 +4121,5 @@
   if assigned(ANode) then
   begin
-    if(not assigned(ANode.Data)) or (TTemplate(ANode.Data).RealType <> ttClass) then
+    if (not assigned(ANode.Data)) or (TTemplate(ANode.Data).RealType <> ttClass) then
     begin
       Node := ANode.Parent;
@@ -4087,18 +4130,18 @@
             Node := nil
           else
-          if (TTemplate(Node.Data).RealType in AllTemplateLinkTypes) then
-          begin
-            case TTemplate(Node.Data).RealType of
-              ttTitles:     Result := ltTitle;
-              ttConsults:   Result := ltConsult;
-              ttProcedures: Result := ltProcedure;
-            end;
-          end
-          else
-            Node := Node.Parent;
+            if (TTemplate(Node.Data).RealType in AllTemplateLinkTypes) then
+            begin
+              case TTemplate(Node.Data).RealType of
+                ttTitles: Result := ltTitle;
+                ttConsults: Result := ltConsult;
+                ttProcedures: Result := ltProcedure;
+              end;
+            end
+            else
+              Node := Node.Parent;
         end
         else
           Node := nil;
-      until(Result <> ltNone) or (not assigned(Node));
+      until (Result <> ltNone) or (not assigned(Node));
     end;
   end;
@@ -4106,5 +4149,5 @@
 
 procedure TfrmTemplateEditor.cbxLinkNeedData(Sender: TObject;
-  const StartFrom: String; Direction, InsertAt: Integer);
+  const StartFrom: string; Direction, InsertAt: Integer);
 var
   tmpSL: TStringList;
@@ -4116,13 +4159,13 @@
   try
     case TTemplateLinkType(pnlLink.Tag) of
-      ltTitle:     FastAssign(SubSetOfAllTitles(StartFrom, Direction), tmpSL);
+      ltTitle: FastAssign(SubSetOfAllTitles(StartFrom, Direction), tmpSL);
 //      ltConsult:
       ltProcedure:
         begin
           FastAssign(SubSetOfProcedures(StartFrom, Direction), tmpSL);
-          for i := 0 to tmpSL.Count-1 do
+          for i := 0 to tmpSL.Count - 1 do
           begin
             tmp := tmpSL[i];
-            setpiece(tmp,U,1,piece(piece(tmp,U,4),';',1));
+            setpiece(tmp, U, 1, piece(piece(tmp, U, 4), ';', 1));
             tmpSL[i] := tmp;
           end;
@@ -4135,12 +4178,12 @@
 end;
 
-procedure TfrmTemplateEditor.cbxLinkChange(Sender: TObject);
-var
-  Template,LinkTemplate: TTemplate;
+procedure TfrmTemplateEditor.cbxLinkExit(Sender: TObject);
+var
+  Template, LinkTemplate: TTemplate;
   update: boolean;
 
 begin
-  if((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
-     (FCurTree = tvShared)) then
+  if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
+    (FCurTree = tvShared)) then
   begin
     Template := TTemplate(FCurTree.Selected.Data);
@@ -4154,5 +4197,6 @@
         begin
           ShowMsg(GetLinkName(cbxLink.ItemID, TTemplateLinkType(pnlLink.tag)) +
-                      ' is already assigned to another template.');
+            ' is already assigned to another template.');
+          cbxLink.SetFocus;
           cbxLink.SelectByID(Template.LinkIEN);
           update := False;
@@ -4162,5 +4206,5 @@
           Template.FileLink := ConvertFileLink(cbxLink.ItemID, TTemplateLinkType(pnlLink.tag));
           if Template.LinkName <> '' then
-            edtName.Text := copy(Template.LinkName,1,edtName.MaxLength);
+            edtName.Text := copy(Template.LinkName, 1, edtName.MaxLength);
         end;
       end
@@ -4180,5 +4224,5 @@
     if ssShift in Shift then
       FindNextControl(Sender as TWinControl, False, True, False).SetFocus //previous control
-    else if ssCtrl	in Shift then
+    else if ssCtrl in Shift then
       FindNextControl(Sender as TWinControl, True, True, False).SetFocus; //next control
     FNavigatingTab := False;
@@ -4194,5 +4238,5 @@
 begin
   if FNavigatingTab then
-    Key := #0;  //Disable shift-tab processinend;
+    Key := #0; //Disable shift-tab processinend;
 end;
 
@@ -4202,5 +4246,5 @@
   //The navigating tab controls were inadvertantently adding tab characters
   //This should fix it
-  FNavigatingTab := (Key = VK_TAB) and ([ssShift,ssCtrl] * Shift <> []);
+  FNavigatingTab := (Key = VK_TAB) and ([ssShift, ssCtrl] * Shift <> []);
   if FNavigatingTab then
     Key := 0;
