Index: cprs/branches/tmg-cprs/CPRS-Chart/Templates/fTemplateDialog.pas
===================================================================
--- cprs/branches/tmg-cprs/CPRS-Chart/Templates/fTemplateDialog.pas	(revision 698)
+++ cprs/branches/tmg-cprs/CPRS-Chart/Templates/fTemplateDialog.pas	(revision 729)
@@ -47,4 +47,7 @@
     FAnswerOpenTag : string; //kt added 12/28/09
     FAnswerCloseTag : string; //kt added 12/28/09
+    NameToObjID : TStringList; //kt added 3/26/10
+    Formulas  : TStringList; //kt added 3/26/10
+    TxtObjects  : TStringList; //kt added 3/28/10
     procedure SizeFormToCancelBtn();
     procedure ChkAll(Chk: boolean);
@@ -81,10 +84,4 @@
   frmTemplateDialog: TfrmTemplateDialog;
 
-const
-  HTMLBEGINNINGTAG = '{HTML:';   //kt
-  HTMLENDINGTAG = '}';       //kt
-  HTMLBEGINNINGTAGLEN = length(HTMLBEGINNINGTAG);  //kt
-  HTMLENDINGTAGLEN = length(HTMLENDINGTAG);   //kt
-
 implementation
 
@@ -153,9 +150,10 @@
   i, j, idx, Indent: integer;
   DlgProps, Txt: string;
+  Temp : string; //kt
+  Changed : boolean; //kt
   DlgIDCounts: TStringList;
   DlgInt: TIntStruc;
   CancelDlg: Boolean;
   CancelMsg: String;
-
 
   procedure IncDlgID(var id: string); //Appends an item count in the form of id.0, id.1, id.2, etc
@@ -221,5 +219,7 @@
     frmTemplateDialog.HTMLAnswerOpenTag := '<I>'; //kt 12/28/09
     //SL.Text := RemoveHTMLTags(SL.Text);  //elh
-    AssignFieldIDs(SL);
+    AssignFieldIDs(SL,frmTemplateDialog.NameToObjID);   //kt  added NameToObjID param 3/26/10
+    HideFormulas(SL,frmTemplateDialog.Formulas); //kt added 3/26/10
+    HideTxtObjects(SL,frmTemplateDialog.TxtObjects); //kt added 3/28/10
     frmTemplateDialog.SL := SL;
     frmTemplateDialog.Index := '';
@@ -231,17 +231,13 @@
     repeat
       i := pos(ObjMarker, Txt);
-      if(i > 1) then
-      begin
+      if(i > 1) then begin
         j := pos(DlgPropMarker, Txt);
-        if(j > 0) then
-          begin
+        if(j > 0) then begin
           DlgProps := copy(Txt, j + DlgPropMarkerLen, (i - j - DlgPropMarkerLen));
           CountDlgProps(DlgProps);
-          end
-        else
-          begin
+        end else begin
           DlgProps := '';
           j := i;
-          end;
+        end;
         inc(frmTemplateDialog.Count);
         frmTemplateDialog.Index := frmTemplateDialog.Index +
@@ -249,14 +245,14 @@
         inc(idx,i+ObjMarkerLen-1);
         Indent := StrToIntDef(Piece(DlgProps, ';', 5),0);
-        if(frmTemplateDialog.FirstIndent > Indent) then
+        if(frmTemplateDialog.FirstIndent > Indent) then begin
           frmTemplateDialog.FirstIndent := Indent;
-      end;
-      if(i > 0) then
+        end;
+      end;
+      if(i > 0) then begin
         delete(txt, 1, i + ObjMarkerLen - 1);
+      end;
     until (i = 0);
-    if(frmTemplateDialog.Count > 0) then
-    begin
-      if(frmTemplateDialog.OneOnly) then
-      begin
+    if(frmTemplateDialog.Count > 0) then begin
+      if(frmTemplateDialog.OneOnly) then begin
         frmTemplateDialog.btnNone.Visible := FALSE;
         frmTemplateDialog.btnAll.Visible := FALSE;
@@ -264,42 +260,53 @@
       frmTemplateDialog.BuildAllControls;
       repeat
