Index: cprs/trunk/CPRS-Chart/fLabs.pas
===================================================================
--- cprs/trunk/CPRS-Chart/fLabs.pas	(revision 830)
+++ cprs/trunk/CPRS-Chart/fLabs.pas	(revision 1679)
@@ -1,5 +1,5 @@
 unit fLabs;
 
-interface
+interface                                     
 
 uses
@@ -22,9 +22,4 @@
 
   TfrmLabs = class(TfrmHSplit)
-    PopupMenu1: TPopupMenu;
-    GotoTop1: TMenuItem;
-    GotoBottom1: TMenuItem;
-    FreezeText1: TMenuItem;
-    UnfreezeText1: TMenuItem;
     popChart: TPopupMenu;
     popValues: TMenuItem;
@@ -61,10 +56,5 @@
     chkGraphZoom: TCheckBox;
     pnlButtons: TORAutoPanel;
-    lblOld: TOROffsetLabel;
-    lblPrev: TOROffsetLabel;
-    lblNext: TOROffsetLabel;
-    lblRecent: TOROffsetLabel;
     lblMostRecent: TLabel;
-    lblCollection: TLabel;
     lblDate: TVA508StaticText;
     cmdNext: TButton;
@@ -72,5 +62,4 @@
     cmdRecent: TButton;
     cmdOld: TButton;
-    TabControl1: TTabControl;
     grdLab: TCaptionStringGrid;
     pnlChart: TPanel;
@@ -82,6 +71,4 @@
     serTest: TLineSeries;
     pnlRightTopHeader: TPanel;
-    lblHeading: TOROffsetLabel;
-    lblTitle: TOROffsetLabel;
     PopupMenu2: TPopupMenu;
     Print1: TMenuItem;
@@ -92,8 +79,8 @@
     Copy2: TMenuItem;
     SelectAll2: TMenuItem;
-    MenuItem1: TMenuItem;
-    MenuItem2: TMenuItem;
-    MenuItem3: TMenuItem;
-    MenuItem4: TMenuItem;
+    GoToTop1: TMenuItem;
+    GoToBottom1: TMenuItem;
+    FreezeText1: TMenuItem;
+    UnFreezeText1: TMenuItem;
     sptHorzRight: TSplitter;
     pnlFooter: TORAutoPanel;
@@ -117,5 +104,12 @@
     bvlOtherTests: TBevel;
     cmdOtherTests: TButton;
+    TabControl1: TTabControl;
+    pnlRightTopHeaderTop: TPanel;
+    lblHeading: TOROffsetLabel;
     chkMaxFreq: TCheckBox;
+    lblTitle: TOROffsetLabel;
+    Label1: TLabel;
+    lblSample: TLabel;
+    Label2: TLabel;
     procedure FormCreate(Sender: TObject);
     procedure DisplayHeading(aRanges: string);
@@ -143,5 +137,4 @@
     procedure FreezeText1Click(Sender: TObject);
     procedure UnfreezeText1Click(Sender: TObject);
-    procedure PopupMenu1Popup(Sender: TObject);
     procedure chkZoomClick(Sender: TObject);
     procedure chtChartUndoZoom(Sender: TObject);
@@ -198,5 +191,6 @@
     procedure SelectAll2Click(Sender: TObject);
     procedure chkMaxFreqClick(Sender: TObject);
-    procedure sptHorzRightMoved(Sender: TObject);
+    procedure PopupMenu3Popup(Sender: TObject);
+    procedure grdLabTopLeftChanged(Sender: TObject);
   private
     { Private declarations }
@@ -219,4 +213,5 @@
     procedure GoRemote(Dest: TStringList; AItem: string; AQualifier, ARpc: string; AHSTag: string; AHDR: string; aFHIE: string);
     procedure ShowTabControl;
+    procedure HideTabControl;
     procedure ChkBrowser;
     procedure CommonComponentVisible(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12: Boolean);
@@ -235,5 +230,5 @@
   frmLabs: TfrmLabs;
   uFormat: integer;
-  uPrevReportNode: TTreeNode;  
+  uPrevReportNode: TTreeNode;
   uDate1, uDate2: Tdatetime;
   tmpGrid: TStringList;
@@ -241,5 +236,4 @@
   uLabRemoteReportData: TStringList; //Storage for Remote lab query
   uUpdateStat: boolean;              //flag turned on when remote status is being updated
-  uScreenSplitMoved: boolean;        //set if user moves the sptHorzRight Bar
   uScreenSplitLoc: Integer;          //location of user changed split - sptHorzRight Bar
   uTreeStrings: TStrings;
@@ -266,8 +260,5 @@
 uses uCore, rLabs, rCore, rCover, rOrders, fLabPrint, fFrame, fRptBox, Printers, fReportsPrint,
      clipbrd, rReports, rGraphs, activex, mshtml, VA508AccessibilityRouter, uReports,
-     VAUtils
-     { TODO -oRV -cWVEHR Long Age : Changed to use long age }
-     , rWVEHR;
-
+  VAUtils;
 
 const
@@ -405,7 +396,7 @@
 begin
   inherited;
+  LabRowObjects := TLabRowObject.Create;
   PageID := CT_LABS;
   uFrozen := False;
-  uScreenSplitMoved := False;
   aList := TStringList.Create;
   FastAssign(rpcGetGraphSettings, aList);
@@ -424,5 +415,5 @@
   uPrevReportNode := tvReports.Items.GetFirstNode;
   tvReports.Selected := uPrevReportNode;
-  if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 5;
+  if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 4;
   lblSingleTest.Caption := '';
   lblSpecimen.Caption := '';
@@ -490,5 +481,5 @@
   uLabRemoteReportData.Clear;
   TabControl1.Tabs.Clear;
-  TabControl1.Visible := false;
+  HideTabControl;
   tmpGrid.Clear;
   lvReports.SmallImages := uEmptyImageList;
@@ -506,4 +497,8 @@
 var
   i: integer;
+  {OrigSelection: integer;
+  OrigDateIEN: Int64;
+  OrigDateItemID: Variant;
+  OrigReportCat: TTreeNode; }
 begin
   inherited DisplayPage;
@@ -517,8 +512,5 @@
                   + '<TD nowrap><B>Patient: ' + Patient.Name + '</B></TD>'
                   + '<TD nowrap><B>' + Patient.SSN + '</B></TD>'
-{ TODO -oRV -cWVEHR Long Age : Changed to use long age }
-                  //+ '<TD nowrap><B>Age: ' + IntToStr(Patient.Age) + '</B></TD>'
-                  + '<TD nowrap><B>Age: ' + GetPatientBriefAge(Patient.DFN) + '</B></TD>'
-{}
+                  + '<TD nowrap><B>Age: ' + IntToStr(Patient.Age) + '</B></TD>'
                   + '</TR></TABLE></DIV><HR>';
                   //the preferred method would be to use headers and footers
@@ -535,5 +527,5 @@
   begin
     uColChange := '';
-    if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 5;
+    if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 4;
     tvReports.Selected := tvReports.Items.GetFirstNode;
     tvReportsClick(self);
@@ -571,5 +563,5 @@
     CC_INIT_PATIENT:  if not InitPatient then
       begin
-        if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 5;
+        if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 4;
         tvReports.Selected := tvReports.Items.GetFirstNode;
         tvReportsClick(self);
@@ -590,4 +582,50 @@
       end;
     CC_NOTIFICATION:  ProcessNotifications;
