<%
	Dim DBC, Command, rs, EarnUpTo, LastBatchNumber(9), Bet


	Command = Request.Querystring("Command")

	If Command = "LogOut" Then
		Session("IsAdminLogin") = False
		Response.Redirect "default.asp"
	End If

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


	LoadData

	If Command = "LogIn" Then
		Session("IsAdminLogin") = (MyEGold = Str2Num(Request.Form("EGold")) And LCase(Password) = LCase(Request.Form("Password")))
	End If



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


	Sub PageHeader()

	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">
	<meta name="author" content="http://free-sign-up.com/LowCostScripts">
	<link rel="stylesheet" href="common.css">
	</style>
	</head>
	<body bgcolor="FFCE38">
<%
	End Sub


	Sub PageBegin()
%>
	<table border="0" width="100%" cellspacing="0" cellpadding="1" bgcolor="F7E394">
	<tr>
		<td align="center">
			<img src="images/logo.gif" WIDTH="200" HEIGHT="70" alt="<%= SiteName %>">
		</td>
		<td align="right">
			<img src="images/back.gif" WIDTH="556" HEIGHT="70"></td>
	</tr>
	<tr bgcolor="white">
		<td width="200" valign="top" class="PageText" align="center" bgcolor="FFEFBF">
			<br>
			<font color="blue">
			Your Sponsor<br>
			<b><%= Session("EGG_Ref") %></b><br><br>
			</font>

			We only accept<br>
			<a href="https://www.e-gold.com/newacct/newaccount.asp?cid=<%= MyEGold %>" target="_blank">
			<img src="images/egold.gif" border="0" WIDTH="88" HEIGHT="31" alt="e-gold.com"></a>
			<br><br>
			<a href="default.asp"><img src="images/play.gif" border="0" WIDTH="180" HEIGHT="25" alt="Play"></a><br><br class="5px">
			<a href="payouts.asp"><img src="images/payouts.gif" border="0" WIDTH="180" HEIGHT="25" alt="Payouts"></a><br><br class="5px">
			<a href="javascript:window.external.AddFavorite('<%= CurrentPath %>','<%= SiteName %>')"><img src="images/bookmarkus.gif" border="0" WIDTH="180" HEIGHT="25" alt="Bookmark us"></a><br><br class="5px">
			<a href="mailto:<%= ContactEMail %>"><img src="images/contactus.gif" border="0" WIDTH="180" HEIGHT="25" alt="Contact us"></a>

			<br><br><br>
			<% Links %>
			<br>
		</td>
		<td valign="top" bgcolor="white">
<%
	End Sub


	Sub PageEnd ()
%>
		</td>
	</tr>
	<tr>
		<td colspan="2" align="center" class="PageText">
			Copyright 2004 © <a href="mailto:<%= ContactEMail %>"><%= SiteName %></a>  All Rights Reserved
		</td>
	</tr>
	</table>

	<center class="PageText">
	<br>Website development by <a href="http://free-sign-up.com/LowCostScripts/" target="_blank">Low Cost Scripts</a>
	</center>

	</body>
	</html>
