<%
	Dim DBC, MyEGold, SiteName, ContactEMail, ReferralCommission, MyCommission, PlayerEarnings
	Dim AltPass, rsCommon, Spend, i

'Application("PL_MyEGold") = 0
	If Str2Num(Application("PL_MyEGold")) = 0 Then
		OpenConnection

		Set rsCommon = DBC.Execute("SELECT * FROM Admin")

		If Not rsCommon.EOF Then
			Application.Lock
			Application("PL_SiteName")		= rsCommon("SiteName")
			Application("PL_MyEGold")		= rsCommon("MyEGold")
			Application("PL_ContactEMail")		= rsCommon("ContactEMail")
			Application("PL_ReferralCommission")	= rsCommon("ReferralCommission")
			Application("PL_AltPass")		= rsCommon("AltPass")
			Application("PL_PlayerEarnings")	= rsCommon("PlayerEarnings")
			Application("PL_MyCommission")		= rsCommon("MyCommission")
			Application.Unlock
		End If

		'  ------ load spends ------------
		Set rsCommon = DBC.Execute("SELECT COUNT(*) FROM Spends")

		ReDim Spend(rsCommon(0) - 1)

		Set rsCommon = DBC.Execute("SELECT * FROM Spends ORDER BY Spend")

		i = 0

		Do While Not rsCommon.EOF
			Spend(i) = FormatNumber(rsCommon("Spend"), 2)

			i = i + 1
			rsCommon.MoveNext
		Loop

		Application.Lock
		Application("PL_Spend") = Spend
		Application.Unlock
		' -------------------------------

		'  ------ load stats ------------
		Set rsCommon = DBC.Execute("SELECT SUM(Cash) FROM PayOuts WHERE BatchNumber <> 0")

		Application.Lock
		Application("PL_TotalPayouts") = FormatNumber(Str2Float(rsCommon(0)), 2)
		Application.Unlock
		' -------------------------------
	End If

	SiteName		= Application("PL_SiteName")
	MyEGold			= Application("PL_MyEGold")
	ContactEMail		= Application("PL_ContactEMail")
	ReferralCommission	= Application("PL_ReferralCommission")
	AltPass			= Application("PL_AltPass")
	PlayerEarnings		= Application("PL_PlayerEarnings")
	MyCommission		= Application("PL_MyCommission")
	Spend			= Application("PL_Spend")

	If Str2Num(Application("PL_NeedToBurst")) = 0 Then
		CheckForPayouts
	End If

	If Not IsEmpty(Request.QueryString("Ref")) Then
		Session("PL_Ref") = Str2Num(Request.QueryString("Ref"))
	End If



	Sub OpenConnection()
		Set DBC = Server.CreateObject("ADODB.Connection")
		DBC.Open "DBQ=" & DataBaseFileName & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;", "", "pass"
	End Sub


	Sub PageHeader()

	Server.ScriptTimeout = 180	' seconds

	Response.Expires = -1
%>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
	<html>
	<head>
	<title><%= SiteName %></title>
	<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
	<link rel=stylesheet href=common.css>
	</style>
	</head>
	<body bgcolor=AADDFF>
<%
	End Sub


	Function IIf(cond, value1, value2)
		If cond Then IIf = value1 Else IIf = value2 End If
	End Function


	Function HTMLEncode(s)

		If IsNull(s) Then
			HTMLEncode = ""
		Else
			HTMLEncode = Server.HTMLEncode(s)
		End If

	End Function


	Function EscapeSQLData(s)

		If IsNull(s) Then
			EscapeSQLData = "''"
		Else
			EscapeSQLData = "'" & Replace(s, "'", "''") & "'"
		End If

	End Function


	Function Str2Num(s)

		If IsNull(s) Then
			Str2Num = 0
		Else
			If Not IsNumeric(s) Then
				Str2Num = 0
			Else
				Str2Num = CLng(s)
			End If
		End If

	End Function


	Function Str2Float(s)

		If IsNull(s) Then
			Str2Float = 0
		Else
			If Not IsNumeric(s) Then
				Str2Float = 0
			Else
				Str2Float = CDbl(s)
			End If
		End If

	End Function


	Sub GenerateOption(rs, OptValue, OptText, CurValue)

		Dim sel

		If IsNull(rs) Then
			sel = IIf(OptValue = CurValue, " selected", "")

			Response.Write "<option value='" & HTMLEncode(OptValue) & "'" & sel & ">" & OptText & vbNewLine
		Else
			If (rs.EOF) Or (rs.BOF) Then Exit Sub

			rs.MoveFirst

			Do While Not rs.EOF
				sel = IIf(rs(OptValue).Value = CurValue, " selected", "")

				Response.Write "<option value='" & HTMLEncode(rs(OptValue).Value) & "'" & sel & ">" & rs(OptText).Value & vbNewLine

				rs.MoveNext
			Loop
		End If

	End Sub


	Function CurrentPath()
		Dim s
		s = Request.ServerVariables("SCRIPT_NAME")

		s = Left(s, InStrRev(s, "/"))

		CurrentPath = "http://" & Request.ServerVariables("SERVER_NAME") & s
	End Function


	Function CurrentFile()
		CurrentFile = "http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("SCRIPT_NAME")
	End Function


	Sub Menu()