+
+    //This corrects the reload of the labs when switching back to the tab.
+    {This code was causing the processing of Lab notifications to display
+     the wrong set of labs for a given notification the 1st notification
+     after selecting/switching patients.  Upon checking the problem that
+     this code was trying to solve, we found that the problem no longer
+     exists, which may be a result of subsequent changes for similar
+     issues found during development/testing of V28 (CQ 18267, 18268)
+     CC_CLICK: if not InitPatient then
+      begin
+        //Clear our local variables
+        OrigReportCat := nil;
+        OrigDateIEN := -1;
+        OrigSelection := -1;
+        OrigDateItemID := '';
+
+        //What was last selected before they switched tabs.
+        if tvReports.Selected <> nil then OrigReportCat := tvReports.Selected;
+        if lstDates.ItemIEN > 0 then OrigDateIEN := lstDates.ItemIEN;
+        if lvReports.Selected <> nil then OrigSelection := lvReports.Selected.Index;
+        if lstQualifier.ItemID <> '' then OrigDateItemID := lstQualifier.ItemID;
+
+        //Load the tree and select the last selected
+        if OrigReportCat <> nil then begin
+         tvReports.Select(OrigReportCat);
+         tvReportsClick(self);
+        end;
+
+        //Did they click on a date (lstDates box)
+        if OrigDateIEN > -1 then begin
+          lstDates.SelectByIEN(OrigDateIEN);
+          lstDatesClick(self);
+        end;
+
+        //Did they click on a date (lstQualifier)
+        if OrigDateItemID <> '' then begin
+          lstQualifier.SelectByID(OrigDateItemID);
+          lstQualifierClick(self);
+        end;
+
+        //Did they click on a lab
+        if OrigSelection > -1 then begin
+         lvReports.Selected := lvReports.Items[OrigSelection];
+         lvReportsSelectItem(self, lvReports.Selected, true);
+        end;
+      end; }
   end;
 end;
@@ -695,5 +733,5 @@
                                   end;
                               end;
-                            RowObjects.Add(aSite, IntToStr(aCurRow) + ':' + IntToStr(aCurCol), uColumns.Strings[aCurCol], aTmpAray);
+                            LabRowObjects.Add(aSite, IntToStr(aCurRow) + ':' + IntToStr(aCurCol), uColumns.Strings[aCurCol], aTmpAray);
                             aTmpAray.Clear;
                           end;
@@ -740,5 +778,5 @@
                             ListItem.SubItems.Add(c);
                           end;
-                        RowObjects.Add(aSite, IntToStr(aCurRow) + ':' + IntToStr(aCurCol), uColumns.Strings[aCurCol], aTmpAray);
+                        LabRowObjects.Add(aSite, IntToStr(aCurRow) + ':' + IntToStr(aCurCol), uColumns.Strings[aCurCol], aTmpAray);
                         aTmpAray.Clear;
                       end;
@@ -776,5 +814,5 @@
                         ListItem.SubItems.Add(c);
                       end;
-                    RowObjects.Add(aSite, IntToStr(aCurRow) + ':' + IntToStr(aCurCol), uColumns.Strings[aCurCol], aTmpAray);
+                    LabRowObjects.Add(aSite, IntToStr(aCurRow) + ':' + IntToStr(aCurCol), uColumns.Strings[aCurCol], aTmpAray);
                     aTmpAray.Clear;
                   end;
@@ -815,8 +853,12 @@
               end;
             DaysBack := Copy(aRanges, 2, Length(aRanges));
+            if DaysBack = '' then DaysBack := '7';
             if DaysBack = '0' then
               aRanges := 'T' + ';T'
             else
-              aRanges := 'T-' + DaysBack + ';T';
+              if Copy(aRanges, 2, 1) = 'T' then
+                aRanges := DaysBack + ';T'
+              else
+                aRanges := 'T-' + DaysBack + ';T';
           end;
         if length(piece(aRanges,';',1)) > 0 then
@@ -844,5 +886,5 @@
         end;
       end;
-    if piece(uRemoteType, '^', 9) = '1' then x := x + ' <<ONLY REMOTE DOD DATA INCLUDED IN REPORT>>';
+    if piece(uRemoteType, '^', 9) = '1' then x := x + ' <<ONLY REMOTE DATA INCLUDED IN REPORT>>';
     Caption := x;
   end;
@@ -1235,5 +1277,5 @@
   var
   MoreID: String;  //Restores MaxOcc value
-  aRemote, aHDR, aFHIE: string;
+  aRemote, aHDR, aFHIE, aMax: string;
   i: integer;
   tmpList: TStringList;
@@ -1287,4 +1329,9 @@
       SetPiece(uQualifier,';',3,'');
     end;
+  aMax := piece(uQualifier,';',3);
+  if (CharAt(lstQualifier.ItemID,1) = 'd')
+    and (length(aMax)>0)
+    and (StrToInt(aMax)<101) then
+      MoreID := ';101';
   aRemote :=  piece(uRemoteType,'^',1);
   aHDR := piece(uRemoteType,'^',7);
@@ -1385,5 +1432,5 @@
           lvReports.Items.Clear;
           memLab.Lines.Clear;
-          RowObjects.Clear;
+          LabRowObjects.Clear;
           if ((aRemote = '1') or (aRemote = '2')) then
             GoRemote(uRemoteReportData, 'L:' + uRptID, lstQualifier.ItemID + MoreID, uReportRPC, uHState, aHDR, aFHIE);
@@ -1416,5 +1463,5 @@
           lvReports.SmallImages := uEmptyImageList;
           lvReports.Items.Clear;
-          RowObjects.Clear;
+          LabRowObjects.Clear;
           memLab.Lines.Clear;
           if ((aRemote = '1') or (aRemote = '2'))  then
@@ -1641,5 +1688,6 @@
           uHTMLDoc := uHTMLPatient + uLocalReportData.Text;
         WebBrowser1.Navigate('about:blank');
-      end;               }
+      end;
+               }
 end;
 
@@ -1711,5 +1759,5 @@
       uLabRemoteReportData.Clear;
       StatusText('Retrieving data for cumulative report...');
-      GoRemoteOld(uLabRemoteReportData,0,2,'',uReportRPC,'',IntToStr(daysback),'',date1,date2);
+      GoRemoteOld(uLabRemoteReportData,21,2,'',uReportRPC,'',IntToStr(daysback),'',date1,date2);
       TabControl1.OnChange(nil);
       Cumulative(uLabLocalReportData, Patient.DFN, daysback, date1, date2, uReportRPC);
@@ -1728,5 +1776,5 @@
       uLabRemoteReportData.Clear;
       StatusText('Retrieving data for interim report...');
-      GoRemoteOld(uLabRemoteReportData,0,3,'',uReportRPC,'','','',date1,date2);
+      GoRemoteOld(uLabRemoteReportData,3,3,'',uReportRPC,'','','',date1,date2);
       TabControl1.OnChange(nil);
       Interim(uLabLocalReportData, Patient.DFN, date1, date2, uReportRPC);
@@ -1833,5 +1881,5 @@
       uLabRemoteReportData.Clear;
       StatusText('Retrieving microbiology data...');
-      GoRemoteOld(uLabRemoteReportData,0,4,'',uReportRPC,'','','',date1,date2);
+      GoRemoteOld(uLabRemoteReportData,4,4,'',uReportRPC,'','','',date1,date2);
       TabControl1.OnChange(nil);
       Micro(uLabLocalReportData, Patient.DFN, date1, date2, uReportRPC);
