unit fCosign;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ORCtrls, ExtCtrls,ORNet,ORFn,Trpcb,
  VA508AccessibilityManager,
  // interceptor unit should be listed last
  u508Interceptors;

type
  TfrmCosign = class(TForm)
    pnlCoSign: TPanel;
    pnlButtons: TPanel;
    butXCoSign: TButton;
    butCoSign: TButton;
    pnlTop: TPanel;
    pnlList: TPanel;
    edtCoSign: TEdit;
    lbCoSign: TORListBox;
    AccessibilityManager: TVA508AccessibilityManager;
    procedure butCoSignClick(Sender: TObject);
    procedure lbCoSignClick(Sender: TObject);
    procedure butXCoSignClick(Sender: TObject);
    procedure edtCoSignChange(Sender: TObject);
    procedure edtCoSignKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure edtCoSignKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCosign: TfrmCosign;

implementation

uses
  fAnticoagulator, ORNetIntf;
{$R *.dfm}

procedure TfrmCosign.butCoSignClick(Sender: TObject);
VAR
  TEST: STRING;
  aList: iORNetMult;
begin
  if cofactor = 1 then         //note
  begin
    if csduz = '' then
    begin
      if frmCosign.edtCoSign.Text = '' then
      begin
        InfoBox('No cosigner is selected.', 'Select Cosigner', MB_OK or MB_ICONEXCLAMATION);
        exit;
      end
      else
      begin
        csitem := lbCoSign.Items[lbCoSign.ItemIndex];
        edtCoSign.Text := PIECE(csitem, '^', 2);
        csduz := PIECE(csitem, '^', 1);
        if ((csduz = '') or (csduz = '0') or (csduz = '-1')) then
          InfoBox('Unrecognized selection.' + CRLF +
                  'Please click on the provider who will cosign your note.',
                  'Select Cosigner', MB_OK or MB_ICONEXCLAMATION);
      end;
    end;
    if (edtCoSign.Text = '') or ((csduz = '') or (csduz = '0') or (csduz = '-1')) then
      exit;
    neworNetMult(aList);
    aList.AddSubscript(['1202'], PDUZ);
    aList.AddSubscript(['1208'], csduz);
    CallVistA('TIU UPDATE RECORD', [NoteNumber, aList], TEST, True);
    // cvw Broker call *sometimes* returns NN^Description
    TEST := Piece(TEST, '^', 1);
    if StrToInt(TEST) < 1 then
      InfoBox('Unrecognized co-signer.' + CRLF +
              'Please check in CPRS for the status of this note.',
              'Problem Identifying Cosigner', MB_OK or MB_ICONEXCLAMATION);
  end    //end cofactor=1

  else if cofactor = 2 then       //lab order
  begin
    if csduz = '' then
    begin
      if frmCosign.edtCoSign.Text = '' then
      begin
        InfoBox('No Pharmacist selected.', 'Need Pharmacist', MB_OK or MB_ICONEXCLAMATION);
        exit;
      end
      else
      begin
        csitem := lbCoSign.Items[lbCoSign.ItemIndex];
        edtCoSign.Text := PIECE(csitem, '^', 2);
        csduz := PIECE(csitem, '^', 1);
        if ((csduz = '') or (csduz = '0') or (csduz = '-1')) then
          InfoBox('Unrecognized selection.' + CRLF + 'Please click on the pharmacist who will sign the lab order.',
                  'Select Pharmacist', MB_OK or MB_ICONEXCLAMATION);
      end;
    end;
    if (edtCoSign.Text = '') or ((csduz = '') or (csduz = '0') or (csduz = '-1')) then
      exit;
    if frmAnticoagulate.ckbINROrder.checked = true then
    begin
      lab := 'INR';
      orderit(PDFN, csduz, lab);
    end;
    if frmAnticoagulate.ckbCBC.checked = true then
    begin
      lab := 'CBC';
      orderit(PDFN, csduz, lab);
    end;
  end;   //end cofactor = 2
  Close;
end;

procedure TfrmCosign.lbCoSignClick(Sender: TObject);
begin
  csitem := lbCoSign.Items[lbCoSign.ItemIndex];
  edtCoSign.Text := PIECE(csitem, '^', 2);
  csduz:=PIECE(csitem,'^',1);
  butCoSign.Enabled := true;
end;


procedure TfrmCosign.butXCoSignClick(Sender: TObject);
var
  errmsg: string;
begin
  if cofactor = 1 then
  begin
    CallVistA('TIU DELETE RECORD', [NoteNumber], errmsg);
    if strtoint(errmsg) = 1 then
      InfoBox('Unable to delete note: ' + PIECE(errmsg, '^', 2),
              'Deletion not Permitted', MB_OK or MB_ICONEXCLAMATION);
    NoteNumber := '';
    close;
  end;
  if cofactor = 2 then
    close;
end;


procedure TfrmCosign.edtCoSignChange(Sender: TObject);
var
  startstr:string;
begin
  startstr:=Copy(UpperCase(edtCoSign.Text), 0, (Length(edtCoSign.Text) - 2));
  CallVistA('ORWTPP GETCOS', [startstr,1,''], lbCoSign.Items);
end;

procedure TfrmCosign.edtCoSignKeyPress(Sender: TObject; var Key: Char);
begin
  if key=chr (13) then
  begin
    if butCoSign.Enabled=true then
    begin
      butCoSign.Click;
      close;
    end
    else
    begin
      csitem := lbCoSign.Items[0];
      edtCoSign.Text := PIECE(csitem,'^',2);
      csduz := PIECE(csitem,'^',1);
      butCoSign.Enabled := true;
    end;
  end;
end;

procedure TfrmCosign.FormShow(Sender: TObject);
begin
  edtCoSign.SetFocus;
  butCoSign.Enabled := false;
end;

procedure TfrmCosign.edtCoSignKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key<>VK_RETURN  then butCoSign.Enabled := false;
end;

end.
