Index: cprs/branches/tmg-cprs/CPRS-Chart/fNotes.pas
===================================================================
--- cprs/branches/tmg-cprs/CPRS-Chart/fNotes.pas	(revision 453)
+++ cprs/branches/tmg-cprs/CPRS-Chart/fNotes.pas	(revision 541)
@@ -1,3 +1,3 @@
-//kt -- Modified with SourceScanner on 7/19/2007
+//kt -- Modified with SourceScanner on 7/19/2007, also 8/09
 unit fNotes;
 {$O-}
@@ -9,5 +9,14 @@
   fHSplit, StdCtrls, ExtCtrls, Menus, ComCtrls, ORCtrls, ORFn, uConst, ORDtTm,
   uPCE, ORClasses, fDrawers, ImgList, rTIU, uTIU, uDocTree, fRptBox, fPrintList,
-  fNoteST, ORNet, fNoteSTStop, OleCtrls, SHDocVw, DKLang;
+  MSHTML,TMGHTML2,ActiveX,  //kt added this line 8/09 for HTML editing
+  fNoteST, ORNet, fNoteSTStop, OleCtrls, SHDocVw, DKLang, Buttons, ToolWin;
+
+type
+  TEditModes = (emNone,emText,emHTML);        //kt 8/09
+  TViewModes = (vmEdit,vmView,vmText,vmHTML); //kt 8/09
+  TViewModeSet = Set of TViewModes;           //kt 8/09
+const
+  vmHTML_MODE : array [false..true] of TViewModes = (vmText,vmHTML); //kt 8/09
+  emHTML_MODE : array [false..true] of TEditModes = (emText,emHTML); //kt 8/09
 
 type
@@ -150,4 +159,25 @@
     mnuViewRemoteData: TMenuItem;
     mnuViewPostings: TMenuItem;
+    pnlHtmlViewer: TPanel;                 //kt 8/09
+    pnlTextWrite: TPanel;                  //kt 8/09
+    popNoteMemoHTMLFormat: TMenuItem;      //kt 8/09
+    pnlHTMLWrite: TPanel;                  //kt 8/09
+    pnlHTMLEdit: TPanel;                   //kt 8/09
+    ToolBar: TToolBar;                     //kt 8/09
+    cbFontNames: TComboBox;                //kt 8/09
+    cbFontSize: TComboBox;                 //kt 8/09
+    btnFonts: TSpeedButton;                //kt 8/09
+    btnItalic: TSpeedButton;               //kt 8/09
+    btnBold: TSpeedButton;                 //kt 8/09
+    btnUnderline: TSpeedButton;            //kt 8/09
+    btnBullets: TSpeedButton;              //kt 8/09
+    btnNumbers: TSpeedButton;              //kt 8/09
+    btnLeftAlign: TSpeedButton;            //kt 8/09
+    btnCenterAlign: TSpeedButton;          //kt 8/09
+    btnRightAlign: TSpeedButton;           //kt 8/09
+    btnMoreIndent: TSpeedButton;           //kt 8/09
+    btnLessIndent: TSpeedButton;           //kt 8/09
+    btnTextColor: TSpeedButton;            //kt 8/09
+    btnBackColor: TSpeedButton;            //kt 8/09
     procedure mnuChartTabClick(Sender: TObject);
     procedure lstNotesClick(Sender: TObject);
@@ -222,6 +252,5 @@
     procedure memNewNoteKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
     procedure sptHorzCanResize(Sender: TObject; var NewSize: Integer; var Accept: Boolean);
-    procedure WebBrowser1DocumentComplete(Sender: TObject;   //kt added
-      const pDisp: IDispatch; var URL: OleVariant);          //kt added
+    //kt procedure WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);          //kt added
     procedure popNoteMemoInsTemplateClick(Sender: TObject);
     procedure popNoteMemoPreviewClick(Sender: TObject);
@@ -235,6 +264,4 @@
     procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
       Y: Integer);
