Why does this script jump around? (One for Jacques for sure)

Well, I have this script about done. It is suppost to start a search and then go directly to the page of the result you want to start on. From there, it is suppost to open each link on that result page and then go back. When it is done, it is suppost to move to the next page and do the same thing. The dialog tell you how many pages you want to go through by the repeats. If you can, try this script out. Is starts fine, but them it gets erratic. It will jump to page 1 and back. Therre is no explaination from my stand point. I do not know alot about Javascript. One thing. This script starts at the second link of each result page. I had to do this because of the google stuff. That is ok…I am not worried about it. I just cant tell why it jumps around. Any ideas? Thanks!

-----SETTING VARIABLES
set searchT to text returned of (display dialog "What do you want to search for?" default answer "Enter Request Here")
set numberT to text returned of (display dialog "How many pages do you want to go through?" default answer "50")
set pageN to text returned of (display dialog "What result number do you want to start on?" & return & "Google does not allow more than 1000 at a time!" default answer "1")
set xa to 2
set z to text returned of (display dialog "How many reults does your Google browser display on each page?" default answer "10")
set y to z - 1
------------OPEN SAFARI AND ENTER SEARCH
tell application "Safari"
	activate
end tell
tell application "System Events"
	repeat until exists process "Safari"
		delay 0.5
	end repeat
end tell
tell application "Safari"
	open location "http://www.google.com"
end tell
delay 3
tell application "System Events" to tell process "Safari"
	set frontmost to true
	delay 1
	keystroke searchT
	keystroke return
end tell
delay 5
------GO TO PAGEN THE RESULT NUMBER YOU WANT TO START AT
set myStart_number to pageN
set Next_result_page to ((ASCII character 10) & "Next") as string
tell application "Safari"
	set the link_count to (do JavaScript "document.links.length" in document 1)
	repeat with i from (the link_count - 1) to 0 by -1
		if (do JavaScript "document.links[" & (i as string) & "].text" in document 1) is Next_result_page then
			set Start_at_this_result to (do JavaScript "document.links[" & (i as string) & "].href" in document 1) -- the Next link
			set sta to (offset of "&start=" in Start_at_this_result) + 6
			set URL of document 1 to text 1 thru sta of Start_at_this_result & myStart_number & "&sa=N" --display the result 500 to  600
			exit repeat
		end if
	end repeat
end tell
set pageN to pageN + z
-----------------MAIN
repeat numberT times
	-----------OPENENING LINKS AFTER #2
	repeat y times
		set nbr to 1
		tell application "Safari"
			set the link_count to (do JavaScript "document.links.length" in document 1)
			repeat with i from 0 to (the link_count - 1)
				if (do JavaScript "document.links[" & (i as string) & "].href" in document 1) contains "=related:" then set nbr to nbr + 1
				if nbr = xa then -- the next link is the third
					set URL of document 1 to (do JavaScript "document.links[" & (i + 1 as string) & "].href" in document 1) -- ,open the third
					exit repeat
				end if
			end repeat
			----
			delay 3 ---*********
			----
			tell application "System Events" to tell process "Safari"
				set frontmost to true
				keystroke "[" using command down
			end tell
			set xa to xa + 1
		end tell
	end repeat
	------MOVING TO NEXT PAGE
	set myStart_number to pageN
	set Next_result_page to ((ASCII character 10) & "Next") as string
	tell application "Safari"
		set the link_count to (do JavaScript "document.links.length" in document 1)
		repeat with i from (the link_count - 1) to 0 by -1
			if (do JavaScript "document.links[" & (i as string) & "].text" in document 1) is Next_result_page then
				set Start_at_this_result to (do JavaScript "document.links[" & (i as string) & "].href" in document 1) -- the Next link
				set sta to (offset of "&start=" in Start_at_this_result) + 6
				set URL of document 1 to text 1 thru sta of Start_at_this_result & myStart_number & "&sa=N" --display the result 500 to  600
				exit repeat
			end if
		end repeat
	end tell
	set pageN to pageN + z
end repeat
-----END

Works perfect! Thanks again!