MESSAGE "Writing pension procedures to library..."


PROC Ask_For_Output()
  PRIVATE File_Id
  Subset_Table = "P_Output"
  ECHO OFF
  FORMKEY
  MOVETO "Output"
  MOVETO FIELD "File_No"
  File_Id = [Pensions->File_No] + [Pensions->Version]
  LOCATE File_Id
  if RetVal then  ; output record located
    COPYTOARRAY Temp_Array
    CREATE Subset_Table LIKE "Output"
    VIEW Subset_Table
    COEDITKEY
    COPYFROMARRAY Temp_Array
    DO_IT!
    FORMKEY
    Select_Forms()
    DELETE Subset_Table
    else MESSAGE("Please compute and save results of this entry first!")
  endif  
  MOVETO "Pensions"
  FORMKEY  
ENDPROC;
WRITELIB Off_Lib Ask_For_Output


PROC Discount(DR, IR, Max_COLA, Pmts, Monthly)
PRIVATE COLA, PV_Pmt, Months_Delayed
  DR = DR * 0.01/12
;  if (IR = 0) then
;    Payout   = Round(Pmts * Monthly,0)
;    Fund_Bal = Round(PV(Monthly, DR, Pmts),0)
;  else 
  
;  F = "TEMP.TXT"
;       FILEWRITE F FROM "This is a printout of the annuity stream."+"\n"+"\n"
       Months_Delayed = 12; number of monthly payments until COLA kicks in
       Payout   = 0
       Fund_Bal = 0
       DR       = 1/(1+DR)
       FOR N FROM 1 to Pmts
         if (IR > 0.0) then
           if (N > Months_Delayed) AND (MOD(N,12) = 1) then
             if (IR < 1.0) then
               COLA = Round(Monthly*IR,2)
               else COLA = IR
             endif  
             if (Max_COLA > 0.0) AND (COLA > Max_COLA) then
               COLA = Max_COLA
             endif  
             Monthly = Monthly + COLA
           endif; (if N > Months_Delayed AND Divisible by 12)
         endif; (if Increase Rate > 0.0)

         Payout   = Payout + Monthly
         PV_Pmt   = Round(Monthly * Round(POW(DR,N),5),2)
         Fund_Bal = Fund_Bal + PV_Pmt

;         PRINT FILE F FORMAT("W5", N), FORMAT("W15.2,EC", Monthly), FORMAT("W15.2,EC", Payout), FORMAT("W15.2,EC", PV_Pmt), FORMAT("W15.2,EC", Fund_Bal),"\n"

       ENDFOR; FOR N FROM 1 to Pmts
       Payout   = Round(Payout,0)
       Fund_Bal = Round(Fund_Bal,0)
;  endif; if (IR > 0)
  Last_Pmt = Monthly
ENDPROC;
WRITELIB Off_Lib Discount


PROC Life_Stats( Age_1, Age_2, Race_Sex_Code )
PRIVATE
  I, N,
  Alive_Now, Alive_Then
  FORMKEY; get out of pension table form view
  MOVETO Life_Table
  MOVETO FIELD "Age"
  LOCATE INT(Age_1)                          ; integer, current age
  MOVETO FIELD "NA_" + Race_Sex_Code
; F = "TEMP.TXT"
  FOR I FROM 1 to ARRAYSIZE(Num_Alive)       ; copy number alive to array
    Num_Alive[I] = []
;   PRINT FILE F FORMAT( "W9.2", I )
;   PRINT FILE F FORMAT( "W9.2", Num_Alive[I] ), "\n"
    DOWN
  ENDFOR
  I = ARRAYSIZE(Num_Alive)
  Alive_Now  = Round(Num_Alive[1]-(Num_Alive[1]-Num_Alive[2])*(Age_1-INT(Age_1)),0)
  Alive_Then = Round(Num_Alive[I-1]-(Num_Alive[I-1]-Num_Alive[I])*(Age_2-INT(Age_2)),0)
  Num_Alive[1] = Alive_Now
  Num_Alive[I] = Alive_Then
