program knaveos;

uses
 windos, crt, dos;

type
 str2=string[2];
 str80=string[80];
 str7= string[7];
 str120=string[120];
 str20=string[20];
 str11=string[11];

 adrek = record
          street: string[15];
          apt: string[10];
          city: string[11];
          state: string[2];
          zip: string[5];
          telephone: string[11];
         end;
 drek = record
         month: integer;
         day: integer;
         year: integer;
        end;
 rekord= record
           name:str20;
           address: adrek;
           ssnum: string[11];
           dob: drek;
           sex: char;
           mstatus: char;
           numdep: 1..12;
           vdays: integer;
           vused: integer;
           sdays: integer;
           paytype: char;
           pay: real;
           hours: integer;
           totalearnings: real;
           ssperiod: real;
           fedperiod:real;
           ss: real;
           fed: real;
           medplan: char;
           doe: drek;
         end;
 recarray = array[1..50] of rekord;
 barray = array[1..50] of boolean;
 charay = array[0..8] of char;
var
 temprec:
           rekord;
 emprek:
           recarray;
 rpath,
 comstring:
           str120;
 p,
 ofile,
 infile:
           text;
 ctoggle,
 acksess:
           boolean;
 formempnum,
 tcount,
 trace,
 numofemp,
 target:
           integer;
 sarray:
           charay;
const
  days : array [0..6] of String[9] =
    ('Sunday','Monday','Tuesday',
     'Wednesday','Thursday','Friday',
     'Saturday');

procedure printdate;

var
  y, m, d, dow : Word;
begin
  GetDate(y,m,d,dow);
  Write(days[dow],', ',
        m:0, '/', d:0, '/', y:0);
end;

procedure printtime;

var
  h, m, s, hund : Word;

function LeadingZero(w : Word) : String;
var
  s : String;
begin
  Str(w:0,s);
  if Length(s) = 1 then
    s := '0' + s;
  LeadingZero := s;
end;

begin
  GetTime(h,m,s,hund);
  Write(LeadingZero(h),':',  LeadingZero(m),':',LeadingZero(s))
end;

procedure letterhead;
begin
  writeln ('ACME Buttered Toast Company');
  write ('Printed at: ');
  printtime;
  write (' On ');
  printdate;
  writeln;
  writeln ('1205 Sandpiper');
  writeln ('Seabrook, TX, 77586');
  writeln ('281-474-4709');
  writeln;
end;

procedure erase (var emprek: recarray;
                  count: integer);

begin
     emprek[count].name := ' ';
      with emprek[count].address do
       begin
        street:=' ';
        apt:=' ';
        city:=' ';
        state:=' ';
        zip:=' ';
        telephone:=' ';
       end;
      emprek[count].ssnum:=' ';
      emprek[count].dob.month:=0;
      emprek[count].dob.day:=0;
      emprek[count].dob.year:=0;
      with emprek[count] do
       begin
        sex:=' ';
        mstatus:=' ';
        numdep:=1;
        vdays:=0;
        vused:=0;
        sdays:=0;
        paytype:=' ';
        pay:=0;
        hours:=0;
        totalearnings:=0;
        ssperiod:=0;
        fedperiod:=0;
        ss:=0;
        fed:=0;
        medplan:=' ';
       end;
      with emprek[count].doe do
       begin
        month:=0;
        day:=0;
        year:=0;
       end;
     end;

procedure initialize(var emprek: recarray);
var
 xcount: integer;
begin
 for xcount := 1 to 50 do
 erase (emprek, xcount);
end;

procedure readfile(var emprek: recarray;
                   var count: integer;
                   var path: str120);
var
 xcount: integer;
 zcount: integer;
 begin
  zcount:=1;
  assign (infile, path);
  reset(infile);
  count :=0;
  if path <> 'C:\PASCAL\EMPREC.97' then readln(infile, xcount);
   while not eof(infile) do
    begin
     count:=count+1;
     readln (infile, emprek[count].name);
      with emprek[count].address do
       begin
        readln (infile, street);
        readln (infile, apt);
        readln (infile, city);
        readln (infile, state);
        readln (infile, zip);
        readln (infile, telephone);
       end;
      readln (infile, emprek[count].ssnum);
      read (infile, emprek[count].dob.month);
      read (infile, emprek[count].dob.day);
      readln (infile, emprek[count].dob.year);
      with emprek[count] do
       begin
        readln (infile, sex);
        readln (infile, mstatus);
        readln (infile, numdep);
        readln (infile, vdays);
        readln (infile, vused);
        readln (infile, sdays);
        readln (infile, paytype);
        readln (infile, pay);
        readln (infile, hours);
        readln (infile, totalearnings);
        readln (infile, ssperiod);
        readln (infile, fedperiod);
        readln (infile, ss);
        readln (infile, fed);
        readln (infile, medplan);
       end;
      with emprek[count].doe do
       begin
        read (infile, month);
        read (infile, day);
        readln (infile, year);
       end;
     end;
   close(infile);
 end;

procedure setrpath(xpath: str120);
begin
 rpath:= xpath;
end;

procedure slowprint (pstring: string; color,del: integer; ffeed: char);
 var
  xcnt: integer;
 begin
  textcolor(color);
  xcnt := 1;
   repeat
     write (pstring[xcnt]);
      sound(500);
      delay(1);
      nosound;
      xcnt:= xcnt+1;
      if keypressed then halt;
      delay(del);
   until xcnt = length(pstring)+1;
   if ffeed = 'y' then writeln;
 end;

function leftdigit(tnumber:real): integer;
var
 ftnumber: real;
 ftcount: integer;
  begin
    ftcount :=0;
    ftnumber := tnumber;
    repeat
     ftnumber := ftnumber / 10;
     ftcount := ftcount+1;
    until ftnumber < 1;
    leftdigit := ftcount;
  end;

function upscase(var upstring: str120) : str120;
 var
  i: integer;

 begin
  for i := 1 to length(upstring) do
    upstring[i] := upcase(upstring[i]);
  upscase := upstring;
 end;

function upscase2(var upstring: str20): str20;
 var
  i: integer;

 begin
  for I:=1 to length(upstring) do
   upstring[i] := upcase(upstring[i]);
   upscase2:=upstring;
  end;