-         if (assigned(frmNotes)) and (frmTemplateDialog.HTMLMode) then frmNotes.HTMLEditor.SetMsgActive(False);  //kt 2-1-10
-         frmTemplateDialog.ShowModal;
-         if (assigned(frmNotes)) and (frmTemplateDialog.HTMLMode) then frmNotes.HTMLEditor.SetMsgActive(True);   //kt 2-1-10
-         if(frmTemplateDialog.ModalResult = mrOK) then begin
-           GetText(SL, TRUE);     {TRUE = Include embedded fields}
-           {
-           if uTemplates.bUsingHTMLMode then begin
-              SL.Text := FormatHTMLTags(SL.Text);
-           end else begin
-              SL.Text := RemoveHTMLTags(SL.Text);
-           end;
-           }
-         end else begin
-          if (not PreviewMode) and (not frmTemplateDialog.Silent) and (not uInit.TimedOut) then
-            begin
-//            CancelMsg := 'If you cancel, your changes will not be saved.  Are you sure you want to cancel?';  <-- original line.  //kt 8/8/2007
-              CancelMsg := DKLangConstW('fTemplateDialog_If_you_cancelx_your_changes_will_not_be_savedx__Are_you_sure_you_want_to_cancelx'); //kt added 8/8/2007
-//            if (InfoBox(CancelMsg, 'Cancel Dialog Processing', MB_YESNO or MB_DEFBUTTON2 or MB_ICONQUESTION) = ID_YES) then   <-- original line.  //kt 8/8/2007
-              if (InfoBox(CancelMsg, DKLangConstW('fTemplateDialog_Cancel_Dialog_Processing'), MB_YESNO or MB_DEFBUTTON2 or MB_ICONQUESTION) = ID_YES) then  //kt added 8/8/2007
-                begin
-                  SL.Clear;
-                  Result := TRUE;
-                  CancelDlg := TRUE;
-                end
-              else
-                CancelDlg := FALSE;
-            end
-          else
-            begin
+        if (assigned(frmNotes)) and (frmTemplateDialog.HTMLMode) then frmNotes.HTMLEditor.SetMsgActive(False);  //kt 2-1-10
+        frmTemplateDialog.ShowModal;
+        if (assigned(frmNotes)) and (frmTemplateDialog.HTMLMode) then frmNotes.HTMLEditor.SetMsgActive(True);   //kt 2-1-10
+        if(frmTemplateDialog.ModalResult = mrOK) then begin
+          //kt -- begin mod --  3/27/10
+          Changed := RestoreTransformTxtObjects(SL, frmTemplateDialog.TxtObjects, frmTemplateDialog.NameToObjID);
+          Changed := RestoreTransformFormulas(SL, frmTemplateDialog.Formulas, frmTemplateDialog.NameToObjID) or Changed;
+          if Changed then begin //kt 3/27/10
+            Txt := SL.Text;
+            i := pos(ObjMarker, Txt);
+            if(i > 1) then begin
+              j := pos(DlgPropMarker, Txt);
+              if (j <= 0) then j := i;
+              Temp := frmTemplateDialog.Index;
+              SetPiece(Temp,'~',2,IntToStr(j-1));
+              frmTemplateDialog.Index := Temp;
+            end;
+          end;
+          //kt -- end mod --
+          GetText(SL, TRUE);     {TRUE = Include embedded fields}
+          {
+          if uTemplates.bUsingHTMLMode then begin
+             SL.Text := FormatHTMLTags(SL.Text);
+          end else begin
+             SL.Text := RemoveHTMLTags(SL.Text);
+          end;
+          }
+        end else begin
+          if (not PreviewMode) and (not frmTemplateDialog.Silent) and (not uInit.TimedOut) then begin
+//          CancelMsg := 'If you cancel, your changes will not be saved.  Are you sure you want to cancel?';  <-- original line.  //kt 8/8/2007
+            CancelMsg := DKLangConstW('fTemplateDialog_If_you_cancelx_your_changes_will_not_be_savedx__Are_you_sure_you_want_to_cancelx'); //kt added 8/8/2007
+//          if (InfoBox(CancelMsg, 'Cancel Dialog Processing', MB_YESNO or MB_DEFBUTTON2 or MB_ICONQUESTION) = ID_YES) then   <-- original line.  //kt 8/8/2007
+            if (InfoBox(CancelMsg, DKLangConstW('fTemplateDialog_Cancel_Dialog_Processing'), MB_YESNO or MB_DEFBUTTON2 or MB_ICONQUESTION) = ID_YES) then begin  //kt added 8/8/2007
               SL.Clear;
               Result := TRUE;
               CancelDlg := TRUE;