;  PRINT FILE F FORMAT( "W9.2", Num_Alive[1] ), "\n"
;  PRINT FILE F FORMAT( "W9.2", Num_Alive[I] ), "\n"
  Mortality = Round((Alive_Then/Alive_Now), 4)
  if (Mortality > 1.0) then
    Mortality = 1.0
  endif
  LEFT
  UP
  N = []
  UP
  I = []
  LE = Round(I-(I-N)*(Age_2-INT(Age_2)),2)
ENDPROC;  
WRITELIB Off_Lib Life_Stats


PROC Is_Vested(Full, Per_Vested, V_Date)
; check for expected value of future vesting
  PRIVATE
    I, Prob, Expected
  Expected = Round(Full*Per_Vested*0.01, 2)
  if (Per_Vested < 100.0) then
    MOVETO "Separate"
    if (NIMAGERECORDS() > 0) then
      Prob = 1 - [Separation_Rate] * 0.01
      if (Prob < 1.0) then
        MOVETO "Schedule"
        FOR I FROM 1 TO NIMAGERECORDS()
          MOVETO RECORD I
          if ([Vests_On]<> BLANKDATE()) And ([Vests_At] > 0.0) then
            Expected = Expected + Round(Full*[Vests_At]*0.01*POW(Prob, ([Vests_On]-[Pensions->Valu])/365 ), 2)
          endif
        ENDFOR
      endif
    endif
    MOVETO "Pensions"   ; move to next table
  endif
  RETURN Expected
ENDPROC;
WRITELIB Off_Lib Is_Vested


PROC Death_Benefit(Is_Age, Pay_Age, Last_Pmt)
; check for death benefit -- Lump1, Lump2
  PRIVATE
    N, I,
    DB_1, DB_2,
    B1, B2,
    Partial,
    Probability,
    B_Age,
    B_Starts
  MOVETO "Death"
  DB_1 = 0.0
  DB_2 = 0.0
  if (NIMAGERECORDS() > 0) then
    if ([Lump1] > 0.0) Or ([Lump2] > 0.0) then
      B1 = [Lump1]
      B2 = [Lump2]
      N = ARRAYSIZE(Num_Alive)
      FOR I FROM 1 to N - 1
        Probability = 1 - Round(Num_Alive[I+1]/Num_Alive[I], 5)
        if (I = 1) then
          Partial = Is_Age - INT(Is_Age)
        else if (I = N - 1) then
               Partial = Pay_Age - INT(Pay_Age)
             else Partial = 1
             endif
        endif
        B1 = Round(B1*(1+[Growth1]*.01*Partial)/(1+[Disc1]*.01*Partial), 0)
        B2 = Round(B2*(1+[Growth2]*.01*Partial)/(1+[Disc2]*.01*Partial), 0)
        DB_1 = DB_1 + Round((B1+B2)*Probability,0)
      ENDFOR
    endif ; pre-payment lump sum distribution
  endif ; a record exists
  R["PV_Pre_DB"]  = DB_1
  R["PV_Annuity"] = R["PV_AMT"] + DB_1
  if (R["PV_Annuity"] >= R["WV_AT"]) then
    R["PV_Plan"] = R["PV_Annuity"]
    else R["PV_Plan"] = R["WV_AT"]
  endif
ENDPROC; 
WRITELIB Off_Lib Death_Benefit


PROC Marriage_Factor()
; check for marriage factor --- first dates, then years, finally %
  PRIVATE
    M, S, M_Per
  MOVETO "Marriage"
  M = 0.0
  S = 0.0
  M_Per = 100.0
  if (NIMAGERECORDS() > 0) then
    ; calculate years married based on dates, or accept entry
    if ([Married_From] <> BLANKDATE()) And ([Married_To] <> BLANKDATE()) then
      M = Round(([Married_To]-[Married_From])/365,2)
      [Married_Years] = M
      else if ([Married_Years] > 0.0) then
             M = [Married_Years]
           endif
    endif
    ; calculate years of service based on dates, or accept entry
    if ([Service_From] <> BLANKDATE()) And ([Service_To] <> BLANKDATE()) then
      S = Round(([Service_To]-[Service_From])/365,2)
      [Service_Years] = S
      else if ([Service_Years] > 0.0) then
             S = [Service_Years]
           endif
    endif
    ; calculate marriage factor, or accept entry
    if (M > 0.0) And (S >= M) then
      M_Per = Round(M/S, 4) * 100
      [Marital_%] = M_Per
      else if ([Marital_%] > 0.0) then
             M_Per = [Marital_%]
           endif
     endif
  endif; if an image is present
  R["Years_Married"] = M
  R["Years_Service"] = S
  R["Marr_Per"] = M_Per
  R["Marr_Amt"] = Round(M_Per * 0.01 * R["PV_Plan"], 0)
  MOVETO "Pensions"