procedure xwrite(wstr: str80;
                 tempcol, rspaces, sfield: integer;
                 var cfield: integer);
var
 col: integer;
begin
  col:=textattr;
  textcolor(tempcol);
  cfield := cfield + 1;
  if sfield = cfield then textbackground(1);
  write (wstr:rspaces);
  textbackground(0);
  textcolor(col);
end;

procedure xwrite2(wstr: str80;
                 tempcol, rspaces, sfield, cfield: integer);
var
 col: integer;
begin
  col:=textattr;
  textcolor(tempcol);
  if sfield = cfield then textbackground(1);
  write (wstr:rspaces);
  textbackground(0);
  textcolor(col);
end;


procedure rwriteln(outnum: real;
                 tempcol, rspaces, dig, sfield: integer;
                 var cfield: integer);
var
 col: integer;
begin
  col:=textattr;
  textcolor(tempcol);
  cfield :=cfield + 1;
  if sfield = cfield then textbackground(1);
  writeln (outnum:rspaces:dig);
  textbackground(0);
  textcolor(col);
end;

procedure rwrite(outnum: real;
                 tempcol, rspaces, dig, sfield: integer;
                 var cfield: integer);
var
 col: integer;
begin
  col:=textattr;
  textcolor(tempcol);
  cfield :=cfield + 1;
  if sfield = cfield then textbackground(1);
  write (outnum:rspaces:dig);
  textbackground(0);
  textcolor(col);
end;

procedure xwriteln(wstr: str80;
                   tempcol, rspaces, sfield: integer;
                   var cfield: integer);
var
 col: integer;
begin
  col:=textattr;
  textcolor(tempcol);
  cfield := cfield + 1;
  if sfield = cfield then textbackground(1);
  writeln (wstr:rspaces);
  textbackground(0);
  textcolor(col);
end;


procedure sinput1(var thing1: str11);
var
 it: char;
 ipos: integer;
 begin
  ipos:=1;
   gotoxy (14,5);
  write ('(');
  repeat
  it  := readkey;
  if (it <> chr(13)) and (it <> chr(8)) then
  begin
   write (it);
   thing1[ipos] := it;
   ipos:=ipos+1;
   if ipos = 4 then write (')');
  end;
  if (it = chr(8)) and (ipos > 1) then
  begin
   write (chr(8));
   if ipos = 4 then write (chr(8));
   if ipos = 4 then write (' ');
   write (' ');
   write (chr(8));
   if ipos = 4 then write (chr(8));
   ipos:=ipos-1;
   thing1[ipos] := ' ';
  end;
  thing1:=thing1;
  until it = chr(13);
 end;

function dayval(int:integer): integer;
begin
 case int of
  1,3,5,7,8,10,12: dayval:=31;
  4,6,9,11: dayval := 30;
  2: dayval := 28;
 end;
end;

procedure writefile (var emprek: recarray;
                     count: integer;
                     pathname: str120);
var
 tcount: integer;
begin
 assign (ofile, pathname);
 rewrite(ofile);
 writeln (ofile, count);
 for tcount := 1 to count do
  begin
   writeln (ofile, emprek[tcount].name);
      with emprek[tcount].address do
       begin
        writeln (ofile, street);
        writeln (ofile, apt);
        writeln (ofile, city);
        writeln (ofile, state);
        writeln (ofile, zip);
        writeln (ofile, telephone);
       end;
      writeln (ofile, emprek[tcount].ssnum);
      write (ofile, emprek[tcount].dob.month,' ');
      write (ofile, emprek[tcount].dob.day,' ');
      writeln (ofile, emprek[tcount].dob.year);
      with emprek[tcount] do
       begin
        writeln (ofile, sex);
        writeln (ofile, mstatus);
        writeln (ofile, numdep);
        writeln (ofile, vdays);
        writeln (ofile, vused);
        writeln (ofile, sdays);
        writeln (ofile, paytype);
        writeln (ofile, pay:0:2);
        writeln (ofile, hours);
        writeln (ofile, totalearnings:0:2);
        writeln (ofile, ssperiod:0:2);
        writeln (ofile, fedperiod:0:2);
        writeln (ofile, ss:0:2);
        writeln (ofile, fed:0:2);
        writeln (ofile, medplan);
       end;
      with emprek[tcount].doe do
       begin
        write (ofile, month,' ');
        write (ofile, day,' ');
        writeln (ofile, year);
       end;
     end;
   close(ofile);
 end;

procedure selsort(var emprek: recarray;
                      numoemp : integer);

var
 jcount, kcount: integer;
 index: integer;
 temprec: rekord;

 begin
   for jcount := 1 to numoemp -1 do
    begin
      temprec := emprek[jcount];
      index := jcount;
       for kcount := jcount + 1 to numoemp do
        if emprek[kcount].name < temprec.name then
         begin
          temprec := emprek[kcount];
          index := kcount;
         end;
       emprek[index] := emprek[jcount];
       emprek[jcount] := temprec;
    end;
end;


procedure editval(var emprek: recarray;
                  count, sfield: integer);
var
inchar: char;
eraseint: integer;
thing1: str11;
thing2: integer;
begin
  if sfield = 1 then
  begin
   gotoxy(16,2);
   for eraseint := 1 to length(emprek[count].name) do
   write (' ');
   gotoxy(16,2);
   readln (emprek[count].name);
  end;
  if sfield = 2 then
  begin
   gotoxy(10,3);
   for eraseint := 1 to length(emprek[count].address.street) do
   write (' ');
   gotoxy(10,3);
   readln (emprek[count].address.street);
  end;
 if sfield = 3 then
 begin
  gotoxy(56,3);
  if length(emprek[count].address.apt) > 0 then
  for eraseint := 1 to length(emprek[count].address.apt) do
  write (' ')
  else
  write ('   ');
  gotoxy(56,3);
  readln (emprek[count].address.apt);
 end;
 if sfield = 4 then
 begin
  gotoxy(7,4);
  for eraseint := 1 to length(emprek[count].address.city) do
  write (' ');
  gotoxy(7,4);
  readln (emprek[count].address.city);
 end;
