<% Option Explicit %>
<!--#include file="config.asp"-->
<!--#include file="common.asp"-->
<!--#include file="md5.asp"-->
<%
	Dim TotalIncome, TotalPayouts, MyCommissions, ReferralCommissions, ReferralCommissionsForUnpaidPlayers, Balance

	If Len(Request.Form.Item) > 0 Then
		If IsValidPayment Then
			ReceivePayment
		End If
	End If

	CheckForPayouts


	PageHeader
%>
	<table border="0" width="100%" cellspacing="0" cellpadding="8">
	<tr valign=top class=PageText>
		<td>
<%
			If Request("Payment") <> "Continue" Then
				Welcome
			End If

			Menu
			Response.Write "<br><br>"
			SpendNow
%>
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center" class="PageText">
		<hr noshade color="black" size="1">
		Copyright 2003 © <%= SiteName %>  All Rights Reserved
		</td>
	</tr>
	</table>

	</body>
	</html>
<%


	Sub SpendNow()
%>
		The payouts are not automatically done !!!<br><br>
		<form action="https://www.e-gold.com/sci_asp/payments.asp" method=post>
		<table border=0 align=center cellspacing=6>
		<tr class=PageText>
			<td>
				Amount
			</td>
			<td>
				<select name=PAYMENT_AMOUNT>
<%
				For i = 0 To UBound(Spend)
					If i = UBound(Spend) \ 2 Then
						Response.Write "<option value=" & Spend(i) & " selected>$" & Spend(i) & "</option>" & vbNewLine
					Else
						Response.Write "<option value=" & Spend(i) & ">$" & Spend(i) & "</option>" & vbNewLine
					End If
				Next
%>
				</select>
			</td>
		</tr>
		<tr class=PageText>
			<td>
				Link's Title
			</td>
			<td>
				<input name=Title size=30 maxlength=40>
			</td>
		</tr>
		<tr class=PageText>
			<td>
				Link's URL
			</td>
			<td>
				<input name=Link value="http://" size=30 maxlength=150>
			</td>
		</tr>
		<tr class=PageText>
			<td>
				Referral
			</td>
			<td>
<%
				If Session("PL_Ref") = 0 Then
					Response.Write MyEGold
				Else
					Response.Write Session("PL_Ref")
				End If
%>
			</td>
		</tr>
		<tr>
			<td colspan=2 align=center>
				<input type=hidden name=PAYEE_ACCOUNT value="<%= MyEGold %>">
				<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="<%= CurrentPath %>default.asp">
				<input type=hidden name=NOPAYMENT_URL value="<%= CurrentPath %>default.asp">
				<input type=hidden name=SUGGESTED_MEMO value="<%= SiteName %> invest">
				<input type=hidden name=BAGGAGE_FIELDS value="Link Title Referral">
				<input type=hidden name=Referral value="<%= Session("PL_Ref") %>">
				<input type=submit name=PAYMENT_METHOD value=Spend class=Button>
			</td>
		</tr>
		</table>
		</form>
<%
	End Sub


	Sub Welcome()
%>
		<font color=green size=7><b><%= SiteName %></b></font><br><br>
