     {$I EQUATES.PAS}
     {$I ALLCAPS.PAS}
     {$I EXIST.PAS}
     {$I CURSOR.PAS}
     {$I VIDEO.PAS}
     {$I STATUS.PAS}
     {$I MENU.PAS}
     {$I IOCHECK.PAS}
     {$I GETINSTR.PAS}
     {$I GETYESNO.PAS}
     {$I ERASE.PAS}
     {$C-}
     label Start;
     const
          Changed  : Boolean = False;
          DiskFull : Boolean = False;
          Init     : Boolean = FALSE;
          MaxLines = 11;
          Lines    : Array[0..MaxLines] of String[40] =
                     ('* What Do You Want To Search By *',
                      ' First Name',
                      ' Last Name',
                      ' Address',
                      ' City',
                      ' State',
                      ' Zip Code',
                      ' Phone Number',
                      ' Business Phone',
                      ' Occupation',
                      ' Enter New Member',
                      ' Quit: Go to CP/M ');
     type
          Entry = record
               First_name     : String[20];
               Last_Name      : String[20];
               Street_Address : String[25];
               City           : String[15];
               State          : String[2];
               Zip_Code       : String[10];
               Home_Phone     : String[17];
               Business_Phone : String[17];
               Occupation     : String[25];
          end;
     var
          Reply                     : Char;
          Choice                    : Integer;
          DataFile                  : File of Entry;
          Individual                : Entry;
          Search_For                : String[30];

     {$I MODEM.LIB}
     {$I DIAL.PAS}

     procedure Print_Name;
          var
               Result  : Byte;
          begin
               With Individual Do
               begin
                    Result := BIOS(14);
                    If Result = 0 then
                    begin
                         Write_Status('** Printer {NOT} Ready **');
                         Delay(1000);
                         Exit
                    end;
                    writeln(Lst);
                    writeln(Lst);
                    writeln(Lst,First_Name,' ',Last_Name);
                    writeln(Lst,Street_Address);
                    writeln(Lst,City,', ',State,'   ',Zip_Code);
                    writeln(Lst);
                    writeln(Lst,'Business Phone : ',Business_Phone);
                    writeln(Lst,'    Home Phone : ',Home_Phone);
                    write
               end
          end;

     procedure Enter_Names;
          var
               Answer     : Char;
          begin
               Draw_Status_Border;
               BDOS(13);
               assign(DataFile,'PHONE.DTA');
               if not Exist('PHONE.DTA') then Rewrite(DataFile);
               Reset(DataFile);
               {$I-} seek(DataFile,FileSize(DataFile)) {$I+};
               DiskFull := (IOresult = $F0);
               if DiskFull then
               begin
                    Write_Status('{+++ DISK FULL. Exit and ERASE Unnecessary Files +++}');
                    Close(Datafile);
                    Exit
               end;
               ClrScr;
               Draw_Status_Border;
               with Individual do
               begin
                    Write_Status('Enter a {Carriage Return} to Leave Blank');
                    Get_InStr(1,1,'Enter First Name            : ',20);
                    First_Name := InStr;
                    Get_InStr(1,2,'Enter Last Name             : ',20);
                    Last_Name := InStr;
                    Get_InStr(1,3,'Enter Street Address        : ',25);
                    Street_Address := InStr;
                    Get_InStr(1,4,'Enter City                  : ',15);
                    City := InStr;
                    Get_InStr(1,5,'Enter Two Letter State Code : ',2);
                    State := AllCaps(InStr);
                    Get_InStr(1,6,'Enter Zip Code              : ',10);
                    Zip_Code := InStr;
                    GotoXY(1,18);
                    writeln(BEL,'Enter Phone Numbers EXACTLY As You Would Dial Them.');
                    writeln('Use Punctuation To Make Them More Readable If Desired.');
                    write('Examples: 15551212  1-555-1212  1(615)555-1212  1/615/555-1212');
                    Get_InStr(1,7,'Enter Home Phone Number     : ',17);
                    Home_Phone := InStr;
                    Get_InStr(1,8,'Enter Business Phone Number : ',17);
                    Business_phone := InStr;
                    EraseXY(18,20);
                    Get_InStr(1,9,'Enter Occupation            : ',25);
                    Occupation := InStr;
                    Write_Status('Enter ''{Y}'' or ''{N}''');
                    Get_YesNo(1,13,'Are ALL Entries Correct (Y/N) :');
                    If YesNo in ['Y','y'] then
                    begin
                         write(DataFile,Individual);
                         Flush(DataFile);
                         Close(DataFile);
                    end
                    else
                         Exit
               end
          end;

     procedure Change_Names;
          begin
               with Individual do
               begin
                    Write_Status('Enter {NEW} Last Name  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',20);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              Last_Name := ''
                         else
                              Last_Name := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} First Name  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',20);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              First_Name := ''
                         else
                              First_Name := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} Street Address  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',25);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              Street_Address := ''
                         else
                              Street_Address := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} City  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',15);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              City := ''
                         else
                              City := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} State Code  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',2);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              State := ''
                         else
                              State := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} Zip Code  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',10);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              Zip_Code := ''
                         else
                              Zip_Code := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} Home Phone  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',17);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              Home_Phone := ''
                         else
                              Home_Phone := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} Business Phone  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',17);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              Business_Phone := ''
                         else
                              Business_Phone := InStr;
                         Changed := True
                    end;
                    Write_Status('Enter {NEW} Occupation  -  {<cr>} = No Change  {<ESC>} = Blank');
                    Get_InStr(12,18,'CHANGE TO : ',25);
                    GotoXY(12,18); ClrEol;
                    if InStr <> '' then
                    begin
                         if InStr = ESC then
                              Occupation := ''
                         else
                              Occupation := InStr;
                         Changed := True
                    end
               end
          end;

     procedure Search_Names (Search_Type : Str80; Len : Integer);
          label Rewrite;
          var
               I,J            : Integer;
               Ch             : Char;
               Search_Key     : String[25];
               Temp           : Integer;
          begin
               ClrScr;
               Get_InStr(4,12,'Enter '+Search_Type+' : ',Len);
               Search_Key := InStr;
               BDOS(13);
               assign(DataFile,'PHONE.DTA');
               if not Exist('PHONE.DTA') then
               begin
                    Draw_Status_Border;
                    Write_Status('{**   CANNOT FIND DATA FILE   **}');
                    Close(Datafile);
                    halt;
               end;
               reset(DataFile);
               with Individual do
               begin
                    I := 0; ClrScr; LowVideo;
                    GotoXY(12,7);
                    write('FULL NAME :');
                    GotoXY(7,8);
                    write('STREET ADDRESS :');
                    GotoXY(4,9);
                    write('CITY, STATE & ZIP :');
                    GotoXY(6,11);
                    write('AREA/HOME PHONE :');
                    GotoXY(2,12);
                    write('AREA/BUSINESS PHONE :');
                    GotoXY(11,14);
                    write('OCCUPATION :');
                    NormVideo;
                    Draw_Status_Border;
                    Write_Status('Searching ...');
                    while not EOF(DataFile) do
                    begin
                         seek(DataFile,I); read(DataFile,Individual);
                         case Choice of
                              1 : Search_For := First_Name;
                              2 : Search_For := Last_Name;
                              3 : Search_For := Street_Address;
                              4 : Search_For := City;
                              5 : Search_For := State;
                              6 : Search_For := Zip_Code;
                              7 : Search_For := Home_Phone;
                              8 : Search_For := Business_Phone;
                              9 : Search_For := Occupation;
                         end;
                         if AllCaps(Copy(Search_For,1,Length(Search_Key)))
                              = AllCaps(Search_Key) then
                         begin
