Index: cprs/branches/tmg-cprs/CPRS-Chart/rHTMLTools.pas
===================================================================
--- cprs/branches/tmg-cprs/CPRS-Chart/rHTMLTools.pas	(revision 729)
+++ cprs/branches/tmg-cprs/CPRS-Chart/rHTMLTools.pas	(revision 793)
@@ -14,12 +14,16 @@
 
   var
-    DesiredHTMLFontSize : byte;       
-     
-  procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string; 
+    DesiredHTMLFontSize : byte;
+    CPRSDir : string;
+    URL_CPRSDir : string;  //This is CPRSDir, but all '\'s are converted to '/'s
+
+  CONST ALT_IMG_TAG_CONVERT = 'alt="convert to $CPRSDIR$"';
+
+  procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string;
                             PtName, DOB, VisitDate, Location:string; Application : TApplication);  //kt added 5-2-05
   function  IsHTML(Lines : TStrings): boolean; overload;
   function  IsHTML(Line : String): boolean; overload;
   function  HasHTMLTags(Text: string) : boolean;
-  procedure FixHTML(Lines : TStrings); 
+  procedure FixHTML(Lines : TStrings);
   function  FixHTMLCRLF(Text : String) : string;
   procedure SplitToArray (HTMLText: string; Lines : TStrings);
@@ -38,4 +42,5 @@
   function ExtractDateOfNote(Lines : TStringList) : string;
   Procedure ScanForSubs(Lines : TStrings);
+  Procedure InsertSubs(Lines : TStrings);
 
 implementation
@@ -65,4 +70,5 @@
   var
     PrinterEvents : TPrinterEvents;
+    SubsFoundList : TStringList;
 
   const CRLF = #$0D#$0A;
@@ -150,5 +156,5 @@
        have decided to try to let the user click a button when the printer has
        been selected.                                         }    
-  
+
   var
     UseUI          : OleVariant;   
@@ -176,82 +182,14 @@
       PrinterEvents.RestorePrinterTimer.Enabled := true; //launch a restore event in 30 seconds
       //RestoreIEPrinting;  //elh - This was omitted from below. Not sure why.  11/10/09
-    finally  
+    finally
       PrinterEvents.PrintingNow := false;
     end;
   end;
 
-(*
-  Safe copy of above.  Delete later...
-    
-  procedure PrintHTMLReport(Lines: TStringList; var ErrMsg: string; 
-                            PtName, DOB, Location:string;
-                            Application : TApplication);
-  //      Web browser printing options:
-  //        OLECMDEXECOPT_DODEFAULT       Use the default behavior, whether prompting the user for input or not.
-  //        OLECMDEXECOPT_PROMPTUSER      Execute the command after obtaining user input.
-  //        OLECMDEXECOPT_DONTPROMPTUSER  Execute the command without prompting the user.
-
-  {Notice:  When IE is asked to print, it immediately returns from the function,
-           but the printing has not yet occured.  If UI is requested, then the
-          printing will not start until after the user selects a printer and
-         presses [OK].  I could not find any reliable way to determine when the
-        print job had been created.  I had to know this event because I need to
-       restore some IE settings AFTER the printing has finished.  I even tried to
-      get the active windows and see if it was a print dialog.  But IE print dlg
-     apparently is owned by another thread than CPRS, because GetActiveWindow would
-     not bring back a handle to the printer dialog window.  I therefore told IE
-     to print WITHOUT asking which printer via UI.  In that case it prints to the
-     system wide default printer.  So I have to set the default printer to the 
-     user's choice, and then change it back again.  This is bit of a kludge,
-     but I couldn't figure out any other way after hours of trial and error.
-     NOTE: I tried to query IE to see if it was able to print, thinking that it
-     would return NO if in the process of currently printing.  It didn't work, 
-     and would return OK immediately.                                               }    
-  
-
-
-  var
-    UseUI          : OleVariant;   
-    NewPrinterName : string;    
-    dlgWinPrinter  : TPrintDialog;
-  begin
-    if PrinterEvents.RestorePrinterTimer.Enabled = false then begin
-      PrinterEvents.SavedDefaultPrinter := GetCurrentPrinterName;
-    end;  
-    dlgWinPrinter := TPrintDialog.Create(nil);
-    frmTMGPrinting.Show;
-    //FIX: get printer name for the one used last time somehow...
-    if dlgWinPrinter.Execute then begin  //only sets a local printer
-      NewPrinterName := GetCurrentPrinterName;  
-      SetDefaultPrinter(NewPrinterName); //Set global setting that IE will use.
-      PrinterEvents.LastChosenPrinterName := NewPrinterName;
-      try
-        frmNotes.SetDisplayToHTMLvsText([vmView,vmHTML],Lines);  //ActivateHtmlViewer(Lines);
-        if frmNotes.HtmlViewer.WaitForDocComplete = false then begin
-          ErrMsg := 'The web browser timed out trying to set up document.';
-          exit;
-        end;
-        SetupHTMLPrinting(PtName,DOB,Location,' ');  {elh 6/19/09} //kt
-        frmNotes.HtmlViewer.PrintFinished := false;               
-        UseUI := false;  //UseUI := true;
-        frmNotes.HtmlViewer.PrintDocument(UseUI);   //Returns immediately, not after printing done.
-        PrinterEvents.RestorePrinterTimer.Enabled := true; //launch a restore event in 30 seconds
-        Wait(4,Application); 
-        //WaitForBrowserOK(10, Application); //wait up to 10 seconds  //Note: this doesn't do what I want.  Status is immediately OK.
-        //RestoreIEPrinting;   {elh 6/19/09}  //kt
-      finally   //any needed final code goes here.
-        //SetDefaultPrinter(DefaultPrinter);
-        //beep;
-      end;
-    end;
-    dlgWinPrinter.Free;
-    frmTMGPrinting.Hide;        
-  end;
-*)  
   (*
   function WaitForBrowserOK(MaxSecDelay: integer; Application : TApplication) : boolean;
   //Returns TRUE if can print
   var
-    StartTime : TDateTime; 
+    StartTime : TDateTime;
     Status: OLECMDF;
     MaxDelay,ElapsedTime : Double;
@@ -260,5 +198,5 @@
     OneMin = 0.0007;  //note: 0.0007 is about 1 minute
   begin
-    StartTime := GetTime; 
+    StartTime := GetTime;
     MaxDelay := OneMin * MaxSecDelay;
     repeat
@@ -271,18 +209,45 @@
   end;
   *)
