#pragma mode( separator(.,;) integer(h32) )
// Global variable to store the total time in minutes.
// The EXPORT keyword makes this variable and the functions below
// accessible from the calculator's Home screen.
EXPORT T_MINUTES := 0;

// Function to add a new time entry (hours and minutes)
// to the total
LOCAL ADD_TIME()
BEGIN
  LOCAL h_input, m_input, result;
  LOCAL current_total; 
  LOCAL display_h, display_m;
  current_total := T_MINUTES;
  LOCAL keep_looping := 1;
  LOCAL display_h, display_m;

  WHILE keep_looping DO
    display_m := irem(T_MINUTES, 60);
    display_h := iquo(T_MINUTES, 60);
    h_input := 0; 
    m_input := 0;
    result :=0;

    LOCAL title :=  "Tot: "+ display_h + "h " + display_m + "m    " + "OK to Add Time";
    result:= INPUT({h_input, m_input}, title, {"Hours", "Minutes"});

    IF result == 1 THEN
      // Update the total time.
      IF TYPE(h_input) != 1 THEN h_input = 0; END;
      IF TYPE(m_input) != 1 THEN m_input = 0; END;
      current_total := current_total + (IP(h_input) * 60) + IP(m_input);
      T_MINUTES := current_total;
      display_m := irem(T_MINUTES, 60);
      display_h := iquo(T_MINUTES, 60);
  
      IF h_input == 0 AND m_input == 0 THEN
        keep_looping := 0;
        END;
     END;
  END; // WHILE

  // Show the current total in a message box.
   MSGBOX("Current Total: " + display_h + "h " + display_m + "m");
END;

// Function to reset the total time to zero.
LOCAL RESET_TIME()
BEGIN
  // Reset the global variable to 0.
  T_MINUTES := 0;
  MSGBOX("Total time has been reset.");
END;

// Main function to run when the program is selected from the menu.
// This function will display a menu to the user.
EXPORT MAIN()
BEGIN
  LOCAL selection;
  CHOOSE(selection,"Select Function:", "Add Time", "Reset Total");
PRINT;
    IF selection == 1 THEN ADD_TIME();
    ELSE
    IF selection == 2 THEN RESET_TIME();
    END;
  END;
END;