<!--#include file="asp/processpage.asp"-->
<%

	dim level_id, ranking, voters_ip
	' each form passess a query string which is the ID to edit
	level_id = Request.QueryString("level_id")

	if not isNumeric(level_id) then
		'ok, they have fudge it somehow, so kick them
		Response.Redirect("./")
		
	end if	
	
	level_id = fix(level_id)
		'this one makes sure we have a solid number
	
	'what they gave the level
	ranking = Request.Form("ranking")

	'stops cheap cheating tricks
	if Cint(ranking) > 10 then
		ranking = "0"
	elseif Cint(ranking) < 0 then
		ranking = "0"
	end if 
	
	'use this to help track of idiots
	voters_ip = Request.ServerVariables("REMOTE_ADDR")

	'Response.Write("voter_ip=["& voters_ip &"]<br>")
	'add dates like this so we don't get confused along the way
	'using getdate() as default to set the date
	'datestamp = DatePart("d",date) & " " & MonthName(DatePart("m",date),true) & " " & DatePart("yyyy",date) & " " & time
	
	dim tmp_date_now
		
		'check to see if they have vote recently
		tmp_date_now = sqldate(dateadd("d",-7,now()))
		
	set tigCON = Server.CreateObject("ADODB.Connection")
	
		tigCON.Open(DSN)
	
		'have they voted this week?
		query = "SELECT Count(voters_ip) as counted " & _
				"FROM q3a_voters " & _
				"WHERE level_id = '" & level_id & "' AND voters_ip = '" & voters_ip & "' " & _
				"and datestamp > '" & tmp_date_now & "' "  & _
				"GROUP BY voters_ip"
			
		'Response.Write("query(count)=[" & query & "]<br>")
 		
		set tigRS = tigCON.Execute(query)
			
		'here we check to see if they have voted yet
		dim can_vote
		dim vote_this_week
 		
		if (tigRS.EOF) then
			'they have not voted yet, or they have not voted 3 times yet
			can_vote = true
 		else
 			if (tigRS("counted") < 1) then
			'they have not voted yet the week
				can_vote = true
			else
				vote_this_week = true
 			end if
 		end if
 			
 		if can_vote then
 			'now check to see if they have voted more than 3 times
 			query = "SELECT Count(voters_ip) as counted " & _
					"FROM q3a_voters " & _
					"WHERE level_id = '" & level_id & "' AND voters_ip = '" & voters_ip & "' " & _
					"GROUP BY voters_ip"
				
			'Response.Write("query(count)2=[" & query & "]<br>")
 		
 			if (tigRS.EOF) then
				'they have not voted yet, or they have not voted 3 times yet
				can_vote = true
 			elseif (tigRS("counted") > 3) then
				'they have not voted yet, or they have not voted 3 times yet
				can_vote = false
			end if

 		end if		
		
		dim new_level
		
		if can_vote then
			'add this vote to the voters table
			query = "INSERT INTO q3a_voters " & _
					"(level_id, voters_ip) " & _
					"VALUES (" & level_id & ", " & _
					"'" & SQLsafe(voters_ip) & "')"
			
			'Response.Write("Query = [" & query & "]<br>")
			tigCON.Execute(query)

			'check to see if there has been a vote yet!
			query = "SELECT votes " & _
					"FROM q3a_levels " & _
					"WHERE id = '" & level_id & "'"

			'Response.Write("Query = [" & query & "]<br>")
			set tigRS = tigCON.Execute(query)
			
			'Response.Write("we are adding a new vote=[" & tigRS("votes") & "]<br>")
				
			if tigRS.EOF or IsNull(tigRS("votes")) then
				'Response.Write("we are adding a new vote<br>")
				'no votes yet for this map
				query = "UPDATE q3a_levels " & _
						"SET votes = 1, " & _
						"ranking = " & ranking  & _
						" WHERE id = '" & level_id & "'" 
			
				tigCON.Execute(query)																			
			else
				'now update the votes and ranking
				query = "UPDATE q3a_levels " & _
						"SET votes = " & _
						"((SELECT votes " & _
						"FROM q3a_levels " & _
						"WHERE id = '" & level_id & "') + 1), " & _
						"ranking = "  & _
						"((SELECT ranking "  & _
						"FROM q3a_levels "  & _
						"WHERE id = '" & level_id & "') + " & ranking & ") "  & _
						"WHERE id = '" & level_id & "'" 
			
				'Response.Write("Query = [" & query & "]<br>")
				'set tigRS to execute the query
				tigCON.Execute(query)																			
			end if
		end if
 		'close the connecttion?
 		tigRS.close		

		'Response.Write("can_vote=[" & can_vote & "]<br>")
		'Response.Write("vote_this_week=[" & vote_this_week & "]<br>")

		if can_vote then
			'Response.Write("everything is cool = [" & query & "]<br>")
			Randomize
				
			dim clearall
			clearall = Int((9) * Rnd)

			Response.Redirect("comments.asp?id=" & level_id & "&clear=" & clearall)
		else
			'they must have voted already three times
		%>
<!--#include file="asp/header.asp"-->
<!--- page title graphic in here --->
		<td width="100%" colspan="2" background="skinz/images<%=customcolours%>/top_bg2.gif" align="right" valign="top"><img src="skinz/images<%=customcolours%>/t_comments.gif" width="194" height="46" alt="[comments]"></td>
<!--#include file="asp/navbar.asp"-->
<!--- page content goes in here --->		
		<td width="100%" valign="top" class="main">
		<% if vote_this_week then %>
			Our records show that a score from the ip number - <i><%=voters_ip%></i> - 
			has been placed already this week for this map, sorry.
			<p>
			Please come back later to rank this level again.
			<p>
			If you believe this information is incorrect, get in touch with <a href="mailto:<%=tig_mail%>"><%=tig_mail%></a> and state the following:
			<ul>
				<li>the map number - <b><%=level_id%></b>
				<li>your ip number - <b><%=voters_ip%></b>
				<li>the ranking you wanted to give this map - <b><%=ranking%></b>
				<li>and that it was for <i>LvL - Q3A edition</i>
			</ul>
			From there, Tigger-oN can check the records, and make any adjustments that are needed.
			<br><br><br>
		<% else %>
			Our records show that 3 votes have been recorded for this map, from the ip number - <i><%=voters_ip%></i> - already.
			<p>
			If this is not that case, ie. <i>our records are wrong</i>, please get in touch with <a href="mailto:<%=tig_mail%>"><%=tig_mail%></a> and state the following:
			<ul>
				<li>the map number - <b><%=level_id%></b>
				<li>your ip number - <b><%=voters_ip%></b>
				<li>the ranking you wanted to give this map - <b><%=ranking%></b>
				<li>and that it was for <i>LvL - Q3A edition</i>
			</ul>
			From there, Tigger-oN can check the records, and make any adjustments that are needed.
			<br><br><br>
		<% end if %>
		</td>
<!--- end page content --->
<!--#include file="asp/footer1.asp"-->
		<%
		end if
		
		%>