<% Option Explicit %>
<!--#include file="config.asp"-->
<!--#include file="common.asp"-->
<%
	Dim rs, Password, EGold, SponsorEGold

	Command = Request.Form("Command")


	PageBegin
	PageContent
	PageEnd


	Sub PageContent()

	OpenConnection

	If Command = "Register" Then
		EGold		= Str2Num(Request.Form("EGold"))
		SponsorEGold	= Str2Num(Request.Form("SponsorEGold"))

		If MemberID = 0 Then	' new user
			Set rs = DBC.Execute("SELECT COUNT(*) FROM Members WHERE EGold = " & EGold)

			If rs(0) > 0 Then
				' EGold already exists
%>
				<p class=PageText>
					<br><font color=red><b>Sorry, Your e-gold account is already in our database!</b></font>
				</p>
<%
			Else
				' dali sponsora e nash chlen
				Set rs = DBC.Execute("SELECT COUNT(*) FROM Members WHERE EGold = " & SponsorEGold)

				If rs(0) = 0 Then
					' sponsora ne e nash chlen
					SponsorEGold = 0
				End If

				If SponsorEGold = 0 Then
					SponsorEGold = GetGoldMemberEGoldWithMinReferrals
				End If

				' member not exists -> insert it
				DBC.Execute("INSERT INTO Members " & _
					"(EGold, Password, SponsorEGold) " & _
					"VALUES (" &	EGold					& "," & _
									EscapeSQLData(Request.Form("Password"))	& "," & _
									SponsorEGold			& ")")
%>
				<br><br>Thank you for becoming a <%= SiteName %> Member!<br><br>
				Use this URL to refer new members and advertisers<br>
				<b><%= CurrentPath %>default.asp?sponsor=<%= EGold %></b><br><br>
				<a href=log.asp>Log In</a><br><br><br><br><br>
<%
				Application.Lock
				Application(Prefix & "TotalMembers") = Str2Num(Application(Prefix & "TotalMembers")) + 1
				Application.Unlock
			End If
		End If
	Else
		Set rs = DBC.Execute("SELECT * FROM Members WHERE MemberID = " & MemberID)

		If rs.EOF Then
			MemberID	= 0
			Password		= ""
			EGold		= ""
			SponsorEGold	= 0

			AddMember
		End If
	End If

	End Sub


	Sub AddMember()
%>
	<script language=JavaScript>
	<!--
	function AddMember()
	{
		if (document.F.EGold.value == '')
		{
			alert ('Please enter your E-Gold account number.');
			document.F.EGold.focus();
			return false;
		}
		else
		{
			if (isNaN(document.F.EGold.value))
			{
				alert ('Please enter valid E-Gold account number.');
				document.F.EGold.focus();
				return false;
			}
		}

		var Pass1 = document.F.Password.value;
		var Pass2 = document.F.Password2.value;

		if (! (Pass1.indexOf(Pass2) == 0 && Pass1.length == Pass2.length))
		{
			alert ('The passwords do not match!');
			document.F.Password.value = "";
			document.F.Password2.value = "";
			document.F.Password.focus();
			return false;
		}

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

	<form name=F method=post onsubmit="return AddMember()">

	<table cellpadding=2 cellspacing=1>
	<col width=200>
	<col>
	<tr class=Title>
		<td align=center colspan=2>
			New Member<br><br class=HalfBreak>
		</td>
	</tr>
	<tr class=Odd>
		<td align=right>Your E-Gold</td>
		<td>
			<input name=EGold size=15 maxlength=15 class=ClientText>
			<a href=https://www.e-gold.com/newacct/newaccount.asp?cid=<%= MyEGold %> target=_blank>open an account for free</a>
		</td>
	</tr>
	<tr class=Odd>
		<td align=right>Choose Password</td>
		<td>
			<input type=password name=Password size=15 maxlength=15 class=ClientText>
			don't use your e-gold password (for your security !)
		</td>
	</tr>
	<tr class=Odd>
		<td align=right>Retype Password</td>
		<td>
			<input type=password name=Password2 size=15 maxlength=15 class=ClientText>
		</td>
	</tr>
	<tr class=Odd>
		<td align=right>Your Sponsor</td>
		<td>
			<%= Session(Prefix & "SponsorEGold") %>
			<input type=hidden name=SponsorEGold value='<%= Session(Prefix & "SponsorEGold") %>'>
		</td>
	</tr>
	<tr class=Odd>
		<td valign=top colspan=2 align=center>
			<br>
			<b>By clicking "Register" you accept all our <a href=terms.asp target=_blank>Terms and Conditions</a></b><br><br>
			<input type=submit name=Command value=Register class=Button>
		</td>
	</tr>
	</form>
	</table>
<%
	End Sub


	Function GetGoldMemberEGoldWithMinReferrals()
		Dim rsGold
		Set rsGold = DBC.Execute("SELECT TOP 1 GM.EGold, COUNT(M.EGold) " & _
					"FROM Members AS M RIGHT JOIN GoldMembers AS GM ON M.SponsorEGold = GM.EGold " & _
					"GROUP BY GM.EGold " & _
					"ORDER BY COUNT(M.EGold)")

		If rsGold.EOF Then
			GetGoldMemberEGoldWithMinReferrals = 0
		Else
			GetGoldMemberEGoldWithMinReferrals = rsGold("EGold")
		End If
	End Function
%>