<% Option Explicit %>
<!--#include file="config.asp"-->
<!--#include file="common.asp"-->
<!--#include file="adm_admin.asp"-->
<!--#include file="md5.asp"-->
<%
	Dim rs, ID, BatchNumber

	ID		= Str2Num(Request.Querystring("ID"))
	Command	= Request.Querystring("Command")


	OpenConnection

	If Command = "Send" Then
		Send
		Response.End
	End If

	If Len(Request.Form.Item) > 0 Then
'		If IsValidPayment Then
			ID = Str2Num(Request("ID"))

			BatchNumber = Str2Num(Request("PAYMENT_BATCH_NUM"))

			Set rs = DBC.Execute("SELECT COUNT(*) FROM PayOuts WHERE BatchNumber = " & BatchNumber)

			If rs(0) = 0 Then
				DBC.Execute("UPDATE PayOuts SET BatchNumber = " & BatchNumber & " WHERE ID = " & ID)

				Application.Lock
				Application("PL_TotalPayouts") = FormatNumber(Str2Float(Application("PL_TotalPayouts")) + Str2Float(Request("PAYMENT_AMOUNT")), 2)
				Application.Unlock
			End If
'		End If
	End If



	PageHeader

	If Session("IsAdminLogin") Then
		PageMenu
	Else
		PageLogin
		Response.End
	End If


	WaitingPayoutsList

	Response.Write "</body>"
	Response.Write "</html>"


	Sub WaitingPayoutsList()

		Dim i, rsCount, Page

		Dim PageSize	:	PageSize	= 4
		Dim PageCount	:	PageCount	= 0
		Dim Pos			:	Pos			= 0


		Set rsCount = DBC.Execute("SELECT COUNT(*) FROM PayOuts WHERE BatchNumber = 0 AND To <> " & MyEGold)

		If rsCount(0) > PageSize Then
			PageCount = rsCount(0) \ PageSize
			If rsCount(0) - (PageCount * PageSize) > 0 Then
				PageCount = PageCount + 1
			End If
		Else
			PageCount = 1
			Pos = 0
		End If


		Set rs = DBC.Execute("SELECT * FROM PayOuts WHERE BatchNumber = 0 AND To <> " & MyEGold & " ORDER BY ID")

		Page = Str2Num(Request.QueryString("page"))

		If Page < 1 Then
			Page = 1
		End If
		If Page > PageCount Then
			Page = PageCount
		End If

		If Pos = 0 Then
			Pos = (Page - 1) * PageSize
		End If
		If Pos < 0 Then
			Pos = 0
		End If

		If Not rs.EOF Then
			rs.Move Pos
		End If
%>
		<br>
		<table border=0 align=center cellspacing=1 cellpadding=3 width=250>
		<tr class=PageText>
			<td>
				Waiting payments: <b><%= rsCount(0) %></b>
			</td>
			<td align=right>
				Page <%= Page & " of " & PageCount %>
			</td>
		</tr>
		</table>

		<table border=0 align=center bgcolor=006699 cellspacing=1 cellpadding=3 width=250>
		<col>
		<col>
		<col align=right>
		<col align=center>
		<tr bgcolor=006699 class=PageText>
			<td align=center>
				<font color=white>Payout ID</font>
			</td>
			<td align=center>
				<font color=white>E-Gold</font>
			</td>
			<td align=center>
				<font color=white>Payout</font>
			</td>
			<td>
				&nbsp;
			</td>
		</tr>
<%
		i = 0

		Do While (Not rs.EOF) And (i < PageSize)
%>
		<tr bgcolor=<%= IIf(i Mod 2 = 1, "EEEEEE", "white") %> class=PageText>
			<td><%= rs("ID") %></td>
			<td><%= rs("To") %></td>
			<td>$ <%= FormatNumber(rs("Cash"), 3) %></td>
			<td><a href=adm_payments.asp?Command=Send&ID=<%= rs("ID") %>>Send</a></td>
		</tr>
