Synchronization in apple scripting


I am new to applescript. I have started on this last week. I need to automate a web page using applescript. I am able to insert text in text boxes and clicking button etc. But I want to synchronize with the web page. Currently, I am giving delay of 20 seconds after opening the site. But if site loads early then it waits for the predefined amount of time. Is there any way with which we can check that the last control of page is loaded and visible? And until that control is not visible we can define a loop. So that minimum time delay is used.
And one more thing, Is there any way to reference one apple script file in another? For example, in a form of two pages. For automating second page, we will have to move to second page every time. So, can we write the script to move to page 2 in one file and use it while automating second page to avoid writing same thing again and again.

Akash Kansal.