procedure open_log(log: file_type);
begin
  assign(logfile,log);
  {$I-}
  reset(logfile);
end;

procedure check_dup;
var duplog : file_type;
    dupline,
    callsign : msg_type;
    log_name : file_type;
begin
  case mode of
    CW    : log_name := cw_log_file;
    RTTY  : log_name := rtty_log_file;
    ASCII : log_name := ascii_log_file;
  end;
  halt_xmt;
  save_screen;
  aux_color;
  frame(1,3,80,6);
  window(2,4,79,5);
  clrscr;
  get_file_name(duplog,1,1,'View log ['+log_name+']',2,4,79,5,4);
  if (duplog = '') then duplog := log_name;
  open_log(duplog);
  if (IOresult <> 0)
  then
    begin
      writeln; writeln('File not found ... press any key to continue.');
      wait_for_key;
    end
  else
    begin
      clrscr;
      write('Enter callsign for dup check ... ');
      readln(callsign);
      UpperCase(callsign);
      repeat
        readln(logfile,dupline);
        UpperCase(dupline);
      until (pos(callsign,dupline) > 0) OR Eof(logfile) ;
      if (pos(callsign,dupline) > 0) then
      begin
        clrscr;
        writeln(dupline);
        write('Press any key to continue ...');
        wait_for_key;
      end;
    end;
  close(logfile);
  full_window;
  restore_screen;
end;

procedure view_log;
label endview;
var viewlog : file_type;
    viewline : string[80];
    i : integer;
    log_name : file_type;
begin
  case mode of
    CW    : log_name := cw_log_file;
    RTTY  : log_name := rtty_log_file;
    ASCII : log_name := ascii_log_file;
  end;
  halt_xmt;
  save_screen;
  aux_color;
  frame(1,3,80,20);
  window(2,4,79,19);
  clrscr;
  get_file_name(viewlog,1,1,'View log ['+log_name+']',2,4,79,19,4);
  if (viewlog = '') then viewlog := log_name;
  open_log(viewlog);
  if (IOresult <> 0)
  then
    begin
      writeln; writeln('File not found ... press any key to continue.');
      wait_for_key;
    end
  else
    begin
      i := 0;
      clrscr;
      repeat
        readln(logfile,viewline);
        writeln(viewline);
        i := i + 1;
        if (i = 15) then
        begin
          write('<ESC> escape ... <RET> continue ...');
          repeat
            qkey := readkey;
            if (qkey = #0) then qkey := readkey;
          until (qkey in [#27,#13]);
          if (qkey = #27) then goto endview;
          gotoxy(1,16); ClrEol;
          i := 0;
        end;
      until (Eof(logfile));
      gotoxy(1,16); write('Press any key to continue..');
      wait_for_key;
    end;
endview:
  close(logfile);
  full_window;
  restore_screen;
end;

procedure update_log;
label save_fault;
var i : integer;
    dummy : string[80];
    log_name : file_type;
begin
  case mode of
    CW    : log_name := cw_log_file;
    RTTY  : log_name := rtty_log_file;
    ASCII : log_name := ascii_log_file;
  end;
  if (qso_nbr <> 0) then
  begin
    halt_xmt;
    save_screen;
    aux_color;
    frame(19,7,51,17);
    window(20,8,50,16);
    clrscr;
    gotoxy(5,4); writeln('UPDATING ',log_name);
    open_log(log_name);
    if (IOresult <> 0) then
      begin
        rewrite(logfile);
        if (IOresult <> 0) then
          begin
            writeln('Unable to open file ',log_name);
            delay(2000);
            goto save_fault;
          end
        else
          begin
            writeln(logfile,
'  DATE    TIME      FREQ  PWR   RST     CALL           COMMENTS');
            writeln(logfile,
'                              OUT  IN');
            writeln(logfile);
          end;
      end
    else
      begin
        while not eof(logfile) do readln(logfile,dummy);
        append(logfile);
      end;
  for i := 1 to qso_nbr do
    begin
      write(logfile,logbook[i].date:8);
      write(logfile,logbook[i].time:6);
      write(logfile,logbook[i].freq:10);
      write(logfile,logbook[i].power:5);
      write(logfile,logbook[i].rst_out:4);
      write(logfile,logbook[i].rst_in:4);
      write(logfile,logbook[i].callsign:8);
      writeln(logfile,logbook[i].comments:31);
      if (IOresult <> 0) then goto save_fault;
    end;
  qso_nbr := 0;
save_fault:
  close(logfile);
  full_window;
  restore_screen;
  end;
end;

procedure xxx(nbr : integer);
var  i : integer;
begin
  for i := 1 to nbr do write('x');
  for i := 1 to nbr do write(chr(8));
end;

procedure log_qso;
begin
  halt_xmt;
  save_screen;
  aux_color;
  frame(14,7,66,19);
  window(15,8,65,18);
  clrscr;
  qso_nbr := qso_nbr + 1;
  gotoxy(1,1); write('Date .............. ');
  gotoxy(1,2); write('Time .............. ');
  gotoxy(1,3); write('Freq .............. ');
  gotoxy(1,4); write('Power ............. ');
  gotoxy(1,5); write('Outgoing RST ...... ');
  gotoxy(1,6); write('Incoming RST ...... ');
  gotoxy(1,7); write('Call sign worked .. ');
  gotoxy(1,8); write('Comments [name, etc]');
  gotoxy(20,1); xxx(8); readln(logbook[qso_nbr].date);
  if (logbook[qso_nbr].date = '') then
    logbook[qso_nbr].date := date;
  gotoxy(20,1); ClrEol; write(logbook[qso_nbr].date);
  gotoxy(20,2); xxx(8); readln(logbook[qso_nbr].time);
  if (logbook[qso_nbr].time = '') then
      logbook[qso_nbr].time := time;
  gotoxy(20,2); ClrEol; write(logbook[qso_nbr].time);
  gotoxy(20,3); xxx(8); readln(logbook[qso_nbr].freq);
  gotoxy(20,3); ClrEol; write(logbook[qso_nbr].freq);
  gotoxy(20,4); xxx(4); readln(logbook[qso_nbr].power);
  gotoxy(20,4); ClrEol; write(logbook[qso_nbr].power);
  gotoxy(20,5); xxx(3); readln(logbook[qso_nbr].rst_out);
  gotoxy(20,5); ClrEol; write(logbook[qso_nbr].rst_out);
  gotoxy(20,6); xxx(3); readln(logbook[qso_nbr].rst_in);
  gotoxy(20,6); ClrEol; write(logbook[qso_nbr].rst_in);
  gotoxy(20,7); xxx(8); readln(logbook[qso_nbr].callsign);
  gotoxy(20,7); ClrEol; write(logbook[qso_nbr].callsign);
  gotoxy(10,10); xxx(30); readln(logbook[qso_nbr].comments);
  restore_screen;
  if (qso_nbr = 100) then update_log;
end;