+            end else begin
+              CancelDlg := FALSE;
             end;
-         end;      
+          end else begin
+            SL.Clear;
+            Result := TRUE;
+            CancelDlg := TRUE;
+          end;
+        end;
       until CancelDlg or (frmTemplateDialog.ModalResult = mrOK)
-    end
-    else
+    end else begin
       SL.Clear;
+    end;
   finally
     //frmTemplateDialog.Free;    v22.11e RV
@@ -324,5 +331,5 @@
   tempString := Txt;
   //here we will strip out all HTML formatting tags  //elh
-  beginning := pos(HTMLBEGINNINGTAG, tempString);
+  beginning := pos(HTML_BEGIN_TAG, tempString);
   if beginning = 0 then begin
      Result := Txt;
@@ -331,9 +338,9 @@
     begin
       tempResult := tempResult + Leftstr(tempString,beginning-1);
-      tempString := Rightstr(tempString,length(tempString)-beginning-HTMLBEGINNINGTAGLEN);
-      ending := pos(HTMLENDINGTAG, tempString);
+      tempString := Rightstr(tempString,length(tempString)-beginning-HTML_BEGIN_TAGLEN);
+      ending := pos(HTML_ENDING_TAG, tempString);
       tempString := Rightstr(tempString,length(tempString)-ending);
-      beginning := pos(HTMLBEGINNINGTAG, tempString);
-  //    tempString := Midstr(Txt,i,HTMLBEGINNINGTAGLEN);
+      beginning := pos(HTML_BEGIN_TAG, tempString);
+  //    tempString := Midstr(Txt,i,HTML_BEGIN_TAGLEN);
     end;
     Result := tempResult + tempString;
@@ -348,5 +355,5 @@
   tempString := Txt;
   //here we will strip out all HTML formatting tags  //elh
-  beginning := pos(HTMLBEGINNINGTAG, tempString);
+  beginning := pos(HTML_BEGIN_TAG, tempString);
   if beginning = 0 then begin
      Result := Txt;
@@ -355,10 +362,10 @@
     begin
       tempResult := tempResult + Leftstr(tempString,beginning-1);
-      tempString := Rightstr(tempString,length(tempString)-beginning-HTMLBEGINNINGTAGLEN+1);
-      ending := pos(HTMLENDINGTAG, tempString);
+      tempString := Rightstr(tempString,length(tempString)-beginning-HTML_BEGIN_TAGLEN+1);
+      ending := pos(HTML_ENDING_TAG, tempString);
       tempResult := tempResult + Leftstr(tempString,ending-1);
       tempString := Rightstr(tempString,length(tempString)-ending);
-      beginning := pos(HTMLBEGINNINGTAG, tempString);
-  //    tempString := Midstr(Txt,i,HTMLBEGINNINGTAGLEN);
+      beginning := pos(HTML_BEGIN_TAG, tempString);
+  //    tempString := Midstr(Txt,i,HTML_BEGIN_TAGLEN);
     end;
     Result := tempResult + tempString;
@@ -565,5 +572,5 @@
   end;
   tmp := copy(SL.Text, p1, p2);
-  tmp := RemoveHTMLTags(tmp);
+  tmp := RemoveHTMLTags(tmp);  //kt
   if(copy(tmp, length(tmp)-1, 2) = CRLF) then
     delete(tmp, length(tmp)-1, 2);
@@ -747,4 +754,7 @@
   Entries := TStringList.Create;
   NoTextID := TStringList.Create;
+  NameToObjID := TStringList.Create ; //kt
+  Formulas  := TStringList.Create ; //kt
+  TxtObjects  := TStringList.Create; //kt added 3/28/10
   FOldHintEvent := Application.OnShowHint;
   Application.OnShowHint := AppShowHint;
@@ -774,4 +784,7 @@
   Entries.Free;
   BuildIdx.Free;
+  NameToObjID.Free; //kt
+  Formulas.Free; //kt
+  TxtObjects.Free; //kt
 end;
 
