<% Option Explicit %>
<!--#include file="../config.asp"-->
<!--#include file="../common.asp"-->
<!--#include file="main.asp"-->
<%
	PageHeader

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


	OpenConnection

	If Request.Querystring("Command") = "Delete" Then
		DBC.Execute("DELETE FROM News WHERE NID = " & Str2Num(Request.Querystring("NID")))
	End If

	AdminPageBegin "News List"
	NewsList
	AdminPageEnd



	Sub NewsList()
		Dim rs, rsCount, i, Index

		Dim PageSize	:	PageSize	= 10
		Dim PageCount	:	PageCount	= 0
		Dim Pos			:	Pos			= 0


		Set rsCount = DBC.Execute("SELECT COUNT(*) FROM News")

		If rsCount(0) > PageSize Then
			PageCount = rsCount(0) \ PageSize
			If rsCount(0) - (PageCount * PageSize) > 0 Then
				PageCount = PageCount + 1
			End If
		Else
			PageCount = 1
			Pos = 0
		End If


		Set rs = DBC.Execute("SELECT * FROM News ORDER BY NID DESC")

		Session(Prefix & "NewsList_Page") = Str2Num(Request.Form("page"))

		If Session(Prefix & "NewsList_Page") < 1 Then
			Session(Prefix & "NewsList_Page") = 1
		End If
		If Session(Prefix & "NewsList_Page") > PageCount Then
			Session(Prefix & "NewsList_Page") = PageCount
		End If

		If Pos = 0 Then
			Pos = (Session(Prefix & "NewsList_Page") - 1) * PageSize
		End If

		If Pos < 0 Then
			Pos = 0
		End If

		If Not rs.EOF Then
			rs.Move Pos
		End If
%>
		<form name=F action="news_list.asp" method=POST>
		<input type=hidden name=Page value="<%= Session(Prefix & "NewsList_Page") %>">

		<table border=0 align=center bgcolor=white width=100% cellspacing=0 cellpadding=0>
		<tr valign=top>
			<td>

			</td>
			<td>
				<table border=0 align=center bgcolor=black width=500 cellspacing=1 cellpadding=3>
				<col>
				<col>
				<col>
				<tr class=SelectedParent>
					<td width=20>&nbsp;</td>
					<td align=center>New</td>
					<td align=center width=90>Date</td>
					<td width=70>&nbsp;</td>
				</tr>
<%
				i = 0

				Index = rsCount(0) - ((Session(Prefix & "NewsList_Page") - 1) * PageSize) - 1

				Do While (Not rs.EOF) And (i < PageSize)
%>
					<tr class=Odd>
						<td>
							<%= Index - i + 1 %>.
						</td>
						<td>
							<b><%= rs("Headline") %></b><br>
							<%= rs("Description") %>
						</td>
						<td align=right>
							<%= rs("FromDate") %>
						</td>
						<td align=center>
							<a href="news_edit.asp?NID=<%= rs("NID") %>">Edit</a>&nbsp;
							<a href="javascript: Delete(<%= rs("NID") %>)">Delete</a>
						</td>
					</tr>
<%
					i = i + 1
					rs.MoveNext
				Loop
%>
				</table>

				<p align=center class=PageText>
<%
				If rsCount(0) >= PageSize Then
					If Session(Prefix & "NewsList_Page") > 1 Then
						Response.Write "<a href='javascript: Page(1)'>First</a>&nbsp;&nbsp;&nbsp;"
						Response.Write "<a href='javascript: Page(" & Session(Prefix & "NewsList_Page")-1 & ")'>Previous</a>&nbsp;&nbsp;&nbsp;"
					Else
						Response.Write "First&nbsp;&nbsp;&nbsp;"
						Response.Write "Previous&nbsp;&nbsp;&nbsp;"
					End If

					If Session(Prefix & "NewsList_Page") < PageCount Then
						Response.Write "<a href='javascript: Page(" & Session(Prefix & "NewsList_Page")+1 & ")'>Next</a>&nbsp;&nbsp;&nbsp;"
						Response.Write "<a href='javascript: Page(" & PageCount & ")'>Last</a>"
					Else
						Response.Write "Next&nbsp;&nbsp;&nbsp;"
						Response.Write "Last"
					End If
				End If
%>
				</p>
			</td>
		</tr>
		</table>
		</form>

		<script language=JavaScript>
		<!--
		function Page(PageN)
		{
			document.F.Page.value = PageN;
			document.F.submit();
		}

		function Delete(ID)
		{
			if (confirm("This record will be delete. Do you want to proceed?"))
				document.location = "news_list.asp?Command=Delete&NID=" + ID;
		}
		//-->
		</script>
<%
	End Sub
%>