<%
			i = i + 1
			rs.MoveNext
		Loop
%>
		</table>

		<p align=center class=PageText>
<%
		If rsCount(0) >= PageSize Then
			If Page > 1 Then
				Response.Write "<a href=adm_payments.asp?page=1>First</a>&nbsp;&nbsp;&nbsp;"
				Response.Write "<a href=adm_payments.asp?page=" & Page - 1 & ">Previous</a>&nbsp;&nbsp;&nbsp;"
			Else
				Response.Write "First&nbsp;&nbsp;&nbsp;"
				Response.Write "Previous&nbsp;&nbsp;&nbsp;"
			End If

			If Page < PageCount Then
				Response.Write "<a href=adm_payments.asp?page=" & Page + 1 & ">Next</a>&nbsp;&nbsp;&nbsp;"
				Response.Write "<a href=adm_payments.asp?page=" & PageCount & ">Last</a>"
			Else
				Response.Write "Next&nbsp;&nbsp;&nbsp;"
				Response.Write "Last"
			End If
		End If
%>
		</p>
<%
	End Sub


	Sub Send()

		Set rs = DBC.Execute("SELECT * FROM PayOuts WHERE ID = " & ID)

		If rs.EOF Then
			Response.Write "Error."
			Exit Sub
		End If

		Response.Expires = -1
%>
		<html>
		<body onload="document.forms[0].submit()">
		<form action="https://www.e-gold.com/sci_asp/payments.asp" method=POST>
		<input type=hidden name=FORCED_PAYER_ACCOUNT value=<%= MyEGold %>>
		<input type=hidden name=PAYEE_ACCOUNT value=<%= rs("To") %>>
		<input type=hidden name=PAYEE_NAME value="<%= SiteName %>">
		<input type=hidden name=PAYMENT_UNITS value=1>
		<input type=hidden name=PAYMENT_METAL_ID value=1>
		<input type=hidden name=STATUS_URL value="<%= CurrentFile %>">
		<input type=hidden name=PAYMENT_URL value="<%= CurrentFile %>">
		<input type=hidden name=NOPAYMENT_URL value="<%= CurrentFile %>">
		<input type=hidden name=PAYMENT_AMOUNT value="<%= rs("Cash") %>" maxlength=30>	
<%
		If rs("IsReferral") Then
			Response.Write "<input type=hidden name=SUGGESTED_MEMO value='" & ReferralCommission & "% referral commission at " & SiteName & "'>"
		Else
			Response.Write "<input type=hidden name=SUGGESTED_MEMO value='" & 100 + PlayerEarnings & "% of your invest at " & SiteName & "'>"
		End If
%>
		<input type=hidden name=BAGGAGE_FIELDS value=ID>
		<input type=hidden name=ID value=<%= ID %>>
		<input type=hidden name=PAYMENT_METHOD value=Send>
		</body>
		</html>
<%
	End Sub


	Function IsValidPayment()
		Dim MD, MD5String

		Set MD = New MD5

		MD5String =	Request("PAYMENT_ID")				& ":" & _
					Request("PAYEE_ACCOUNT")			& ":" & _
					Request("PAYMENT_AMOUNT")			& ":" & _
					Request("PAYMENT_UNITS")			& ":" & _
					Request("PAYMENT_METAL_ID")			& ":" & _
					Request("PAYMENT_BATCH_NUM")		& ":" & _
					Request("PAYER_ACCOUNT")			& ":" & _
					MD.DigestStrToHexStr(AltPass)		& ":" & _
					Request("ACTUAL_PAYMENT_OUNCES")	& ":" & _
					Request("USD_PER_OUNCE")			& ":" & _
					Request("FEEWEIGHT")				& ":" & _
					Request("TIMESTAMPGMT")

		IsValidPayment = (MD.DigestStrToHexStr(MD5String) = Request("V2_HASH"))

	End Function
%>