<%
	End Sub


	Sub ReceivePayment()
		Dim PAYMENT_AMOUNG, BatchNumber, PAYER_ACCOUNT, Referral, rsPayment, Link, Title

		BatchNumber		= Str2Num(Request.Form("PAYMENT_BATCH_NUM"))

		OpenConnection

		' ---- is payment recorded -------------
		Set rsPayment = DBC.Execute("SELECT COUNT(*) FROM PayIns WHERE BatchNumber = " & BatchNumber)

		If rsPayment(0) > 0 Then
			Exit Sub
		End If
		' --------------------------------------

		PAYMENT_AMOUNG	= Str2Float(Request.Form("PAYMENT_AMOUNT"))
		PAYER_ACCOUNT	= Str2Num(Request.Form("PAYER_ACCOUNT"))
		Referral		= Str2Num(Request.Form("Referral"))
		Link			= RequestForm("Link")
		Title			= RequestForm("Title")


		If Referral = 0 Then
			Referral = 528194
		End If


		' add payment
		DBC.Execute("INSERT INTO PayIns ([From], Cash, BatchNumber, ReferralEGold, Title, Link) " & _
			"VALUES (" &	PAYER_ACCOUNT		& "," & _
					PAYMENT_AMOUNG		& "," & _
					BatchNumber		& "," & _
					Referral		& "," & _
					EscapeSQLData(Title)	& "," & _
					EscapeSQLData(Link)	& ")")

	End Sub


	Sub CheckForPayouts()

		Dim rs

		OpenConnection

		Set rs = DBC.Execute("SELECT SUM(Cash) FROM PayIns")
		TotalIncome = Str2Float(rs(0))

		Set rs = DBC.Execute("SELECT SUM(Cash) FROM PayOuts")
		TotalPayouts = Str2Float(rs(0))

'		Set rs = DBC.Execute("SELECT SUM(Cash) FROM PayIns WHERE Paid = False")
'		ReferralCommissionsForUnpaidPlayers = Str2Float(rs(0)) * ReferralCommission / 100

		MyCommissions		= TotalIncome * MyCommission / 100
		ReferralCommissions	= TotalIncome * ReferralCommission / 100
		Balance				= TotalIncome - TotalPayouts - MyCommissions' - ReferralCommissions


		'Response.Write "TotalIncome: $" & TotalIncome & "<BR>"
		'Response.Write "TotalPayouts: $" & TotalPayouts & "<BR>"
		'Response.Write "MyCommissions: $" & MyCommissions & "<BR>"
'		Response.Write "ReferralCommissions: $" & ReferralCommissions & "<BR>"
'		Response.Write "ReferralCommissionsForUnpaidPlayers: $" & ReferralCommissionsForUnpaidPlayers & "<BR>"

		'Response.Write "Balance: $" & Balance & "<BR>"



		Set rs = DBC.Execute("SELECT *, Cash + Cash * " & PlayerEarnings & " / 100 AS [CashPlusWin] " & _
								"FROM PayIns WHERE Paid = False")

		Do While Not rs.EOF
			If (rs("CashPlusWin") + rs("Cash") * ReferralCommission / 100) <= Balance Then
				' update na PayIns pole, Paid
				DBC.Execute("UPDATE PayIns SET Paid = True WHERE ID = " & rs("ID"))

				' insert v PayOuts pole, From i Cash
				DBC.Execute("INSERT INTO PayOuts (To, Cash) VALUES (" & rs("From") & ", " & rs("CashPlusWin") & ")")

				' insert v PayOuts i referral-a
				DBC.Execute("INSERT INTO PayOuts (To, Cash, IsReferral) VALUES (" & rs("ReferralEGold") & ", " & rs("Cash") * ReferralCommission / 100 & ", True)")


				'Response.Write rs("CashPlusWin") & " " & Balance & "<br>"

				Balance = Balance - rs("CashPlusWin")
				'Response.Write "Balance: "& Balance & "<br>"
			Else
				Exit Do
			End If

			rs.MoveNext
		Loop
	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


	Function RequestForm(param)

		Dim x, qs
		Dim Separator	:	Separator = "&"

		qs = Request.Form.Item

		If Len(param) = 0 Then
			RequestForm = ""
			Exit Function
		End If

		If InStr(qs, "?" & param & "=") > 0 Then
			x = Right(qs, Len(qs) - InStr(qs, "?" & param & "=") - Len(param) - 1)

			If InStr(x, Separator) > 0 Then
				x = Left(x, InStr(x, Separator) - 1)
			End If
		End If

		If InStr(qs, Separator & param & "=") > 0 Then
			x = Right(qs, Len(qs) - InStr(qs, Separator & param & "=") - Len(param) - 1)

			If InStr(x, Separator) > 0 Then
				x = Left(x, InStr(x, Separator) - 1)
			End If
		End If

		RequestForm = x

	End Function
%>