Doing that really depends on the tab panel you are using. You would have to look at the documentation for that tab panel to see how to set the active tab and then use the logic you describe to get it selected.
This is something I could help with in a premier support ticket.