@@ -1929,4 +1977,18 @@
 end;
 
+procedure TfrmLabs.grdLabTopLeftChanged(Sender: TObject);
+var
+  i: integer;
+begin
+  inherited;
+  if piece(uRptID,':',1) ='1' then
+    begin
+      for i := 2 to grdLab.RowCount do
+        grdLab.Cells[0,i] := '';
+      if not(grdLab.TopRow = 1) then
+        grdLab.Cells[0,grdLab.TopRow] := lblDate.Caption;
+    end;
+end;
+
 procedure TfrmLabs.HGrid(griddata: TStrings);
 var
@@ -2059,5 +2121,5 @@
       end;
       //------------------------------------------------------------------------------------------
-      Cells[i - testcnt, 1] := MixedCase(Piece(griddata[i + offset], '^', 4));
+      Cells[i - testcnt, 1] := MixedCase(Piece(griddata[i + offset], '^', 4)) + ' ' + Piece(griddata[i + offset], '^', 5);
     end;
     for i := testcnt + datecnt + 1 to linecnt do
@@ -2109,4 +2171,5 @@
   uLocalReportData.Free;
   uRemoteReportData.Free;
+  LabRowObjects.Free;
 end;
 
@@ -2119,11 +2182,12 @@
   begin
     if testcnt = 0 then RowCount := 3 else RowCount := testcnt + 1;
-    ColCount := 5;
+    ColCount := 6;
     DefaultColWidth := agrid.Width div ColCount - 2;
-    ColWidths[0] := agrid.Width div 4;
-    ColWidths[4] := agrid.Width div 4;
-    ColWidths[2] := agrid.Width div 9;
-    ColWidths[3] := agrid.Width div 6;
-    ColWidths[1] := agrid.Width - ColWidths[0] - ColWidths[2] - ColWidths[3] - ColWidths[4] - 8;
+    ColWidths[0] := 120;               //agrid.Width div 6;
+    ColWidths[1] := agrid.Width div 4; //5
+    ColWidths[5] := agrid.Width div 7; //5
+    ColWidths[3] := agrid.Width div 14;//12
+    ColWidths[4] := agrid.Width div 12;//9
+    ColWidths[2] := agrid.Width div 5; //agrid.Width - ColWidths[0] - ColWidths[1] - ColWidths[3] - ColWidths[4] - 8;
     FixedCols := 0;
     FixedRows := 1;
@@ -2131,16 +2195,19 @@
       for x := 0 to ColCount - 1 do
         Cells[x, y] := '';
-    Cells[0, 0] := 'Test';
-    Cells[1, 0] := 'Result';
-    Cells[2, 0] := 'Flag';
-    Cells[3, 0] := 'Units';
-    Cells[4, 0] := 'Ref Range';
+    Cells[0, 0] := 'Collection Date/Time';
+    Cells[1, 0] := 'Test';
+    Cells[2, 0] := 'Result / Status';
+    Cells[3, 0] := 'Flag';
+    Cells[4, 0] := 'Units';
+    Cells[5, 0] := 'Ref Range';
     for i := 1 to testcnt do
     begin
-      Cells[0, i] := Piece(aitems[i], '^', 2);
-      Cells[1, i] := Piece(aitems[i], '^', 3);
-      Cells[2, i] := Piece(aitems[i], '^', 4);
-      Cells[3, i] := Piece(aitems[i], '^', 5);
-      Cells[4, i] := Piece(aitems[i], '^', 6);
+      if i = 1 then Cells[0, i] := lblDate.Caption
+      else Cells[0, i] := '';
+      Cells[1, i] := Piece(aitems[i], '^', 2);
+      Cells[2, i] := Piece(aitems[i], '^', 3);
+      Cells[3, i] := Piece(aitems[i], '^', 4);
+      Cells[4, i] := Piece(aitems[i], '^', 5);
+      Cells[5, i] := Piece(aitems[i], '^', 6);
     end;
   end;
@@ -2167,10 +2234,15 @@
   tmpList: TStringList;
   nexton, prevon: boolean;
-  newest, oldest, DisplayDate: string;
+  newest, oldest, DisplayDate, aCollection, aSpecimen, aX: string;
+  i,ix: integer;
 begin
   tmpList := TStringList.Create;
   GetNewestOldest(Patient.DFN, newest, oldest);  //****** PATCH
-  nexton := true;
   prevon := true;
+  aCollection := '';
+  aSpecimen := '';
+  aX := '';
+  lblSample.Caption := '';
+  lblSample.Color := clBtnFace;
   try
     FastAssign(InterimGrid(Patient.DFN, adatetime, direction, uFormat), tmpList);
@@ -2213,4 +2285,40 @@
       if (not prevon) and (uFormat = 2) then
         prevon := true;
+      if Piece(tmpList[0], '^', 2) = 'CH' then
+        begin
+          lblSample.Caption := 'Specimen: ' + Piece(tmpList[0], '^', 5);
+          lblSample.Color := clWindow;
+        end;
+      if Piece(tmpList[0], '^', 2) = 'MI' then
+        begin
+          for i  := 0 to tmpList.Count - 1 do
+            begin
+              if i > 5 then break;
+              if ansiContainsStr(tmpList[i],'Collection sample:') then
+                begin
+                  ix := 0;
+                  if length(piece(tmpList[i], ':',2)) > 0 then
+                    begin
+                      ix := Length(piece(tmpList[i], ':',2));
+                      if ix > 15 then ix := ix - 15;
+                    end;
+                  aCollection := '  Sample: ' + LeftStr(piece(tmpList[i], ':',2),ix);
+                end;
+            end;
+          for i  := 0 to tmpList.Count - 1 do
+            begin
+              if i > 5 then break;
+              if ansiContainsStr(tmpList[i],'Site/Specimen:') then
+                begin
+                  aSpecimen := 'Specimen: ' + piece(tmpList[i], ':', 2);
+                end;
+            end;
+          aX := aSpecimen + aCollection;
+          if Length(aX) > 0 then
+            begin
+              lblSample.Caption := aX;
+              lblSample.Color := clWindow;
+            end;
+        end;
     end
     else
@@ -2218,13 +2326,11 @@
       lblDateFloat.Caption := '';
       lblDate.Caption := '';
+      nexton := false;
+      prevon := false;
     end;
     cmdNext.Enabled := nexton;
     cmdRecent.Enabled := nexton;
-    lblNext.Enabled := nexton;
-    lblRecent.Enabled := nexton;
     cmdPrev.Enabled := prevon;
     cmdOld.Enabled := prevon;
-    lblPrev.Enabled := prevon;
-    lblOld.Enabled := prevon;
     if cmdOld.Enabled and cmdRecent.Enabled then
       lblMostRecent.Visible := false
@@ -2233,9 +2339,9 @@
       lblMostRecent.Visible := true;
       if (not cmdOld.Enabled) and (not cmdRecent.Enabled) then
-        lblMostRecent.Caption := 'No Lab Results'
+        lblMostRecent.Caption := 'No Lab Data'
       else if cmdOld.Enabled then
-        lblMostRecent.Caption := 'Most Recent Lab Result'
+        lblMostRecent.Caption := 'Most Recent Lab Data'
       else