if sfield = 5 then
 begin
  gotoxy(31,4);
  for eraseint := 1 to length(emprek[count].address.state) do
  write (' ');
  gotoxy(31,4);
  readln (emprek[count].address.state);
 end;
if sfield = 6 then
 begin
  gotoxy(53,4);
  for eraseint := 1 to length(emprek[count].address.zip) do
  write (' ');
  gotoxy(53,4);
  readln (emprek[count].address.zip);
 end;
if sfield = 7 then
 begin
  sinput1(thing1);
  emprek[count].address.telephone:=thing1;
 end;
if sfield = 8 then
 begin
  gotoxy(62,5);
  for eraseint := 1 to length(emprek[count].ssnum) do
  write (' ');
  gotoxy(62,5);
  readln (emprek[count].ssnum);
 end;
if sfield = 9 then
 begin
  repeat
  window(1,24,80,24);
  write ('Type the number of the month then hit enter');
  window(1,1,80,24);
  gotoxy(11,6);
  readln(thing2);
  until thing2 <= 12;
  emprek[count].dob.month:=thing2;
  gotoxy(13,6);
  write ('/');
  repeat
  window(1,24,80,24);
  write ('Type the number of the day then hit enter');
  window(1,1,80,24);
  gotoxy(14,6);
  readln(thing2);
  until thing2 <= dayval(emprek[count].dob.month);
  emprek[count].dob.day:=thing2;
  gotoxy(16,6);
  write ('/');
  repeat
  window(1,24,80,24);
  write ('Type the number of the year <two digit> then hit enter');
  window(1,1,80,24);
  gotoxy(17,6);
  readln(thing2);
  emprek[count].dob.year:=thing2;
  until thing2 > -1;
  window(1,1,80,24);
 end;
 if sfield = 10 then
 begin
  gotoxy(48,6);
  write (' ');
  gotoxy(48,6);
  readln(emprek[count].sex);
 end;
 if sfield = 11 then
 begin
  gotoxy(17,7);
  write (' ');
  gotoxy(17,7);
  repeat
  window(1,24,80,24);
  write('''S'' for Single, ''M'' for Married, or ''D'' for Divorced');
  window(1,1,80,24);
  inchar:=upcase(readkey);
  until (inchar = 'M') or (inchar = 'S') or (inchar = 'D');
  emprek[count].mstatus := inchar;
 end;
 if sfield = 12 then
 begin
  gotoxy(65,7);
  write ('  ');
  gotoxy(65,7);
  readln(emprek[count].numdep);
 end;
 if sfield = 13 then
 begin
  gotoxy(16,8);
  write ('    ');
  gotoxy(16,8);
  readln(emprek[count].vdays);
 end;
 if sfield = 14 then
 begin
  gotoxy(58,8);
  write('      ');
  gotoxy(58,8);
  readln(emprek[count].vused);
 end;
 if sfield = 15 then
  begin
   gotoxy(18,9);
   write('      ');
   gotoxy(18,9);
   readln(emprek[count].sdays);
  end;
 if sfield = 16 then
  begin
   window(1,23,80,23);
   write('Enter ''H'' for hourly or ''S'' for salaried');
   window(1,1,80,24);
   gotoxy(63,9);
   write('                 ');
   gotoxy(63,9);
   emprek[count].paytype := upcase(readkey);
  end;
 if sfield = 17 then
  begin
   gotoxy(16,10);
   write('               ');
   gotoxy(16,10);
   readln(emprek[count].pay);
  end;
 if sfield = 18 then
  begin
   gotoxy(57,10);
   write('     ');
   gotoxy(57,10);
   readln(emprek[count].hours);
  end;
 if sfield = 19 then
  begin
   gotoxy(17, 11);
   write('              ');
   gotoxy(17,11);
   readln(emprek[count].totalearnings);
  end;
 if sfield = 20 then
  begin
   gotoxy(66,11);
   write('         ');
   gotoxy(66,11);
   readln(emprek[count].ssperiod);
  end;
 if sfield = 21 then
  begin
   gotoxy(23,12);
   write('           ');
   gotoxy(23,12);
   readln(emprek[count].fedperiod);
  end;
 if sfield = 22 then
  begin
   gotoxy(62, 12);
   write('           ');
   gotoxy(62,12);
   readln(emprek[count].ss);
  end;
 if sfield = 23 then
  begin
   gotoxy(19,13);
   write('           ');
   gotoxy(19,13);
   readln(emprek[count].fed);
  end;
 if sfield = 24 then
  begin
   window(1,24,80,24);
   write('Hit ''H'' for HMO or ''R'' for Regular');
   window(1,1,80,24);
   gotoxy(57,13);
   write('             ');
   gotoxy(57,13);
   emprek[count].medplan := readkey;
  end;
 if sfield = 25 then
 begin
  repeat
  window(1,24,80,24);
  write ('Type the number of the month then hit enter');
  window(1,1,80,24);
  gotoxy(21,14);
  readln(thing2);
  until thing2 <= 12;
  emprek[count].doe.month:=thing2;
  gotoxy(23,14);
  write ('/');
  repeat
  window(1,24,80,24);
  write ('Type the number of the day then hit enter');
  window(1,1,80,24);
  gotoxy(24,14);
  readln(thing2);
  until thing2 <= dayval(emprek[count].doe.month);
  emprek[count].doe.day:=thing2;
  gotoxy(26,14);
  write ('/');
  repeat
  window(1,23,80,23);
  write ('Type the number of the year <two digit> then hit enter');
  window(1,1,80,24);
  gotoxy(27,14);
  readln(thing2);
  emprek[count].doe.year:=thing2;
  until thing2 > -1;
  window(1,1,80,24);
 end;
end;

procedure delemp(var emprek: recarray;
                 var empnum: integer;
                 var numuvemp: integer;
                 var trec: rekord);
var
 zcount: integer;
begin
 if empnum <> numuvemp then
 begin
  trec := emprek[empnum];
  for zcount := empnum to numuvemp do
  emprek[empnum] := emprek[empnum+1];
  erase(emprek,numuvemp);
  setrpath('C:\CUREMP.TXT');
  numuvemp:=numuvemp-1;
  writefile(emprek, numuvemp, rpath);
  setrpath('C:\FORMEMP.TXT');
  readfile(emprek, formempnum, rpath);
  formempnum:=formempnum+1;
  emprek[formempnum]:=trec;
  writefile(emprek, formempnum, 'C:\FORMEMP.TXT');
  empnum:=formempnum;
 end
 else
 if empnum = numuvemp then
 begin
  trec:=emprek[empnum];
  erase(emprek,empnum);
  setrpath('C:\CUREMP.TXT');
  numuvemp:=numuvemp-1;
  writefile(emprek, numuvemp, rpath);
  repeat until keypressed;
  setrpath('C:\FORMEMP.TXT');
  readfile(emprek, formempnum, rpath);
  formempnum:=formempnum+1;
  emprek[formempnum]:=trec;
  writefile(emprek, formempnum, 'C:\FORMEMP.TXT');
  empnum:=formempnum;
 end;
end;

procedure edit(var emprek: recarray;
               var count: integer;
               var numoemp: integer;
               var fempnum: integer);
var
 commchar: char;
 phone: integer;
 scount: string[10];
 exitflag: boolean;
 sfield: integer;
 curfield: integer;
 commchar2: char;
begin
 clrscr;
 exitflag:=false;
 count:=1;
 sfield:=1;
 commchar:='z';
 repeat
 curfield:=0;
 textcolor(7);
 gotoxy(1,1);
 writeln ('File: ', rpath,'           ');
 write ('Employee Name: ');
 xwrite (emprek[count].name, 8,0,sfield,curfield);
 write ('Employee Number: ':44-length(emprek[count].name));
 write (count);
 writeln;
 write ('Address: ');
 xwrite (emprek[count].address.street,8,0,sfield,curfield);
 write ('Apartment #: ':46-length(emprek[count].address.street));
 xwrite (emprek[count].address.apt,8,0,sfield,curfield);
 if (length(emprek[count].address.apt) = 0) and (sfield=curfield) then
 begin
  textbackground(1);
  write ('   ');
  textbackground(0);
 end;
 writeln;
 write ('City: ');
 xwrite (emprek[count].address.city,8,0,sfield,curfield);
 write ('State: ':24-length(emprek[count].address.city));
 xwrite (emprek[count].address.state,8,0,sfield,curfield);
 write ('Zip Code: ':22-length(emprek[count].address.state));
 xwriteln (emprek[count].address.zip,8,0,sfield,curfield);
 write ('Telephone #: ');
 curfield:=curfield+1;
 for phone := 1 to 11 do
 begin
  if phone = 1 then
   xwrite2 ('(',7,0,sfield,curfield);
   xwrite2 (emprek[count].address.telephone[phone],8,0,sfield,curfield);
  if phone = 3 then xwrite2 (')',7,0,sfield,curfield);
 end;
 write ('Social Security #: ':46-length(emprek[count].address.telephone));
 xwriteln (emprek[count].ssnum,8,0,sfield,curfield);
 curfield:=curfield+1;
 write ('Birthday: ');
 str (emprek[count].dob.month,scount);
 if length (scount) = 1 then xwrite2 ('0',8,0,sfield,curfield);
 xwrite2 (scount,8,0,sfield,curfield);
 xwrite2 ('/',7,0,sfield,curfield);
 str (emprek[count].dob.day,scount);
 if length (scount) = 1 then xwrite2 ('0',8,0,sfield,curfield);
 xwrite2 (scount,8,0,sfield,curfield);
 xwrite2 ('/',7,0,sfield,curfield);
 str (emprek[count].dob.year,scount);
 xwrite2 (scount,8,0,sfield,curfield);
 write ('Sex: ':29);
 xwriteln(upcase(emprek[count].sex),8,0,sfield,curfield);
 write ('Marital Status: ');
 xwrite (upcase(emprek[count].mstatus),8,0,sfield,curfield);
 write ('Number of Dependents: ':47);
 str (emprek[count].numdep,scount);
 xwriteln (scount,8,0,sfield,curfield);
 write ('Vacation days: ');
 str (emprek[count].vdays, scount);
 xwrite (scount,8,0,sfield,curfield);
 write ('Vacation used: ':42-length(scount));
 str (emprek[count].vused, scount);
 xwriteln (scount,8,0,sfield,curfield);
 write ('Sick days taken: ');
 str (emprek[count].sdays, scount);
 xwrite (scount,8,0,sfield,curfield);
 write ('Hourly or Salaried: ':45-length(scount));
 if upcase(emprek[count].paytype) = 'H' then xwriteln ('Hourly',8,0,sfield,curfield)
 else
 if upcase(emprek[count].paytype) = 'S' then xwriteln ('Salaried',8,0,sfield,curfield)
 else
 xwriteln ('Hired Assassin',8,0,sfield,curfield);
 if upcase(emprek[count].paytype) = 'S' then write ('Salary / year: ')
 else
 if upcase(emprek[count].paytype) = 'H' then write ('Wage per hour: ')
 else
 write ('Price per hit: ');
 rwrite (emprek[count].pay,8,0,2,sfield,curfield);
 write ('Hours worked: ':38-leftdigit(emprek[count].pay));
 str(emprek[count].hours, scount);
 xwriteln (scount,8,0,sfield,curfield);
 write ('Total Earnings: ');
 rwrite (emprek[count].totalearnings,8,0,2,sfield, curfield);
 write ('Soc. Sec. this period: ':46-leftdigit(emprek[count].totalearnings));
 rwriteln (emprek[count].ssperiod,8,0,2,sfield,curfield);
 write ('Fed. Tax this period: ');
 rwrite (emprek[count].fedperiod,8,0,2,sfield,curfield);
 write ('Soc. Sec. to date: ':36-leftdigit(emprek[count].fedperiod));
 rwriteln (emprek[count].ss,8,0,2,sfield,curfield);
 write ('Fed. Tax to date: ');
 rwrite (emprek[count].fed,8,0,2,sfield,curfield);
 write ('Medical Plan: ':35-leftdigit(emprek[count].fed));
 if upcase(emprek[count].medplan) = 'H' then
 xwriteln('HMO',8,0,sfield,curfield)
 else
 if upcase(emprek[count].medplan) = 'R' then
 xwriteln('Regular',8,0,sfield,curfield)
 else
 xwriteln('Organ Donor',8,0,sfield,curfield);
 curfield := curfield+1;
 write ('Date of Employment: ');
 str (emprek[count].doe.month,scount);
 if length (scount) = 1 then xwrite2 ('0',8,0,sfield,curfield);
 xwrite2 (scount,8,0,sfield,curfield);
 xwrite2 ('/',7,0,sfield,curfield);
 str (emprek[count].doe.day,scount);
 if length (scount) = 1 then xwrite2 ('0',8,0,sfield,curfield);
 xwrite2 (scount,8,0,sfield,curfield);
 xwrite2 ('/',7,0,sfield,curfield);
 str (emprek[count].doe.year,scount);
 xwrite2 (scount,8,0,sfield,curfield);
 writeln;
 writeln;
 writeln ('Hit '']'' to scroll forward and ''['' to scroll back');
 if rpath = 'C:\CUREMP.TXT' then
 begin
  writeln ('Hit ''TAB'' to move forward a field, ''*'' to move backwards');
  writeln ('Hit ''A'' to add employee');
  writeln ('Hit ''D'' to delete employee');
  writeln ('Hit ''E'' to edit value');
  writeln ('Hit ''S'' to save changes into C:\CUREMP.TXT');
 end;
 writeln ('Hit ''Q'' to quit');
 repeat
 commchar := upcase(readkey);
  if (rpath = 'C:\FORMEMP.TXT') then
  begin
   if (commchar = ']') and (count < fempnum) then
  begin
   count := count + 1;
   clrscr;
   exitflag:=true;
  end;
  if (commchar = '[') and (count > 1) then
  begin
   count := count - 1;
   clrscr;
   exitflag:=true;
  end;
 end
 else
 begin
  if (commchar = ']') and (count < numoemp) then
  begin
   count := count + 1;
   clrscr;
   exitflag:=true;
  end;
  if (commchar = '[') and (count > 1) then
  begin
   count := count - 1;
   clrscr;
   exitflag:=true;
  end;
  if commchar = 'A' then
  begin
   numoemp := numoemp +1;
   erase(emprek,numoemp);
  end;
  if commchar = 'D' then
  begin
   write ('Are you SURE? (y/N)');
   commchar2 := upcase(readkey);
   if commchar2 = 'Y' then delemp(emprek, count, numoemp,temprec);
   clrscr;
   exitflag := true;
  end;
 if (commchar = '*') and (sfield > 1) then
 begin
  sfield := sfield - 1;
  clrscr;
  exitflag:=true;
 end;
  if (commchar = chr(9)) and (sfield < 25) then
 begin
  sfield := sfield + 1;
  clrscr;
  exitflag := true;
 end;
 if (commchar = 'E') then
 begin
  editval(emprek, count, sfield);
  clrscr;
  exitflag := true;
 end;
 if (commchar = 'S') then
 writefile (emprek, numoemp, 'C:\CUREMP.TXT');
 end;
 if commchar = 'Q' then exitflag := true;
 until exitflag = true;
 exitflag:=false;
 until upcase(commchar)='Q';
end;

procedure lsearch(var emprek: recarray;
                  sstring: str20;
                  numuvemp: integer;
                  var ttarget: integer);
VAR
xcount, ycount: integer;
tempray: barray;
matchnum: integer;
tempint: integer;

 begin
  ttarget:=0;
  matchnum:=0;
  for xcount := 1 to 50 do
   tempray[xcount]:=false;
  for xcount := 1 to numuvemp do
    if pos(upscase2(sstring), upscase2(emprek[xcount].name)) > 0 then
    begin
      tempray[xcount]:=true;
      matchnum:=matchnum+1;
    end;
  if matchnum > 1 then
  begin
   repeat
    writeln (Matchnum, ' matches found.');
    writeln;
    for xcount := 1 to 50 do
     if tempray[xcount]=true then writeln (xcount, '. ', emprek[xcount].name);
    writeln('Select employee number: ');
    readln(Ttarget);
   until tempray[ttarget] = true;
  end;
  if matchnum = 1 then
  for xcount := 1 to 50 do
   if tempray[xcount]=true then ttarget:=xcount;
 end;

procedure print (var emprek: recarray;
                 var numuvemp, fempnum: integer;
                 commchar: char);
var
phone, count: integer;
scount: string[12];
searchstr: str20;
net, gross: real;
fedtaxp, fedtax: real;
begin
 case commchar of
  'A': begin
         setrpath('C:\CUREMP.TXT');
         readfile(emprek,numofemp,rpath);
         selsort(emprek,numofemp);
         letterhead;
         for count := 1 to numuvemp do
         begin
          gross:=0.0;
          net:=0.0;
          fedtaxp:=0.0;
          fedtax:=0.0;
          writeln ('Employee Name: ', EMprek[count].name);
          if emprek[count].paytype = 'S' then gross := emprek[count].pay
          else
          gross:=emprek[count].pay * 52;
          writeln('Gross Earnings: ',Gross:0:2);
          emprek[count].ssperiod := gross * 0.0765;
          writeln ('Social Security: ', emprek[count].ssperiod:0:2);
          if emprek[count].mstatus = 'M' then
           case round(gross) div 10 of

            0..600:         fedtaxp:=0.09;
            600..2400:      fedtaxp:=0.15;
            2400..5815:     fedtaxp:=0.28;
            5815..12130:    fedtaxp:=0.31;
            12130..26375:   fedtaxp:=0.396;
           end
          else
          case round(gross) div 10 of
            0..1200:         fedtaxp:=0.09;
            1200..4010:      fedtaxp:=0.15;
            4010..9690:      fedtaxp:=0.28;
            9690..14770:     fedtaxp:=0.31;
           end;
         emprek[count].fedperiod := gross * fedtaxp;
         writeln ('Fed. Tax: ',emprek[count].fedperiod:0:2);
         Net:= gross - (emprek[count].ssperiod + emprek[count].fedperiod);
         emprek[count].ss:=emprek[count].ss+emprek[count].ssperiod;
         emprek[count].fed:=emprek[count].fed+emprek[count].fedperiod;
         writeln ('Net earnings: ', net:0:2);
         writeln ('Soc. Sec to date: ', emprek[count].ss:0:2);
         writeln ('Fed. Tax to date: ', emprek[count].fed:0:2);
         writeln ('Vacation days left: ',
                    emprek[count].vdays-emprek[count].vused);
         writeln ('Vacation days used: ', emprek[count].vused);
         writeln ('Sick days: ', emprek[count].sdays);
         writeln;
       end;
      end;
  'B': begin
        setrpath('C:\CUREMP.TXT');
        readfile(emprek,numofemp,rpath);
        selsort(emprek,numofemp);
        letterhead;
        for count:=1 to numuvemp do
        begin
         {assign (P,'PRN');}
         write ('Employee Name: ');
         write (emprek[count].name);
         write ('Employee Number: ':44-length(emprek[count].name));
         write (count);
         writeln;
         write ('Address: ');
         write (emprek[count].address.street);
         write ('Apartment #: ':46-length(emprek[count].address.street));
         write (emprek[count].address.apt);
         writeln;
         write ('City: ');
         write (emprek[count].address.city);
         write ('State: ':24-length(emprek[count].address.city));
         write (emprek[count].address.state);
         write ('Zip Code: ':22-length(emprek[count].address.state));
         writeln (emprek[count].address.zip);
         write ('Telephone #: ');
         for phone := 1 to 11 do
         begin
          if phone = 1 then
          write ('(');
          write (emprek[count].address.telephone[phone]);
          if phone = 3 then write (')');
         end;
         write ('Social Security #: ':46-length(emprek[count].address.
         telephone));
         writeln (emprek[count].ssnum);
         write ('Birthday: ');
         str (emprek[count].dob.month,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].dob.day,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].dob.year,scount);
         write (scount);
         write ('Sex: ':29);
         writeln(upcase(emprek[count].sex));
         write ('Marital Status: ');
         write (upcase(emprek[count].mstatus));
         write ('Number of Dependents: ':47);
         str (emprek[count].numdep,scount);
         writeln (scount);
         write ('Vacation days: ');
         str (emprek[count].vdays, scount);
         write (scount);
         write ('Vacation used: ':42-length(scount));
         str (emprek[count].vused, scount);
         writeln (scount);
         write ('Sick days taken: ');
         str (emprek[count].sdays, scount);
         write (scount);
         write ('Hourly or Salaried: ':45-length(scount));
         if upcase(emprek[count].paytype) = 'H' then writeln ('Hourly')
         else
         if upcase(emprek[count].paytype) = 'S' then writeln ('Salaried')
         else
         writeln ('Hired Assassin');
         if upcase(emprek[count].paytype) = 'S' then write ('Salary / year: ')
         else
         if upcase(emprek[count].paytype) = 'H' then write ('Wage per hour: ')
         else
         write ('Price per hit: ');
         write (emprek[count].pay:0:2);
         write ('Hours worked: ':38-leftdigit(emprek[count].pay));
         str(emprek[count].hours, scount);
         writeln (scount);
         write ('Total Earnings: ');
         write (emprek[count].totalearnings:0:2);
         write ('Soc. Sec. this period: ':46-
          leftdigit(emprek[count].totalearnings));
         writeln (emprek[count].ssperiod:0:2);
         write ('Fed. Tax this period: ');
         write (emprek[count].fedperiod:0:2);
         write ('Soc. Sec. to date: ':36-leftdigit(emprek[count].fedperiod));
         writeln (emprek[count].ss:0:2);
         write ('Fed. Tax to date: ');
         write (emprek[count].fed:0:2);
         write ('Medical Plan: ':35-leftdigit(emprek[count].fed));
         if upcase(emprek[count].medplan) = 'H' then
         writeln('HMO')
         else
         if upcase(emprek[count].medplan) = 'R' then
         writeln('Regular')
         else
         writeln('Organ Donor');
         write ('Date of Employment: ');
         str (emprek[count].doe.month,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].doe.day,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].doe.year,scount);
         write (scount);
         writeln;
         writeln;

              {assigncrt (p);
              Rewrite(P);}
         end;
       end;
  'C': begin
        setrpath('C:\FORMEMP.TXT');
        readfile(emprek,formempnum,rpath);
        selsort(emprek,formempnum);
        letterhead;
        for count:=1 to formempnum do
        begin
         {assign (P,'PRN');}
         write ('Employee Name: ');
         write (emprek[count].name);
         write ('Employee Number: ':44-length(emprek[count].name));
         write (count);
         writeln;
         write ('Address: ');
         write (emprek[count].address.street);
         write ('Apartment #: ':46-length(emprek[count].address.street));
         write (emprek[count].address.apt);
         writeln;
         write ('City: ');
         write (emprek[count].address.city);
         write ('State: ':24-length(emprek[count].address.city));
         write (emprek[count].address.state);
         write ('Zip Code: ':22-length(emprek[count].address.state));
         writeln (emprek[count].address.zip);
         write ('Telephone #: ');
         for phone := 1 to 11 do
         begin
          if phone = 1 then
          write ('(');
          write (emprek[count].address.telephone[phone]);
          if phone = 3 then write (')');
         end;
         write ('Social Security #: ':46-length(emprek[count].address.
         telephone));
         writeln (emprek[count].ssnum);
         write ('Birthday: ');
         str (emprek[count].dob.month,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].dob.day,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].dob.year,scount);
         write (scount);
         write ('Sex: ':29);
         writeln(upcase(emprek[count].sex));
         write ('Marital Status: ');
         write (upcase(emprek[count].mstatus));
         write ('Number of Dependents: ':47);
         str (emprek[count].numdep,scount);
         writeln (scount);
         write ('Vacation days: ');
         str (emprek[count].vdays, scount);
         write (scount);
         write ('Vacation used: ':42-length(scount));
         str (emprek[count].vused, scount);
         writeln (scount);
         write ('Sick days taken: ');
         str (emprek[count].sdays, scount);
         write (scount);
         write ('Hourly or Salaried: ':45-length(scount));
         if upcase(emprek[count].paytype) = 'H' then writeln ('Hourly')
         else
         if upcase(emprek[count].paytype) = 'S' then writeln ('Salaried')
         else
         writeln ('Hired Assassin');
         if upcase(emprek[count].paytype) = 'S' then write ('Salary / year: ')
         else
         if upcase(emprek[count].paytype) = 'H' then write ('Wage per hour: ')
         else
         write ('Price per hit: ');
         write (emprek[count].pay:0:2);
         write ('Hours worked: ':38-leftdigit(emprek[count].pay));
         str(emprek[count].hours, scount);
         writeln (scount);
         write ('Total Earnings: ');
         write (emprek[count].totalearnings:0:2);
         write ('Soc. Sec. this period: ':46-
          leftdigit(emprek[count].totalearnings));
         writeln (emprek[count].ssperiod:0:2);
         write ('Fed. Tax this period: ');
         write (emprek[count].fedperiod:0:2);
         write ('Soc. Sec. to date: ':36-leftdigit(emprek[count].fedperiod));
         writeln (emprek[count].ss:0:2);
         write ('Fed. Tax to date: ');
         write (emprek[count].fed:0:2);
         write ('Medical Plan: ':35-leftdigit(emprek[count].fed));
         if upcase(emprek[count].medplan) = 'H' then
         writeln('HMO')
         else
         if upcase(emprek[count].medplan) = 'R' then
         writeln('Regular')
         else
         writeln('Organ Donor');
         write ('Date of Employment: ');
         str (emprek[count].doe.month,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].doe.day,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].doe.year,scount);
         write (scount);
         writeln;
         writeln;

              {assigncrt (p);
              Rewrite(P);}
         end;
       end;
  'D': begin
        target:=0;
        setrpath('C:\CUREMP.TXT');
        readfile(emprek,numofemp,rpath);
        write ('Enter name of employee: ');
        readln (searchstr);
        lsearch(emprek, searchstr, numofemp, target);
        if target= 0 then writeln ('No one found');
        if target > 0 then
        begin
         count:=target;
         letterhead;
         {assign (P,'PRN');}
         write ('Employee Name: ');
         write (emprek[count].name);
         write ('Employee Number: ':44-length(emprek[count].name));
         write (count);
         writeln;
         write ('Address: ');
         write (emprek[count].address.street);
         write ('Apartment #: ':46-length(emprek[count].address.street));
         write (emprek[count].address.apt);
         writeln;
         write ('City: ');
         write (emprek[count].address.city);
         write ('State: ':24-length(emprek[count].address.city));
         write (emprek[count].address.state);
         write ('Zip Code: ':22-length(emprek[count].address.state));
         writeln (emprek[count].address.zip);
         write ('Telephone #: ');
         for phone := 1 to 11 do
         begin
          if phone = 1 then
          write ('(');
          write (emprek[count].address.telephone[phone]);
          if phone = 3 then write (')');
         end;
         write ('Social Security #: ':46-length(emprek[count].address.
         telephone));
         writeln (emprek[count].ssnum);
         write ('Birthday: ');
         str (emprek[count].dob.month,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].dob.day,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].dob.year,scount);
         write (scount);
         write ('Sex: ':29);
         writeln(upcase(emprek[count].sex));
         write ('Marital Status: ');
         write (upcase(emprek[count].mstatus));
         write ('Number of Dependents: ':47);
         str (emprek[count].numdep,scount);
         writeln (scount);
         write ('Vacation days: ');
         str (emprek[count].vdays, scount);
         write (scount);
         write ('Vacation used: ':42-length(scount));
         str (emprek[count].vused, scount);
         writeln (scount);
         write ('Sick days taken: ');
         str (emprek[count].sdays, scount);
         write (scount);
         write ('Hourly or Salaried: ':45-length(scount));
         if upcase(emprek[count].paytype) = 'H' then writeln ('Hourly')
         else
         if upcase(emprek[count].paytype) = 'S' then writeln ('Salaried')
         else
         writeln ('Hired Assassin');
         if upcase(emprek[count].paytype) = 'S' then write ('Salary / year: ')
         else
         if upcase(emprek[count].paytype) = 'H' then write ('Wage per hour: ')
         else
         write ('Price per hit: ');
         write (emprek[count].pay:0:2);
         write ('Hours worked: ':38-leftdigit(emprek[count].pay));
         str(emprek[count].hours, scount);
         writeln (scount);
         write ('Total Earnings: ');
         write (emprek[count].totalearnings:0:2);
         write ('Soc. Sec. this period: ':46-
          leftdigit(emprek[count].totalearnings));
         writeln (emprek[count].ssperiod:0:2);
         write ('Fed. Tax this period: ');
         write (emprek[count].fedperiod:0:2);
         write ('Soc. Sec. to date: ':36-leftdigit(emprek[count].fedperiod));
         writeln (emprek[count].ss:0:2);
         write ('Fed. Tax to date: ');
         write (emprek[count].fed:0:2);
         write ('Medical Plan: ':35-leftdigit(emprek[count].fed));
         if upcase(emprek[count].medplan) = 'H' then
         writeln('HMO')
         else
         if upcase(emprek[count].medplan) = 'R' then
         writeln('Regular')
         else
         writeln('Organ Donor');
         write ('Date of Employment: ');
         str (emprek[count].doe.month,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].doe.day,scount);
         if length (scount) = 1 then write ('0');
         write (scount);
         write ('/');
         str (emprek[count].doe.year,scount);
         write (scount);
         writeln;
         writeln;
               {assigncrt (p);
              Rewrite(P);}
         end;
        end;
      end;

  end;


procedure interpret (commstring: str120);
var
result: integer;
commchar2: char;
 begin
  textattr := 7;
  if commstring = 'QUIT' then
  begin
   writeln ('Logged Out.');
   result := -1;
  end
  else
   if (pos('?',commstring)) > 0 then result := 1
   else
   if pos('HELP',commstring) > 0 then result := 1
   else
   if pos('EDIT',commstring) > 0 then result := 2
   else
   if pos('LOAD CURRENT',commstring) > 0 then result := 3
   else
   if pos('LOAD FORMER',commstring) > 0 then result := 4
   else
   if pos('LOAD BASIC',commstring) > 0 then result := 5
   else
   if pos('NOTE',commstring) > 0 then result := 6
   else
   if pos('PRINT',commstring) > 0 then result := 7
   else
   if pos('NATHNOTE',commstring > 0 then result := 8
   else
   result:=0;
    if result = 1 then
     begin
      textattr := 7;
      writeln ('Valid Commands are:');
      writeln;
      writeln ('EDIT');
      writeln ('HELP');
      writeln ('LOAD CURRENT');
      writeln ('LOAD FORMER');
      writeln ('LOAD BASIC');
      writeln ('NATHNOTE');
      writeln ('NOTE');
      writeln ('PRINT');
      writeln ('QUIT');
      writeln;
    end
   else
    if result = 8 then
     begin
      textattr := 7;
      writeln('Hello Nathy Boy!');
      writeln('This program is silly');
      writeln('Type Load Current to load C:\CUREMP.TXT');
      writeln('Type Load Former to load C:\FORMEMP.TXT');
      writeln('Type load basic to load the default settings');
      writeln('When you load basic, type edit, then hit S to save');
      writeln('the file to C:\curemp.txt');
      writeln;
      writeln('Have fun');
     end
   else
    if result = 2 then edit(emprek,tcount,numofemp, formempnum)
  else
    if result = 3 then
     begin
      setrpath('C:\CUREMP.TXT');
      readfile(emprek,numofemp,rpath)
     end
  else
    if result = 4 then
     begin
      setrpath('C:\FORMEMP.TXT');
      readfile(emprek,formempnum,rpath);
     end
  else
    if result = 5 then
     begin
      setrpath('C:\PASCAL\EMPREC.97'); {or you can change this to whatever}
      readfile(emprek,numofemp,rpath);
     end
  else
    if result= 6 then
     begin
      repeat
       clrscr;
       gotoxy(20,20);
       writeln ('This program was written by [redacted].');
       gotoxy(25,21);
       writeln ('"It''s a great big disco world!"');
       gotoxy(5,5);
       writeln (' o/ ');
       gotoxy(5,6);
       writeln ('(  ');
       gotoxy(5,7);
       writeln ('/\  ');
       delay(500);
       gotoxy(5,5);
       writeln (' o  ');
       gotoxy(5,6);
       writeln ('(/  ');
       gotoxy(5,7);
       writeln ('/\  ');
       delay(500);
      until keypressed;
      clrscr;
     end
  else
  if result = 7 then
   begin
    Writeln ('Available Printable Reports:');
    writeln;
    writeln ('A. Payroll');
    Writeln ('B. Alphabetical listing of Current Employees');
    writeln ('C. Alphabetical listing of Former Employees');
    writeln ('D. Single Employee Record');
    commchar2 := upcase(readkey);
    print (emprek, numofemp, formempnum, commchar2);
   end
  else
  if result = 0 then
   begin
    write ('Unknown Command. Valid commands are listed with ''?''');
    writeln(' or ''help''');
  end;
 end;

