MESSAGE "Writing rolodex procedures to library..."

PROC Rolodex_Wait(M_Tbl)
  PRIVATE Fld_Prompt, Answer_Menu

  PROC Rolodex_Answer_Wait_Proc(TriggerType, EventInfo, CycleNumber)
    PRIVATE Answer_Tbl, Menu_Tag, Return_Code
    if (TriggerType = "ARRIVEFIELD") then
      PROMPT Fld_Prompt[FIELD()]
      RETURN 1
    endif
    if (EventInfo["TYPE"]    = "MESSAGE")    AND 
       (EventInfo["MESSAGE"] = "MENUSELECT") AND
       (SUBSTR(EventInfo["MENUTAG"],1,2) = "R_") then       
       Menu_Tag = EventInfo["MENUTAG"]
       SHOWPULLDOWN
       ENDMENU
       CLEARSPEEDBAR
       MESSAGE "One moment please..."
       ECHO OFF
       SWITCH
         CASE (Menu_Tag = "R_Envelope") :
           FORMKEY
           Print_Report(Subset_Table, "1", Rpt_St["Envelope"])  
         CASE (Menu_Tag = "R_Phone") OR (Menu_Tag = "R_Phone_Address") OR (Menu_Tag = "R_Rolodex") :
          {Ask} TYPEIN Subset_Table ENTER 
          TAB CHECKPLUS EXAMPLE "LINK" TYPEIN "!"
          {Ask} {Phone} CHECKPLUS TAB CHECK EXAMPLE "LINK"
          DO_IT!
          Answer_Tbl = PRIVDIR() + TABLE()
          CLEARIMAGE                ; erase answer image
          MOVETO "Phone(Q)"
          CLEARIMAGE                ; erase 1st query image
          MOVETO (Subset_Table + "(Q)")
          CLEARIMAGE                ; erase 2nd query image
          if ISEMPTY(Answer_Tbl) then
            No_Matches_Found()
            else if (Menu_Tag = "R_Phone") then
                   COPYREPORT "Phone" "1" Answer_Tbl "R"
                   Print_Report(Answer_Tbl, "R", Rpt_St["Phone_Book"])
                   else if (Menu_Tag = "R_Phone_Address") then
                          COPYREPORT "Phone" "2" Answer_Tbl "R"
                          Print_Report(Answer_Tbl, "R", Rpt_St["Phone_Book"])
                          else COPYREPORT "Phone" "3" Answer_Tbl "R"
                               Print_Report(Answer_Tbl, "R", Rpt_St["Rolodex_Info"])
                        endif       
                 endif       
          endif
       ENDSWITCH
       MOVETO Subset_Table
       FORMKEY
       ECHO NORMAL
       EXECPROC "Rolodex_Answer_Menu"
       RETURN 1
       else Return_Code = Answer_Table_Wait_Proc(TriggerType, EventInfo, CycleNumber)
            if (SYSMODE() = "Main") then
              MENUENABLE "Assemble"
              else MENUDISABLE "Assemble"
            endif
            RETURN Return_Code
    endif
  ENDPROC;  Rolodex_Answer_Wait_Proc

  PROC Ask_Rolodex()
    ; user selects rolodex records based on search criteria
    PRIVATE Id, Last, Title, A1, A2, A3, City, Abrev, Zip, Group, Phone_No, 
            I, Form_List, Form_Num
    MOVETO M_Tbl
    Form_Num = FORM()
    Id         = [Id]
    Last       = ""
    Title      = ""  
    A1         = ""
    A2         = ""
    A3         = ""  
    City       = ""
    Abrev      = ""
    Zip        = ""  
    Group      = ""  
    Phone_No   = ""
    FORMKEY       ; switch to table view
    SHOWPULLDOWN  ; hide rolodex main menu
    ENDMENU
    CLEARSPEEDBAR ; clear rolodex speedber
    PROMPT "Enter selection criteria.  Press Search to find matches, Cancel to quit."
    MOUSE SHOW
    SHOWDIALOG "Rolodex Selection Criteria"
      @3, 15 HEIGHT 17 WIDTH 50
      @1, 4 ?? "Id"
        ACCEPT @1,15
        WIDTH 25 "A80" PICTURE "*!"
        TAG ""
        TO   Id
      @2, 4 ?? "Last Name"
        ACCEPT @2,15
        WIDTH 25 "A80" PICTURE "*!"
        TAG ""
        TO   Last
      @3, 4 ?? "Group"
        ACCEPT @3,15
        WIDTH 25 "A45" 
        TAG ""
        TO   Group
      @4, 4 ?? "Title"
        ACCEPT @4,15
        WIDTH 25 "A45" PICTURE "*!"
        TAG ""
        TO   Title
      @5, 4 ?? "Street 1"
        ACCEPT @5,15
        WIDTH 25 "A45"
        TAG ""
        TO   A1
      @6, 4 ?? "Street 2"
        ACCEPT @6,15
        WIDTH 25 "A45"
        TAG ""
        TO   A2
      @7, 4 ?? "Street 3"
        ACCEPT @7,15
        WIDTH 25 "A45"
        TAG ""
        TO   A3
      @8, 4 ?? "City"
        ACCEPT @8,15
        WIDTH 25 "A80" PICTURE "*!"
        TAG ""
        TO   City
      @9, 4 ?? "State"
        ACCEPT @9,15
        WIDTH 25 "A45" PICTURE "*!"
        TAG ""
        TO   Abrev
      @10, 4 ?? "Zip Code"
        ACCEPT @10,15
        WIDTH 25 "A45" 
        TAG ""
        TO   Zip
      @11, 4 ?? "Phone No."
        ACCEPT @11,15
        WIDTH 25 "A45" 
        TAG ""
        TO   Phone_No
      PUSHBUTTON @13,12 WIDTH 10
        "~S~earch"
        OK
        DEFAULT
        VALUE ""
        TAG "OK"
        TO Button
      PUSHBUTTON @13,25 WIDTH 10
        "~C~ancel"
        CANCEL
        VALUE ""
        TAG "Cancel"
        TO Button
    ENDDIALOG
    MOUSE HIDE
    PROMPT ""
    if (RetVal = True) then
      MESSAGE "Searching..."
      ECHO OFF
      if NOT ISBLANK(Phone_No) then
        {Ask} {Phone} TAB EXAMPLE "LINK" TAB TYPEIN Phone_No
        {Ask} {Rolodex} Check TAB EXAMPLE "LINK"   ; only 1 Id per phone #
        if NOT ISBLANK(Id) then
          TYPEIN ", "
        endif  
        else {Ask} {Rolodex} CheckPlus 
      endif  
      [Id]      = Id
      [Title]   = Title
      [Last]    = Last
      [A1]      = A1
      [A2]      = A2
      [A3]      = A3
      [City]    = City
      [Abrev]   = Abrev
      [Zip]     = Zip
      [Group]   = Group
      DO_IT!
      ; if error in query form, abort selection process
      if (TABLE() <> "Answer") then
        ; erase query image(s)
        WHILE (IMAGETYPE() = "Query")
          CLEARIMAGE
        ENDWHILE
        MESSAGE "Invalid selection criteria.  Query unsuccessful."
        FORMKEY
        RETURN
      endif
      Subset_Table = PRIVDIR() + "SUBSET"
      RENAME TABLE() Subset_Table
      MOVETO "Rolodex(Q)"  CLEARIMAGE     ; erase rolodex query image
      if NOT ISBLANK(Phone_No) then
        MOVETO "Phone(Q)"    CLEARIMAGE
      endif
      MOVETO Subset_Table
      if ISEMPTY(Subset_Table) then
        CLEARIMAGE
        No_Matches_Found()
        else ; copy form and display on screen
               COPYFORM Main_Table Form_Num Subset_Table "1"
               COPYREPORT Main_Table "1" Subset_Table "1"
               View_Answer_Table(Subset_Table, 2, 2)
               FORMTABLES Subset_Table "1" Form_List
               FOR I FROM 1 to ARRAYSIZE(Form_List)
                 MOVETO Form_List[I]
                 IMAGERIGHTS READONLY ; make all detail records uneditable
               ENDFOR
               MOVETO Subset_Table
               Rolodex_Answer_Wait()
               if ISTABLE(Subset_Table) then  ; delete subset table
                 ECHO OFF
                 DELETE(Subset_Table)
               endif
      endif
    endif
    FORMKEY ; return to form view
  ENDPROC;  Ask_Rolodex

  PROC Rolodex_Answer_Menu()
    SHOWPULLDOWN
      "Modify"    : "Toggle between edit and main mode"               : "Modify"
        SUBMENU
          "Edit Mode - F9" : "Allow data to be edited, deleted, etc." : "Edit\Mode",
          "Main Mode - F2" : "Discontinue editing"                    : "Main\Mode"
        ENDSUBMENU,
      "Reports"          : "Choose report to generate"               : "Reports"
        SUBMENU
          "Envelope"     : "Create an envelope"                      : "R_Envelope",
          "Phone Book"   : "Generate list of phone numbers & addresses" : ""
            SUBMENU
              "Address & Phone #" : "List address & phone numbers"    : "R_Phone_Address",
              "Phone # Only"      : "List only names & phone numbers" : "R_Phone"
            ENDSUBMENU,
          "Rolodex Info" : "All info in rolodex"                     : "R_Rolodex"
        ENDSUBMENU,
      "Assemble"         : "Combine data and forms into documents"   : "Assemble",
      "Quit"         : "Return to previous menu"     : ""
        SUBMENU
          "No      " : "Continue working with selected data"  : "Return\No",
          "Yes - F8" : "Return to complete data set"          : "Return\Yes"
        ENDSUBMENU
    ENDMENU    
    if (SYSMODE() = "Main") then
      MENUDISABLE  "Main\Mode"
      else MENUDISABLE "Edit\Mode"
           MENUDISABLE "Reports"
           MENUDISABLE "Assemble"
    endif
    Rolodex_Speedbar()
  ENDPROC;  Rolodex_Answer_Menu
  
  PROC Rolodex_Answer_Wait()  
    Rolodex_Answer_Menu()
    Sound_Off()
    ECHO NORMAL
    Message_Box("Search Completed", "Matching Rolodex Entries: " + STRVAL(NRECORDS(Subset_Table)))
    WAIT WORKSPACE
      PROC    "Rolodex_Answer_Wait_Proc"
      MESSAGE "MENUSELECT"
      TRIGGER "ARRIVEFIELD"
      KEY   -60,   -66,   -67,  -83,   -50
           ; DO_IT  Clear  Edit  Delete Alt-M
           ; F2     F8     F9    DEL    Memo 
    ENDWAIT
    CLEARSPEEDBAR
    MESSAGE ""
  ENDPROC;  Rolodex_Answer_Wait

  PROC Rolodex_Wait_Proc(TriggerType, EventInfo, CycleNumber)
    PRIVATE Key_Code, Menu_Pick
    if (TriggerType = "ARRIVEFIELD") then
      PROMPT Fld_Prompt[FIELD()]
      RETURN 1
    endif
    if (EventInfo["TYPE"] = "KEY") then  
      Key_Code = EventInfo["KEYCODE"]
      SWITCH
        ; F9 - COEDIT
        CASE (Key_Code = -67) : RETURN Main_Table_Edit()
        ; F2 - DO_IT!
        CASE (Key_Code = -60) : if ISEMPTY(Main_Table) then
                                  RETURN Main_Table_Clear()
                                  else RETURN Main_Table_End_Edit()
                                endif    
        ; F8 - CLEAR
        CASE (Key_Code = -66) : RETURN Main_Table_Clear()
        ; Alt-M - Memo
        CASE (Key_Code = -50) : Display_Memo(Main_Table)
                                Main_Table_Menu()
                                Rolodex_Speedbar()
                                RETURN 1
        ; DELETE
        CASE (Key_Code = -83) : if (SYSMODE() = "CoEdit") then
                                  RETURN Display_Delete_Box()
                                  else RETURN 1
                                endif
        ; ALT-F - Switch form
        CASE (Key_Code = -33) :  if (SYSMODE() = "Main") then
                                   ECHO OFF
                                   MOVETO M_Tbl
                                   if (FORM() = "1") then
                                     PICKFORM 4
                                     else if (FORM() = "4") then
                                            PICKFORM 1
                                          endif  
                                   endif
                                   ECHO NORMAL
                                 endif  
                                 RETURN 1
        OTHERWISE             : SOUND 400 100 RETURN 1             
      ENDSWITCH
    endif
    if (EventInfo["MESSAGE"] = "MENUSELECT") then 
      Menu_Pick = EventInfo["MENUTAG"]
      SWITCH
        CASE (Menu_Pick = "Edit\Mode")  : RETURN Main_Table_Edit()
        CASE (Menu_Pick = "Main\Mode")  : if ISEMPTY(Main_Table) then
                                            RETURN Main_Table_Clear()
                                            else RETURN Main_Table_End_Edit()
                                          endif    
        CASE (Menu_Pick = "Ask")        : Ask_Rolodex()
                                          Main_Table_Menu()
                                          Rolodex_Speedbar()
                                          RETURN 1
        CASE (Menu_Pick = "Close\Yes")  : RETURN Main_Table_Clear()
        CASE (Menu_Pick = "Close\No")   : RETURN 1
        OTHERWISE                       : SOUND 400 100 RETURN 1
      ENDSWITCH
    endif
    SOUND 400 100 RETURN 1  ; safety valve
  ENDPROC; Rolodex_Wait_Proc

  PROC Rolodex_Speedbar()
    CLEARSPEEDBAR
    SPEEDBAR "~F10~ Menu":-68, "~Alt-M~ Memo":-50
    if ISASSIGNED(Fld_Prompt[FIELD()]) then
      PROMPT Fld_Prompt[FIELD()]
      else PROMPT ""
    endif  
  ENDPROC;  Rolodex_Speedbar

  ; MAIN PROCEDURE BEGINS HERE
  Answer_Menu            = "Rolodex_Answer_Menu"
  DYNARRAY Fld_Prompt[]
  Fld_Prompt["Id"]       = "Unique key to identify & sort rolodex (required)"
  Fld_Prompt["Last"]     = "Last name or company name"
  Fld_Prompt["Prefix"]   = "Appropriate title, e.g. Mr., Ms., Dr., etc."
  Fld_Prompt["First"]    = "First name of individual(s)"
  Fld_Prompt["Middle"]   = "Middle name or initial"
  Fld_Prompt["Suffix"]   = "Appropriate title, e.g. Jr., Sr., M.D., etc."
  Fld_Prompt["Group"]    = "Press F1 to select group for this entry"
  Fld_Prompt["Title"]    = "Official title or postion, e.g. President"
  Fld_Prompt["A1"]       = "First line of address, or firm name"
  Fld_Prompt["A2"]       = "Second line of address"
  Fld_Prompt["A3"]       = "Third line of address"
  Fld_Prompt["City"]     = "City of mailing address"
  Fld_Prompt["Abrev"]    = "Press F1 to select two letter state abbreviation."
  Fld_Prompt["Zip"]      = "Zip code, 5 or 9 digits"
  Fld_Prompt["Email"]    = "Email address"
  Fld_Prompt["DOB"]      = "Date of Birth: ##/##/##"
  Fld_Prompt["SS#"]      = "Social Security No.: ###-##-###"
  Fld_Prompt["Legal_Status"] = "Petitioner/Respondent, etc."
  Fld_Prompt["Memo"]     = "Add or edit notes for this rolodex entry"
  Fld_Prompt["Location"] = "Location for phone no., e.g. Office, Home, etc."
  Fld_Prompt["Phone"]    = "Phone # formats: ###-####, 1-###-###-####"
  Fld_Prompt["Identifier"] = "Name of variable"
  Fld_Prompt["Response"]   = "Text to replace variable"
  Main_Table_View(M_Tbl, 2, 2)
  Rolodex_Speedbar()
  ECHO NORMAL
  WAIT WORKSPACE
    PROC    "Rolodex_Wait_Proc"
    MESSAGE "MENUSELECT"              
    TRIGGER "ARRIVEFIELD"
    KEY   -60,   -66,   -67,  -83,   -50,   -33
         ; DO_IT  Clear  Edit  Delete Alt-M  Alt-F
         ; F2     F8     F9    DEL    Memo   Switch form
  ENDWAIT
  CLEARSPEEDBAR
  PROMPT ""
  MESSAGE ""
ENDPROC
WRITELIB Off_Lib Rolodex_Wait

RELEASE PROCS ALL