-        lblMostRecent.Caption := 'Oldest Lab Result';
+        lblMostRecent.Caption := 'Oldest Lab Data';
     end;
     if tmpList.Count > 0 then
@@ -2245,11 +2351,7 @@
         FillGrid(grdLab, tmpList);
         FillComments(memLab, tmpList);
-        if uScreenSplitMoved = false then
-          begin
-            pnlRightTop.Height := pnlRight.Height - (pnlRight.Height div 5);
-            uScreenSplitLoc := sptHorzRight.Top;
-          end
-        else
-          pnlRightTop.Height := uScreenSplitLoc;
+        pnlRightTop.Height := pnlRight.Height - (pnlRight.Height div 5);
+        sptHorzRight.Top := pnlRightTop.Height;
+        uScreenSplitLoc := sptHorzRight.Top;
         pnlRightBottom.Height := pnlLeft.Height div 5;
         memLab.Height := pnlLeft.Height div 5;
@@ -2278,5 +2380,6 @@
         pnlFooter.Visible := false;
         sptHorzRight.Visible := true;
-        pnlRightTop.Height := pnlHeader.Height + TabControl1.Height;
+        TabControl1.Visible := false;
+        pnlRightTop.Height := pnlHeader.Height;
         memLab.Height := pnlRight.Height - (lblHeading.Height + lblTitle.Height + pnlHeader.Height);
         pnlRightTop.Visible := true;
@@ -2389,42 +2492,42 @@
   if numtest > 0 then
   begin
-  for i := 1 to numtest do
-    if testnum = Piece(aitems[i], '^', 2) then
-    begin
-      testorder := inttostr(i);
-      break;
-    end;
-  GetStartStop(start, stop, aitems);
-  if OKFloatValue(high) then
-  begin
-    serHigh.AddXY(FMToDateTime(start), strtofloat(high), '',clTeeColor);
-    serHigh.AddXY(FMToDateTime(stop), strtofloat(high), '',clTeeColor);
-  end;
-  if OKFloatValue(low) then
-  begin
-    serLow.AddXY(FMToDateTime(start), strtofloat(low), '',clTeeColor);
-    serLow.AddXY(FMToDateTime(stop), strtofloat(low), '',clTeeColor);
-  end;
-  numspec := Piece(specimen, '^', 1);
-  chtChart.Legend.Color := grdLab.Color;
-  chtChart.Title.Font.Size := MainFontSize;
-  chtChart.LeftAxis.Title.Caption := units;
-  serTest.Title := Piece(test, '^', 2);
-  serHigh.Title := 'Ref High ' + high;
-  serLow.Title := 'Ref Low ' + low;
-  testcheck := testorder;
-  for i := numtest + numcol + 1 to numtest + numcol + numvalues do
-    if Piece(aitems[i], '^', 2) = testcheck then
-      if Piece(aitems[numtest + strtoint(Piece(aitems[i], '^', 1))], '^', 3) = numspec then
+    for i := 1 to numtest do
+      if testnum = Piece(aitems[i], '^', 2) then
       begin
-        value := Piece(aitems[i], '^', 3);
-        if OkFloatValue(value) then
+        testorder := inttostr(i);
+        break;
+      end;
+    GetStartStop(start, stop, aitems);
+    if OKFloatValue(high) then
+    begin
+      serHigh.AddXY(FMToDateTime(start), strtofloat(high), '',clTeeColor);
+      serHigh.AddXY(FMToDateTime(stop), strtofloat(high), '',clTeeColor);
+    end;
+    if OKFloatValue(low) then
+    begin
+      serLow.AddXY(FMToDateTime(start), strtofloat(low), '',clTeeColor);
+      serLow.AddXY(FMToDateTime(stop), strtofloat(low), '',clTeeColor);
+    end;
+    numspec := Piece(specimen, '^', 1);
+    chtChart.Legend.Color := grdLab.Color;
+    chtChart.Title.Font.Size := MainFontSize;
+    chtChart.LeftAxis.Title.Caption := units;
+    serTest.Title := Piece(test, '^', 2);
+    serHigh.Title := 'Ref High ' + high;
+    serLow.Title := 'Ref Low ' + low;
+    testcheck := testorder;
+    for i := numtest + numcol + 1 to numtest + numcol + numvalues do
+      if Piece(aitems[i], '^', 2) = testcheck then
+        if Piece(aitems[numtest + strtoint(Piece(aitems[i], '^', 1))], '^', 3) = numspec then
         begin
-          labvalue := strtofloat(value);
-          datevalue := FMToDateTime(Piece(aitems[numtest + strtoint(Piece(aitems[i], '^', 1))], '^', 2));
-          serTest.AddXY(datevalue, labvalue, '', clTeeColor);
-          inc(valuecount);
+          value := Piece(aitems[i], '^', 3);
+          if OkFloatValue(value) then
+          begin
+            labvalue := strtofloat(value);
+            datevalue := FMToDateTime(Piece(aitems[numtest + strtoint(Piece(aitems[i], '^', 1))], '^', 2));
+            serTest.AddXY(datevalue, labvalue, '', clTeeColor);
+            inc(valuecount);
+          end;
         end;
-      end;
   end;
   if valuecount = 0 then
@@ -2980,15 +3083,15 @@
                         begin
                           aSID := Items[i].SubItems[0];
-                          for j := 0 to RowObjects.ColumnList.Count - 1 do
-                            if piece(aSID,':',1) = piece(TCellObject(RowObjects.ColumnList[j]).Handle,':',1) then
-                              if Item.Caption = (piece(TCellObject(RowObjects.ColumnList[j]).Site,';',1)) then
-                                if (TCellObject(RowObjects.ColumnList[j]).Data.Count > 0) and
-                                  (TCellObject(RowObjects.ColumnList[j]).Include = '1') then
+                          for j := 0 to LabRowObjects.ColumnList.Count - 1 do
+                            if piece(aSID,':',1) = piece(TCellObject(LabRowObjects.ColumnList[j]).Handle,':',1) then
+                              if Item.Caption = (piece(TCellObject(LabRowObjects.ColumnList[j]).Site,';',1)) then
+                                if (TCellObject(LabRowObjects.ColumnList[j]).Data.Count > 0) and
+                                  (TCellObject(LabRowObjects.ColumnList[j]).Include = '1') then
                                   begin
                                     aWPFlag := true;
-                                    MemLab.Lines.Add(TCellObject(RowObjects.ColumnList[j]).Name);
-                                    FastAssign(TCellObject(RowObjects.ColumnList[j]).Data, aBasket);
+                                    MemLab.Lines.Add(TCellObject(LabRowObjects.ColumnList[j]).Name);
+                                    FastAssign(TCellObject(LabRowObjects.ColumnList[j]).Data, aBasket);
                                     for k := 0 to aBasket.Count - 1 do
-                                      MemLab.Lines.Add('  ' + aBasket[k]);
+                                      MemLab.Lines.Add(' ' + aBasket[k]);
                                   end;
                           if aWPFlag = true then
@@ -3001,15 +3104,15 @@
                 aBasket.Clear;
                 aWPFlag := false;
