<% Option Explicit %>
<!--#include file="../config.asp"-->
<!--#include file="../common.asp"-->
<!--#include file="main.asp"-->
<%
	Dim FilterTitle, FilterURL, FilterMsg, FilterBatchNumber, FilterIsBold, FilterIsHighlight, WHERE_Clause

	PageHeader

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

	' ----- WHERE clause ------------------------
	FilterTitle		= Request.Form("FilterTitle")
	FilterURL		= Request.Form("FilterURL")
	FilterMsg		= Request.Form("FilterMsg")
	FilterBatchNumber	= Str2Num(Request.Form("FilterBatchNumber"))
	FilterIsBold	= Str2Num(Request.Form("FilterIsBold"))
	FilterIsHighlight	= Str2Num(Request.Form("FilterIsHighlight"))

	WHERE_Clause = ""

	If Len(FilterTitle) > 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND Title LIKE " & EscapeSQLData("%" & FilterTitle & "%") & " "
		Else
			WHERE_Clause = WHERE_Clause & "Title LIKE " & EscapeSQLData("%" & FilterTitle & "%") & " "
		End If
	End If

	If Len(FilterURL) > 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND URL LIKE " & EscapeSQLData("%" & FilterURL & "%") & " "
		Else
			WHERE_Clause = WHERE_Clause & "URL LIKE " & EscapeSQLData("%" & FilterURL & "%") & " "
		End If
	End If

	If Len(FilterMsg) > 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND Msg LIKE " & EscapeSQLData("%" & FilterMsg & "%") & " "
		Else
			WHERE_Clause = WHERE_Clause & "Msg LIKE " & EscapeSQLData("%" & FilterMsg & "%") & " "
		End If
	End If

	If FilterBatchNumber > 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND BatchNumber = " & FilterBatchNumber & " "
		Else
			WHERE_Clause = WHERE_Clause & "BatchNumber = " & FilterBatchNumber & " "
		End If
	End If

	If FilterIsBold <> 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND IsBold = " & FilterIsBold
		Else
			WHERE_Clause = WHERE_Clause & "IsBold = " & FilterIsBold
		End If
	End If

	If FilterIsHighlight <> 0 Then
		If Len(WHERE_Clause) > 0 Then
			WHERE_Clause = WHERE_Clause & "AND IsHighlight = " & FilterIsHighlight
		Else
			WHERE_Clause = WHERE_Clause & "IsHighlight = " & FilterIsHighlight
		End If
	End If

	If Len(WHERE_Clause) > 0 Then
		WHERE_Clause = " WHERE " & WHERE_Clause
	End If
	' -------------------------------------------


	OpenConnection

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

	AdminPageBegin "Message List"
	MessageList
	AdminPageEnd



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

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


		Set rsCount = DBC.Execute("SELECT COUNT(*) FROM MsgBoard" & WHERE_Clause)

		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 MsgBoard " & WHERE_Clause & " ORDER BY MID DESC")

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

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

		If Pos = 0 Then
			Pos = (Session(Prefix & "MsgList_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="msg_list.asp" method=POST>
		<input type=hidden name=Page value="<%= Session(Prefix & "MsgList_Page") %>">

		<table border=0 align=center bgcolor=white width=100% cellspacing=0 cellpadding=0>
		<tr valign=top>
			<td>
				<% Filter %>
			</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>Message</td>
					<td align=center width=90>Batch number</td>
					<td width=70>&nbsp;</td>
				</tr>
<%
				i = 0

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

				Do While (Not rs.EOF) And (i < PageSize)
%>
					<tr class=Odd>
						<td>
							<%= Index - i + 1 %>.
						</td>
						<td <%= IIf(rs("IsHighlight"), "bgcolor=" & MsgBoardHighlightedColor, "") %>>
							<a href='<%= rs("URL") %>'>
<%
							If rs("IsBold") Then
								Response.Write "<b>" & HTMLEncode(rs("Title")) & "</b>"
							Else
								Response.Write HTMLEncode(rs("Title"))
							End If
%>
							</a><br>
							<%= rs("Msg") %>
						</td>
						<td align=right>
							<%= rs("BatchNumber") %>
						</td>
						<td align=center>
							<a href="msg_edit.asp?MID=<%= rs("MID") %>">Edit</a>&nbsp;
							<a href="javascript: Delete(<%= rs("MID") %>)">Delete</a>
						</td>
					</tr>
<%
					i = i + 1
					rs.MoveNext
				Loop
%>
				</table>

				<p align=center class=PageText>
<%
				If rsCount(0) >= PageSize Then
					If Session(Prefix & "MsgList_Page") > 1 Then
						Response.Write "<a href='javascript: Page(1)'>First</a>&nbsp;&nbsp;&nbsp;"
						Response.Write "<a href='javascript: Page(" & Session(Prefix & "MsgList_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 & "MsgList_Page") < PageCount Then
						Response.Write "<a href='javascript: Page(" & Session(Prefix & "MsgList_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 = "msg_list.asp?Command=Delete&MID=" + ID;
		}
		//-->
		</script>
<%
	End Sub


	Sub Filter()
%>
	<table border=0 align=center width=110 bgcolor=black cellspacing=1 cellpadding=3>
	<tr class=SelectedParent>
		<td align=center>Filter</td>
	</tr>
	<tr class=Odd>
		<td>
			Title<br>
			<input name=FilterTitle value="<%= FilterTitle %>" size=11 maxlength=40>
			<br><br class=HalfBreak>
			URL<br>
			<input name=FilterURL value="<%= FilterURL %>" size=11 maxlength=40>
			<br><br class=HalfBreak>
			Message<br>
			<input name=FilterMsg value="<%= FilterMsg %>" size=11 maxlength=40>
			<br><br class=HalfBreak>
			BatchNumber<br>
			<input name=FilterBatchNumber value="<%= FilterBatchNumber %>" size=11 maxlength=11>
			<br><br class=HalfBreak>
			<input type=checkbox name=FilterIsBold value="1" <%= IIf(FilterIsBold = 1, "checked", "") %>>
			Bold<br class=HalfBreak>
			<input type=checkbox name=FilterIsHighlight value="1" <%= IIf(FilterIsHighlight = 1, "checked", "") %>>
			Highlighted<br class=HalfBreak>
		</td>
	</tr>
	<tr class=Odd>
		<td align=center>
			<input type=submit name=Command value="Search">
		</td>
	</tr>
	</table>
<%
	End Sub


	Function HTMLEncode (s)
		If IsNull(s) Then
			HTMLEncode = ""
		Else
			HTMLEncode = Server.HTMLEncode(s)
		End If
	End Function
%>