<% Option Explicit %>
<!--#include file="../config.asp"-->
<!--#include file="../common.asp"-->
<!--#include file="main.asp"-->
<%
	Dim rs, FilterMember, FilterSponsor, FilterMemberType, WHERE_Clause

	PageHeader

	If Not Session("IsAdminLogin") Then
		PageLogin
		Response.End
	End If

	' ----- WHERE clause ------------------------
	FilterMember	= Request.Form("FilterMember")
	FilterSponsor	= Request.Form("FilterSponsor")
	FilterMemberType	= Request.Form("FilterMemberType")

	WHERE_Clause = ""

	If Str2Num(FilterMember) > 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND EGold = " & FilterMember & " "
		Else
			WHERE_Clause = WHERE_Clause & "EGold = " & FilterMember & " "
		End If
	End If

	If Str2Num(FilterSponsor) > 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND SponsorEGold = " & FilterSponsor & " "
		Else
			WHERE_Clause = WHERE_Clause & "SponsorEGold = " & FilterSponsor & " "
		End If
	End If

	If FilterMemberType <> "" Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND IsGold = " & FilterMemberType
		Else
			WHERE_Clause = WHERE_Clause & "IsGold = " & FilterMemberType
		End If
	End If

	If Len(WHERE_Clause) > 0 Then
		WHERE_Clause = " WHERE " & WHERE_Clause
	End If
	' -------------------------------------------


	OpenConnection

	If Request.Querystring("Command") = "Delete" Then
		Set rs = DBC.Execute("SELECT EGold FROM Members WHERE MemberID = " & Str2Num(Request.Querystring("MemberID")))

		DBC.Execute("DELETE FROM History WHERE MemberID = " & Str2Num(Request.Querystring("MemberID")))
		DBC.Execute("UPDATE Members SET SponsorEGold = 0 WHERE SponsorEGold = " & rs("EGold"))
		DBC.Execute("DELETE FROM Members WHERE MemberID = " & Str2Num(Request.Querystring("MemberID")))

		rs.Close

		Application.Lock
		Application(Prefix & "MyEGold") = 0
		Application.Unlock
	End If

	AdminPageBegin "Members List"
	MembersList
	AdminPageEnd



	Sub MembersList()
		Dim rs, rsCount, i, Index

		Dim PageSize	:	PageSize		= 10
		Dim PageCount	:	PageCount	= 0
		Dim Pos		:	Pos		= 0


		Set rsCount = DBC.Execute("SELECT COUNT(*) FROM Members" & WHERE_Clause)

		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 Members " & WHERE_Clause & " ORDER BY MemberID DESC")

		Session(Prefix & "MemberList_Page") = Str2Num(Request.Form("page"))

		If Session(Prefix & "MemberList_Page") < 1 Then
			Session(Prefix & "MemberList_Page") = 1
		End If
		If Session(Prefix & "MemberList_Page") > PageCount Then
			Session(Prefix & "MemberList_Page") = PageCount
		End If

		If Pos = 0 Then
			Pos = (Session(Prefix & "MemberList_Page") - 1) * PageSize
		End If

		If Pos < 0 Then
			Pos = 0
		End If

		If Not rs.EOF Then
			rs.Move Pos
		End If
%>
		<form name=F action="member_list.asp" method=POST>
		<input type=hidden name=Page value="<%= Session(Prefix & "MemberList_Page") %>">

		<table border=0 align=center bgcolor=white width=100% cellspacing=0 cellpadding=0>
		<tr valign=top>
			<td>
				<% Filter %>
			</td>
			<td>
				<table border=0 align=center bgcolor=black width=500 cellspacing=1 cellpadding=3>
				<col>
				<col>
				<col>
				<col align=right>
				<col align=right>
				<col align=center>
				<col>
				<tr class=SelectedParent>
					<td>&nbsp;</td>
					<td align=center>Member</td>
					<td align=center>Type</td>
					<td align=center>Sponsor</td>
					<td align=center>Cash</td>
					<td align=center>Last IP</td>
					<td>&nbsp;</td>
				</tr>