-                for i := 0 to RowObjects.ColumnList.Count - 1 do
-                  if piece(aID,':',1) = piece(TCellObject(RowObjects.ColumnList[i]).Handle,':',1) then
-                    if Item.Caption = (piece(TCellObject(RowObjects.ColumnList[i]).Site,';',1)) then
-                      if (TCellObject(RowObjects.ColumnList[i]).Data.Count > 0) and
-                        (TCellObject(RowObjects.ColumnList[i]).Include = '1') then
+                for i := 0 to LabRowObjects.ColumnList.Count - 1 do
+                  if piece(aID,':',1) = piece(TCellObject(LabRowObjects.ColumnList[i]).Handle,':',1) then
+                    if Item.Caption = (piece(TCellObject(LabRowObjects.ColumnList[i]).Site,';',1)) then
+                      if (TCellObject(LabRowObjects.ColumnList[i]).Data.Count > 0) and
+                        (TCellObject(LabRowObjects.ColumnList[i]).Include = '1') then
                         begin
                           aWPFlag := true;
-                          MemLab.Lines.Add(TCellObject(RowObjects.ColumnList[i]).Name);
-                          FastAssign(TCellObject(RowObjects.ColumnList[i]).Data, aBasket);
+                          MemLab.Lines.Add(TCellObject(LabRowObjects.ColumnList[i]).Name);
+                          FastAssign(TCellObject(LabRowObjects.ColumnList[i]).Data, aBasket);
                           for j := 0 to aBasket.Count - 1 do
-                            MemLab.Lines.Add('  ' + aBasket[j]);
+                            MemLab.Lines.Add(' ' + aBasket[j]);
                         end;
                 if aWPFlag = true then
@@ -3120,26 +3223,15 @@
 begin
   inherited;
-with memLab do
-  begin
-    SetFocus;
-    SelStart :=0;
-    SelLength :=0;
-  end;
+  SendMessage(memLab.Handle, WM_VSCROLL, SB_TOP, 0);
+  {GoToTop1.Enabled := false;
+  GoToBottom1.Enabled := true; }
 end;
 
 procedure TfrmLabs.GotoBottom1Click(Sender: TObject);
-var
-  I,CharCount : Integer;
 begin
   Inherited;
-  CharCount :=0;
-  with memLab do
-  begin
-    for I := 0 to lines.count-1 do
-      CharCount := CharCount + Length(Lines[I]) + 2;
-    SetFocus;
-    SelStart := CharCount;
-    SelLength :=0;
-  end;
+  SendMessage(memLab.Handle, WM_VSCROLL, SB_BOTTOM, 0);
+  {GoToTop1.Enabled := true;
+  GoToBottom1.Enabled := false; }
 end;
 
@@ -3177,5 +3269,5 @@
 end;
 
-procedure TfrmLabs.PopupMenu1Popup(Sender: TObject);
+procedure TfrmLabs.PopupMenu3Popup(Sender: TObject);
 begin
  inherited;
@@ -3191,5 +3283,5 @@
   If Memo1.Visible Then
     UnFreezeText1.Enabled := True;
-  If memLab.SelStart > 0 then
+  {If memLab.SelStart > 0 then
     GotoTop1.Enabled := True
   Else
@@ -3199,5 +3291,5 @@
     GotoBottom1.Enabled := True
   Else
-    GotoBottom1.Enabled := False;
+    GotoBottom1.Enabled := False; }
   {case lstReports.ItemIEN of
     1: FreezeText1.Enabled := False;
@@ -3233,13 +3325,13 @@
   if StrToIntDef(OrderIFN,0) > 0 then
    begin
-    //the following if condition & clause resolves CQ 16405 & 17076 - a mixture of two different patient's lab results in one display.
+    //the following if condition & clause resolves CQ 16405 & 17076 - a mixture of two different patient's lab results in one display (TC).
     if (AnsiContainsStr(tvReports.Selected.Text, 'Microbiology')) or (AnsiContainsStr(tvReports.Selected.Text, 'Anatomic Pathology'))
     or (AnsiContainsStr(tvReports.Selected.Text, 'Cytology')) or (AnsiContainsStr(tvReports.Selected.Text, 'Electron Microscopy'))
     or (AnsiContainsStr(tvReports.Selected.Text, 'Surgical Pathology')) and (lvReports.Visible = TRUE) then
        begin
-         tvReports.Selected := tvReports.TopItem;
          lvReports.Visible := FALSE;
-         DisplayHeading('');
        end;
+    tvReports.Selected := tvReports.TopItem;    //moved here to fix the conflicting lab results caption header that is displayed with the alert message text.
+    DisplayHeading('');    //fixes part B of CQ #17548 - CPRS v28.1 (TC)
     lstDates.ItemIndex      := -1;
     Memo1.Visible           := false;
@@ -3270,5 +3362,5 @@
    else
    begin
-     if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 5;
+     if Patient.Inpatient then lstDates.ItemIndex := 2 else lstDates.ItemIndex := 4;
      tvReports.Selected := tvReports.Items.GetFirstNode;
      tvReportsClick(self);
@@ -3518,5 +3610,5 @@
 procedure TfrmLabs.Timer1Timer(Sender: TObject);
 var
-  i,j: integer;
+  i,j,fail: integer;
   r0: String;
 begin
@@ -3526,23 +3618,46 @@
     for i := 0 to Count - 1 do
       if TRemoteSite(Items[i]).Selected then
+       begin
         if Length(TRemoteSite(Items[i]).LabRemoteHandle) > 0 then
           begin
             r0 := GetRemoteStatus(TRemoteSite(Items[i]).LabRemoteHandle);
             TRemoteSite(Items[i]).LabQueryStatus := r0; //r0='1^Done' if no errors
+            UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, piece(r0,'^',2));
             if piece(r0,'^',1) = '1' then
               begin
+                GetRemoteData(TRemoteSite(Items[i]).LabData,
+                  TRemoteSite(Items[i]).LabRemoteHandle,Items[i]);
                 RemoteReports.Add(TRemoteSite(Items[i]).CurrentLabQuery,
                   TRemoteSite(Items[i]).LabRemoteHandle);
-                GetRemoteData(TRemoteSite(Items[i]).LabData,
-                  TRemoteSite(Items[i]).LabRemoteHandle,Items[i]);
                 TRemoteSite(Items[i]).LabRemoteHandle := '';
                 TabControl1.OnChange(nil);
+                if (length(piece(uHState,';',2)) > 0) then
+                  begin
+                    uRemoteReportData.Clear;
+                    QuickCopy(TRemoteSite(Items[i]).LabData,uRemoteReportData);
+                    fail := 0;
+                    if uRemoteReportData.Count > 0 then
+                      begin
+                        if uRemoteReportData[0] = 'Report not available at this time.' then
+                          begin
+                            fail := 1;
+                            UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID,'Report not available');
+                          end;
+                        if piece(uRemoteReportData[0],'^',1) = '-1' then
+                          begin
+                            fail := 1;
+                            UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID,'Communication failure');
+                          end;
+                        if fail = 0 then
+                          LoadListView(uRemoteReportData);
+                      end;
+                  end;
               end
             else
               begin
                 uRemoteCount := uRemoteCount + 1;
-                if uRemoteCount > 60 then //5 minute limit
+                if uRemoteCount > 90 then //~7 minute limit
                   begin
-                    Timer1.Enabled := False;
+                    TRemoteSite(Items[i]).LabRemoteHandle := '';
                     TRemoteSite(Items[i]).LabQueryStatus := '-1^Timed out';
                     UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'Timed out');
@@ -3554,12 +3669,18 @@
                     + TRemoteSite(Items[i]).SiteName + '...');
               end;
-            Timer1.Interval := 5000;
+            Timer1.Interval := 10000;
           end;