-  
+
   Procedure ScanForSubs(Lines : TStrings);
   //Purpose: To scan note for constant $CPRS$ and replace with CPRS's actual directory
-  var i : integer;
-      CPRSDir : string;
-  begin
+  var i,p,p2 : integer;
+      tempS : String;
+  begin
+    SubsFoundList.Clear;
     for i := 0 to Lines.Count-1 do begin
-      if Pos('$CPRSDIR$',Lines.Strings[i])>0 then begin
-        CPRSDir := ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
-        Lines.Strings[i] := AnsiReplaceStr(Lines.Strings[i],'$CPRSDIR$',CPRSDir);
-        //Ensure images are downloaded before passing page to web browser        
-        frmImages.timLoadImagesTimer(nil);
-        //NOTE: This often doesn't get the job completed before passing to browser
-        //      ?? check for completion?
+      p := Pos('$CPRSDIR$',Lines.Strings[i]);
+      if p>0 then begin
+        p := p + Length('$CPRSDIR$\Cache\');
+        p2 := PosEx('"',Lines.Strings[i],p);
+        tempS := MidStr(Lines.Strings[i],p,(p2-p));
+        SubsFoundList.Add(tempS);
+        if Pos('file:///',Lines.Strings[i]) > 0 then begin
+          Lines.Strings[i] := AnsiReplaceStr(Lines.Strings[i],'$CPRSDIR$',URL_CPRSDir);
+        end else begin
+          Lines.Strings[i] := AnsiReplaceStr(Lines.Strings[i],'$CPRSDIR$',CPRSDir);
+        end;
+        //Ensure images are downloaded before passing page to web browser
+      end;
+    end;
+    frmImages.EnsureImagesDownloaded(SubsFoundList);
+  end;
+
+
+  Procedure InsertSubs(Lines : TStrings);
+  //Purpose: To scan a edited note images, and replace references to CPRS's
+  //         actual local directory with $CPRS$
+  var i,p : integer;
+     TempS: string;
+
+  begin
+    for i := 0 to Lines.Count-1 do begin
+      Lines.Strings[i] := AnsiReplaceStr(Lines.Strings[i],URL_CPRSDir,'$CPRSDIR$');
+      Lines.Strings[i] := AnsiReplaceStr(Lines.Strings[i],ALT_IMG_TAG_CONVERT,'IMAGE'); //Remove signal
+      p := pos(ALT_IMG_TAG_CONVERT,Lines.Strings[i]);
+      if p > 0 then begin
+        TempS := MidStr(Lines.Strings[i],1,p-1);
+        TempS := TempS + MidStr(Lines.Strings[i],p+length(ALT_IMG_TAG_CONVERT),length(Lines.Strings[i])+1);
+        Lines.Strings[i] := TempS;
       end;
     end;
@@ -539,5 +504,5 @@
     Result := AnsiReplaceText(Result,'>'+CRLF,'>'#$1F); //protect sequences we want
     Result := AnsiReplaceText(Result,CRLF,'<BR>'+CRLF); //Add <BR>'s to CrLf's
-    Result := AnsiReplaceText(Result,'>'#$1F,'><BR>'+CRLF); //Restore sequences we wanted
+    Result := AnsiReplaceText(Result,'>'#$1F,'>'+CRLF); //Restore sequences we wanted
     Result := AnsiReplaceText(Result,#$1E,'<NO DATA>'); //Restore sequences we wanted
   end;
@@ -927,7 +892,12 @@
 initialization
   DesiredHTMLFontSize := 2; //probably overwritten in fNotes initialization
-  PrinterEvents := TPrinterEvents.Create;  
+  PrinterEvents := TPrinterEvents.Create;
+  CPRSDir := ExcludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0)));
+  URL_CPRSDir := AnsiReplaceStr(CPRSDir,'\','/');
+  SubsFoundList := TStringList.Create;
 
 finalization
-  PrinterEvents.Free;   
+  PrinterEvents.Free;
+  SubsFoundList.Free;
+
 end.
