<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug=true %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Register TagPrefix="xedit" Namespace="Swarren.XmlEditGrid" Assembly="Swarren.XmlEditGrid" %>
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Quote of the Day</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<script language="VB" type="text/VB" runat="server">
		Dim edit As string
	    Sub Page_Load(Src As Object, E As EventArgs) 
		' This is the filename of the Quote File
		Dim FILENAME As String = "quotes.xml"
        Dim textcolor As String = Request.QueryString("textcolor")
			if textcolor = "" then 
				textcolor = "black"

			end if
        Dim bgcolor As String = Request.QueryString("bgcolor")
				if bgcolor = "" then 
					bgcolor = "black"
				end if
		Dim ds As New DataSet
        Dim FS As New FileStream(Server.MapPath(FILENAME), FileMode.Open)
        ds.ReadXml(FS)
        FS.close()
		
Dim action As String = Request.QueryString("action")

Select Case action
   Case "qotd":
		DayQuote(ds)
   Case "list":
   		QuoteList(ds)
   Case "random":
   		RandomQuote(ds)
	  Case "?":
response.Write("<h1><hr>This page will generate a Quote of the Day, a Random Quote, or a Quote List based on the value of action<br />")
response.Write("Acceptable values are ""qotd"" ""random"" ""list"" ""?"" (this option) <br />")  
response.Write("Setting edit=yes and password = *** will allow you to edit the quotes. <br /></h1><hr>")  
	Case "edit"
	   		EditQuote()
		Case else
   		RandomQuote(ds)
End Select
	End Sub

Sub QuoteList(ds)
		quotegrid.DataSource = new DataView(ds.Tables(0))
        quotegrid.DataBind()
	End Sub

Sub DayQuote(ds as Dataset)
			' Get today's date
            Dim dt As New DateTime(DateTime.Now.Ticks)
            
            ' holds the record number which will determine which quote to use
            Dim RowNum As Integer = 0
            
            ' Cycle through the days of the year and stop on today's date
            Dim x As Integer
            For x = 0 To dt.DayOfYear
                If RowNum >= ds.Tables(0).Rows.Count - 1 Then
                    RowNum = 0
                Else
                    RowNum += 1
                End If
            Next x 
            ' Return the quote for today
            Dim strQuote As [String] = Server.HtmlEncode(ds.Tables(0).Rows(RowNum)(1).ToString())
            Dim authQuote As [String] = Server.HtmlEncode(ds.Tables(0).Rows(RowNum)(0).ToString())
			
			' Set to Label

			lblQuote.Text = "&quot;" & strQuote & "&quot;"
			lblQuote2.Text = " -- " & authQuote
End Sub

Sub RandomQuote(ds as Dataset)

            ' holds the record number which will determine which quote to use
            Dim RowNum As Integer = 0
			' Get number of Roww           
			Rownum = ds.Tables(0).Rows.Count -1
 
			 'Pick a random number up to Rownum
			RANDOMIZE
			Rownum = INT(Rownum*RND)+1

            ' Return the quote for today
            Dim strQuote As [String] = Server.HtmlEncode(ds.Tables(0).Rows(RowNum)(1).ToString())
            Dim authQuote As [String] = Server.HtmlEncode(ds.Tables(0).Rows(RowNum)(0).ToString())
			
			' Set to Label

			lblQuote.Text = "&quot;" & strQuote & "&quot;"
			lblQuote2.Text = " -- " & authQuote
							
End Sub

Sub EditQuote()
	if Request.QueryString("password") = "atlantis" then 
	edit = "yes"
else
	Response.Write("<h1>You must enter the correct password to edit the quotes!</h1>")
	Response.End()
	end if
End Sub

</script>
<body>

<asp:Label id="lblQuote" runat="server" Font-Name="Verdana" ToolTip="Quote of the Day 1.0 by David Veksler" />
<asp:Label id="lblQuote2" runat="server" Font-Name="Verdana" ToolTip="Quote of the Day 1.0 by David Veksler" />

 <asp:DataGrid runat="server" id="quotegrid" 
		BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
 />
 
<% if edit ="yes" then %>
<form id="Form1" method="post" runat="server">
      <xedit:xmleditgrid 
       id="XmlEditGrid1" runat="server" 
       xmlfile="quotes.xml">
      </xedit:xmleditgrid>
    </form>
<% end if %>


</body>
</html>