%>
	<table border=0 align=center>
	<tr>
		<td>
			<a href=default.asp><font size=3>Home</font></a>
		</td>
		<td width=30></td>
		<td>
			<a href=payouts.asp><font size=3>Payouts</font></a>
		</td>
		<td width=30></td>
		<td>
			<a href=spend.asp><font size=3>Make a spend</font></a>
		</td>
		<td width=30></td>
		<td>
			<a href="javascript:window.external.AddFavorite('<%= CurrentPath %>','<%= SiteName %> - Receive <%= 100 + PlayerEarnings %>% of your spend')"><font size=3>Bookmark us</font></a>
		</td>
		<td width=30></td>
		<td>
			<a href="mailto:<%= ContactEMail %>?subject=<%= SiteName %>"><font size=3>Contact us</font></a>
		</td>
	</tr>
	</table>
<%
	End Sub


	Sub CheckForPayouts()

		Dim rs2, TotalIncome2, TotalPayouts2, MyCommissions2, Balance2

		OpenConnection

		Set rs2 = DBC.Execute("SELECT SUM(Cash) FROM PayIns")
		TotalIncome2	= Str2Float(rs2(0))


		Set rs2 = DBC.Execute("SELECT SUM(Cash), COUNT(*) FROM PayOuts")
		TotalPayouts2	= Str2Float(rs2(0))

		Set rs2 = DBC.Execute("SELECT SUM(Cash) FROM PayIns WHERE Paid = True")
		MyCommissions2	= Str2Float(rs2(0)) * MyCommission / 100


		Balance2 = TotalIncome2 - TotalPayouts2 - MyCommissions2

		'Response.Write "Balance: " & Balance2 & "<br>"

		Set rs2 = DBC.Execute("SELECT * FROM PayIns WHERE Paid = False")

		Do While Not rs2.EOF
			If rs2("Cash") + rs2("Cash") * (PlayerEarnings / 100) + rs2("Cash") * (ReferralCommission / 100) + rs2("Cash") * (MyCommission / 100) <= Balance2 Then
				'Response.Write rs2("Cash") + rs2("Cash") * (PlayerEarnings / 100) & " " & rs2("Cash") * (ReferralCommission / 100) & " " & rs2("Cash") * (MyCommission / 100) & " = "
				'Response.Write rs2("Cash") + rs2("Cash") * (PlayerEarnings / 100) + rs2("Cash") * (ReferralCommission / 100) + rs2("Cash") * (MyCommission / 100) & "<BR>"

				' update na PayIns pole, Paid
				DBC.Execute("UPDATE PayIns SET Paid = True WHERE ID = " & rs2("ID"))

				' insert v PayOuts pole, From i Payout
				DBC.Execute("INSERT INTO PayOuts (To, Cash) VALUES (" & rs2("From") & ", " & rs2("Cash") + rs2("Cash") * PlayerEarnings / 100 & ")")

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

				'Response.Write "Balance: " & Balance2 & " - " & rs2("Cash") + rs2("Cash") * (ReferralCommission / 100) + rs2("Cash") * (MyCommission / 100) & "<BR>"
				Balance2 = Balance2 - (rs2("Cash") + rs2("Cash") * (PlayerEarnings / 100) + rs2("Cash") * ReferralCommission / 100 + rs2("Cash") * MyCommission / 100)
				'Response.Write "Balance: " & Balance2 & "<BR><BR>"
			Else
				Application.Lock
				Application("PL_NeedToBurst") = rs2("Cash") + rs2("Cash") * PlayerEarnings / 100 + rs2("Cash") * ReferralCommission / 100 + rs2("Cash") * MyCommission / 100 - Balance2
				Application.Unlock

				Exit Do
			End If

			rs2.MoveNext
		Loop
	End Sub
%>