<% 'article.asp - Program to display one of Louie Celerier's Bits & Pieces articles..
' The default script (default.asp) displays a table of contents of the articles, and then
' calls this script to display one article.  It passes one query string parameter:
'    id=the base filename.
' There should be a .htm file with that base filename.
' The base filename is a date in yyyy-mm-ddx format, with x being an optional letter that
' allows multiple articles to occur on the same date.
' This script is VERY similar to article.asp in the letters subdirectory, and the terms
' "article" and "letter" are synonymous.
'
Option Explicit

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

dim articles, articleDate(100), articleTitle(100) 'Up to 100 articles
articles = 0 'Counts the articles as they are read, and then as they are displayed

Dim dashdate, pagetitle, slashdate, shortdate, thisarticle
shortdate = "(Requested article is not in the index!?!)"
'Build our own array of month names for date conversion.
dim months
months = Array("","January","February","March","April","May","June","July","August","September","October","November","December")

'**** Read the index file into an array of file identifiers (essentially, dates), and an array of titles.
'**** We need these arrays to help with links to the next and previous article.
dim  FSO, filename, Filepath, file, TextStream, Line
filename = "index.txt"
' Create the filesystem object, then map the logical path to the physical system path
set FSO = server.createObject("Scripting.FileSystemObject")
Filepath = Server.MapPath(Filename)
if FSO.FileExists(Filepath) Then
    ' Get file handle and open the file.
    set file = FSO.GetFile(Filepath)
    Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)
    ' Read the index file line by line, and store every line in the arrays articleDate() and articleTitle()
    Do While Not TextStream.AtEndOfStream
        Line = TextStream.readline
        'Read 11 characters. One character after the date allows more than one article to appear on the same date.
        dashdate = trim(left(line,11)) 'yyyy-mm-ddx
        pagetitle = mid(line,12)       'may contain <br> tags
        articles = articles + 1
        articleDate(articles) = dashdate
        articleTitle(articles) = pagetitle

        'If this is the article that has been requested, save its index number in thisarticle, and
        'convert its date to a MMMMMM dd, yyyy  format.
        If request.querystring("id")=dashdate Then
            thisarticle = articles  '
            if right(dashdate,1)="x" Then
                shortdate="" 'For articles that don't have a valid date.
            else shortdate = months(cint(mid(dashdate,6,2))) & " " & trim(cint(mid(dashdate,9,2))) & ", " & left(dashdate,4)
            End If
        End If
    Loop
    articleDate(articles+1)="" 'Make sure the list ends with an empty entry, not "null"
    Set TextStream = nothing
Else %>
    <html><head><link type="text/css" rel="stylesheet" href="stylesheet.css">
    </head>
    <body>
    Response.Write "<p class='error'>Major error!! The index of articles is missing.  Please try again later.<br/>"
    Response.Write "If this error persists, please <a href='contactus.asp'>notify us </a><p>" & vbcrlf
    </body>
    </html>
    <%
    Response.End
End If

Set FSO = nothing
%>

<html>
<head>
<title>Bits &amp; Pieces: <%=articletitle(thisarticle)%></title>
<link type="text/css" rel="stylesheet" href="stylesheet.css">
</head>
<body>
<div id="pageheading">
<h1 class="masthead"><a href="./" title="Home Page"><span style="color:#0066CC">Bits &amp;</span>
<span style="color:#cc0033">Pieces</span></a></h1>
<%

'*** Page heading - Display links to the previous and next articles, using graphic arrows.
dim nextfile,prevfile
nextfile="gifs/next2.png"
prevfile="gifs/prev2.png"

'First, display a left arrow to link to the previous artile (if there is one).
'(Remember, the array is in reverse chronological order, so the "previous" articles comes
' after this one in the array.
If thisArticle>0 and articleDate(thisArticle+1)<>"" Then
	response.write "<a href='article.asp?id=" & articleDate(thisArticle+1) & "'>"
    response.write "<img src='" & prevfile & "' border='0' title='Older article: " & linkdate(thisArticle+1) & articleTitle(thisArticle+1) & "' style='padding-right:10em;'></a>"
Else 'Even if there is no previous article to link to (and thus no arrow), we need to display a space
     'with padding so that the navigation remains centered.
     response.write "<span style='padding-right:10em;'>&nbsp;</span>"
End If

