I am not sure I understood correctly what you are trying to do...
this script is run when you open the layout ?
how it is fired ?
me I would display the values inside the dialog box without using the gotonthrecord function. You need only to display the currently active record.
You are using the $$Counter to reference two different records in two different tables. I suspect that the the portal is finding the related time using a diferent criteria than nthrecord as nthrecord is relative to the found set.
Have a look at the two tables and change to the nth record in both tables what name / time do you see? Change the sort order found set etc and the results will change.
Make sure you pull the data from the same table or use the relationship to get the related information from the second table.
I hope this helps or at least points you in the right direction.