<%
				i = 0

				Index = rsCount(0) - ((Session(Prefix & "MemberList_Page") - 1) * PageSize) - 1

				Do While (Not rs.EOF) And (i < PageSize)
%>
					<tr class=Odd>
						<td><%= Index - i + 1 %>.</td>
						<td><%= rs("EGold") %></td>
						<td align=center><%= IIf(rs("IsGold"), "<font color=FFAA88><b>GOLD</b></font>", "FREE") %></td>
						<td><%= rs("SponsorEGold") %></td>
						<td align=right>$<%= FormatNumber(rs("Cash"), 3) %></td>
						<td align=right><%= rs("LastIP") %></td>
						<td align=center>
							<a href="member_edit.asp?MemberID=<%= rs("MemberID") %>">Edit</a>&nbsp;
							<a href="javascript: Delete(<%= rs("MemberID") %>)">Delete</a>
						</td>
					</tr>
<%
					i = i + 1
					rs.MoveNext
				Loop
%>
				</table>

				<p align=center class=PageText>
<%
				If rsCount(0) >= PageSize Then
					If Session(Prefix & "MemberList_Page") > 1 Then
						Response.Write "<a href='javascript: Page(1)'>First</a>&nbsp;&nbsp;&nbsp;"
						Response.Write "<a href='javascript: Page(" & Session(Prefix & "MemberList_Page")-1 & ")'>Previous</a>&nbsp;&nbsp;&nbsp;"
					Else
						Response.Write "First&nbsp;&nbsp;&nbsp;"
						Response.Write "Previous&nbsp;&nbsp;&nbsp;"
					End If

					If Session(Prefix & "MemberList_Page") < PageCount Then
						Response.Write "<a href='javascript: Page(" & Session(Prefix & "MemberList_Page")+1 & ")'>Next</a>&nbsp;&nbsp;&nbsp;"
						Response.Write "<a href='javascript: Page(" & PageCount & ")'>Last</a>"
					Else
						Response.Write "Next&nbsp;&nbsp;&nbsp;"
						Response.Write "Last"
					End If
				End If
%>
				</p>
			</td>
		</tr>
		</table>

		</form>

		<script language=JavaScript>
		<!--
		function Page(PageN)
		{
			document.F.Page.value = PageN;
			document.F.submit();
		}

		function Delete(ID)
		{
			if (confirm("This record will be delete. Do you want to proceed?"))
				document.location = "member_list.asp?Command=Delete&MemberID=" + ID;
		}
		//-->
		</script>
<%
	End Sub


	Sub Filter()
%>
	<table border=0 align=center width=110 bgcolor=black cellspacing=1 cellpadding=3>
	<tr class=SelectedParent>
		<td align=center>Filter</td>
	</tr>
	<tr class=Odd>
		<td>
			Member<br>
			<input name=FilterMember value="<%= FilterMember %>"size=11 maxlength=11>
			<br><br class=HalfBreak>
			Sponsor<br>
			<input name=FilterSponsor value="<%= FilterSponsor %>" size=11 maxlength=11>
			<br><br class=HalfBreak>
			Type<br>
			<select name=FilterMemberType>
<%
			GenerateOption "",		"never mind",	FilterMemberType
			GenerateOption "false",	"FREE",			FilterMemberType
			GenerateOption "true",	"GOLD",			FilterMemberType
%>
			</select>
		</td>
	</tr>
	<tr class=Odd>
		<td align=center>
			<input type=submit name=Command value="Search">
		</td>
	</tr>
	</table>
<%
	End Sub


	Sub GenerateOption(OptValue, OptText, CurValue)
		Dim sel
		sel = IIf(OptValue = CurValue, " selected", "")

		Response.Write "<option value='" & OptValue & "'" & sel & ">" & OptText & vbNewLine
	End Sub
%>