'Display this article's date between the navigation arrows.
Response.write shortdate

'Display a right arrow to link to the subsequent article (if there is one).
If thisArticle>1 Then
	response.write "<a href='article.asp?id=" & articleDate(thisArticle-1) & "'>"
    response.write "<img src='" & nextfile & "' border='0' title='Newer article: " & linkdate(thisArticle-1) & articleTitle(thisArticle-1) & "' style='padding-left:10em;'></a>"
Else response.write "<span style='padding-left:10em;'>&nbsp;</span>"
End If
response.write vbcrlf & "</div>" & vbcrlf ' End of the page heading div.

response.write "<h2 style='text-align:center;margin-bottom:0;'>" & articleTitle(thisArticle) & "</h2>" & vbcrlf

dashdate = request("id")

If right(dashdate,1)="P" Then
	'***** Embed a PDF file *****
    filename = "htmfiles/" & dashdate & ".pdf"
	response.write "<p style='text-align:center;margin:0;font-size:90%'><a href='"& filename & "'>Link directly to the PDF for printing</a></p>" & vbcrlf
	response.write "<div style='width:100%;margin:auto;'>"
	response.write "<object data='" & filename & "' style='width:100%;height:80%;'></object>"& vbcrlf
    response.write "</div>" & vbcrlf
Else
   '**** Embed an HTML file *****

	'Read the HTM file that contains the selected letter/article.
	'Define a File System object, and get the physical system filespec for this filename.
	filename = "htmfiles/" & dashdate & ".htm"
	Filepath = Server.MapPath(Filename)
	set FSO = server.createObject("Scripting.FileSystemObject")

	if FSO.FileExists(Filepath) Then
		' Get a handle to the file and open the file
		set file = FSO.GetFile(Filepath)
		Set TextStream = file.OpenAsTextStream(ForReading, TristateUseDefault)
		' Read & display the file line by line
		Do While Not TextStream.AtEndOfStream
			Line = TextStream.readline
			'Repoint all images, since the html files and their images are in a subdirectory.
			line = replace(Line, "src=""20", "src=""htmfiles/20")
			response.write line & vbcrlf
		Loop
		Set TextStream = nothing
		response.write "<p style='margin-left:0.7em;text-indent:-0.7em;'>- Luis R. Celerier"
		response.write "<br>Longview, Texas</p>" & vbcrlf
	Else
		Response.Write "<p class='error'>Sorry - there is no article at " & request("id") & ".htm</p>" & vbcrlf
	End If
	Set FSO = nothing
End If
%>
<div id="footer">
<div><strong><span style="color:#0066CC">Bits &amp; Pieces - </span><span style="color:#cc0033">History of Panama</span></strong>
- <%=shortdate%></div>
<%
'****In the footer, display words instead of arrows to link to the next and previous letters/articles.

If thisArticle>0 and articleDate(thisArticle+1)<>"" Then
	response.write "<a href='article.asp?id=" & articleDate(thisArticle+1) & "' title='" & linkdate(thisArticle+1) &  articleTitle(thisArticle+1) & "'>Older article</a>"
	response.write "&nbsp;|&nbsp;" & vbcrlf
End If

'Display a 'home page' link between the Older and Newer links
response.write "<a href='./'>Home</a>" & vbcrlf

If thisArticle>1 Then
	response.write "&nbsp;|&nbsp;" & vbcrlf
	response.write "<a href='article.asp?id=" & articleDate(thisArticle-1) & "' title='" & linkdate(thisArticle-1) & articleTitle(thisArticle-1) & "'>Newer article</a>" & vbcrlf
End If

If session("Letters-loggedin")="True" Then
	response.write "<div><a href='letters/'>(Return to Papi's Letters)</a></div>" & vbcrlf
End If
%>
<div><a href="contactus.asp">Contact Us</a></div>
</div> <!-- end of footer -->
</body>
</html>

<%'=================================================================
'       Functions and Subroutines
'===================================================================
'Convert the yyyy-mm-ddx-formatted date stored in the array into a parenthesized ddMMMyy format.
'The array of months gives full month names; we just want the first 3 letters here.
Function linkdate(i)
    dim thedate
    thedate = articledate(i)
    linkdate = "(" & mid(thedate,9,2) & left(months(cint(mid(thedate,6,2))),3) & left(thedate,4) & ")"
End Function
%>