+       end;
      if Timer1.Enabled = True then
        begin
          j := 0;
          for i := 0 to Count -1 do
-           if Length(TRemoteSite(Items[i]).LabRemoteHandle) > 0 then
-             j := 1;
+           begin
+             if Length(TRemoteSite(Items[i]).LabRemoteHandle) > 0 then
+               begin
+                 j := 1;
+                 break;
+               end;
+           end;
          if j = 0 then  //Shutdown timer if all sites have been processed
            begin
@@ -3570,5 +3691,8 @@
          for i := 0 to Count -1 do
            if TRemoteSite(Items[i]).Selected = true then
-             j := 1;
+             begin
+               j := 1;
+               break;
+             end;
          if j = 0 then  //Shutdown timer if user has de-selected all sites
            begin
@@ -3584,11 +3708,15 @@
 var
   i: integer;
-  ListItem: TListItem;
   aHeading, aReportType, aRPC, aQualifier, aStartTime, aStopTime, aMax, aRptCode, aRemote, aCategory, aSortOrder, aDaysBack, x, x1, x2: string;
   aIFN, aOldID: integer;
-  aID, aHSTag, aRadParam, aColChange, aDirect, aHDR, aFHIE, aFHIEONLY, aQualifierID: string;
-  CurrentParentNode, CurrentNode: TTreeNode;
-begin
-  inherited;
+  aID, aHSTag, aColChange, aDirect, aHDR, aFHIE, aFHIEONLY, aQualifierID: string;
+  CurrentNode: TTreeNode;
+begin
+  inherited;
+  if (Length(lblHeading.Caption) > 0) and (Length(frmFrame.stsArea.Panels.Items[1].Text) > 0) then
+    begin                                           //ProcessNotfications post-cleanup and clearing of notification message text
+      lblHeading.Caption := '';                     //in the header and status bar display when clicking to view lab results.
+      frmFrame.stsArea.Panels.Items[1].Text := '';
+    end;
   lvReports.Hint := 'To sort, click on column headers|';
   tvReports.TopItem := tvReports.Selected;
@@ -3638,4 +3766,5 @@
   uReportRPC := aRPC;
   uRptID := aID;
+  uLabRepID := aID;
   uDirect := aDirect;
   uReportType := aReportType;
@@ -3648,16 +3777,10 @@
   uHState := aHSTag;
   Timer1.Enabled := False;
-  TabControl1.Visible := false;
-  TabControl1.TabStop := false;
+  HideTabControl;
   sptHorzRight.Visible := true;
   lvReports.Visible := false;
   if (aRemote = '1') or (aRemote = '2') then
     if not(uReportType = 'V') then
-      if TabControl1.Tabs.Count > 1 then
-        begin
-          TabControl1.Visible := true;
-          TabControl1.TabStop := true;
-          pnlRightTop.Height := lblTitle.Height + TabControl1.Height;
-        end;
+      ShowTabControl;
   StatusText('');
   uHTMLDoc := '';
@@ -3669,5 +3792,4 @@
   lvReports.Items.Clear;
   lvReports.Columns.Clear;
-  lblHeading.Caption := '';  //clears Notification text to reduce confusion with lblTitle.Caption.
   DisplayHeading('');
   if uReportType = 'H' then
@@ -3738,5 +3860,5 @@
       end;
   uLocalReportData.Clear;
-  RowObjects.Clear;
+  LabRowObjects.Clear;
   uRemoteReportData.Clear;
   lstHeaders.Visible := false;
@@ -3835,5 +3957,10 @@
                      uPrevReportNode := tvReports.Selected;
                     end
-                    else tvReports.Selected := uPrevReportNode;
+                    else
+                      begin
+                        uPrevReportNode := tvReports.Items.GetFirstNode;
+                        tvReports.Selected := uPrevReportNode;
+                        tvReportsClick(self);
+                      end;
                   end
             else if aID = '5:WORKSHEET' then
@@ -3864,13 +3991,28 @@
                      //chkZoom.Checked := false;
                      //chkZoomClick(self);
-                     lstDatesClick(self);
+                     //lstDatesClick(self);
                      //lstQualifierClick(self);
                      cmdOtherTests.SetFocus;
                      cmdOtherTests.Default := true;
                      uPrevReportNode := tvReports.Selected;
+                     if lstDates.ItemIndex = -1 then
+                      if Patient.Inpatient then lstDates.ItemIndex := 2
+                      else lstDates.ItemIndex := 4;
+                     //for i := 1 to lstDates.Count - 1 do  //Sets default date range to next item > 1 Month (which should be 6 months)
+                      //if strToInt(piece(lstDates.Items[i],'^',1)) > 31 then
+                        //begin
+                          //lstDates.ItemIndex := i;
+                          //break;
+                        //end;
+                     lstDatesClick(self);
                      if ScreenReaderSystemActive then
                        grdLab.SetFocus;
                     end
-                    else tvReports.Selected := uPrevReportNode;
+                    else
+                      begin
+                        uPrevReportNode := tvReports.Items.GetFirstNode;
+                        tvReports.Selected := uPrevReportNode;
+                        tvReportsClick(self);
+                      end;
                   end
 
@@ -3943,5 +4085,5 @@
                      end
                      else
-                       tvReports.Selected := uPrevReportNode;
+                       tvReports.Selected := uPrevReportNode; 
                     end;
                   end
@@ -3957,11 +4099,23 @@
                     sptHorzRight.Visible := false;
                     pnlRightTop.Height := lblHeading.Height;
+                    if ((aRemote = '1') or (aRemote = '2')) then
+                      ShowTabControl;
+                    pnlRightTopHeader.Align := alTop;
                     pnlRightTop.Align := alTop;
+                    TabControl1.Align := alTop;
                     pnlRightBottom.Align := alclient;
                     sptHorzRight.Visible := true;
                     pnlRightBottom.Visible := true;
                     lvReports.Visible := false;
-                    memLab.Height := pnlRight.Height - (lblHeading.Height + lblTitle.Height);
                     memLab.Align := alClient;
+                    if lstDates.ItemIndex = -1 then
+                      if Patient.Inpatient then lstDates.ItemIndex := 2
+                      else lstDates.ItemIndex := 4;
+                    {for i := 1 to lstDates.Count - 1 do  //Sets default date range to next item > 1 Month (which should be 6 months)
+                      if strToInt(piece(lstDates.Items[i],'^',1)) > 31 then
+                        begin
+                          lstDates.ItemIndex := i;
+                          break;
+                        end;  }
                     FormResize(self);
                     aOldID := 1;
@@ -3984,5 +4138,5 @@
                           CommonComponentVisible(false,false,false,false,false,false,false,false,false,false,false,false);
                           StatusText('Retrieving data...');
-                          GoRemoteOld(uLabRemoteReportData,0,aOldID,'',uReportRPC,'0','9999','1',0,0);
+                          GoRemoteOld(uLabRemoteReportData,StrToInt(Piece(aID,':',1)),aOldID,'',uReportRPC,'0','9999','1',0,0);
                           //GoRemote(uRemoteReportData, aID, aRptCode, aRPC, uHState, aHDR, aFHIE);
                           TabControl1.OnChange(nil);
@@ -4011,5 +4165,5 @@
                           lstHeaders.Clear;
                           StatusText('Retrieving data...');
