<% Option Explicit %>
<!--#include file="config.asp"-->
<!--#include file="common.asp"-->
<%
	Dim LID, Title, URL, FromEGold, BatchNumber, ValidTo


	Command	= Request.Form("Command")
	LID		= Str2Num(Request("LID"))

	OpenConnection

	If Command = "Save" Then
		Set rs = DBC.Execute("SELECT * FROM Links WHERE LID = " & LID)

		If rs.EOF Then
			DBC.Execute("INSERT INTO Links (Title, URL, FromEGold, BatchNumber, ValidTo) " & _
				"VALUES (" &	EscapeSQLData(Request.Form("Title"))	& ", " & _
								EscapeSQLData(Request.Form("URL"))		& ", " & _
								Request.Form("FromEGold")				& ", " & _
								Request.Form("BatchNumber")				& ", " & _
								"#" & Request.Form("ValidTo")			& "#"						& ")")
		Else
			DBC.Execute("UPDATE Links SET " & _
				"Title			= " & EscapeSQLData(Request.Form("Title"))	& ", " & _
				"URL			= " & EscapeSQLData(Request.Form("URL"))	& ", " & _
				"FromEGold		= " & Request.Form("FromEGold")				& ", " & _
				"BatchNumber	= " & Request.Form("BatchNumber")			& ", " & _
				"ValidTo		= " & "#" & Request.Form("ValidTo") & "#"	& "  " & _
				"WHERE LID = " & LID)
		End If

		Response.Redirect "admin_links.asp"
	End If



	PageHeader

	If Session("IsAdminLogin") Then
		AdminMenu
	Else
		UserLogin
		Response.End
	End If

	Edit


	Sub Edit()
		Set rs = DBC.Execute("SELECT * FROM Links WHERE LID = " & LID)

		If rs.EOF Then
			Title		= ""
			URL			= ""
			FromEgold	= 0
			BatchNumber	= 0
			ValidTo		= Month(Date + 30) & "/" & Day(Date + 30) & "/" & Year(Date + 30)
		Else
			Title		= rs("Title")
			URL			= rs("URL")
			FromEGold	= rs("FromEGold")
			BatchNumber	= rs("BatchNumber")
			ValidTo		= rs("ValidTo")
		End If
%>
		<script language=JavaScript>
		<!--
		function Validate()
		{
			if (document.F.Title.value.length == 0)
			{
				alert("Please enter link's title.");
				document.F.Title.focus();
				return false;
			}

			if (document.F.URL.value.length == 0)
			{
				alert("Please enter link's URL.");
				document.F.URL.focus();
				return false;
			}

			if (document.F.FromEGold.value.length == 0)
			{
				alert("Please enter an e-gold number.");
				document.F.FromEGold.focus();
				return false;
			}
			else
			{
				if (isNaN(document.F.FromEGold.value))
				{
					alert("Please enter an e-gold number.");
					document.F.FromEGold.focus();
					return false;
				}
			}

			if (document.F.BatchNumber.value.length == 0)
			{
				alert("Please enter a batch number.");
				document.F.BatchNumber.focus();
				return false;
			}
			else
			{
				if (isNaN(document.F.BatchNumber.value))
				{
					alert("Please enter a batch number.");
					document.F.BatchNumber.focus();
					return false;
				}
			}

			if (! ValidateDate(document.F.ValidTo.value))
			{
				alert("Please enter a correct date.");
				document.F.ValidTo.focus();
				return false;
			}

			return true;
		}

		function ValidateDate(strDate)
		{
			var parsedDate = strDate.split ("/");
			var day, month, year;

			if (parsedDate.length != 3) return false;

			month = parsedDate[0]-1;
			day = parsedDate[1];
			year = parsedDate[2];

			if (year.length != 4) return false;

			var objDate = new Date (strDate);

			if (objDate.getFullYear() < 1900) return false;
			if (objDate.getFullYear() > 9999) return false;

			if (month != objDate.getMonth()) return false;
			if (day != objDate.getDate()) return false;
			if (year != objDate.getFullYear()) return false;

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

		<form name=F action="admin_links_edit.asp" method=post onsubmit="return Validate()">
		<input type=hidden name=LID value="<%= LID %>">
		<table border=0 align=center class=Header cellspacing=1 cellpadding=3>
		<tr>
			<td colspan=2 align=center>
				<font color=black>Link information</font>
			</td>
		</tr>
		<tr class=PageText bgcolor=white>
			<td>Title</td>
			<td>
				<input name=Title value="<%= Title %>" size=40 maxlength=40 class=ClientText>
			</td>
		</tr>
		<tr class=PageText bgcolor=white>
			<td>URL</td>
			<td>
				<input name=URL value="<%= URL %>" size=40 maxlength=120 class=ClientText>
			</td>
		</tr>
		<tr class=PageText bgcolor=white>
			<td>Advertiser's e-gold number</td>
			<td>
				<input name=FromEGold value="<%= FromEGold %>" size=40 maxlength=10 class=ClientText>
			</td>
		</tr>
		<tr class=PageText bgcolor=white>
			<td>Batch number</td>
			<td>
				<input name=BatchNumber value="<%= BatchNumber %>" size=40 maxlength=15 class=ClientText>
			</td>
		</tr>
		<tr class=PageText bgcolor=white>
			<td>Active to date (mm/dd/yyyy)</td>
			<td>
				<input name=ValidTo value="<%= ValidTo %>" size=40 maxlength=15 class=ClientText>
			</td>
		</tr>
		<tr>
			<td colspan=2 bgcolor=white align=center>
				<input type=submit name=Command value=Save style="width:200">
			</td>
		</tr>
		</table>
		</form>
<%
	End Sub
%>