I would go through the scripts themselves to try and understand what they are doing. Each pokeball in the lab will have a script assigned to it (you can view/open them using Advance map).
Each script uses the showpokepic/hidepokpic commands to show/hide a picture of a specific pokemon ID, and asks if you would like to select this pokemon. Then, if the player selects 'yes,' it uses the givepokemon command to physically give it to the player, setting a flag to make the pokeball sprite disappear (the flag will be the same as the Person ID value in advancemap). It also sets variable 0x4031 to a given value depending on which pokemon you select to determine which pokemon your rival will take.