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

	Command = Request.Querystring("Command")

	Msg = ""


	If Command = "LogIn" Then
		OpenConnection

		Set rs = DBC.Execute("SELECT MemberID, EGold, Cash, SponsorEGold, IsGold FROM Members WHERE " & _
			"EGold		= " & Request.Form("EGold") & " AND " & _
			"Password	= " & EscapeSQLData(Request.Form("Password")))

		If rs.EOF Then
			Msg = "<br><span class=PageText>Incorrect e-gold or password!<br><br>If you not a member, <a href='addmember.asp'>join us</a>.</span>"
		Else
			MemberID			= rs("MemberID")
			Session(Prefix & "MemberID")		= MemberID
			Session(Prefix & "EGold")		= rs("EGold")
			Session(Prefix & "IsGold")		= rs("IsGold")
			Session(Prefix & "SponsorEGold")	= rs("SponsorEGold")
			Session(Prefix & "Cash")		= CDbl(rs("Cash"))
			Session(Prefix & "LastVisit")		= Date()
			Session.TimeOut			= 5		' minutes

			' ---- update LastLogin & LastIP ---------------
			DBC.Execute("UPDATE Members SET LastLogin = #" & Month(Date()) & "/" & Day(Date()) & "/" & Year(Date()) & "#, " & _
						"LastIP = " & EscapeSQLData(Request.ServerVariables("REMOTE_HOST")) & " " & _
						"WHERE MemberID = " & MemberID)

			' ---- koi kampanii e posetil usera ---------------
			Set rs = DBC.Execute("SELECT CID FROM History " & _
				"WHERE MemberID = " & MemberID)

			Session(Prefix & "VisitedCampaigns") = " "
			Do While Not rs.EOF
				Session(Prefix & "VisitedCampaigns") = Session(Prefix & "VisitedCampaigns") & rs("CID") & " "
				rs.MoveNext
			Loop

			' ----- user's downline ------
			Set rs = DBC.Execute("SELECT EGold FROM Members WHERE SponsorEGold = " & Session(Prefix & "EGold"))

			Session(Prefix & "MemberDownLine") = ""
			Do While Not rs.EOF
				Session(Prefix & "MemberDownLine") = Session(Prefix & "MemberDownLine") & rs("EGold") & " "
				rs.MoveNext
			Loop
		End If
	End If

	If Command = "LogOut" Then
		MemberID			= 0
		Session(Prefix & "MemberID")		= MemberID
		Session(Prefix & "EGold")		= 0
		Session(Prefix & "SponsorEGold")	= 0
		Session(Prefix & "Cash")		= 0
		Session.Timeout			= 1		' minutes

		Msg = "<br><span class=PageText>Your current session has been logged out.</span><br><br><br><br>"
	End If

	If Command = "RequestPayment" Then
		If CDbl(Session(Prefix & "Cash")) >= CDbl(IIf(Session(Prefix & "IsGold"), GoldMinimumPayout, MinimumPayout)) Then
			OpenConnection

			Set rs = DBC.Execute("SELECT Cash FROM Members WHERE MemberID = " & MemberID)

			If rs.EOF Then
%>
				<p class=PageText>
					<br>Your session has been expired. Please <a href=log.asp>log in</a> again.
				</p>
<%
			Else
				DBC.Execute("UPDATE Members SET Cash = 0 WHERE MemberID = " & MemberID)

				DBC.Execute("INSERT INTO Payouts (ToEGold, Cash, Description) " & _
						"VALUES (" &	Session(Prefix & "EGold")			& ", " & _
								CDbl(rs("Cash"))			& ", " & _
								"'Your earnings at " & Sitename & "'"	& ")")

				Session(Prefix & "Cash") = 0

				Msg = "<p class=PageText><br>Your request has been received.<br>We will check your account and if everything is ok you will get your money in short time.<br><br>Return to <a href='memberaccount.asp'>Member`s Area</a></p>"
			End If
		End If
	End If


	If (Command = "LogIn" Or Command = "") And Len(Msg) < 4 Then
		Response.Redirect "memberaccount.asp"
	End If

	PageBegin
	Response.Write Msg
	PageEnd
%>