-    procedure WebBrowser1CommandStateChange(Sender: TObject;
-      Command: Integer; Enable: WordBool);   //kt
     procedure memNewNoteKeyPress(Sender: TObject; var Key: Char);
     procedure memNewNoteKeyUp(Sender: TObject; var Key: Word;
@@ -245,4 +272,20 @@
     procedure ViewInfo(Sender: TObject);
     procedure mnuViewInformationClick(Sender: TObject);
+    procedure btnBackColorClick(Sender: TObject);    //kt 8/09
+    procedure btnBoldClick(Sender: TObject);         //kt 8/09
+    procedure btnBulletsClick(Sender: TObject);      //kt 8/09
+    procedure btnCenterAlignClick(Sender: TObject);  //kt 8/09
+    procedure btnFontsClick(Sender: TObject);        //kt 8/09
+    procedure btnItalicClick(Sender: TObject);       //kt 8/09
+    procedure btnLeftAlignClick(Sender: TObject);    //kt 8/09
+    procedure btnLessIndentClick(Sender: TObject);   //kt 8/09
+    procedure btnMoreIndentClick(Sender: TObject);   //kt 8/09
+    procedure btnNumbersClick(Sender: TObject);      //kt 8/09
+    procedure btnRightAlignClick(Sender: TObject);   //kt 8/09
+    procedure btnTextColorClick(Sender: TObject);    //kt 8/09
+    procedure btnUnderlineClick(Sender: TObject);    //kt 8/09
+    procedure cbFontNamesChange(Sender: TObject);    //kt 8/09
+    procedure cbFontSizeChange(Sender: TObject);     //kt 8/09
+    procedure popNoteMemoHTMLFormatClick(Sender: TObject); //kt 8/09
   private
     FNavigatingTab : Boolean; //Currently Using tab to navigate
@@ -256,4 +299,6 @@
     FImageFlag: TBitmap;
     FEditNote: TEditNoteRec;
+    FViewNote : TStringList;  //kt 8/09
+    FWarmedUp : boolean;      //kt 8/09
     FVerifyNoteTitle: Integer;
     FDocList: TStringList;
@@ -271,4 +316,6 @@
     LastAuthor: Int64;  //kt 6-1-05
     LastAuthorName: string; //kt 6-1-05
+    FHTMLEditMode : TEditModes;  //kt 7/09 This is the mode of the note being edited (even if not actively displayed)
+    FViewMode : TViewModeSet;    //kt 7/09  This is the status of the display
     procedure frmFramePnlPatientExit(Sender: TObject);
     procedure frmDrawerPnlTemplatesButtonExit(Sender: TObject);
@@ -307,6 +354,14 @@
     function SetNoteTreeLabel(AContext: TTIUContext): string;
     procedure UpdateNoteAuthor(DocInfo: string);
+    procedure SetHTMLEditMode(HTMLEditMode : boolean; Quiet : Boolean=false);  //kt
+    procedure ToggleHTMLEditMode;                                              //kt
+    procedure BroadcastImages(Note: TStrings);                                 //kt
+    procedure ProperRepaint(Editing : Boolean);                                //kt
+    procedure SetEditorFocus;                                                  //kt
+    function  EditorHasText : boolean;                                         //kt
   public
-    HTMLfilename : string;   //kt added
+    HtmlEditor : THtmlObj; //kt 8/09
+    HtmlViewer : THtmlObj; //kt 8/09
+    procedure SetDisplayToHTMLvsText(Mode :TViewModeSet; Lines : TStrings; ActivateOnly : boolean=False); //kt 8/09
     function ActiveEditOf(AnIEN: Int64; ARequest: integer): Boolean;
     function AllowContextChange(var WhyNot: string): Boolean; override;
@@ -319,5 +374,8 @@
     procedure AssignRemForm;
     property  OrderID: string read FOrderID;
+    property  ViewMode :TViewModeSet read FViewMode;   //kt 8/09
     procedure LstNotesToPrint;
+    constructor Create(AOwner: TComponent); override;  //kt 8/09
+    destructor Destroy; override;                      //kt 8/09
   published
     property Drawers: TFrmDrawers read GetDrawers; // Keep Drawers published
@@ -339,6 +397,6 @@
      uAccessibleTreeView, uAccessibleTreeNode, fTemplateDialog, DateUtils,
      StrUtils {//KT added 1-1-05},
-     mshtml,  {//kt added 5-2-05}
-     fImages, {//kt added 9-20-05}
+     //fImages, {//kt added 9-20-05}
+     fOptionsNotes, //kt 8/09
      rHTMLTools;  {//kt added 5-27-05 for IsHTMLDocument}
 
@@ -352,4 +410,6 @@
   NT_ACT_EDIT_NOTE = 4;
   NT_ACT_ID_ENTRY  = 5;
+
+  VIEW_ACTIVATE_ONLY = true; //kt 8/09
 
 //TX_NEED_VISIT = 'A visit is required before creating a new progress note.';  <-- original line.  //kt 7/19/2007
@@ -534,4 +594,19 @@
   uIDNotesActive: Boolean;
 
+constructor TfrmNotes.Create(AOwner: TComponent);
+//kt Added function 8/09
+begin
+  inherited Create(AOwner);
+  FViewNote := TStringList.Create;
+end;
+
+destructor TfrmNotes.Destroy;
+//kt Added function 8/09
+begin
+  FViewNote.Free;
+  FEditNote.Lines.Free; //kt
+  inherited Destroy;
+end;
+
 
 { TPage common methods --------------------------------------------------------------------- }
@@ -578,10 +653,12 @@
     case BOOLCHAR[frmFrame.CCOWContextChanging] of
       '1': begin
-             if memNewNote.GetTextLen > 0 then
+             if ((vmHTML in FViewMode) and (HTMLEditor.Text <> ''))                 //kt 8/09
+               or ((not (vmHTML in FViewMode)) and (memNewNote.GetTextLen > 0 )) then begin  //kt
 //             WhyNot := WhyNot + 'A note in progress will be saved as unsigned.  '  <-- original line.  //kt 7/19/2007
                WhyNot := WhyNot + DKLangConstW('fNotes_A_note_in_progress_will_be_saved_as_unsignedx') //kt added 7/19/2007
-             else
+             end else begin                                                         //kt
 //             WhyNot := WhyNot + 'An empty note in progress will be deleted.  ';  <-- original line.  //kt 7/19/2007
                WhyNot := WhyNot + DKLangConstW('fNotes_An_empty_note_in_progress_will_be_deletedx'); //kt added 7/19/2007
+             end;                                                                   //kt
              Result := False;
            end;
@@ -649,4 +726,8 @@
   lstNotes.Clear;
   memNote.Clear;
+  HTMLViewer.Clear;   //kt
+  HTMLEditor.Clear;   //kt
+  FWarmedUp := false; //kt
+  SetDisplayToHTMLvsText([vmText,vmView],nil,VIEW_ACTIVATE_ONLY); //kt
   memPCEShow.Clear;
   uPCEShow.Clear;
@@ -774,5 +855,6 @@
     Addend       := 0;
     {LastCosigner & LastCosignerName aren't cleared because they're used as default for next note.}
-    Lines        := nil;
+    //kt 8/09  Lines        := nil;
+    if Assigned (Lines) then Lines.Clear;  //kt 8/09
     PRF_IEN := 0;
     ActionIEN := '';
@@ -781,4 +863,7 @@
   txtSubject.Text := '';
   if memNewNote <> nil then memNewNote.Clear; //CQ7012 Added test for nil
+  HTMLEditor.Clear;  //kt 8/09
+  HTMLViewer.Clear;  //kt 8/09
+  FHTMLEditMode := emNone; //kt 8/09
   timAutoSave.Enabled := False;
   // clear the PCE object for editing
@@ -795,5 +880,9 @@
   if(ShouldShow) then
     sptVert.Top := memPCEShow.Top - sptVert.Height;
-  memNote.Invalidate;
+  if (vmHTML in FViewMode) then begin              //kt 8/09
+    HTMLViewer.Invalidate;                         //kt 8/09
+  end else begin                                   //kt 8/09
+    memNote.Invalidate;
+  end;                                             //kt 8/09
 end;
 
@@ -1047,4 +1136,6 @@
   x, WhyNot, DocInfo: string;
   tempPos : integer; //kt 6-1-05
+  Mode : TViewModeSet; //kt 8/09
+  BoilerplateIsHTML : boolean;//kt 8/09
 
 begin
@@ -1057,5 +1148,7 @@
   try
     ClearEditControls;
+    FEditNote.Lines.Free; //(done d/t full wipe-out on line below}  //kt 8/09
     FillChar(FEditNote, SizeOf(FEditNote), 0);  //v15.7
+    FEditNote.Lines := TStringList.Create; //Freed in Destructor    //kt 8/09
     with FEditNote do
     begin
@@ -1074,6 +1167,6 @@
           TitleName := '';
         end;
-      //DateTime     := FMNow;  //kt 6-1-05
-      DateTime     := Encounter.DateTime;  //kt 6-1-05
+      //DateTime     := FMNow;                    //kt 6-1-05
+      DateTime     := Encounter.DateTime;         //kt 6-1-05
       //kt begin changes -------------- 6-1-05
       if LastAuthor<>0 then begin
@@ -1174,7 +1267,14 @@
         FChanged := False;
         cmdChangeClick(Self); // will set captions, sign state for Changes
+        Mode := [vmEdit] + [vmHTML_MODE[fOptionsNotes.DefaultEditHTMLMode]];   //kt 8/09
+        SetDisplayToHTMLvsText(Mode, nil, VIEW_ACTIVATE_ONLY);                 // kt 8/09
         lstNotesClick(Self);  // will make pnlWrite visible
         if timAutoSave.Interval <> 0 then EnableAutosave := TRUE;
-        if txtSubject.Visible then txtSubject.SetFocus else memNewNote.SetFocus;
+        //kt 6/3/09  original --> if txtSubject.Visible then txtSubject.SetFocus else memNewNote.SetFocus;
+        if txtSubject.Visible then begin                          //kt 8/09
+          txtSubject.SetFocus;                                    //kt 8/09
+        end else begin                                            //kt 8/09
+          SetEditorFocus; //kt memNewNote.SetFocus;               //kt 8/09
+        end;                                                      //kt 8/09
       end else
       begin
@@ -1195,5 +1295,15 @@
       DocInfo := MakeXMLParamTIU(IntToStr(CreatedNote.IEN), FEditNote);
       ExecuteTemplateOrBoilerPlate(TmpBoilerPlate, FEditNote.Title, ltTitle, Self, 'Title: ' + FEditNote.TitleName, DocInfo);
-      memNewNote.Lines.Assign(TmpBoilerPlate);
+      //kt original --> memNewNote.Lines.Assign(TmpBoilerPlate);
+      BoilerplateIsHTML := rHTMLTools.IsHTML(TmpBoilerPlate.Text);    //kt 8/09
+      FEditNote.Lines.Assign(TmpBoilerPlate);                         //kt 8/09
+      if not ((vmHTML in FViewMode)) and BoilerplateIsHTML then begin //kt 8/09
+        FViewMode := FViewMode - [vmText] + [vmHTML];                 //kt 8/09
+      end;                                                            //kt 8/09
+      SetDisplayToHTMLvsText(FViewMode,FEditNote.Lines);              //kt 8/09
+      if (vmHTML in FViewMode) then begin                             //kt 8/09
+        HtmlEditor.MoveCaretToEnd;                                    //kt 8/09
+        Application.ProcessMessages;                                  //kt 8/09
+      end;                                                            //kt 8/09
       UpdateNoteAuthor(DocInfo);
       TmpBoilerPlate.Free;
@@ -1204,4 +1314,22 @@
   frmNotes.pnlWriteResize(Self);
 end;
+
+
+procedure TfrmNotes.SetEditorFocus;
+//kt added function 8/09
+begin
+  try
+    if (vmHTML in FViewMode) then begin  //kt 8/09
+      HtmlEditor.SetFocus;               //kt 8/09
+    end else begin                       //kt 8/09
+      memNewNote.SetFocus;
+    end;                                 //kt 8/09
+  except
+    on E: Exception do begin
+      // ignore error.  Info in E
+    end;
+  end;
+end;
+
 
 procedure TfrmNotes.InsertAddendum;
@@ -1290,5 +1418,5 @@
       lstNotesClick(Self);  // will make pnlWrite visible
       if timAutoSave.Interval <> 0 then timAutoSave.Enabled := True;
-      memNewNote.SetFocus;
+      SetEditorFocus;  //kt memNewNote.SetFocus;  //kt 8/09
     end else
     begin
@@ -1307,4 +1435,6 @@
   tmpNode: TTreeNode;
   x: string;
+  Mode : TViewModeSet; //kt
+
 begin
   ClearEditControls;
@@ -1313,5 +1443,7 @@
   Changes.Add(CH_DOC, lstNotes.ItemID, GetTitleText(EditingIndex), '', CH_SIGN_YES);
   GetNoteForEdit(FEditNote, lstNotes.ItemIEN);
-  memNewNote.Lines.Assign(FEditNote.Lines);
+  Mode := [vmEdit] + [vmHTML_MODE[IsHTML(FEditNote.Lines) or (vmHTML in FViewMode)]]; //kt 8/09
+  SetDisplayToHTMLvsText(Mode,FEditNote.Lines);                                       //kt 8/09
+  //kt memNewNote.Lines.Assign(FEditNote.Lines);                                      //kt 8/09
   FChanged := False;
   if FEditNote.Title = TYP_ADDENDUM then
@@ -1361,5 +1493,5 @@
   lstNotesClick(Self);  // will make pnlWrite visible
   if timAutoSave.Interval <> 0 then timAutoSave.Enabled := True;
-  memNewNote.SetFocus;
+  SetEditorFocus;    //kt memNewNote.SetFocus;  //kt 8/09
 end;
 
@@ -1369,9 +1501,19 @@
   UpdatedNote: TCreatedDoc;
   x: string;
+  EmptyNote : boolean; //kt
+
 begin
   SetupVars;  //kt added 7/19/2007 to replace constants with vars.
   Saved := False;
-  if (memNewNote.GetTextLen = 0) or (not ContainsVisibleChar(memNewNote.Text)) then
-  begin
+  if not (vmHTML in FViewMode) then begin                         //kt 8/09
+    EmptyNote := (memNewNote.GetTextLen = 0)                      //kt 8/09
+                  or (not ContainsVisibleChar(memNewNote.Text));  //kt 8/09
+  end else begin                                                  //kt 8/09
+    EmptyNote := (HtmlEditor.GetTextLen = 0)                      //kt 8/09
+                  or (not ContainsVisibleChar(HtmlEditor.Text));  //kt 8/09
+  end;                                                            //kt 8/09
+  //kt 8/09 if (memNewNote.GetTextLen = 0) or (not ContainsVisibleChar(memNewNote.Text)) then
+  //kt 8/09  begin
+  if EmptyNote then begin
     lstNotes.ItemIndex := EditingIndex;
     x := lstNotes.ItemID;
@@ -1395,5 +1537,11 @@
   end;
   //ExpandTabsFilter(memNewNote.Lines, TAB_STOP_CHARS);
-  FEditNote.Lines    := memNewNote.Lines;
+  if FEditNote.Lines = nil then FEditNote.Lines := TStringList.Create; //kt
+  //kt 8/09 FEditNote.Lines    := memNewNote.Lines;
+  if (vmHTML in FViewMode) then begin                             //kt 8/09
+    SplitToArray(WrapHTML(HtmlEditor.HTMLText), FEditNote.Lines); //kt 8/09
+  end else begin                                                  //kt 8/09
+    FEditNote.Lines.Assign(memNewNote.Lines);                     //kt 8/09
+  end;                                                            //kt 8/09
   //FEditNote.Lines:= SetLinesTo74ForSave(memNewNote.Lines, Self);
   FEditNote.Subject  := txtSubject.Text;
@@ -1417,4 +1565,5 @@
     FNewIDChild := False;
     FChanged := False;
+    HTMLEditor.KeyStruck := false; //kt
   end else
   begin
@@ -1438,13 +1587,34 @@
 begin
   inherited;
-  //kt --- Begin Modification 9-2005 kt
+  //kt --- Begin Modification 9-2005, 8/09 -------------
   CacheDir := ExtractFilePath(ParamStr(0))+ 'Cache'; //kt 9-05
-  if not DirectoryExists(CacheDir) then begin
-    CreateDir(CacheDir);
-  end;
-  HTMLfilename := CacheDir + '\html_note.html';   //kt 5-05
+  if not DirectoryExists(CacheDir) then CreateDir(CacheDir);
+  //kt HTMLfilename := CacheDir + '\html_note.html';   //kt 5-05
   LastAuthor :=0;  //kt 5-05
   LastAuthorName:='';  //kt 5-05
-  //kt --- End Modification 9-2005 kt
+  DesiredHTMLFontSize := 2;  //Used later to downsize during printing.
+  SetRegHTMLFontSize(DesiredHTMLFontSize);   //0=SMALLEST ... 4=LARGEST
+  //kt  Note: On creation, THtmlObj will remember Application.OnMessage.  But if
+  //          another object (say a prior THtmlObj) has become active and already
+  //          changed the handler, then there will be a problem.  So probably best
+  //          to create them all at one.  //kt
+  HtmlViewer := THtmlObj.Create(pnlHTMLViewer,Application);
+  HtmlEditor := THtmlObj.Create(pnlHTMLEdit,Application);
+  TWinControl(HtmlViewer).Parent:=pnlHTMLViewer;
+  TWinControl(HtmlViewer).Align:=alClient;
+  HtmlEditor.PrevControl := cmdPCE;
+  HtmlEditor.NextControl := cmdChange;
+  //Note: A 'loaded' function will initialize the THtmlObj's, but it can't be
+  //      done until after this constructor is done, and this TfrmNotes has been
+  //      assigned a parent.  So done elsewhere.
+  HtmlViewer.PopupMenu := popNoteMemo;
+  TWinControl(HtmlEditor).Parent:=pnlHTMLEdit;
+  TWinControl(HtmlEditor).Align:=alClient;
+  HtmlEditor.PopupMenu := popNoteMemo;
+  cbFontNames.Items.Assign(Screen.Fonts);
+  FViewMode := [vmView,vmText];
+  FHTMLEditMode := emNone;
+  HTMLViewer.BackgroundColor := ReadOnlyColor;
+  //kt --- End Modification 9-2005, 8/09 ------
   PageID := CT_NOTES;
   memNote.Color := ReadOnlyColor;
@@ -1458,4 +1628,6 @@
   frmDrawers.Align := alBottom;
   frmDrawers.RichEditControl := memNewNote;
+  frmDrawers.HTMLEditControl := HtmlEditor;       //kt 8/09
+  frmDrawers.HTMLModeSwitcher := SetHTMLEditMode; //kt 8/09
   frmDrawers.NewNoteButton := cmdNewNote;
   frmDrawers.Splitter := splDrawers;
@@ -1468,4 +1640,5 @@
   FDocList := TStringList.Create;
   TAccessibleTreeView.WrapControl(tvNotes);
+  RestoreRegHTMLFontSize;                         //kt, elh  8/09
 end;
 
@@ -1475,5 +1648,9 @@
   inherited;
   pnlRight.Refresh;
-  memNote.Repaint;
+  if (vmHTML in FViewMode) then begin //kt 8/09
+    HTMLViewer.Repaint;               //kt 8/09
+  end else begin                      //kt 8/09
+    memNote.Repaint
+  end;                                //kt 8/09
 end;
 
@@ -1483,4 +1660,5 @@
 begin
   inherited;
+  //kt NOTE: I don't know how to do the equivalent for HTML.  Actually, I don't think it applies.
   LimitEditWidth(memNewNote, MAX_PROGRESSNOTE_WIDTH - 1);
 
@@ -1498,66 +1676,107 @@
 { loads the text for the selected note or displays the editing panel for the selected note }
 var
-  ImageList : TStringList;  //kt added 1/1/05
-  i       : integer;      //kt added 1/1/05
-begin
-  inherited;
-  with lstNotes do if ItemIndex = -1 then Exit
-  else if ItemIndex = EditingIndex then
-  begin
-    pnlWrite.Visible := True;
-    pnlRead.Visible := False;
-    mnuViewDetail.Enabled    := False;
-    if (FEditNote.IDParent <> 0) and (not FNewIDChild) then
-      mnuActChange.Enabled     := False
-    else
-      mnuActChange.Enabled     := True;
-    mnuActLoadBoiler.Enabled := True;
-    UpdateReminderFinish;
-  end else
-  begin
-//  StatusText('Retrieving selected progress note...');  <-- original line.  //kt 7/19/2007
-    StatusText(DKLangConstW('fNotes_Retrieving_selected_progress_notexxx')); //kt added 7/19/2007
-    Screen.Cursor := crAppStart;  //kt changed from crHourGlass; 1/1/05
-    //Screen.Cursor := crHourGlass;  //kt
-    pnlRead.Visible := True;
-    pnlWrite.Visible := False;
-    UpdateReminderFinish;
-    lblTitle.Caption := Piece(Piece(Items[ItemIndex], U, 8), ';', 1) + #9 + Piece(Items[ItemIndex], U, 2) + ', ' +
-                        Piece(Items[ItemIndex], U, 6) + ', ' + Piece(Piece(Items[ItemIndex], U, 5), ';', 2) +
-                        '  (' + FormatFMDateTime('mmm dd,yy@hh:nn', MakeFMDateTime(Piece(Items[ItemIndex], U, 3)))
-                        + ')';
-    lvNotes.Caption := lblTitle.Caption;
-    LoadDocumentText(memNote.Lines, ItemIEN);
-    memNote.SelStart := 0;
-    mnuViewDetail.Enabled    := True;
-    mnuViewDetail.Checked    := False;
-    mnuActChange.Enabled     := False;
-    mnuActLoadBoiler.Enabled := False;
-    Screen.Cursor := crDefault;
-    StatusText('');
-  end;
-  if(assigned(frmReminderTree)) then
-    frmReminderTree.EnableActions;
-
-  //kt changed to following block 5/31/05
-  if IsHTMLDocument(MemNote.Lines) then begin
-    rHTMLTools.ActivateWebBrowser;
-  end else begin
-    rHTMLTools.ActivateMemo;
-  end;
-  //------end block --------
-
+  Note      : TStrings;     //kt 6/09.  Will be pointer to FViewNote, or FEditNote.Lines
+  Editing   : boolean;      //kt 8/09
+  Mode      : TViewModeSet; //kt 8/09
+  IsHTML    : boolean;      //kt 8/09
+
+begin
+  inherited;
+  //kt 8/09  with lstNotes do if ItemIndex = -1 then Exit
+  //kt 8/09  else if ItemIndex = EditingIndex then
+
+  with lstNotes do begin                                                   //kt 8/09
+    if ItemIndex = -1 then Exit;                                           //kt 8/09
+    Editing := (ItemIndex = EditingIndex);                                 //kt 8/09
+    if Editing then begin                                                  //kt 8/09
+      if FEditNote.Lines = nil then FEditNote.Lines := TStringList.Create; //kt 8/09
+      Note := FEditNote.Lines;                                             //kt 8/09
+      {pnlWrite.Visible := True;                                           //kt 8/09
+      pnlRead.Visible := False; } //kt done in SetHTMLorTextEditor         //kt 8/09
+      mnuViewDetail.Enabled    := False;
+      if (FEditNote.IDParent <> 0) and (not FNewIDChild) then
+        mnuActChange.Enabled     := False
+      else
+        mnuActChange.Enabled     := True;
+      mnuActLoadBoiler.Enabled := True;
+      UpdateReminderFinish;
+      Mode := [vmEdit] + [vmHTML_MODE[(FHTMLEditMode=emHTML)]];            //kt 8/09
+      SetDisplayToHTMLvsText(Mode,FEditNote.Lines,VIEW_ACTIVATE_ONLY);     //kt 8/09
+      FWarmedUp := true;                                                   //kt 8/09
+    end else begin
+  //  StatusText('Retrieving selected progress note...');  <-- original line.  //kt 7/19/2007
+      StatusText(DKLangConstW('fNotes_Retrieving_selected_progress_notexxx')); //kt added 7/19/2007
+      Screen.Cursor := crAppStart;  //kt changed from crHourGlass; 1/1/05
+      //kt 8/09  pnlRead.Visible := True;
+      //kt 8/09  pnlWrite.Visible := False;
+      UpdateReminderFinish;
+      lblTitle.Caption := Piece(Piece(Items[ItemIndex], U, 8), ';', 1) + #9 + Piece(Items[ItemIndex], U, 2) + ', ' +
+                          Piece(Items[ItemIndex], U, 6) + ', ' + Piece(Piece(Items[ItemIndex], U, 5), ';', 2) +
+                          '  (' + FormatFMDateTime('mmm dd,yy@hh:nn', MakeFMDateTime(Piece(Items[ItemIndex], U, 3)))
+                          + ')';
+      lvNotes.Caption := lblTitle.Caption;
+      //kt 8/09 LoadDocumentText(memNote.Lines, ItemIEN);
+      LoadDocumentText(FViewNote, ItemIEN);  //kt 8/09
+      Note := FViewNote;                     //kt 8/09
+      memNote.SelStart := 0;
+      mnuViewDetail.Enabled    := True;
+      mnuViewDetail.Checked    := False;
+      mnuActChange.Enabled     := False;
+      mnuActLoadBoiler.Enabled := False;
+      Screen.Cursor := crDefault;
+      StatusText('');
+      IsHTML := rHTMLTools.IsHTML(FViewNote);            //kt 8/09
+      Mode := [vmView] + [vmHTML_MODE[IsHTML]];          //kt 8/09
+      SetDisplayToHTMLvsText(Mode,FViewNote);            //kt 8/09
+      if not FWarmedUp and IsHTML then begin             //kt 8/09
+        FWarmedUp := true;                               //kt 8/09
+        //First HTML page won't display without this...  //kt 8/09
+        SetDisplayToHTMLvsText(Mode,FViewNote);          //kt 8/09
+      end;                                               //kt 8/09
+    end;
+  end;  //kt 8/09
+
+  if(assigned(frmReminderTree)) then frmReminderTree.EnableActions;
   //DisplayPCE;    //kt 5-31-05 (moved down below)
   pnlRight.Refresh;
-  memNewNote.Repaint;
-  memNote.Repaint;
+  ProperRepaint(Editing); //kt 8/09
+  //kt 8/09 memNewNote.Repaint;
+  //kt 8/09 memNote.Repaint;
   NotifyOtherApps(NAE_REPORT, 'TIU^' + lstNotes.ItemID);
-  DisplayPCE;   //kt 5-31-05 (move down from above)
+  //kt RESTORE LATER... frmImages.NewNoteSelected(Editing);  //kt 9/05
+  DisplayPCE;                          //kt 5-31-05 (move down from above)
+  BroadcastImages(Note);               //kt 8/09
 
   //-----------------------------
-  frmImages.NewNoteSelected(lstNotes.ItemIndex=EditingIndex);  //kt added 9-20-05
-  //kt added the following block:  1/1/05
+  //kt RESTORE LATER... frmImages.NewNoteSelected(lstNotes.ItemIndex=EditingIndex);  //kt added 9-20-05
+end;
+
+procedure TfrmNotes.ProperRepaint(Editing : Boolean);
+//kt Added function 8/09
+begin
+  if Editing then begin
+    if (vmHTML in FViewMode) then begin
+      HtmlEditor.Repaint;
+    end else begin
+      memNewNote.Repaint;
+    end;
+  end else begin
+    if (vmHTML in FViewMode) then begin
+      HtmlViewer.Repaint;
+    end else begin
+      memNote.Repaint;
+    end;
+  end;
+  Application.ProcessMessages;
+end;
+
+procedure TfrmNotes.BroadcastImages(Note: TStrings);
+//kt added function 8/09
+var
+  ImageList : TStringList;
+  i         : integer;
+begin
   ImageList := TStringList.Create;
-  if rHTMLTools.CheckForImageLink(memNote.Lines, ImageList) then begin
+  if rHTMLTools.CheckForImageLink(Note, ImageList) then begin
     for i:= 0 to ImageList.Count-1 do begin
       NotifyOtherApps(NAE_REPORT, 'IMAGE^' + ImageList.Strings[i]);
@@ -1565,6 +1784,14 @@
   end;
   ImageList.Free;
-  //kt end of addition 1/1/05
-  //-----------------------------
+end;
+
+function TfrmNotes.EditorHasText : boolean;
+//kt added function  8/09
+begin
+  if (vmHTML in FViewMode) then begin
+    Result := (HTMLViewer.Text <> '');
+  end else begin
+    Result := (memNote.Lines.Count > 0);
+  end;
 end;
 
@@ -1590,5 +1817,6 @@
       PCEObj := nil;
       AnIEN := lstNotes.ItemIEN;
-      if (AnIEN <> 0) and (memNote.Lines.Count > 0) then
+      //kt 8/09 if (AnIEN <> 0) and (memNote.Lines.Count > 0) then
+      if (AnIEN <> 0) and EditorHasText then                          //kt 8/09
       begin
         ActOnDocument(ActionSts, AnIEN, 'VIEW');
@@ -1650,5 +1878,6 @@
   begin
     ExecuteTemplateOrBoilerPlate(BoilerText, FEditNote.Title, ltTitle, Self, 'Title: ' + FEditNote.TitleName, DocInfo);
-    memNewNote.Lines.Assign(BoilerText);
+    SetDisplayToHTMLvsText([vmHTML,vmEdit],BoilerText); //kt 8/09
+    //kt  8/09  memNewNote.Lines.Assign(BoilerText);
     UpdateNoteAuthor(DocInfo);
     FChanged := False;
@@ -1661,5 +1890,9 @@
   BoilerText := TStringList.Create;
   try
-    NoteEmpty := memNewNote.Text = '';
+    if (vmHTML in FViewMode) then begin           //kt 8/09
+      NoteEmpty := (HTMLEditor.Text = '');        //kt 8/09
+    end else begin                                //kt 8/09
+      NoteEmpty := memNewNote.Text = '';
+    end;                                          //kt 8/09
     LoadBoilerPlate(BoilerText, FEditNote.Title);
     if (BoilerText.Text <> '') or
@@ -1673,5 +1906,9 @@
         1: begin
              ExecuteTemplateOrBoilerPlate(BoilerText, FEditNote.Title, ltTitle, Self, 'Title: ' + FEditNote.TitleName, DocInfo);
-             memNewNote.Lines.AddStrings(BoilerText);  // append
+             if (vmHTML in FViewMode) then begin                           //kt 8/09
+               HTMLEditor.Text := HTMLEditor.Text + Text2HTML(BoilerText); //kt 8/09
+             end else begin                                                //kt 8/09
+               memNewNote.Lines.AddStrings(BoilerText);  // append
+             end;                                                          //kt 8/09
              UpdateNoteAuthor(DocInfo);
            end;
@@ -1687,6 +1924,11 @@
           if not NoteEmpty then
 //            if not FChanged and (InfoBox(TX_BLR_CLEAR, TC_BLR_CLEAR, MB_YESNO) = ID_YES)
-            if (InfoBox(TX_BLR_CLEAR, TC_BLR_CLEAR, MB_YESNO) = ID_YES)
-              then memNewNote.Lines.Clear;
+            if (InfoBox(TX_BLR_CLEAR, TC_BLR_CLEAR, MB_YESNO) = ID_YES) then begin
+              if (vmHTML in FViewMode) then begin        //kt 8/09
+                HTMLEditor.Clear;                        //kt 8/09
+              end else begin                             //kt 8/09
+                memNewNote.Lines.Clear;
+              end;                                       //kt 8/09
+            end;
         end;
     end; {if BoilerText.Text <> ''}
@@ -1783,4 +2025,5 @@
 begin
   inherited;
+  //kt 8/09  NOTE: the equivalent functionality for HTML is HTMLEditor.Keystruck.  Don't have to set here.
   FChanged := True;
 end;
@@ -1798,6 +2041,14 @@
 var
   ErrMsg: string;
-begin
-  if (EditingIndex > -1) and FChanged then
+  Changed : boolean; //kt 8/09
+begin
+  Changed := false;                               //kt 8/09
+  if (vmHTML in FViewMode) then begin             //kt 8/09
+    Changed := HTMLEditor.KeyStruck;              //kt 8/09
+  end else begin                                  //kt 8/09
+    Changed := FChanged;                          //kt 8/09
+  end;                                            //kt 8/09
+  //kt 8/09 if (EditingIndex > -1) and FChanged then
+  if (EditingIndex > -1) and Changed then         //kt  8/09
   begin
 //  StatusText('Autosaving note...');  <-- original line.  //kt 7/19/2007
@@ -1806,9 +2057,15 @@
     timAutoSave.Enabled := False;
     try
+      if (vmHTML in FViewMode) then begin                                        //kt
+        rHTMLTools.SplitToArray (HTMLEditor.HTMLText, FEditNote.Lines);          //kt
+        SetText(ErrMsg, FEditNote.Lines, lstNotes.GetIEN(EditingIndex),Suppress);//kt
+      end else begin                                                             //kt
       SetText(ErrMsg, memNewNote.Lines, lstNotes.GetIEN(EditingIndex), Suppress);
+      end;                                                                       //kt
     finally
       timAutoSave.Enabled := True;
     end;
     FChanged := False;
+    HTMLEditor.KeyStruck := false; //kt
     StatusText('');
   end;
@@ -2259,4 +2516,5 @@
     if tvNotes.Selected <> nil then tvNotesChange(Self, tvNotes.Selected) else
     begin*)
+      FHTMLEditMode := emNone; //kt 8/09
       pnlWrite.Visible := False;
       pnlRead.Visible := True;
@@ -2530,4 +2788,5 @@
 
 procedure TfrmNotes.popNoteMemoPopup(Sender: TObject);
+const FORMAT_MODE : array[false..true] of string = ('fNotes_Formatted_Text','fNotes_Plain_Text');  //kt 8/09
 begin
   inherited;
@@ -2545,4 +2804,5 @@
   end else
   begin
+    popNoteMemoHTMLFormat.Enabled := False;  //kt 8/09
     popNoteMemoCut.Enabled      := False;
     popNoteMemoCopy.Enabled     := False;
@@ -2550,9 +2810,15 @@
     popNoteMemoTemplate.Enabled := False;
   end;
+  popNoteMemoHTMLFormat.Caption := DKLangConstW('fNotes_Change_Edit_Mode_To') + ' ' + //kt 8/09
+                                   DKLangConstW(FORMAT_MODE[(vmHTML in FViewMode)]);  //kt 8/09
+  popNoteMemoHTMLFormat.Enabled := pnlWrite.Visible;                                  //kt 8/09
   if pnlWrite.Visible then
   begin
-    popNoteMemoSpell.Enabled    := True;
-    popNoteMemoGrammar.Enabled  := True;
-    popNoteMemoReformat.Enabled := True;
+    //kt 8/09 popNoteMemoSpell.Enabled      := True;
+    //kt 8/09 popNoteMemoGrammar.Enabled    := True;
+    //kt 8/09 popNoteMemoReformat.Enabled := True;
+    popNoteMemoSpell.Enabled      := not pnlHTMLWrite.Visible; //kt 8/09
+    popNoteMemoGrammar.Enabled    := not pnlHTMLWrite.Visible; //kt 8/09
+    popNoteMemoReformat.Enabled   := not pnlHTMLWrite.Visible; //kt 8/09
     popNoteMemoReplace.Enabled  := (FEditCtrl.GetTextLen > 0);
     popNoteMemoPreview.Enabled  := (frmDrawers.TheOpenDrawer = odTemplates) and Assigned(frmDrawers.tvTemplates.Selected);
@@ -2567,4 +2833,18 @@
     popNoteMemoInsTemplate.Enabled  := False;
   end;
+end;
+
+(*//kt 8/09  NOTICE:
+  On the form, popNoteMenu was edited to add a new item as below
+  popNoteMemoHTMLFormat : TMenuItem
+  Captions: ~ Edit as Formatted Text
+  OnClick -- popNoteMemoHTMLFormatClick
+*)
+
+procedure TfrmNotes.popNoteMemoHTMLFormatClick(Sender: TObject);
+//kt added function  8/09
+begin
+  inherited;
+  ToggleHTMLEditMode;
 end;
 
@@ -2698,13 +2978,20 @@
       StatusText(DKLangConstW('fNotes_Retrieving_progress_note_detailsxxx')); //kt added 7/19/2007
       Screen.Cursor := crAppStart;  //kt, was crHourGlass;
-      LoadDetailText(memNote.Lines, lstNotes.ItemIEN);
+      //kt LoadDetailText(memNote.Lines, lstNotes.ItemIEN);
+      LoadDetailText(FViewNote, lstNotes.ItemIEN);  //kt 8/09
+      SetDisplayToHTMLvsText(FViewMode,FViewNote);  //kt 8/09
       Screen.Cursor := crDefault;
       StatusText('');
       memNote.SelStart := 0;
-      memNote.Repaint;
+      if not (vmHTML in FViewMode) then  //kt 8/09
+        memNote.Repaint;
     end
   else
     lstNotesClick(Self);
-  SendMessage(memNote.Handle, WM_VSCROLL, SB_TOP, 0);
+  if (vmHTML in FViewMode) then begin                        //kt 8/09
+    SendMessage(HTMLViewer.Handle, WM_VSCROLL, SB_TOP, 0);   //kt 8/09
+  end else begin                                             //kt 8/09
+    SendMessage(memNote.Handle, WM_VSCROLL, SB_TOP, 0);
+  end;                                                       //kt 8/09
 end;
 
@@ -2720,5 +3007,6 @@
   begin
     FSilent := True;
-    if memNewNote.GetTextLen > 0 then SaveCurrentNote(Saved)
+    //kt 8/09 if memNewNote.GetTextLen > 0 then SaveCurrentNote(Saved)
+    if EditorHasText then SaveCurrentNote(Saved)  //kt 8/09
     else
     begin
@@ -2968,4 +3256,5 @@
   lvNotes.Items.Clear;
   memNote.Clear;
+  HTMLViewer.Clear; //kt 8/09
   tvNotes.FullCollapse;
   tvNotes.Selected := tvNotes.TopItem;
@@ -3041,9 +3330,11 @@
   //kt note: The Images tab will delete all files in .\Cache, which
   //         might include HTMLfilename.  No harm if already deleted.
-  DeleteFile(HTMLfilename);  //kt added 5-2-05
+  //kt 8/09 DeleteFile(HTMLfilename);  //kt added 5-2-05
   TAccessibleTreeView.UnwrapControl(tvNotes);
   FDocList.Free;
   FImageFlag.Free;
   KillDocTreeObjects(tvNotes);
+  HtmlEditor.Free; //kt 6/7/09
+  HtmlViewer.Free; //kt 6/7/09
   inherited;
 end;
@@ -3056,4 +3347,5 @@
 procedure TfrmNotes.AssignRemForm;
 begin
+  //kt 8/09 ReminderDialog interaction has not yet been debugged with HTML formatted text.
   with RemForm do
   begin
@@ -3065,4 +3357,5 @@
     Drawers := frmDrawers;
     NewNoteRE := memNewNote;
+    NewNoteHTMLE := HTMLEditor;  //kt
     NoteList := lstNotes;
   end;
@@ -3093,4 +3386,5 @@
     uChanging := True;
     RedrawSuspend(memNote.Handle);
+    RedrawSuspend(HTMLViewer.Handle); //kt 8/09
     RedrawSuspend(lvNotes.Handle);
     tvNotes.Items.BeginUpdate;
@@ -3101,4 +3395,5 @@
     lvNotes.Items.Clear;
     memNote.Clear;
+    HTMLViewer.Clear;                 //kt 8/09
     memNote.Invalidate;
     lblTitle.Caption := '';
@@ -3217,4 +3512,5 @@
           end;
         memNote.Clear;
+        HTMLViewer.Clear; //kt 8/09
         with lvNotes do
           begin
@@ -3240,5 +3536,10 @@
       end;
   finally
-    RedrawActivate(memNote.Handle);
+    //kt NOTE: Is this enough??
+    if (vmHTML in FViewMode) then begin   //kt 8/09
+      RedrawActivate(HtmlViewer.Handle);  //kt 8/09
+    end else begin                        //kt 8/09
+      RedrawActivate(memNote.Handle);
+    end;                                  //kt 8/09
     RedrawActivate(lvNotes.Handle);
     tmpList.Free;
@@ -3264,4 +3565,5 @@
   i: integer;
   WhyNot: string;
+  Mode : TViewModeSet; //kt 8/09
 begin
   if uChanging then Exit;
@@ -3272,4 +3574,5 @@
     begin
       memNote.Clear;
+      HTMLViewer.Clear; //kt 8/09
       if Selected = nil then Exit;
       if uIDNotesActive then
@@ -3292,4 +3595,5 @@
       RedrawSuspend(lvNotes.Handle);
       RedrawSuspend(memNote.Handle);
+      RedrawSuspend(HTMLViewer.Handle); //kt 8/09
       popNoteListExpandSelected.Enabled := Selected.HasChildren;
       popNoteListCollapseSelected.Enabled := Selected.HasChildren;
@@ -3354,6 +3658,11 @@
                 ShowPCEControls(False);
               end;
-          pnlWrite.Visible := False;
-          pnlRead.Visible := True;
+          //kt 8/09
+          //kt  NOTE: At this point the list has changed.  But what is the new note-to-view's format?
+          //          We can't just use FViewMode.  Because that just states what mode was used on the last note.
+          Mode := [vmView] + [vmHTML_MODE[vmHTML in FViewMode]]; //kt 8/09
+          SetDisplayToHTMLvsText(Mode,nil,VIEW_ACTIVATE_ONLY);   //kt 8/09
+          //kt 8/09 pnlWrite.Visible := False;
+          //kt 8/09 pnlRead.Visible := True;
           //  uncommented next 4 lines in v17.5  (RV)
           //-----------------------------
@@ -3368,12 +3677,21 @@
         begin
           memNote.Clear;
+          HTMLViewer.Clear; //kt 8/09
           lvNotes.Visible := False;
           lstNotes.SelectByID(Piece(x, U, 1));
           lstNotesClick(Self);
-          SendMessage(memNote.Handle, WM_VSCROLL, SB_TOP, 0);
+          if (vmHTML in FViewMode) then begin                        //kt 8/09
+            SendMessage(HTMLViewer.Handle, WM_VSCROLL, SB_TOP, 0);   //kt 8/09
+          end else begin                                             //kt 8/09
+            SendMessage(memNote.Handle, WM_VSCROLL, SB_TOP, 0);
+          end;                                                       //kt 8/09
         end;
       SendMessage(tvNotes.Handle, WM_HSCROLL, SB_THUMBTRACK, 0);
       RedrawActivate(lvNotes.Handle);
-      RedrawActivate(memNote.Handle);
+      if (vmHTML in FViewMode) then begin           //kt 8/09
+        RedrawActivate(HtmlViewer.Handle);          //kt 8/09
+      end else begin                                //kt 8/09
+        RedrawActivate(memNote.Handle);
+      end;                                          //kt 8/09
     end;
 end;
@@ -3599,5 +3917,9 @@
       lstNotes.SelectByID(Item.SubItems[5]);
       lstNotesClick(Self);
-      SendMessage(memNote.Handle, WM_VSCROLL, SB_TOP, 0);
+      if (vmHTML in FViewMode) then begin                        //kt 8/09
+        SendMessage(HTMLViewer.Handle, WM_VSCROLL, SB_TOP, 0);   //kt 8/09
+      end else begin                                             //kt 8/09
+        SendMessage(memNote.Handle, WM_VSCROLL, SB_TOP, 0);
+      end;                                                       //kt 8/09
     end;
 end;
@@ -3900,4 +4222,5 @@
 end;
 
+(*
 //kt 5-2-05 added following function (initially copied from fReports.pas)
 procedure TfrmNotes.WebBrowser1DocumentComplete(Sender: TObject;
@@ -3920,5 +4243,5 @@
   }
 end;
-
+*)
 
 procedure TfrmNotes.popNoteMemoInsTemplateClick(Sender: TObject);
@@ -4075,4 +4398,326 @@
 end;
 
+procedure TfrmNotes.SetHTMLEditMode(HTMLEditMode : boolean; Quiet : boolean);
+//kt added function 7/09
+var   Mode      : TViewModeSet; //kt
+const HTML_MODE_S : Array[false..true] of string[16] = ('PLAIN','FORMATTED');
+begin
+  if FEditNote.Lines = nil then FEditNote.Lines := TStringList.Create; //kt
+  if HTMLEditMode then begin
+    if (FViewMode = [vmHTML,vmEdit]) then exit; //no change needed.
+    FEditNote.Lines.Assign(memNewNote.Lines);
+  end else begin
+    if (FViewMode = [vmText,vmEdit]) then exit; //no change needed.
+    if HtmlEditor.GetTextLen > 0 then begin
+      if MessageDlg('Do you want to convert this note to PLAIN TEXT?'+#10#13+
+                  '(May cause loss of formatting information.)',mtWarning, mbOKCancel,0) <> mrOK then begin
+        exit;
+      end;
+    end;
+    FEditNote.Lines.Text := HTMLEditor.Text;
+  end;
+  Mode := [vmEdit] + [vmHTML_MODE[HTMLEditMode]]; //kt
+  SetDisplayToHTMLvsText(Mode,FEditNote.Lines);
+  if not Quiet then begin
+    if MessageDlg('Start new notes in '+HTML_MODE_S[HTMLEditMode]+' TEXT by default?',mtConfirmation,[mbYES,mbNO],0) = mrYES then begin
+      fOptionsNotes.SetDefaultEditHTMLMode(HTMLEditMode);
+    end;
+  end;
+end;
+
+procedure TfrmNotes.ToggleHTMLEditMode;
+//kt added function 6/09
+var NewHTMLMode: boolean;
+begin
+  if not (vmEdit in FViewMode) then exit;  //quit if not in edit mode
+  NewHTMLMode := not (vmHTML in FViewMode);
+  SetHTMLEditMode(NewHTMLMode);
+end;
+
+
+procedure TfrmNotes.SetDisplayToHTMLvsText(Mode : TViewModeSet;
+                                           Lines : TStrings;
+                                           ActivateOnly : boolean {default=False});
+//kt added function 7/09
+//If ActivateOnly=True, then the visibility is set, but the control is not filled with text.
+
+type
+  TPanelVisibilityMode = (pvmReadMode, pvmWriteMode);  //kt
+
+  procedure SetPanelVisibility(Mode: TPanelVisibilityMode; HTMLMode : boolean);
+
+    procedure SetpnlReadVisibility(Visible : boolean; HTMLMode : boolean);
+    begin
+      pnlRead.Visible := Visible;
+      if Visible then begin
+        memNote.Visible := not HTMLMode;
+        memNote.TabStop := not HTMLMode;
+        pnlHTMLViewer.Visible := HTMLMode;
+        HTMLViewer.Visible := HTMLMode;
+        if HTMLMode then begin
+          HtmlViewer.BringToFront;
+        end else begin
+          MemNote.BringToFront;
+        end;
+      end else begin
+        memNote.Visible := false;
+        memNote.TabStop := false;
+        pnlHTMLViewer.Visible := false;
+        HTMLViewer.Visible := false;
+      end;
+    end; {SetDisplayToHTMLvsText.SetPanelVisibility.SetpnlReadVisibility}
+
+    procedure SetpnlWriteVisibility(Visible : boolean; HTMLMode : boolean);
+    begin
+      pnlWrite.Visible := Visible;
+      if Visible then begin
+        pnlHTMLWrite.Visible := HTMLMode;
+        pnlHTMLEdit.Visible := HTMLMode;
+        HTMLEditor.Visible := HTMLMode;
+        pnlTextWrite.Visible := not HTMLMode;
+        MemNewNote.Visible := not HTMLMode;
+        if HTMLMode then begin
+          HTMLEditor.BringToFront;
+        end else begin
+          MemNewNote.BringToFront;
+        end;
+      end else begin
+        pnlHTMLWrite.Visible := false;
+        pnlHTMLEdit.Visible := false;
+        HTMLEditor.Visible := false;
+        pnlTextWrite.Visible := false;
+        memNewNote.Visible := false;
+      end;
+    end; {SetDisplayToHTMLvsText.SetPanelVisibility.SetpnlWriteVisibility}
+
+  begin {SetDisplayToHTMLvsText.SetPanelVisibility}
+    SetpnlReadVisibility ((Mode=pvmReadMode), HTMLMode);
+    SetpnlWriteVisibility((Mode=pvmWriteMode),HTMLMode);
+    Application.ProcessMessages;
+  end; {SetDisplayToHTMLvsText.SetPanelVisibility}
+
+  procedure SetHTMLorTextEditor(HTMLEditMode : boolean;
+                                Lines : TStrings;
+                                ActivateOnly : boolean {default=False});
+    procedure ActivateHtmlEditor(Lines : TStrings);
+    var HTMLText : string;
+    begin
+      HtmlEditor.Editable := true;
+      if ActivateOnly=false then begin
+        if Lines <> nil then begin
+          if rHTMLTools.IsHTML(Lines) then begin
+            HTMLText := Lines.Text;
+          end else begin
+            HTMLText := Text2HTML(Lines);
+            if HTMLText='' then HTMLText := ' ';
+          end;
+        end else HTMLText := ' ';
+        HtmlEditor.HTMLText := HTMLText;
+        HTMLEditor.KeyStruck := false;
+      end;
+    end; {SetDisplayToHTMLvsText.SetHTMLorTextEditor.ActivateHtmlEditor}
+
+    procedure ActiveMemoEditor(Lines : TStrings);
+    begin
+      if ActivateOnly=false then begin
+        memNewNote.Lines.Assign(Lines); //kt new
+      end;
+      //kt HtmlEditor.Active := false; //stop intercepting OnMessages
+      HtmlEditor.Clear;
+    end; {SetDisplayToHTMLvsText.SetHTMLorTextEditor.ActiveMemoEditor}
+
+  begin {SetDisplayToHTMLvsText.SetHTMLorTextEditor}
+    FHTMLEditMode := emHTML_MODE[HTMLEditMode];
+    FViewMode := [vmEdit] + [vmHTML_MODE[HTMLEditMode]];
+    SetPanelVisibility(pvmWriteMode,HTMLEditMode);
+    pnlRight.Repaint; //kt TEMP
+    if HTMLEditMode then begin
+      ActivateHtmlEditor(Lines);
+    end else begin
+      ActiveMemoEditor(Lines);
+    end;
+    pnlRight.Repaint; //kt TEMP
+  end; {SetDisplayToHTMLvsText.SetHTMLorTextEditor}
+
+  procedure SetHTMLorTextViewer(HTMLViewMode : boolean;
+                                Lines : TStrings;
+                                ActivateOnly : boolean); //kt
+  //Set forms such that either HTML Viewer is visible, or standard edit window.
+    procedure ActivateHtmlViewer(Lines : TStrings);
+    begin
+      with frmNotes do begin
+        //ScanForSubs(MemNote.Lines);
+        pnlHtmlViewer.Visible := true;
+        //memNote.Visible := false;
+        //memNote.TabStop := false;
+        HtmlViewer.BringToFront;
+        if ActivateOnly=False then begin
+          FixHTML(Lines);
+          HtmlViewer.HTMLText := Lines.Text;
+        end;
+        HtmlViewer.Editable := false;
+        HtmlViewer.BackgroundColor := clCream;
+        HtmlViewer.TabStop := true;
+        RedrawActivate(HtmlViewer.Handle);
+      end;
+    end; {SetDisplayToHTMLvsText.SetHTMLorTextViewer.ActivateHtmlViewer}
+
+    procedure ActivateMemoViewer(Lines : TStrings);
+    begin
+      with frmNotes do begin
+        pnlHtmlViewer.Visible := false;
+        HtmlViewer.Clear;
+        //HtmlEditor.Clear;  //don't this here.  Only clear in ClearEditControls...
+        HtmlViewer.TabStop := false;
+        if ActivateOnly=False then memNote.Lines.Assign(Lines);   //new
+        memNote.Visible := true;
+        memNote.TabStop := true;
+        memNote.BringToFront;
+        RedrawActivate(memNote.Handle);
+      end;
+    end; {SetDisplayToHTMLvsText.SetHTMLorTextViewer.ActivateMemoViewer}
+
+  begin {SetHTMLorTextViewer}
+    FViewMode := [vmView] + [vmHTML_MODE[HTMLViewMode]];
+    SetPanelVisibility(pvmReadMode,HTMLviewMode);
+    //pnlRight.Repaint; //kt TEMP
+    if HTMLViewMode then begin
+      ActivateHtmlViewer(Lines);
+    end else begin
+      ActivateMemoViewer(Lines);
+      HtmlViewer.Clear;
+    end;
+    pnlRight.Repaint; //kt TEMP
+  end; {SetDisplayToHTMLvsText.SetHTMLorTextViewer}
+
+begin {SetDisplayToHTMLvsText}
+  if vmEdit in Mode then begin
+    SetHTMLorTextEditor((vmHTML in Mode),Lines,ActivateOnly);
+  end else begin
+    SetHTMLorTextViewer((vmHTML in Mode),Lines,ActivateOnly);
+  end;
+end; {SetDisplayToHTMLvsText}
+
+procedure TfrmNotes.btnBoldClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.ToggleBold;
+end;
+
+procedure TfrmNotes.btnItalicClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.ToggleItalic;
+end;
+
+procedure TfrmNotes.btnLessIndentClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.Outdent;
+end;
+
+procedure TfrmNotes.btnMoreIndentClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.Indent;
+end;
+
+procedure TfrmNotes.btnRightAlignClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.AlignRight;
+end;
+
+procedure TfrmNotes.btnCenterAlignClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.AlignCenter;
+end;
+
+procedure TfrmNotes.btnLeftAlignClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.AlignLeft;
+end;
+
+procedure TfrmNotes.btnNumbersClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.ToggleNumbering;
+end;
+
+procedure TfrmNotes.btnBulletsClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.ToggleBullet;
+end;
+
+procedure TfrmNotes.btnUnderlineClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.ToggleUnderline;
+end;
+
+procedure TfrmNotes.btnTextColorClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.TextForeColorDialog;
+end;
+
+procedure TfrmNotes.btnBackColorClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.TextBackColorDialog;
+end;
+
+procedure TfrmNotes.btnFontsClick(Sender: TObject);
+//kt added function
+begin
+  inherited;
+  HtmlEditor.FontDialog;
+end;
+
+procedure TfrmNotes.cbFontSizeChange(Sender: TObject);
+//kt added function
+const
+  FontSizes : array [0..6] of byte = (8,10,12,14,18,24,36);
+begin
+  inherited;
+  //HtmlEditor.FontSize := StrToInt(cbFontSize.Text);
+  HtmlEditor.FontSize := FontSizes[cbFontSize.ItemIndex];
+end;
+
+procedure TfrmNotes.cbFontNamesChange(Sender: TObject);
+//kt added function
+var i :  integer;
+    FontName : string;
+const
+   TEXT_BAR = '---------------';
+begin
+  inherited;
+  if cbFontNames.Text[1]='<' then exit;
+  FontName := cbFontNames.Text;
+  HtmlEditor.FontName := FontName;
+  i := cbFontNames.Items.IndexOf(TEXT_BAR);
+  if i < 1 then cbFontNames.Items.Insert(0,TEXT_BAR);
+  if i > 5 then cbFontNames.Items.Delete(5);
+  if cbFontNames.Items.IndexOf(FontName)> i then begin
+    cbFontNames.Items.Insert(0,FontName);
+  end;
+end;
+
+(*
 //kt made this entire temp function
 procedure TfrmNotes.WebBrowser1CommandStateChange(Sender: TObject;
@@ -4101,4 +4746,5 @@
   end;
 end;
+*)
 
 procedure TfrmNotes.memNewNoteKeyPress(Sender: TObject; var Key: Char);