-                          GoRemoteOld(uLabRemoteReportData,0,aOldID,'',uReportRPC,'0','9999','1',0,0);
+                          GoRemoteOld(uLabRemoteReportData,StrToInt(Piece(aID,':',1)),aOldID,'',uReportRPC,'0','9999','1',0,0);
                           //GoRemote(uRemoteReportData, aID, aRptCode, aRPC, uHState, aHDR, aFHIE);
                           TabControl1.OnChange(nil);
@@ -4098,5 +4252,5 @@
             lvReports.SmallImages := uEmptyImageList;
             lvReports.Items.Clear;
-            RowObjects.Clear;
+            LabRowObjects.Clear;
             memLab.Lines.Clear;
             if (length(piece(aHSTag,';',2)) > 0) then
@@ -4188,11 +4342,7 @@
         QT_HSWPCOMPONENT:
           begin      //      = 6
-            if uScreenSplitMoved = false then
-              begin
-                pnlRightTop.Height := pnlRight.Height - (pnlRight.Height div 2);
-                uScreenSplitLoc := sptHorzRight.Top;
-              end
-            else
-              pnlRightTop.Height := uScreenSplitLoc;
+            pnlRightTop.Height := pnlRight.Height - (pnlRight.Height div 2);
+            sptHorzRight.top := pnlRightTop.Height;
+            uScreenSplitLoc := sptHorzRight.Top;
             pnlLeftBottom.Visible := false;
             splitter1.Visible := false;
@@ -4205,8 +4355,9 @@
             memLab.Visible := true;
             TabControl1.OnChange(nil);
-            RowObjects.Clear;
+            LabRowObjects.Clear;
             memLab.Lines.Clear;
             lvReports.SmallImages := uEmptyImageList;
             lvReports.Items.Clear;
+            memLab.Repaint;
             if (length(piece(aHSTag,';',2)) > 0) then
               begin
@@ -4456,5 +4607,5 @@
       begin
         TRemoteSite(Items[i]).Selected := true;
-        frmFrame.lstCIRNLocations.Checked[i+2] := true;
+        frmFrame.lstCIRNLocations.Checked[i+1] := true;
       end;
     if TRemoteSite(Items[i]).Selected then
@@ -4463,8 +4614,8 @@
         if (LeftStr(TRemoteSite(Items[i]).SiteID, 5) = '200HD') and not(AHDR = '1') then
           begin
-            TRemoteSite(Items[i]).QueryStatus := '1^Not Included';
+            TRemoteSite(Items[i]).LabQueryStatus := '1^Not Included';
             UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'NOT INCLUDED');
-            TRemoteSite(Items[i]).RemoteHandle := '';
-            TRemoteSite(Items[i]).QueryStatus := '1^Done';
+            TRemoteSite(Items[i]).LabRemoteHandle := '';
+            TRemoteSite(Items[i]).LabQueryStatus := '1^Done';
             if uQualifierType = 6 then seq := '1^';
             TRemoteSite(Items[i]).Data.Add(seq + TRemoteSite(Items[i]).SiteName);
@@ -4478,8 +4629,8 @@
         if (AHDR = '1') and not(LeftStr(TRemoteSite(Items[i]).SiteID, 5) = '200HD') then
           begin
-            TRemoteSite(Items[i]).QueryStatus := '1^Not Included';
+            TRemoteSite(Items[i]).LabQueryStatus := '1^Not Included';
             UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'NOT INCLUDED');
-            TRemoteSite(Items[i]).RemoteHandle := '';
-            TRemoteSite(Items[i]).QueryStatus := '1^Done';
+            TRemoteSite(Items[i]).LabRemoteHandle := '';
+            TRemoteSite(Items[i]).LabQueryStatus := '1^Done';
             if uQualifierType = 6 then seq := '1^';
             TRemoteSite(Items[i]).Data.Add(seq + TRemoteSite(Items[i]).SiteName);
@@ -4493,8 +4644,8 @@
         if (LeftStr(TRemoteSite(Items[i]).SiteID, 5) = '200') and not(aFHIE = '1') then
           begin
-            TRemoteSite(Items[i]).QueryStatus := '1^Not Included';
+            TRemoteSite(Items[i]).LabQueryStatus := '1^Not Included';
             UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'NOT INCLUDED');
-            TRemoteSite(Items[i]).RemoteHandle := '';
-            TRemoteSite(Items[i]).QueryStatus := '1^Done';
+            TRemoteSite(Items[i]).LabRemoteHandle := '';
+            TRemoteSite(Items[i]).LabQueryStatus := '1^Done';
             if uQualifierType = 6 then seq := '1^';
             TRemoteSite(Items[i]).Data.Add(seq + TRemoteSite(Items[i]).SiteName);
@@ -4525,6 +4676,6 @@
             begin
               GetRemoteData(TRemoteSite(Items[i]).Data,LocalHandle,Items[i]);
-              TRemoteSite(Items[i]).RemoteHandle := '';
-              TRemoteSite(Items[i]).QueryStatus := '1^Done';
+              TRemoteSite(Items[i]).LabRemoteHandle := '';
+              TRemoteSite(Items[i]).LabQueryStatus := '1^Done';
               UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'Done');
               TabControl1.OnChange(nil);
@@ -4537,10 +4688,10 @@
               begin
                 StatusText('Retrieving reports from ' + TRemoteSite(Items[i]).SiteName + '...');
-                TRemoteSite(Items[i]).QueryStatus := '1^Direct Call';
+                TRemoteSite(Items[i]).LabQueryStatus := '1^Direct Call';
                 UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'Direct Call');
                 DirectQuery(Dest, AItem, HSType, Daysback, ExamID, Alpha, Omega, TRemoteSite(Items[i]).SiteID, ARpc, AHSTag);
                 if Copy(Dest[0],1,2) = '-1' then
                   begin
-                    TRemoteSite(Items[i]).QueryStatus := '-1^Communication error';
+                    TRemoteSite(Items[i]).LabQueryStatus := '-1^Communication error';
                     UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID,'Communication error');
                     if uQualifierType = 6 then seq := '1^';
@@ -4555,6 +4706,6 @@
                   begin
                     QuickCopy(Dest,TRemoteSite(Items[i]).Data);
-                    TRemoteSite(Items[i]).RemoteHandle := '';
-                    TRemoteSite(Items[i]).QueryStatus := '1^Done';
+                    TRemoteSite(Items[i]).LabRemoteHandle := '';
+                    TRemoteSite(Items[i]).LabQueryStatus := '1^Done';
                     UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'Done');
                     TabControl1.OnChange(nil);
@@ -4569,5 +4720,5 @@
                 if Dest[0] = '' then
                   begin
-                    TRemoteSite(Items[i]).QueryStatus := '-1^Communication error';
+                    TRemoteSite(Items[i]).LabQueryStatus := '-1^Communication error';
                     UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID,'Communication error');
                     if uQualifierType = 6 then seq := '1^';
@@ -4581,6 +4732,6 @@
                 else
                   begin
-                    TRemoteSite(Items[i]).RemoteHandle := Dest[0];
-                    TRemoteSite(Items[i]).QueryStatus := '0^initialization...';
+                    TRemoteSite(Items[i]).LabRemoteHandle := Dest[0];
+                    TRemoteSite(Items[i]).LabQueryStatus := '0^initialization...';
                     UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'initialization');
                     Timer1.Enabled := True;
@@ -4597,5 +4748,5 @@
 var
   i,j: integer;
