Applescript if url of tab contains "text" click link

this works for what i need it to, but need to add function that will click on link if urloftab contains "300"

the last url of each page ends in "300", so i figure that if I add a function that for every tab it open it will also search to see if url contains "300". if it contains "300" click link to next page.

this right now works , it opens a list of URL's and searches each one for "text", the site im searching only allows for 300 records to be shown at one time, i have to search hundreds of thousands of records. and I would rather not have to manually click the next button every time it reaches the last URL in the list.
and it searches 300 records in about 11 minutes, and i would rather not wait every 11 minutes to click to continue.

i would need it to, while it has tab open and efter it has searched the page itself and saved the URL in the text file, then also search the URL of the tab for "300", if it contains "300" then close tab and click the next link in the current safari page

on run {input, parameters}
	read (item 1 of input)
	set ps to paragraphs of the result
	set tot to count ps
	tell application "Safari"
		reopen
		activate
	end tell
	repeat with i from 1 to tot
		set p to item i of ps
		if p is not "" then
			try
				tell application "Safari"
					tell front window
						set r to make new tab with properties {URL:p}
						set current tab to r
						delay 1
						if i = tot then exit repeat
						repeat
							delay 1
							
							tell application "Safari"
								activate
								
								--the source code of current Tab
								set sourceOfTab to source of front document
								
								set urlOfTab to URL of front document
								
								if sourceOfTab contains "1/28/2015" then
									
									--copy of the url in textFile
									do shell script "echo " & urlOfTab & " >> ~/text.txt"
									close current tab of front window
									
								else
									close current tab of front window
								end if
							end tell
							
							
							
							get URL of r
						end repeat
					end tell
				end tell
			end try
		end if
	end repeat
end run

this is what the source of the next button contains on the site im searching, i dont know what class or handle or ID or name that it needs to find the link. every link is a little bit different for each batch of 300 records. the only thing that changes is the ahref


			

	 </table>


<form action="searchresults.asp" method="Post">
<input type="hidden" name="intpage" value="2">

<table width="100%" border="0" cellspacing="0" cellpadding="2">
	<tr>
	
		<td align="center"></td>
		<td align="right"><a href="searchresults.asp?p=2&qs=a&st=2&lqs=ABERNATHY+MARGUERITE+TR+1%2FNCNB&d=False" onmousedown="document.images['Next'].src='images/arrow_rightdown.gif'"><img src="images/arrow_rightup.gif" name="Next" border="0"></a></td>
		</tr>
	
</table>

            </div>
          </td>
        </tr>
      </table>
	</td>
</tr>
</table>



ive been trying to piece it together with some of these, but i keep getting errors, i dont know programming


tell document 1

        do JavaScript "document.getElementsByClassName('proceed')[0].click()"

    end tell

tell application "Safari"
    do JavaScript "document.getElementsByClassName(\"lnkDownloadTransactions.lightbox-launcher\").click();" in window 1
end tell

tell application "Safari"
    activate
    tell document 1
        do JavaScript "document.getElementById('button-id').click();"
    end tell
end tell