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


	PageHeader

	If Session("IsAdminLogin") Then
		PageMenu
	Else
		PageLogin
		Response.End
	End If


	ID		= Str2Num(Request.Querystring("ID"))
	Command	= Request.Querystring("Command")


	OpenConnection

	If Command = "Save" Then
		SpendValue = Str2Float(Request.Querystring("Spend"))

		Set rs = DBC.Execute("SELECT * FROM Spends WHERE ID = " & ID)

		If rs.EOF Then
			' insert
			DBC.Execute("INSERT INTO Spends (Spend) VALUES(" & SpendValue & ")")
		Else
			' update
			DBC.Execute("UPDATE Spends SET Spend = " & SpendValue & " WHERE ID = " & ID)
		End If

		Application("PL_MyEGold") = 0
	End If

	If Command = "Delete" Then
		DBC.Execute("DELETE FROM Spends WHERE ID = " & ID)

		Application("PL_MyEGold") = 0
	End If


%>
	<p align=center>
	<a href="adm_spends.asp">Show all spends</a>&nbsp;&nbsp;&nbsp;
	<a href="adm_spends.asp?Command=Edit">Add a spend</a>
	</p>
<%
	If Command = "Edit" Then
		EditSpend
	Else
		SpendList
	End If
%>
	</body>
	</html>
<%



	Sub SpendList()

		Dim l

		Set rs = DBC.Execute("SELECT * FROM Spends ORDER BY Spend")
%>
		<table border=0 align=center bgcolor=006699 cellspacing=1 cellpadding=3 width=250>
		<tr bgcolor=006699 class=PageText>
			<td align=center>
				<font color=white>N</font>
			</td>
			<td align=center>
				<font color=white>value</font>
			</td>
			<td>
				&nbsp;
			</td>
		</tr>
<%
		l = 0

		Do While Not rs.EOF
%>
		<tr bgcolor=<%= IIf(l Mod 2 = 1, "EEEEEE", "white") %> class=PageText>
			<td align=center>
				<%= l + 1 %>.
			</td>
			<td align=right>
				$<%= FormatNumber(rs("Spend"), 2) %>
			</td>
			<td align=center>
				<a href="adm_spends.asp?Command=Edit&ID=<%= rs("ID") %>">Change</a>
				&nbsp;
				<a href="adm_spends.asp?Command=Delete&ID=<%= rs("ID") %>">Delete</a>
			</td>
		</tr>
<%
			l = l + 1
			rs.MoveNext
		Loop
%>
		</table>
<%
	End Sub


	Sub Editspend()

		Dim b

		Set rs = DBC.Execute("SELECT * FROM Spends WHERE ID = " & ID)

		If rs.EOF Then
			b = ""
		Else
			b = FormatNumber(rs("Spend"), 2, False, False, False)
		End If
%>
		<script language=JavaScript>
		<!--
		function Validate()
		{
			if (document.EF.Spend.value.length == 0)
			{
				alert("Please enter a number.");
				document.EF.Spend.focus();
				return false;
			}
			else
			{
				if (isNaN(document.EF.Spend.value))
				{
					alert("Please enter a number.");
					document.EF.Spend.focus();
					return false;
				}
			}

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

		<form name=EF action="adm_spends.asp" onsubmit="return Validate()">
		<input type=hidden name=ID value="<%= ID %>">
		<p align=center class=PageText>
			Spend value <input name=Spend value="<%= b %>" size=10 maxlength=10 class=ClientText>
			<input type=submit name=Command value=Save>
		</p>
		</form>
<%
	End Sub
%>