procedure menu(var commstring: str120);

 var
  pflag, exit1: boolean;


  begin
    pflag := false;
    exit1 := false;
    writeln;
    writeln('Welcome to Knave-OS v1.02');
    repeat
    textcolor(3);
    write('-=ð> ');
    commstring:=commstring;
    readln(commstring);
    commstring:=upscase(commstring);
    interpret(commstring);
    until (commstring) = 'QUIT';
    repeat until keypressed;
  end;

Procedure access;


var
 password: str7;
 count: integer;
 commchar: char;
 cnt: integer;
 pnum: integer;
 vaccess: boolean;

 begin
  writeln ('Attention. This is a private system. Access is restricted.');
  writeln ('Attempting to use this system without authorization is a');
  writeln ('violation of the Computer Fraud and and Decency act of 1986.');
  writeln ('Distribution of passwords is strictly prohibited by Texas');
  writeln ('law. Unauthorized attempts to use this system are logged and');
  writeln ('WILL be reported.');
  writeln;
  writeln ('Knave-OS version 1.02');
  vaccess:=false;
  acksess:=false;
  for count := 1 to 7 do
  password[count] := ' ';
  cnt:=0;
  repeat {outer loop}
   commchar := ' ';
   pnum:=1;
   cnt:=cnt+1;
   textcolor(7);
   write ('-=ðLogin: ');
   repeat {read in loop}
     if keypressed then
       begin
         commchar := readkey;
         if (commchar = chr(8)) and (pnum > 1) then
       begin {erase the previous letter}
         write (chr(8), ' ', chr(8));
         pnum := pnum - 1;
         password[pnum] := ' ';
       end;
      if (commchar <> chr(8)) and (commchar <> chr(13)) then
        begin
          textcolor(random(15)+1);
          write ('*');
          password[pnum] := commchar;
          pnum := pnum + 1;
        end;
      end;
   until commchar = chr(13); {read in loop}
   writeln;
   password := password;
   if password = 'agg3hon' then
    begin
     vaccess := true;
     acksess := true;
    end;
   until (cnt = 2) or (vAccess = true); {outer loop}
   if not(vaccess) then
   begin
    writeln;
    write('              ');
    slowprint ('Connection closed',4,300,'y');
    repeat
    gotoxy (16,7);
    textcolor(4);
    writeln ('ACCESS DENIED');
    delay (400);
    gotoxy (12,7);
    writeln ('                      ');
    delay (400);
    until keypressed;
    textattr := 7;
    halt;
    end;
   if vaccess = true then
   begin
     textcolor(3);
     write ('Login ');
     delay(1000);
     slowprint ('Confirmation',3,100,'y');
   end;
 end;

procedure filecheck(var s:charay);

begin
 FileSearch(S, 'C:\CUREMP.TXT', GetEnvVar('PATH'));
 if S[0] = #0 then
 begin
  assign (ofile, 'C:\CUREMP.TXT');
  rewrite(ofile);
  writeln (ofile, '0');
  close(ofile);
 end;
 FileSearch(S, 'C:\FORMEMP.TXT', GetEnvVar('PATH'));
 if S[0] = #0 then
 begin
  assign (ofile, 'C:\FORMEMP.TXT');
  rewrite(ofile);
  writeln (ofile, '0');
  close(ofile);
 end;
end;

begin
 clrscr;
 randomize;
 access;
 filecheck(sarray);
 if acksess = true then
  begin
   rpath:='C:\CUREMP.TXT';
   readfile(emprek, numofemp, rpath);
   menu(comstring);
  end;
end.