<% Option Explicit %>
<!--#include file="../config.asp"-->
<!--#include file="../common.asp"-->
<!--#include file="main.asp"-->
<%
	Dim rs, BID, URL, Banner, Approved, EGold, BatchNumber, ValidTo

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

	OpenConnection

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

		If rs.EOF Then
			DBC.Execute("INSERT INTO Banners (URL, Banner, EGold, BatchNumber, ValidTo, Approved) " & _
				"VALUES (" &	EscapeSQLData(Request.Form("URL"))		& ", " & _
						EscapeSQLData(Request.Form("Banner"))	& ", " & _
						Str2Num(Request.Form("EGold"))		& ", " & _
						Str2Num(Request.Form("BatchNumber"))	& ", " & _
						EscapeSQLData(Request.Form("ValidTo"))	& ", " & _
						Str2Num(Request.Form("Approved"))		& ")")
		Else
			DBC.Execute("UPDATE Banners SET " & _
				"URL		= " & EscapeSQLData(Request.Form("URL"))	& ", " & _
				"Banner		= " & EscapeSQLData(Request.Form("Banner"))	& ", " & _
				"EGold		= " & Str2Num(Request.Form("EGold"))		& ", " & _
				"BatchNumber	= " & Str2Num(Request.Form("BatchNumber"))	& ", " & _
				"ValidTo		= " & EscapeSQLData(Request.Form("ValidTo"))	& ", " & _
				"Approved	= " & Str2Num(Request.Form("Approved"))	& "  " & _
				"WHERE BID = " & BID)
		End If

		' za da se sazdade aktualen fail
		Application(Prefix & "BannersFileCreateOn") = Date() - 10

		Response.Redirect "banner_list.asp"
	End If



	PageHeader

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

	AdminPageBegin "Add Banner"
	BannerEdit
	AdminPageEnd



	Sub BannerEdit()
		Set rs = DBC.Execute("SELECT * FROM Banners WHERE BID = " & BID)

		If rs.EOF Then
			URL		= ""
			Banner		= ""
			Approved		= 0
			EGold		= 0
			BatchNumber	= 0
			ValidTo		= ""
		Else
			URL		= rs("URL")
			Banner		= rs("Banner")
			Approved		= rs("Approved")
			EGold		= rs("EGold")
			BatchNumber	= rs("BatchNumber")
			ValidTo		= rs("ValidTo")
		End If
%>
		<script language=JavaScript>
		<!--
		function Validate()
		{
			if (document.F.URL.value.length == 0)
			{
				alert("Please enter a Site's URL.");
				document.F.URL.focus();
				return false;
			}

			if (document.F.Banner.value.length == 0)
			{
				alert("Please enter a Banner URL.");
				document.F.Banner.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="banner_edit.asp" method=post onsubmit="return Validate()">
		<input type=hidden name=BID value="<%= BID %>">
		<table border=0 align=center bgcolor=black cellspacing=1 cellpadding=3>
		<tr>
			<td colspan=2 class=SelectedParent align=center>
				Banner information
			</td>
		</tr>
		<tr class=Odd>
			<td>Site's URL</td>
			<td>
				<input name=URL value="<%= URL %>" size=50 maxlength=120 class=ClientText>
			</td>
		</tr>
		<tr class=Odd>
			<td>Banner's URL</td>
			<td>
				<input name=Banner value="<%= Banner %>" size=50 maxlength=120 class=ClientText>
			</td>
		</tr>
		<tr class=Odd>
			<td>Valid to</td>
			<td>
				<input name=ValidTo value="<%= ValidTo %>" size=15 maxlength=10 class=ClientText>
			</td>
		</tr>
		<tr class=Odd>
			<td>Advertiser's e-gold</td>
			<td>
				<input name=EGold value="<%= EGold %>" size=15 maxlength=7 class=ClientText>
			</td>
		</tr>
		<tr class=Odd>
			<td>Batch number</td>
			<td>
				<input name=BatchNumber value="<%= BatchNumber %>" size=15 maxlength=12 class=ClientText>
			</td>
		</tr>
		<tr class=Odd>
			<td>Approved</td>
			<td>
				<input type=checkbox name=Approved value=1 <%= IIf(Approved, "checked", "") %> 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
%>