Rewrite:                      GotoXY(1,18); ClrEol;
                              GotoXY(25,7); ClrEol;
                              write(Last_Name + ', ' + First_Name);
                              GotoXY(25,8); ClrEol;
                              write(Street_Address);
                              GotoXY(25,9); ClrEol;
                              write(City + ', ' + State + '  ' + Zip_Code);
                              GotoXY(25,11); ClrEol;
                              write(Home_Phone);
                              GotoXY(25,12); ClrEol;
                              write(Business_Phone);
                              GotoXY(25,14); ClrEol;
                              write(Occupation);
                              Write_Status('{C}hange  {D}ial  {P}rint  {Q}uit  {ANY KEY} to CONTINUE');
                              read(Kbd,Ch);
                              case Ch of
                                   'D','d' : begin
                                                  Dial_Number;
                                                  Goto Rewrite
                                             end;
                                   'P','p' : begin
                                                  Print_Name;
                                                  Goto Rewrite
                                             end;
                                   'C','c' : begin
                                                  Change_Names;
                                                  if Changed then
                                                  begin
                                                       Temp := I;
                                                       Write_Status('Updating ...');
                                                       Seek(DataFile,I);
                                                       Write(DataFile,Individual);
                                                       Flush(Datafile);
                                                       Close(Datafile);
                                                       Assign(Datafile,'PHONE.DTA');
                                                       Reset(Datafile);
                                                       Seek(Datafile,Temp);
                                                       Read(Datafile,Individual);
                                                       I := Temp;
                                                       Changed := False;
                                                  end;
                                                  Goto Rewrite;
                                             end;
                                   'Q','q' : Exit
                              end
                         end;
                         I := I + 1
                    end;
                    Close(DataFile);
               end;
          end;

{ ************************************************************************
  *                                                                      *
  *                        MAIN PROGRAM                                  *
  *                                                                      *
  ************************************************************************ }

     begin
Start:  Choice := Menu(MaxLines,Lines);
           Case Choice of
               1 : Search_Names ('First Name',20);
               2 : Search_Names ('Last Name',20);
               3 : Search_Names ('Street Address',25);
               4 : Search_Names ('City',15);
               5 : Search_Names ('Two Letter State Code',2);
               6 : Search_Names ('Zip Code',10);
               7 : Search_Names ('Home Phone Number',17);
               8 : Search_Names ('Business Phone Number',17);
               9 : Search_Names ('Occupation',25);
              10 : Enter_Names;
              11 : begin
                          ClrScr;
                          Cursor_On;
                          NormVideo;
                          writeln('PHONE ver. 1.5');
                          writeln('Program by:');
                          writeln('Joseph L. Fall');
                          writeln('CIS 76555,37');
                          writeln;
                          Halt
                    end
             end;
         goto Start;
     end.