-  LocalHandle, Report, Query, seq: String;
+  LocalHandle, Report, Query: String;
 begin
   { AReportID := 1  Generic report   RemoteLabReports
@@ -4603,5 +4754,5 @@
                  3  Interim          RemoteLabInterim
                  4  Microbioloby     RemoteLabMicro }
-seq := '';
+
   with RemoteSites.SiteList do
     for i := 0 to Count - 1 do
@@ -4618,5 +4769,5 @@
            if (LeftStr(TRemoteSite(Items[i]).SiteID, 5) = '200') then
           begin
-            TRemoteSite(Items[i]).QueryStatus := '1^Not Included';
+            TRemoteSite(Items[i]).LabQueryStatus := '1^Not Included';
             UpdateRemoteStatus(TRemoteSite(Items[i]).SiteID, 'NOT INCLUDED');
             TabControl1.OnChange(nil);
@@ -4624,5 +4775,5 @@
           end;
           TRemoteSite(Items[i]).CurrentLabQuery := 'Lab' + Patient.DFN + ';' + Patient.ICN +
-            '^' + IntToStr(AItem) + '^' + 'L:' + IntToStr(AReportID) + '^^' + ARpc + '^' + AHSType +
+            '^' + 'L:' + IntToStr(AItem) + '^' +  IntToStr(AReportID) + '^^' + ARpc + '^' + AHSType +
             '^' + ADaysBack + '^' + ASection + '^' + DateToStr(ADate1) + '^' + DateToStr(ADate2) + '^' +
             TRemoteSite(Items[i]).SiteID;
@@ -4651,5 +4802,5 @@
               case AReportID of
               1: begin
-                   RemoteLabReports(Dest, Patient.DFN + ';' + Patient.ICN, IntToStr(AItem),
+                   RemoteLabReports(Dest, Patient.DFN + ';' + Patient.ICN, 'L:' + IntToStr(AItem),
                      AHSType, ADaysBack, ASection, ADate1, ADate2,
                      TRemoteSite(Items[i]).SiteID, ARpc);
@@ -4668,5 +4819,5 @@
                  end;
               else begin
-                     RemoteLab(Dest, Patient.DFN + ';' + Patient.ICN, IntToStr(AItem),
+                     RemoteLab(Dest, Patient.DFN + ';' + Patient.ICN, 'L:' + IntToStr(AItem),
                      AHSType, ADaysBack, ASection, ADate1, ADate2,
                      TRemoteSite(Items[i]).SiteID, ARpc);
@@ -4698,7 +4849,8 @@
 begin
   inherited;
-  memLab.Lines.Clear;
+  if (uQualifiertype <> 6) or (length(piece(uHState,';',2)) < 1) then
+    memLab.Lines.Clear;
   lstHeaders.Items.Clear;
-  with TabControl1 do
+  if (length(piece(uHState,';',2)) = 0) then with TabControl1 do
     begin
       memLab.Lines.BeginUpdate;
@@ -4714,5 +4866,5 @@
                   for i := 1 to TRemoteSite(Tabs.Objects[TabIndex]).LabData.Count - 1 do
                     if hook = true then
-                      memLab.Lines.Add(TRemoteSite(Tabs.Objects[TabIndex]).LabData[i])
+                        memLab.Lines.Add(TRemoteSite(Tabs.Objects[TabIndex]).LabData[i])
                     else
                       begin
@@ -4730,7 +4882,7 @@
             memLab.Lines.Add('Remote data transmission error: ' + Piece(aStatus,'^',2));
           if Piece(aStatus,'^',1) = '0' then
-            memLab.Lines.Add('Transmission in progress: ' + Piece(aStatus,'^',2));
+            memLab.Lines.Add('Retrieving data... ' + Piece(aStatus,'^',2));
           if Piece(aStatus,'^',1) = '' then
-            memLab.Lines.Add('Select a report...');
+            memLab.Lines.Add(uReportInstruction);
         end
       else
@@ -4752,8 +4904,13 @@
               end
             else
-              QuickCopy(uLabLocalReportData,memLab);
+              if tvReports.Selected.Text = 'Imaging (local only)' then 
+                   memLab.Lines.clear
+              else
+                QuickCopy(uLabLocalReportData,memLab);
             memLab.Lines.Insert(0,' ');
             memLab.Lines.Delete(0);
-          end;
+          end
+        else
+          memLab.Lines.Add(uReportInstruction);
       memLab.SelStart := 0;
       memLab.Lines.EndUpdate;
@@ -4799,4 +4956,20 @@
 procedure TfrmLabs.CommonComponentVisible(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12: Boolean);
 begin
+  //Clear the last date selection
+  //if not A4 then lstDates.ItemIndex := -1;
+  lstDates.Caption := lblDates.Caption;
+  lstHeaders.Caption := lblHeaders.Caption;
+  if A4 or A2 or A12 then
+    begin
+      pnlLefTop.Height := (frmLabs.Height div 2);
+      pnlLeftBottom.Visible := true;
+      Splitter1.Visible := true;
+    end
+    else
+    begin
+      pnlLefTop.Height := frmLabs.Height;
+      pnlLeftBottom.Visible := false;
+      Splitter1.Visible := false;
+    end;
   lstDates.Visible := false;    // turned off to realign correctly
   lblDates.Visible := false;
@@ -4819,22 +4992,16 @@
   pnlFooter.Visible := A9;
   lvReports.Visible := A10;
-  sptHorzRight.Visible := A10;
+  sptHorzRight.Visible := true;
   if A4 and A1 and (lblDates.Top < lblHeaders.Top) then
-  begin
-    lblDates.Caption := 'Headings';  // swithes captions if not aligned
-    lblHeaders.Caption := 'Date Range';
-  end
-  else
-  begin
-    lblDates.Caption := 'Date Range';
-    lblHeaders.Caption := 'Headings';
-  end;
-  lstDates.Caption := lblDates.Caption;
-  lstHeaders.Caption := lblHeaders.Caption;
-  if A4 or A2 or A12 then
-    begin
-      pnlLeftBottom.Visible := true;
-      Splitter1.Visible := true;
-    end;
+    begin
+      lblDates.Caption := 'Headings';  // swithes captions if not aligned
+      lblHeaders.Caption := 'Date Range';
+    end
+    else
+    begin
+      lblDates.Caption := 'Date Range';
+      lblHeaders.Caption := 'Headings';
+    end;
+  frmLabs.Realign;
 end;
 
@@ -4845,6 +5012,13 @@
       TabControl1.Visible := true;
       TabControl1.TabStop := true;
-      pnlRightTop.Height := lblTitle.Height + TabControl1.Height;
-    end;
+      pnlRightTopHeader.Height := pnlRightTopHeaderTop.Height + TabControl1.Height;
+    end;
+end;
+
+procedure TfrmLabs.HideTabControl;
+begin
+      TabControl1.Visible := false;
+      TabControl1.TabStop := false;
+      pnlRightTopHeader.Height := pnlRightTopHeaderTop.Height;
 end;
 
@@ -4861,13 +5035,6 @@
 begin
   inherited;
-  if NewSize < 10 then
-      Newsize := 10;
-end;
-
-procedure TfrmLabs.sptHorzRightMoved(Sender: TObject);
-begin
-  inherited;
-  uScreenSplitMoved := true;
-  uScreenSplitLoc := sptHorzRight.Top;
+  if NewSize < 5 then
+      Newsize := 5;
 end;
 
