Minimal example dialog:
Using the oncomplete attribute from your command button and really simple script will help you a lot.
oncomplete
Your dialog and command button would be something similar to this:
An the script would be something like this: