Program TalkToMe;



Uses

  Crt,

  CommSoft;



Const {Global}

  Forever: boolean = false;

  MaxAddressNumber = 10;



Var  {Global}

  DumChar: string;

  DumInt: integer;

  UserName:  string[16];

  AddressList:  array[1..MaxAddressNumber] of string[16];

  LastAddressNumber: integer;

  AddressSet:  array[1..MaxAddressNumber] of boolean;

  IncomingMessage: string;

  InX, InY: integer;



Procedure SwitchWindow(WindowNumber: integer);

{***************************************************************************}

begin

  case WindowNumber of

    1:  begin

          Window(1,1,80,19);

          TextBackground(Green);

          TextColor(Yellow);

        end;

    2:  begin

          Window(1,20,80,22);

          TextBackground(Red);

          TextColor(Black);

          clrscr;

        end;

    3:  begin

          Window(1,23,80,25);

          TextBackground(Magenta);

          TextColor(Blue);

          clrscr;

        end;

  end; {case}

end;



Procedure GetUserName;

{***************************************************************************}

begin

  SwitchWindow(2);

  writeln('What is the name that you will be using?:  ');

  SwitchWindow(3);

  readln(UserName); {a global variable}

  clrscr;

  SwitchWindow(2);

end;



Procedure AddAddress;

{***************************************************************************}

begin

  SwitchWindow(2);

  if LastAddressNumber = MaxAddressNumber then

    begin

      write(#7);

      writeln('Too many addresses in the list to add another one.');

      writeln('Hit ENTER to continue');

      dumchar := readkey;

    end

  else

    begin

      Inc(LastAddressNumber);

      writeln('Adding an address -- what is the name of the new address?:');

      SwitchWindow(3);

      readln(AddressList[LastAddressNumber]);

      clrscr;

    end;

  SwitchWindow(2);

end;



Procedure SetDestinationAddress(Address: char);

{***************************************************************************}

{*  Called by SendMessage.  HighLights destination addresses.

{***************************************************************************}

var

  alpha, bravo: integer;

begin

  SwitchWindow(2);

  if address = '0' then

    for alpha := 1 to MaxAddressNumber do

      AddressSet[alpha] := false

  else

    begin

      val(address, alpha, dumint);

      if AddressSet[alpha] = false then

        AddressSet[alpha] := true

      else

        AddressSet[alpha] := false;

    end;

  for alpha := 1 to LastAddressNumber do

    begin

      if AddressSet[alpha] then

        HighVideo

      else

        LowVideo;

      write(alpha,') ',AddressList[alpha],' ');

      for bravo := 1 to 16 - length(AddressList[alpha]) do

        write(' ');

    end;

  LowVideo;

  writeln;

  write('Enter the number for each address you want to send to (0 to clear');

    write(' all).');

end;



Procedure DisplayOptions;

{***************************************************************************}

begin

  SwitchWindow(2);

  write('1) Quit the program 2) Add another address name 3) Change your');

    writeln(' address name');

  write('4) Send a message out [Default option, just hit ENTER]');

    writeln(' 5) Send direct');

  write('Option:  ');

end;



Procedure SendMessage;

{***************************************************************************}

var

  alpha: integer;

  Address: char;

  StrLastAddressNumber: string[1];

  Message: string;

begin

  Address := '0';

  SetDestinationAddress(address);

  str(LastAddressNumber, StrLastAddressNumber);

  repeat

    SwitchWindow(3);

    Address := readkey;

    if not(Address in ['0'..StrLastAddressNumber[1]]) and not(address = #13)

      then

        write(#7) {beep}

    else

      if not(address = #13) then

        begin

          SetDestinationAddress(Address);

          SwitchWindow(3);

        end;

  until Address = #13;

  SwitchWindow(2);

  writeln('Write your message and then hit ENTER to send.');

  SwitchWindow(3);

  readln(Message);

  for alpha := 1 to LastAddressNumber do

    if AddressSet[alpha] then

      OutComm(UserName, AddressList[alpha], Message);

  DisplayOptions;

end;



Procedure SendDirect;

{***************************************************************************}

var

  Message: string;

begin

  SwitchWindow(2);

  writeln('Write your message and then hit ENTER to send.');

  SwitchWindow(3);

  readln(Message);

  OutComm(Nul, Nul, Message);

  DisplayOptions;

end;



Procedure DoOptions;

{***************************************************************************}

var

  Option: char;

begin

  DisplayOptions;

  repeat

    SwitchWindow(3);

    Option := readkey;

    if Option = #13 then

      Option := '4';

    if not(Option in ['1'..'5']) then

      write(#7); {Beep}

  until Option in ['1'..'5'];

  clrscr;

  SwitchWindow(2);

  Case Option of

    '1':  begin

            ClosePortComm;

            halt;

          end;

    '2':  AddAddress;

    '3':  GetUserName;

    '4':  SendMessage;

    '5':  SendDirect;

  end; {case}

end;



Procedure DisplayIncoming;

{***************************************************************************}

begin

  SwitchWindow(1);

  GotoXY(InX, InY);

  write(IncomingMessage);

  InX := WhereX;

  InY := WhereY;

end;



{***************************************************************************}

begin

  IntroComm; {from CommSoft}

  SwitchWindow(1);

  clrscr;

  SwitchWindow(2);

  SwitchWindow(3);

  GetUserName;

  LastAddressNumber := 0;

  AddAddress;

  DisplayOptions;

  repeat

    InComm(IncomingMessage);

    if length(IncomingMessage) > 0 then

      DisplayIncoming;

    if Keypressed then

      DoOptions;

    SwitchWindow(3);

  until Forever;

end.