ENDPROC;  
WRITELIB Off_Lib Marriage_Factor


PROC Save_Results()
  PRIVATE File_Id, I, J
  MESSAGE "Saving results of calculation..."
  FORMKEY; put results table into table view
  CTRLHOME
  DYNARRAY Transfer[]
  J = NFIELDS("Results")
  FOR I FROM 1 TO J
    TAB 
    Transfer[FIELD()] = FIELDSTR()
  ENDFOR
  DEL
  MOVETO "Output"
  MOVETO FIELD "File_No"
  File_Id = [Pensions->File_No] + [Pensions->Version]
  LOCATE File_Id
  if (RetVal = False) then
    END 
    DOWN ; open up a blank row
  endif  
  [File_No] = File_Id
  FOREACH Element IN Transfer
    MOVETO FIELD Element
    [] = Transfer[Element]
  ENDFOREACH
  if (R["Mortality"] < 1.0) then
    [Mortality] = "0" + [Mortality]
  endif
  [Title]  = [Pensions->Title]
  [First]  = [Pensions->First]
  [Last]   = [Pensions->Last]
  if ( SUBSTR([Last], LEN([Last]), 1) = "s") then
    [Last's] = [Last] + "'"
    else [Last's] = [Last] + "'s"
  endif  
  [Plan_Name]  = [Pensions->Plan_Name]
  [Birth]      = FORMAT("d1", [Pensions->Birth])
  if (SEARCH(" ", [Birth]) = 1) then
    [Birth] = "0" + SUBSTR([Birth], 2, 7)
  endif    
  [Race]       = [Pensions->Race]
  [Sex]        = [Pensions->Sex]
  [Info]       = FORMAT("d2", [Pensions->Info])
  I = SEARCH("  ", [Info])
  if (I > 1) then
    [Info] = SUBSTR([Info], 1, I) + SUBSTR([Info], I+2, 15)
  endif    
  [Valu]       = FORMAT("d2", [Pensions->Valu])
  I = SEARCH("  ", [Valu])
  if (I > 1) then
    [Valu] = SUBSTR([Valu], 1, I) + SUBSTR([Valu], I+2, 15)
  endif    
  [Vested_Per] = FORMAT("w7.2", [Pensions->Vested_Per])
  if ([Pensions->Vested_Per] < 100.0) then
    [Vest_Amt_Text] = "Monthly Benefit Reduced For Probability of Vesting"
  endif  
  if ([Pensions->COLA] > 0.0) then
    [Cola_Text] = "Expected Annual Increase In Benefits"  
    if ([Pensions->COLA] > 1.0) then
      [COLA] = FORMAT("W7.2, E$", [Pensions->COLA])
      else [COLA] = FORMAT("W5.2", [Pensions->COLA]*100) + "%"
    endif  
  endif
  [Initial_Amt_Text] = "Accrued Monthly Benefit"
  if ([Pensions->Pre_DR] = 0) then
    [Initial_Amt_Text] = "Present Value of Monthly Annuity"
    [PV_Plan_Text] = "Money Purchase Annuity Benefit"    
    else if ([Pensions->Pre_DR] < [Pensions->Post_DR]) then
           [PV_Plan_Text] = "Money Purchase Annuity Benefit"
           else [PV_Plan_Text] = "Formula Method Annuity Benefit "
         endif
  endif
  if (R["PV_Annuity"] < R["WV_AT"]) then
    [PV_Plan_Text] = "Separation Benefit"
  endif
  if ([Pensions->Tax_Rate] > 0.0) then
    [V_Title]       = "after-tax present value"
    [Tax_Rate_Text] = "Estimated Tax Liability"
    [PV_AT_Text]    = "After-tax Present Value of Annuity"
    else [V_Title]       = ""
         [Tax_Rate_Text] = ""
         [PV_AT_Text]    = ""
  endif

  if ([Pensions->Withdrawal] > 0.0) then
    [With_Text] = "Lump Sum Payment Option in Lieu of Annuity"
    if ([Pensions->Tax_Rate] > 0) then
      [With_AT_Text] = "   After-tax Value of Lump Sum Payment Option"
    endif
    else [With_Text] = "No lump sum payment option available"
         [With_AT_Text] = ""
  endif

  if (R["PV_Pre_DB"] > 0.0) then
    [PV_Ann_Text] = "Total Present Value, Including Death Benefit"
    [Pre_DB_Text] = "Present Value of Pre-retirement Death Benefit"
    else [Pre_DB_Text] = ""
  endif
  if (R["Marr_Per"] < 100.0) then
    [Years_Married] = FORMAT("W7.2", R["Years_Married"])
    [Years_Service] = FORMAT("W7.2", R["Years_Service"])
    [Married_Text]  = "Years of Service While Married"
    [Service_Text]  = "Total Years of Plan Service"
    [Marr_Per_Text] = "Percent of Benefit Accrued While Married"
    [Marr_Amt_Text] = "Marital Component"
    else [Married_Text]  = ""
         [Service_Text]  = ""
         [Marr_Per_Text] = ""
         [Marr_Amt_Text] = ""
  endif
  MESSAGE ""
ENDPROC;  
WRITELIB Off_Lib Save_Results


PROC Life_Exp_Compute()
  PRIVATE
    LE,
    Mortality,
    Payout,
    Fund_Bal,
    Last_Pmt
  ECHO OFF
  if (SYSMODE() <> "CoEdit") then
    COEDITKEY
  endif
  MOVETO "Pensions"
  POSTRECORD NOPOST LEAVELOCKED
  if NOT RetVal then
    MESSAGE "Duplicate file/version entry - please change!"
    MOVETO FIELD "Version"
    RETURN
  endif
  MESSAGE "Computing present value..."
  R["Age"] = Round(([Valu]-[Birth])/365.25, 2)
  if ([Start_Age] > R["Age"]) then
    R["Start_Age"] = [Start_Age]
    else R["Start_Age"] = R["Age"]
         [Start_Age]    = R["Age"]  
         POSTRECORD NOPOST
  endif
  R["Accrued"]    = [Accrued]
  R["COLA"]       = [COLA]
  R["Pre_DR"]     = [Pre_DR]
  R["Post_DR"]    = [Post_DR]
  R["Tax_Rate"]   = [Tax_Rate]
  R["Years_From"] = R["Start_Age"] - R["Age"]
  ARRAY Num_Alive[INT(R["Start_Age"])-INT(R["Age"])+2]
  ; get life expectancy and mortality factor
  Life_Stats(R["Age"], R["Start_Age"], SUBSTR([Race],1,1)+SUBSTR([Sex],1,1))
  MOVETO "Pensions"
  FORMKEY; return to form view
  R["Life_Exp"]   = LE
  R["Payments"]   = INT(LE*12)
  R["Mortality"]  = Mortality
  R["EV_Monthly"] = Is_Vested([Accrued], [Vested_Per], [Valu])
  Discount([Post_DR], [COLA], [Max_COLA], R["Payments"], R["EV_Monthly"])
  R["Pay_Out"]    = Payout
  R["Fund_Value"] = Fund_Bal
  R["PV"]         = Round(Fund_Bal * POW((1+[Pre_DR]*0.01/12), -R["Years_From"]*12), 0)
  R["PV_AM"]      = Round(R["PV"] * Mortality, 0 )
  R["PV_AMT"]     = Round(R["PV_AM"] * (1-[Tax_Rate]*0.01), 0)
  R["Withdrawal"] = [Withdrawal]
  R["WV_AT"]      = Round(R["Withdrawal"] * (1-[Tax_Rate]*0.01), 0)
  Death_Benefit( R["Age"], R["Start_Age"], Last_Pmt )
  Marriage_Factor()
  POSTRECORD NOPOST
  FORMKEY
  MOVETO "Results"
  COPYFROMARRAY R
  PICKFORM 1
  MESSAGE ""
  PROMPT " Press ESC when finished viewing."
  BEEP
  WAIT FIELD
  UNTIL "ESC"
  PROMPT ""
  if Response_Is_Yes("SAVE RESULTS", "Save Results of PV Calculation?") then
    Save_Results()
    else DEL
  endif  
  MOVETO "Pensions"
  FORMKEY ; redisplay pension form
  PROMPT Prompt_String
ENDPROC;
WRITELIB Off_Lib Life_Exp_Compute


PROC Actuarial_Compute()
  PRIVATE
    Increase, Race_Sex_Code, Monthly, COLA, Max_COLA, Pre_Dr, Post_DR, TEP, PV_TEP, PV_TEP_NOW
  TEP    = 0
  PV_TEP = 0
  PV_TEP_NOW = 0
  ECHO OFF
  if (SYSMODE() <> "CoEdit") then
    COEDITKEY
  endif
  MOVETO "Pensions"
  POSTRECORD NOPOST LEAVELOCKED
  if NOT RetVal then
    MESSAGE "Duplicate file/version entry - please change!"
    MOVETO FIELD "Version"
    RETURN
  endif
  MESSAGE "Computing present value..."
  R["Age"] = Round(([Valu]-[Birth])/365.25, 2)
  if ([Start_Age] > R["Age"]) then
    R["Start_Age"] = [Start_Age]
    else R["Start_Age"] = R["Age"]
         [Start_Age]    = R["Age"]  
         POSTRECORD NOPOST
  endif
  Current_Age = INT(R["Age"] * 12)
  Start_Age   = INT(R["Start_Age"] * 12)
  First_Payment_Age = Start_Age + 1     ; first payment is made one month after retirement
  Array_Size = 1200 - Start_Age
  Array Pmt_Age[Array_Size]
  ARRAY Num_Alive[Array_Size]
  ARRAY Prob_Surv[Array_Size]
  ARRAY Payment[Array_Size]
  ARRAY EV_Payment[Array_Size]
  ARRAY Disc_Factor[Array_Size]
  ARRAY EPV_Payment[Array_Size]
  R["Accrued"]    = [Accrued]
  R["EV_Monthly"] = Is_Vested([Accrued], [Vested_Per], [Valu])
  R["COLA"]       = [COLA]
  R["Pre_DR"]     = [Pre_DR]
  R["Post_DR"]    = [Post_DR]
  R["Tax_Rate"]   = [Tax_Rate]
  R["Years_From"] = R["Start_Age"] - R["Age"]
  R["Tax_Rate"]   = [Tax_Rate]
  R["Withdrawal"] = [Withdrawal]
  R["WV_AT"]      = Round(R["Withdrawal"] * (1-[Tax_Rate]*0.01), 0)
  Race_Sex_Code = SUBSTR([Race],1,1) + SUBSTR([Sex],1,1)

  Monthly       = R["EV_Monthly"]
  COLA          = [COLA]
  Max_COLA      = [Max_COLA]
  Increase      = COLA
  Pre_DR        = [Pre_DR]
  Pre_DR        = 1/(1+Pre_DR*0.01/12)
  Post_DR       = [Post_DR]
  Post_DR       = 1/(1+Post_DR*0.01/12)


; (******************* New portion of code ***************)

  FORMKEY; get out of pension table form view
  MOVETO Mort_Table
  MOVETO FIELD "Month"
  LOCATE Current_Age 
  MOVETO FIELD "NA_" + Race_Sex_Code
  Num_Alive_Now = []
  MOVETO FIELD "Month"
  LOCATE First_Payment_Age
  MOVETO FIELD "NA_" + Race_Sex_Code
  FOR I FROM 1 to Array_Size    ; copy number alive to array
    Num_Alive[I] = []
    Pmt_Age[I]   = Start_Age + I
    Prob_Surv[I] = Round((Num_Alive[I] / Num_Alive_Now), 6)
    if (Increase > 0.0) then
      if (I > 12) AND (MOD(I,12) = 1) then
        if (Increase < 1.0) then
          COLA = Round(Monthly * Increase,2)
          else COLA = Increase
        endif  
        if (Max_COLA > 0.0) AND (COLA > Max_COLA) then
          COLA = Max_COLA
        endif  
        Monthly = Monthly + COLA
      endif; (if I > 12 AND Remainder = 1)
    endif; (if Increase > 0.0)
    Payment[I]     = Monthly
    EV_Payment[I]  = Round((Monthly * Prob_Surv[I]), 2)
    TEP            = TEP + EV_Payment[I]
    Disc_Factor[I] = Round(POW(Post_DR,I),6)
    EPV_Payment[I] = Round((EV_Payment[I] * Disc_Factor[I]), 2)
    PV_TEP         = PV_TEP + EPV_Payment[I]
    if (I < Array_Size) then
      DOWN
    endif
  ENDFOR

  TEP    = Round(TEP,0)
  PV_TEP = Round(PV_TEP,0)
  PV_TEP_NOW = Round(PV_TEP * Round(POW(Pre_DR,(Start_Age-Current_Age)),6),0)

  Print_To_File = "N"
  if (Print_To_File = "Y") then
    F = "TEMP.TXT"
    FILEWRITE F FROM "This is a printout of the annuity stream. \n \n"
    PRINT FILE F "Current Age:  ", FORMAT("W9", Current_Age), "\n"
    PRINT FILE F "Age at First Payment:  ", FORMAT("W9", First_Payment_Age), "\n"
    PRINT FILE F "Number Alive at Present:  ", FORMAT( "W9", Num_Alive_Now), "\n", "\n", "\n", "\n"
    PRINT FILE F "Pmt #   Age   #Alive   Prob. Surv.  Payment   Exp. Value    Disc. Fact.      PVEP", "\n", "\n"
    FOR I FROM 1 to Array_Size
      PRINT FILE F FORMAT( "W6", I )
      PRINT FILE F FORMAT( "W6", Pmt_Age[I] )
      PRINT FILE F FORMAT( "W9", Num_Alive[I] )
      PRINT FILE F FORMAT( "W10.6", Prob_Surv[I] )
      PRINT FILE F FORMAT( "W12.2", Payment[I] )
      PRINT FILE F FORMAT( "W12.2", EV_Payment[I] )
      PRINT FILE F FORMAT( "W14.6", Disc_Factor[I] )
      PRINT FILE F FORMAT( "W14.2", EPV_Payment[I] ), "\n"
    ENDFOR
    PRINT FILE F "\n"
    PRINT FILE F "Totals:                                      ", FORMAT( "W10", TEP), FORMAT( "W28", PV_TEP), "\n", "\n"
    PRINT FILE F "Total Expected Payout:                                    ", FORMAT( "W10", TEP), "\n", "\n"
    PRINT FILE F "Total Present Value of Expected Payments @ Commencement:  ", FORMAT( "W10", PV_TEP), "\n", "\n"
    PRINT FILE F "Total Present Value of Expected Payments Now:             ", FORMAT( "W10", PV_TEP_Now), "\n", "\n"
  endif; Print_To_File = "Y"

  MOVETO "Pensions"
  FORMKEY; return to form view
  R["Life_Exp"]   = 0.0
  R["Payments"]   = 0.0
  R["Mortality"]  = 0.0
  R["PV_AM"]      = 0.0

  R["Pay_Out"]    = TEP
  R["Fund_Value"] = PV_TEP
  R["PV"]         = PV_TEP_NOW
  R["PV_AMT"]     = Round(R["PV"] * (1-[Tax_Rate]*0.01), 0)

; skip death benefit calculation for now, set DB values to zero
; Death_Benefit( R["Age"], R["Start_Age"], Monthly )

  R["PV_Pre_DB"]  = 0.0
  R["PV_Annuity"] = R["PV_AMT"] + R["PV_Pre_DB"]
  if (R["PV_Annuity"] >= R["WV_AT"]) then
    R["PV_Plan"] = R["PV_Annuity"]
    else R["PV_Plan"] = R["WV_AT"]
  endif

; (******************* balance of original compute procedure ***************)

  Marriage_Factor()
  POSTRECORD NOPOST
  FORMKEY
  MOVETO "Results"
  COPYFROMARRAY R
  PICKFORM 1
  MESSAGE ""
  PROMPT " Press ESC when finished viewing."
  BEEP
  WAIT FIELD
  UNTIL "ESC"
  PROMPT ""
  if Response_Is_Yes("SAVE RESULTS", "Save Results of PV Calculation?") then
    Save_Results()
    else DEL
  endif  
  MOVETO "Pensions"
  FORMKEY; redisplay pension form
  PROMPT Prompt_String
ENDPROC;  
WRITELIB Off_Lib Actuarial_Compute


PROC Pension_Wait_Proc(TriggerType, EventInfo, CycleNumber)
  PRIVATE Key_Code, Menu_Pick
  if (EventInfo["TYPE"] = "KEY") then  
    Key_Code = EventInfo["KEYCODE"]
    SWITCH
      ; ALT A - Compute Using Actuarial Method
      CASE (Key_Code = -30) : if ISFIELDVIEW() then
                                DO_IT!
                              endif
                              MOVETO "Pensions"
                              Actuarial_Compute() ; pv of a pension
                              Main_Table_Menu()
                              RETURN 1

      ; ALT L  - Compute using Life Exepctancy Method
      CASE (Key_Code = -38) : if ISFIELDVIEW() then
                                DO_IT!
                              endif
                              MOVETO "Pensions"
                              Life_Exp_Compute() ; pv of a pension
                              Main_Table_Menu()
                              RETURN 1
      ; 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()
      ; DELETE
      CASE (Key_Code = -83) : if (SYSMODE() = "CoEdit") then
                                RETURN Display_Delete_Box()
                                else RETURN 1
                              endif
      ; + or - to change date by day
      CASE (Key_Code = 43) OR 
           (Key_Code = 45)   : RETURN Change_Date(Key_Code)
      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_For_Output()
                                        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
WRITELIB Off_Lib Pension_Wait_Proc


PROC Pension_Table_Wait()
; Life Expectancy Method uses LifeTabl
  Life_Table = "LifeTabl"
; Actuarial Method uses NumberAl Mortailty Table
  Mort_Table = "NumberAl"
  R_Table    = "Results"
  Prompt_String = "  Alt-L for Life Expectancy Method;  Alt-A for Actuarial Method"
  Main_Table_View("Pensions", 1, 0); place pension table on workspace in form view
  ECHO OFF
  END; move to last record in pension table
  FORMKEY; move from form view to table view
  VIEW   Life_Table
  VIEW   Mort_Table
  VIEW  "Output"
  VIEW   R_Table
  COEDITKEY
  COPYTOARRAY R
  DO_IT!
  MOVETO "Pensions"  
  FORMKEY
  Main_Table_Edit()
  PROMPT Prompt_String
  ECHO NORMAL
  WAIT WORKSPACE
    PROC    "Pension_Wait_Proc"
    MESSAGE "MENUSELECT"              
;    TRIGGER "ARRIVEFIELD"
    KEY   -60,   -66,   -67,  -83,     -38,        -30,           43,   45
         ; DO_IT  Clear  Edit  Delete   Alt-L       Alt-A          +    -
         ; F2     F8     F9    DEL      LECompute   ActCompute
  ENDWAIT
  ECHO OFF
  EMPTY "Output"; empty output table every time script is exited
  CLEARALL
  PROMPT ""
  MESSAGE ""
ENDPROC;
WRITELIB Off_Lib Pension_Table_Wait


RELEASE PROCS ALL