<% Option Explicit %>
<!--#include file="../config.asp"-->
<!--#include file="../common.asp"-->
<!--#include file="main.asp"-->
<%
	Dim rs, NID, Headline, Description, FromDate

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

	OpenConnection

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

		If rs.EOF Then
			DBC.Execute("INSERT INTO News (Headline, Description, FromDate) " & _
				"VALUES (" &	EscapeSQLData(Request.Form("Headline"))		& ", " & _
								EscapeSQLData(Request.Form("Description"))	& ", " & _
								"#" & Request.Form("FromDate") & "#"		& ")")
		Else
			DBC.Execute("UPDATE News SET " & _
				"Headline		= " & EscapeSQLData(Request.Form("Headline"))		& ", " & _
				"Description	= " & EscapeSQLData(Request.Form("Description"))	& ", " & _
				"FromDate		= " & "#" & Request.Form("FromDate") & "#"			& "  " & _
				"WHERE NID = " & NID)
		End If

		Response.Redirect "news_list.asp?page=" & Session(Prefix & "NewsList_Page")
	End If


	PageHeader

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

	AdminPageBegin "Add News"
	NewsEdit
	AdminPageEnd



	Sub NewsEdit()
		Set rs = DBC.Execute("SELECT * FROM News WHERE NID = " & NID)

		If rs.EOF Then
			Headline	= ""
			Description	= ""
			FromDate	= Month(Date) & "/" & Day(Date) & "/" & Year(Date)
		Else
			Headline	= rs("Headline")
			Description	= rs("Description")
			FromDate	= rs("FromDate")
		End If
%>
		<script language=JavaScript>
		<!--
		function Validate()
		{
			if (document.F.Headline.value.length == 0)
			{
				alert ('Please enter a Headline.');
				document.F.Headline.focus();
				return false;
			}

			if (document.F.Description.value.length == 0)
			{
				alert ('Please enter a Description.');
				document.F.Description.focus();
				return false;
			}

			if (! ValidateDate(document.F.FromDate.value))
			{
				alert("Please enter a correct date.");
				document.F.FromDate.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="news_edit.asp" method=post onsubmit="return Validate()">
		<input type=hidden name=NID value="<%= NID %>">
		<table border=0 align=center bgcolor=black cellspacing=1 cellpadding=3>
		<tr>
			<td colspan=2 class=SelectedParent align=center>
				News information
			</td>
		</tr>
		<tr class=Odd>
			<td>Headline</td>
			<td>
				<input name=Headline value="<%= Headline %>" size=68 maxlength=60 class=ClientText>
			</td>
		</tr>
		<tr class=Odd>
			<td>Description</td>
			<td>
				<textarea name=Description rows=10 cols=70 class=ClientText><%= Description %></textarea>
			</td>
		</tr>
		<tr class=Odd>
			<td>Date (mm/dd/yyyy)</td>
			<td>
				<input name=FromDate value="<%= FromDate %>" size=15 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
%>