Program Advisor; (*Timothy W. Lethlean's Not-So-Inferior Version, 9/14/91*)

Uses CRT,Dos,Win,Printer;

{$M 32768,1,655360}  { Some stupid memory thing }
{$I PGMVar-2.Pas}    { Types/Variables          }
{$I PGMCmd-2.Pas}    { Program Base Commands    }
{$I ADVForms.Pas}    { Modified Forms/Sliders   }

Procedure LoadStudents;

Var
   S:StudentRec;
   X:Integer;

Begin
     Reset (StudentFile);
     X:=0;
     While (Eof(StudentFile)=False) do Begin
           Read(StudentFile,S);
           If S.LName<>'Deleted' then InsertStudent(S,X);
           X:=X+1;
     End;
End;

Procedure EditStudent(Cur:Integer; Var C:Boolean);

Var
   Ptr:StudentPtr;
   Sex,S220,Dis:String;
   S:StudentRec;
   X,Code:Integer;

Begin
     Cls;
     Escape ('ESC - Main Menu   F1 - Save Changes');
     Ptr:=SFirst;
     If Cur<>1 then Begin
        For X:=1 to Cur-1 do Ptr:=Ptr^.Next;
     End;
     Reset (StudentFile);
     Seek (StudentFile,Ptr^.FileLoc);
     Read (StudentFile,S);
        Locate (7,29);
        Write (' Last Name = ',S.LName);
        Locate (8,29);
        Write ('First Name = ',S.FName);
        Locate (9,29);
        Write ('     Class = ',S.Grade:2,'.',S.Class:2);
        Code:=1;
        C:=True;
        Repeat
              Case Code Of
                   0: Code:=4;
                   5: Code:=1;
              End;
              Case Code Of
                   1: SForm(S.LName,7,42,15,Code);
                   2: SForm(S.FName,8,42,15,Code);
                   3: IForm(S.Grade,9,42,12,Code);
                   4: IForm(S.Class,9,45,12,Code);
                  22: C:=True;
              End;
        Until ((Code=20) or (Code=22));
        If Code=20 then C:=False;
        If C=False then Begin
           If S.Sex=True then Sex:='M' else Sex:='F';
           If S.S220=True then S220:='Y' else S220:='N';
           If S.Dis=True then Dis:='Y' else Dis:='N';
           Locate (11,28);
           Write ('        Sex = ',Sex);
           Locate (12,28);
           Write ('220 Student = ',S220);
           Locate (13,28);
           Write (' Disability = ',Dis);
           Code:=1;
           C:=True;
           Repeat
                Case Code Of
                     4: Code:=1;
                     0: Code:=3;
                End;
                Case Code Of
                     1: Repeat
                              Code:=1;
                              SForm(Sex,11,42,1,Code);
                        Until ((Sex='M') or (Sex='m') or (Sex='F') or (Sex='f') or (Code=22));
                     2: Repeat
                              Code:=2;
                              SForm(S220,12,42,1,Code);
                        Until ((S220='N') or (S220='n') or (S220='Y') or (S220='y') or (Code=22));
                     3: Repeat
                              Code:=3;
                              SForm(Dis,13,42,1,Code);
                        Until ((Dis='N') or (Dis='n') or (Dis='Y') or (Dis='y') or (Code=22));
                    22: C:=True;
                End;
           Until ((Code=20) or (Code=22));
           If Code=20 then C:=False;
           If C=False then Begin
              If ((Sex='M') or (Sex='m')) then S.Sex:=True else S.Sex:=False;
              If ((S220='Y') or (Sex='y')) then S.S220:=True else S.S220:=False;
              If ((Dis='Y') or (Sex='y')) then S.Dis:=True else S.Dis:=False;
              Reset (StudentFile);
              Seek (StudentFile,Ptr^.FileLoc);
              Write (StudentFile,S);
              Ptr^.LName:=S.LName;
              Ptr^.FName:=S.FName;
              Ptr^.Grade:=S.Grade;
              Ptr^.Class:=S.Class;
              Escape ('ESC - Main Menu');
           End;
        End;
End;

Procedure DeleteStudent (Cur:Integer);

Var
   Ptr:StudentPtr;
   X:Integer;
   S:StudentRec;

Begin
     Ptr:=SFirst;
     If Cur<>1 then Begin
        For X:=1 to Cur-1 do Ptr:=Ptr^.Next;
     End;
     Reset (StudentFile);
     Seek (StudentFile,Ptr^.FileLoc);
     Read (StudentFile,S);
     S.LName:='Deleted';
     Seek (StudentFile,Ptr^.FileLoc);
     Write (StudentFile,S);
     SFirst:=Nil;
     SLast:=Nil;
     LoadStudents;
End;

Procedure AddStudent (Var C:Boolean);

Var
   S:StudentRec;
   Code:Integer;
   Sex,S220,Dis:String;
   D:Boolean;
   R:Char;

Begin
     D:=False;
     Repeat
        Cls;
        Escape ('ESC - Main Menu   F1 - Save Entry');
        Locate (7,29);
        Write (' Last Name = ');
        Locate (8,29);
        Write ('First Name = ');
        Locate (9,29);
        Write ('     Class =  7. 1');
        S.LName:='';
        S.FName:='';
        S.Class:=1;
        S.Grade:=7;
        Code:=1;
        C:=True;
        Repeat
              Case Code Of
                   0: Code:=4;
                   5: Code:=1;
              End;
              Case Code Of
                   1: SForm(S.LName,7,42,15,Code);
                   2: SForm(S.FName,8,42,15,Code);
                   3: IForm(S.Grade,9,42,12,Code);
                   4: IForm(S.Class,9,45,12,Code);
                  22: C:=True;
              End;
        Until ((Code=20) or (Code=22));
        If Code=20 then C:=False;
        If C=False then Begin
           Sex:='M';
           S220:='N';
           Dis:='N';
           Locate (11,28);
           Write ('        Sex = M');
           Locate (12,28);
           Write ('220 Student = N');
           Locate (13,28);
           Write (' Disability = N');
           Code:=1;
           C:=True;
           Repeat
                Case Code Of
                     4: Code:=1;
                     0: Code:=3;
                End;
                Case Code Of
                     1: Repeat
                              Code:=1;
                              SForm(Sex,11,42,1,Code);
                        Until ((Sex='M') or (Sex='m') or (Sex='F') or (Sex='f') or (Code=22));
                     2: Repeat
                              Code:=2;
                              SForm(S220,12,42,1,Code);
                        Until ((S220='N') or (S220='n') or (S220='Y') or (S220='y') or (Code=22));
                     3: Repeat
                              Code:=3;
                              SForm(Dis,13,42,1,Code);
                        Until ((Dis='N') or (Dis='n') or (Dis='Y') or (Dis='y') or (Code=22));
                    22: C:=True;
                End;
           Until ((Code=20) or (Code=22));
           If Code=20 then C:=False;
           If C=False then Begin
              If ((Sex='M') or (Sex='m')) then S.Sex:=True else S.Sex:=False;
              If ((S220='Y') or (Sex='y')) then S.S220:=True else S.S220:=False;
              If ((Dis='Y') or (Sex='y')) then S.Dis:=True else S.Dis:=False;
              InsertStudent(S,FileSize(StudentFile));
              Reset (StudentFile);
              Seek (StudentFile,FileSize(StudentFile));
              Write (StudentFile,S);
              Escape ('ESC - Main Menu');
           End;
        End;
        If C=False then Begin
           Locate (15,28);
           Write ('Add another? (Y/N)');
           Locate (23,80);
           Hold;
           R:=ReadKey;
           If ((R='N') or (R='n')) then D:=True;
           If ((R='-') or (R=#27)) then C:=True;
        End;
        If C=True then D:=True;
     Until (D=True);
End;

Procedure StudentUtils2(Top,Cur,High:Integer);

Var
   Ptr:StudentPtr;
   X,High2,Low:Integer;

Begin
     Window (20,6,60,18);
     Locate (1,1);
     WriteLn (' LAST            FIRST            CLASS');
     Ptr:=SFirst;
     If Top<>1 then Begin
        For X:=1 to Top-1 do Ptr:=Ptr^.Next;
     End;
     If Top+11>High then High2:=High else High2:=Top+11;
     For X:=Top to High2 do Begin
         If Cur=X then Begin
            TextColor (0);
            TextBackground (15);
         End else Begin
             TextColor (7);
             TextBackground (0);
         End;
         Locate (X-Top+2,1);
         Write ('                                     ');
         Locate (X-Top+2,1);
         Write (' ',Ptr^.LName);
         Locate (X-Top+2,18);
         Write (Ptr^.FName);
         Locate (X-Top+2,36);
         Write (Ptr^.Grade,'.',Ptr^.Class,' ');
         Ptr:=Ptr^.Next;
     End;
     TextColor (7);
     TextBackground (0);
     Window (1,1,80,25);
     Locate (23,80);
End;

Procedure StudentUtils;

Var
   Ptr:StudentPtr;
   S:StudentRec;
   I,X,High,Cur,High2,Code,Top:Integer;
   Sex,S220,Dis:String;
   C:Boolean;
   R:Char;

Begin
     Cls;
     C:=False;
     WorkBox(' STUDENT UTILITIES ');
     Escape('ESC - Main Menu   F3 - Help Screen');
     If SFirst=Nil then Begin
        AddStudent(C);
     End;
     If C=False then Begin
     Cur:=1;
     Top:=1;
     Repeat
        Ptr:=SFirst;
        High:=0;
        While (Ptr<>Nil) do Begin
              High:=High+1;
              Ptr:=Ptr^.Next;
        End;
        If Cur<1 then Cur:=1;
        If Cur>High then Cur:=High;
        If Cur>Top+11 then Top:=Top+1;
        If Cur<Top then Top:=Cur;
        StudentUtils2(Top,Cur,High);
        Hold;
        R:=ReadKey;
        Case R Of
             #0: Begin
                 R:=ReadKey;
                 Case R Of
                      #59: Begin
                           AddStudent(C);
                           Cls;
                           End;

                      #60: Begin
                           DeleteStudent(Cur);
                           If High=1 then C:=True;
                           Cls;
                           End;

                      #61: Begin
                           Cls;
                           Locate (5,35);
                           Write ('HELP SCREEN');
                           Locate (7,15);
                           Write (#24,#25,'   Use up and down arrows to move scroll bar.');
                           Locate (9,15);
                           Write (#17,#217,'   Press this while the scroll bar is on the');
                           Locate (10,15);
                           Write ('     record you want to edit and you will get the');
                           Locate (11,15);
                           Write ('     edit student screen.');
                           Locate (13,15);
                           Write ('F1   This will give you the new entry screen.  It');
                           Locate (14,15);
                           Write ('     will add the student on the end of the list.');
                           Locate (16,15);
                           Write ('F2   This will delete the record that the scroll');
                           Locate (17,15);
                           Write ('     bar is currently on.');
                           Locate (23,80);
                           Hold;
                           R:=ReadKey;
                           Cls;
                           End;

                      #72: Cur:=Cur-1;
                      #80: Cur:=Cur+1;
                 End;
                 End;

            #13: Begin
                 EditStudent(Cur,C);
                 Cls;
                 End;

            '-',
            #27: C:=True;
        End;
     Until (C=True);
     End;
End;

Procedure LoadCounselors;

Var
   S:CounselorRec;
   X:Integer;

Begin
     Reset (CounselorFile);
     X:=0;
     While (Eof(CounselorFile)=False) do Begin
           Read(CounselorFile,S);
           If S.LName<>'Deleted' then InsertCounselor(S,X);
           X:=X+1;
     End;
End;

Procedure EditCounselor(Cur:Integer; Var C:Boolean);

Var
   Ptr:CounselorPtr;
   Sex,S220,Dis:String;
   S:CounselorRec;
   X,Code:Integer;

Begin
     Cls;
     Escape ('ESC - Main Menu   F1 - Save Changes');
     Ptr:=CFirst;
     If Cur<>1 then Begin
        For X:=1 to Cur-1 do Ptr:=Ptr^.Next;
     End;
     Reset (CounselorFile);
     Seek (CounselorFile,Ptr^.FileLoc);
     Read (CounselorFile,S);
        Locate (7,29);
        Write (' Last Name = ',S.LName);
        Locate (8,29);
        Write ('First Name = ',S.FName);
        Locate (9,29);
        Write ('     Grade = ',S.Grade:2);
        Locate (10,29);
        Write ('      Room = ',S.Room);
        Code:=1;
        C:=True;
        Repeat
              Case Code Of
                   0: Code:=4;
                   5: Code:=1;
              End;
              Case Code Of
                   1: SForm(S.LName,7,42,15,Code);
                   2: SForm(S.FName,8,42,15,Code);
                   3: IForm(S.Grade,9,42,12,Code);
                   4: SForm(S.Room,10,42,8,Code);
                  22: C:=True;
              End;
        Until ((Code=20) or (Code=22));
        If C=False then Begin
              Reset (CounselorFile);
              Seek (CounselorFile,Ptr^.FileLoc);
              Write (CounselorFile,S);
              Ptr^.LName:=S.LName;
              Ptr^.FName:=S.FName;
              Ptr^.Grade:=S.Grade;
              Ptr^.Room:=S.Room;
              Escape ('ESC - Main Menu');
        End;
End;

Procedure DeleteCounselor (Cur:Integer);

Var
   Ptr:CounselorPtr;
   X:Integer;
   S:CounselorRec;

Begin
     Ptr:=CFirst;
     If Cur<>1 then Begin
        For X:=1 to Cur-1 do Ptr:=Ptr^.Next;
     End;
     Reset (CounselorFile);
     Seek (CounselorFile,Ptr^.FileLoc);
     Read (CounselorFile,S);
     S.LName:='Deleted';
     Seek (CounselorFile,Ptr^.FileLoc);
     Write (CounselorFile,S);
     CFirst:=Nil;
     CLast:=Nil;
     LoadCounselors;
End;

Procedure AddCounselor (Var C:Boolean);

Var
   S:CounselorRec;
   Code:Integer;
   Sex,S220,Dis:String;
   D:Boolean;
   R:Char;

Begin
     D:=False;
     Repeat
        Cls;
        Escape ('ESC - Main Menu   F1 - Save Entry');
        Locate (7,29);
        Write (' Last Name = ');
        Locate (8,29);
        Write ('First Name = ');
        Locate (9,29);
        Write ('     Grade =  7');
        Locate (10,29);
        Write ('      Room = ');
        S.LName:='';
        S.FName:='';
        S.Grade:=7;
        S.Room:='';
        Code:=1;
        C:=True;
        Repeat
              Case Code Of
                   0: Code:=4;
                   5: Code:=1;
              End;
              Case Code Of
                   1: SForm(S.LName,7,42,15,Code);
                   2: SForm(S.FName,8,42,15,Code);
                   3: IForm(S.Grade,9,42,12,Code);
                   4: SForm(S.Room,10,42,8,Code);
                  22: C:=True;
              End;
        Until ((Code=20) or (Code=22));
        If Code=20 then C:=False;
           If C=False then Begin
              InsertCounselor(S,FileSize(CounselorFile));
              Reset (CounselorFile);
              Seek (CounselorFile,FileSize(CounselorFile));
              Write (CounselorFile,S);
              Escape ('ESC - Main Menu');
           End;
        If C=False then Begin
           Locate (15,28);
           Write ('Add another? (Y/N)');
           Locate (23,80);
           Hold;
           R:=ReadKey;
           If ((R='N') or (R='n')) then D:=True;
           If ((R='-') or (R=#27)) then C:=True;
        End;
        If C=True then D:=True;
     Until (D=True);
End;

Procedure CounselorUtils2(Top,Cur,High:Integer);

Var
   Ptr:CounselorPtr;
   X,High2,Low:Integer;

Begin
     Window (15,6,64,18);
     Locate (1,1);
              { _______  ________  ________  ________  ________ _}
              {/   0   \/    1   \/    2   \/    3   \/    4   \5}
              {12345678901234567890123456789012345678901234567890}
     WriteLn (' LAST           FIRST           GRADE  ROOM');
     Ptr:=CFirst;
     If Top<>1 then Begin
        For X:=1 to Top-1 do Ptr:=Ptr^.Next;
     End;
     If Top+11>High then High2:=High else High2:=Top+11;
     For X:=Top to High2 do Begin
         If Cur=X then Begin
            TextColor (0);
            TextBackground (15);
         End else Begin
             TextColor (7);
             TextBackground (0);
         End;
         Locate (X-Top+2,1);
         Write ('                                                ');
         Locate (X-Top+2,1);
         Write (' ',Ptr^.LName);
         Locate (X-Top+2,17);
         Write (Ptr^.FName);
         Locate (X-Top+2,34);
         Write (Ptr^.Grade:2,'    ',Ptr^.Room);
         Ptr:=Ptr^.Next;
     End;
     TextColor (7);
     TextBackground (0);
     Window (1,1,80,25);
     Locate (23,80);
End;

Procedure CounselorUtils;

Var
   Ptr:CounselorPtr;
   S:CounselorRec;
   I,X,High,Cur,High2,Code,Top:Integer;
   Sex,S220,Dis:String;
   C:Boolean;
   R:Char;

Begin
     Cls;
     C:=False;
     WorkBox(' COUNSELOR UTILITIES ');
     Escape('ESC - Main Menu   F3 - Help Screen');
     If CFirst=Nil then Begin
        AddCounselor(C);
        Cls;
     End;
     If C=False then Begin
     Cur:=1;
     Top:=1;
     Repeat
        Ptr:=CFirst;
        High:=0;
        While (Ptr<>Nil) do Begin
              High:=High+1;
              Ptr:=Ptr^.Next;
        End;
        If Cur<1 then Cur:=1;
        If Cur>High then Cur:=High;
        If Cur>Top+11 then Top:=Top+1;
        If Cur<Top then Top:=Cur;
        CounselorUtils2(Top,Cur,High);
        Hold;
        R:=ReadKey;
        Case R Of
             #0: Begin
                 R:=ReadKey;
                 Case R Of
                      #59: Begin
                           AddCounselor(C);
                           Cls;
                           End;

                      #60: Begin
                           DeleteCounselor(Cur);
                           If High=1 then C:=True;
                           Cls;
                           End;

                      #61: Begin
                           Cls;
                           Locate (5,35);
                           Write ('HELP SCREEN');
                           Locate (7,15);
                           Write (#24,#25,'   Use up and down arrows to move scroll bar.');
                           Locate (9,15);
                           Write (#17,#217,'   Press this while the scroll bar is on the');
                           Locate (10,15);
                           Write ('     record you want to edit and you will get the');
                           Locate (11,15);
                           Write ('     edit counselor screen.');
                           Locate (13,15);
                           Write ('F1   This will give you the new entry screen.  It');
                           Locate (14,15);
                           Write ('     will add the counselor on the end of the list.');
                           Locate (16,15);
                           Write ('F2   This will delete the record that the scroll');
                           Locate (17,15);
                           Write ('     bar is currently on.');
                           Locate (23,80);
                           Hold;
                           R:=ReadKey;
                           Cls;
                           End;

                      #72: Cur:=Cur-1;
                      #80: Cur:=Cur+1;
                 End;
                 End;

            #13: Begin
                 EditCounselor(Cur,C);
                 Cls;
                 End;

            '-',
            #27: C:=True;
        End;
     Until (C=True);
     End;
End;

Procedure Sort;

Var
   Ptr:StudentPtr;
   E:StudentRec;
   X,Num,Next:Integer;
   CPtr:CounselorPtr;
   CSex,C220,CDis:Boolean;

Begin
     Reset(StudentFile);
     Num:=0;
     Next:=1;
     CPtr:=CFirst;
     While (CPtr<>Nil) do Begin
           Num:=Num+1;
           CPtr:=CPtr^.Next;
     End;
     For X:=1 to 8 do Begin
         Ptr:=SFirst;
         Case X Of
              1: Begin        {Male, Normal}
                 CSex:=True;
                 C220:=False;
                 CDis:=False;
                 End;

              2: Begin        {Female, Normal}
                 CSex:=False;
                 C220:=False;
                 CDis:=False;
                 End;

              3: Begin        {Male, 220}
                 CSex:=True;
                 C220:=True;
                 CDis:=False;
                 End;

              4: Begin        {Female, 220}
                 CSex:=False;
                 C220:=True;
                 CDis:=False;
                 End;

              5: Begin        {Male, Dis.}
                 CSex:=True;
                 C220:=False;
                 CDis:=True;
                 End;

              6: Begin        {Female, Dis.}
                 CSex:=False;
                 C220:=False;
                 CDis:=True;
                 End;

              7: Begin        {Male, 220, Dis}
                 CSex:=True;
                 C220:=True;
                 CDis:=True;
                 End;

              8: Begin        {Female, 220, Dis}
                 CSex:=False;
                 C220:=True;
                 CDis:=True;
                 End;
         End;
         While (Ptr<>Nil) do Begin
               Seek(StudentFile,Ptr^.FileLoc);
               Read(StudentFile,E);
               Locate (5,10);
               Write (' ');
               Locate (23,80);
               If ((E.Sex=CSex) and (E.S220=C220) and (E.Dis=CDis)) then Begin
                  InsertGroup (Next,Ptr^.FileLoc);
                  Next:=Next+1;
                  If Next>Num then Next:=1;
               End;
               Locate (5,10);
               Write ('.');
               Locate (23,80);
               Ptr:=Ptr^.Next;
         End;
     End;
End;

Procedure SortGroups;

Var
   CPtr:CounselorPtr;
   GPtr:GroupPtr;
   Ptr:StudentPtr;
   I,X:Integer;
   C,D:Boolean;
   R:Char;

Begin
     Cls;
     WorkBox(' SORT GROUPS ');
     Escape('');
     Locate (12,36);
     Write ('Sorting...');
     Locate (23,80);
     Sort;
     Cls;
     Escape('ESC - Main Menu');
     I:=1;
     C:=False;
     While ((C=False) and (CPtr<>Nil)) do Begin
           D:=False;
           Ptr:=SFirst;
           Repeat
                 Cls;
                 Locate (6,10);
                 Write (CPtr^.Room,'  ',CPtr^.LName,', ',CPtr^.FName);
                 For X:=1 to 10 do Begin
                     If Ptr=Nil then Begin
                        X:=10;
                        D:=True;
                     End
                     Else Begin
                          Locate (7+X,5);
                          Write (Ptr^.Grade,'-',Ptr^.Class);
                          If Ptr^.Grade<10 then Write (' ');
                          If Ptr^.Class<10 then Write (' ');
                          Write (' ',Ptr^.LName,', ',Ptr^.FName);
                     End;
                     Ptr:=Ptr^.Next;
                 End;
                 If D=False then Begin
                 For X:=1 to 10 do Begin
                     If Ptr=Nil then Begin
                        X:=10;
                        D:=True;
                     End
                     Else Begin
                          Locate (7+X,40);
                          Write (Ptr^.Grade,'-',Ptr^.Class);
                          If Ptr^.Grade<10 then Write (' ');
                          If Ptr^.Class<10 then Write (' ');
                          Write (' ',Ptr^.LName,', ',Ptr^.FName);
                     End;
                     Ptr:=Ptr^.Next;
                 End;
                 End;
                 If Ptr=Nil then D:=True;
                 If D=False then Begin
                    Locate (19,5);
                    Write ('Continued...');
                 End;
                 Locate (23,80);
                 Hold;
                 R:=ReadKey;
                 If ((R='-') or (R=#27)) then C:=True;
           Until ((D=True) or (C=True));
     End;
End;

Procedure PrintClassLists;

Var
   R:Char;
   Class,Grade,Code:Integer;
   Ptr:StudentPtr;
   C,D:Boolean;

Begin
     Cls;
     WorkBox(' PRINT CLASS LISTS ');
     Escape('ESC - Print Menu');
     Locate (10,25);
     Write ('Press F1 to print all lists.');
     Locate (11,25);
     Write ('Press any key to print a');
     Locate (12,25);
     Write ('specific list.');
     Locate (23,80);
     Hold;
     R:=ReadKey;
     If (R=#0) then Begin
              R:=ReadKey;
              If R=#59 then Begin
                 Cls;
                 Grade:=1;
                 Class:=1;
                 Repeat
                       D:=False;
                       Ptr:=SFirst;
                       While (Ptr<>Nil) do Begin
                             If ((Ptr^.Grade=Grade) and (Ptr^.Class=Class)) then Begin
                                If D=False then Begin
                                   D:=True;
                                   WriteLn(Lst);
                                   WriteLn(Lst,'          CLASS: ',Grade,'-',Class);
                                   WriteLn(Lst);
                                End;
                                WriteLn(Lst,'          ',Ptr^.LName,', ',Ptr^.FName);
                             End;
                             Ptr:=Ptr^.Next;
                       End;
                       If D=True then Write(Lst,#12);
                       Class:=Class+1;
                       If Class=13 then Begin
                          Class:=1;
                          Grade:=Grade+1;
                       End;
                 Until (Grade=13);
              End;
              End;
     If ((R<>#59) and (R<>'-') and (R<>#27)) then Begin
              Cls;
              Locate (12,30);
              Write ('Enter class number:');
              Locate (13,38);
              Write (' 7. 1');
              Class:=1;
              Grade:=7;
              Code:=1;
              Repeat
                    Case Code Of
                         0: Code:=2;
                         3: Code:=1;
                    End;
                    Case Code Of
                         1: IForm(Grade,13,38,12,Code);
                         2: IForm(Class,13,41,12,Code);
                    End;
              Until ((Code=20) or (Code=22));
              If Code=20 then Begin
                 Ptr:=SFirst;
                 D:=False;
                 While (Ptr<>Nil) do Begin
                       If ((Ptr^.Class=Class) and (Ptr^.Grade=Grade)) then Begin
                          If D=False then Begin
                             D:=True;
                             WriteLn(Lst,'           CLASS: ',Grade,'-',Class);
                             WriteLn(Lst);
                          End;
                          WriteLn(Lst,'          ',Ptr^.LName,', ',Ptr^.FName);
                       End;
                       Ptr:=Ptr^.Next;
                 End;
                 If D=True then Write(Lst,#12);
              End;
     End;
End;

Procedure PrintCounselorList;

Var
   R:Char;
   Ptr:CounselorPtr;
   I:Integer;

Begin
     Cls;
     WorkBox(' PRINT COUNSELOR LIST ');
     Escape('ESC - Counselor Menu');
     Locate (10,30);
     Write ('Press F1 to print');
     Locate (11,30);
     Write ('the counselor list');
     Locate (23,80);
     Hold;
     R:=ReadKey;
     If R=#0 then Begin
        R:=ReadKey;
        If R=#59 then Begin
           Cls;
           Locate (23,80);
           Ptr:=CFirst;
           WriteLn(Lst,'         COUNSELOR LIST');
           WriteLn(Lst);
           WriteLn(Lst,'         GRD ROOM      NAME');
           While (Ptr<>Nil) do Begin
                 Write(Lst,'          ',Ptr^.Grade:2,'  ',Ptr^.Room);
                 For I:=(Length(Ptr^.Room)) to 8 do
                     Write(Lst,' ');
                 WriteLn(Lst,Ptr^.LName,', ',Ptr^.FName);
                 Ptr:=Ptr^.Next;
           End;
           Write(Lst,#12);
        End;
     End;
End;

Procedure PrintMenu;

Var
   R:Char;
   C:Boolean;

Begin
Repeat
     C:=False;
     Cls;
     WorkBox (' PRINT MENU ');
     Escape ('ESC - Main Menu');
     Locate (10,30);
     Write ('1) Print Class Lists');
     Locate (12,30);
     Write ('2) Print Counselor List');
     Locate (14,30);
     Write ('3) Print Group Lists');
     Locate (23,80);
     Hold;
     R:=ReadKey;
     Case R Of
          '-',
          #27: C:=True;

          '1': PrintClassLists;
          '2': PrintCounselorList;
     End;
Until (C=True);
End;

Procedure MainMenu;

Var
   R:Char;
   C:Boolean;

Begin
Repeat
     Cls;
     C:=False;
     WorkBox(' MAIN MENU ');
     Escape ('ESC - Exit Program');
     Locate (9,30);
     Write ('1) Student Utilities');
     Locate (11,30);
     Write ('2) Counselor Utilities');
     Locate (13,30);
     Write ('3) Sort Groups');
     Locate (15,30);
     Write ('4) Print Menu');
     Locate (23,80);
     Hold;
     R:=ReadKey;
     Case R Of
          '-',
          #27: C:=True;

          '1': StudentUtils;
          '2': CounselorUtils;
          '3': SortGroups;
          '4': PrintMenu;
     End;
Until (C=True);
End;

Begin
ClrScr;
SFirst:=Nil;
SLast:=Nil;
CFirst:=Nil;
CLast:=Nil;
Assign (StudentFile,'Advisor2.Dat');
Assign (CounselorFile,'Advisor2.Stp');
LoadStudents;
LoadCounselors;
MainMenu;
ClrScr;
Close (StudentFile);
End.