<%
	End Sub


	Function IIf(cond, value1, value2)
		If cond Then IIf = value1 Else IIf = value2 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 IsNumeric(s) Then
				Str2Num = CLng(s)
			Else
				Str2Num = 0
			End If
		End If
	End Function


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


	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 LoadData()
		Dim rs, i, B

		OpenConnection

		'  ------ load last number values -----------
		Set rs = DBC.Execute("SELECT LastBatchNumber FROM LastBatchNumbers ORDER BY ID ASC")

		i = 0

		Do While Not rs.EOF
			LastBatchNumber(i) = rs("LastBatchNumber")

			i = i + 1
			rs.MoveNext
		Loop

		Set rs = DBC.Execute("SELECT MAX(LastBatchNumber) FROM LastBatchNumbers")

		EarnUpTo = rs(0)
		' ----------------------------------------------

		'  ------ load bets ------------
		Set rs = DBC.Execute("SELECT COUNT(*) FROM Bets")

		ReDim B(rs(0) - 1)


		Set rs = DBC.Execute("SELECT * FROM Bets ORDER BY Bet")

		i = 0

		Do While Not rs.EOF
			B(i) = FormatNumber(rs("Bet"), 2)

			i = i + 1
			rs.MoveNext
		Loop

		Bet = B
		' -------------------------------
	End Sub


	Sub Links()
		Dim rs, i, c, HTML

		' keshiran li e HTML ?
		If Len(Application("EGG_Links")) > 10 And DateDiff("d", Application("EGG_Links_Date"), Now) < 1 Then
			Response.Write Application("EGG_Links")
			Exit Sub
		End If


		OpenConnection

		Set rs = DBC.Execute("SELECT Title, URL FROM Links WHERE DateDiff('d', '" & Date() & "', ValidTo) >= 0")

		HTML =	"<table align=center cellpadding=5 cellspacing=1 width=180 bgcolor=BBBBBB>" & _
			"<col align=center>" & _
			"<tr bgcolor=FFEFB2><td><b>Special Links</b></td></tr>"

		c = 1

		Do While Not rs.EOF
			HTML = HTML & "<tr bgcolor=FFFAC6><td><a href='" & rs("URL") & "' target=_blank>" & rs("Title") & "</a></td></tr>"

			c = c + 1
			rs.MoveNext
		Loop

		For i = c To MaxLinks - 1
			HTML = HTML & "<tr bgcolor=FFFAC6><td><a href=" & CurrentPath & "addlink.asp>$ " & LinkCost4AMonth & " / month</a></td></tr>"
		Next

		HTML = HTML & "</table>"

		Application("EGG_Links") = HTML
		Application("EGG_Links_Date") = Now

		Response.Write 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


	' --- Admin subs and functions ------------------
	Sub UserLogin()
%>
		<form name="F" action="admin_stat.asp?Command=LogIn" method="post" onsubmit="return Validate()">
		<p align="center" class="PageText">
			E-Gold
			<input name="EGold" size="15" maxlength="20" class="ClientText">
			&nbsp;&nbsp;&nbsp;
			Password
			<input type="Password" name="Password" size="15" maxlength="20" class="ClientText">
			&nbsp;
			<input type="submit" value="Log In" class="Button">
		</p>
		</form>

		<script language="JavaScript">
		<!--
		function Validate()
		{
			if (document.F.EGold.value.length == 0)
			{
				alert ('Please enter your E-Gold number.');
				document.F.EGold.focus();
				return false;
			}

			if (document.F.Password.value.length == 0)
			{
				alert ('Please enter your password.');
				document.F.Password.focus();
				return false;
			}

			return true;
		}
		//-->
		</script>

		<script language="JavaScript" for="window" event="onload">
		<!--
		document.F.EGold.focus();
		//-->
		</script>
<%
	End Sub


	Sub AdminMenu()
%>
	<table border=0 width="100%" cellspacing="0" cellpadding="2" bgcolor="006699">
	<tr bgcolor="F7E394">
		<td width=250>
			&nbsp;
		</td>
		<td align="center">
			<img src="images/logo.gif" width="200" height="70" alt="<%= SiteName %>"></td>
		<td width=250 valign=bottom align=right>
			<a href="http://free-sign-up.com/LowCostScripts/" target=_blank>Low Cost Scripts</a>
		</td>
	</tr>
	<tr bgcolor="white">
		<td align="center" colspan=3>
			<a href="admin_bet.asp">Manage Bets</a>&nbsp;|&nbsp;
			<a href="admin_batchnumber.asp">Manage Last Batch Numbers</a>&nbsp;|&nbsp;
			<a href="admin_links.asp">Manage Links</a>&nbsp;|&nbsp;
			<a href="admin_payments.asp">Send Money</a>&nbsp;|&nbsp;
			<a href="admin_stat.asp">Statistics</a>&nbsp;|&nbsp;
			<a href="default.asp?Command=LogOut">Log Out</a>
		</td>
	</tr>
	</table>
<%
	End Sub
%>