<%@ page import = "java.sql.*, javax.servlet.*, javax.servlet.http.*, java.io.*" %>
<%!
public String get_folio_image(String manuscript, String folio) {
	String file = null;
	if (manuscript.toUpperCase().equals("B")) {
		if (folio.startsWith("210v")) { file = "f210v"; }
		if (folio.startsWith("211r")) { file = "f211r"; }
		if (folio.startsWith("235v")) { file = "f235v"; }
		if (folio.startsWith("236r")) { file = "f236r"; }
	}
	if (manuscript.toUpperCase().equals("C")) {
		if (folio.startsWith("129v")) { file = "129v"; }
		if (folio.startsWith("130r")) { file = "130r"; }
		if (folio.startsWith("130v")) { file = "130v"; }
	}
	if (manuscript.toUpperCase().equals("T")) {
		if (folio.startsWith("3v")) { file = "f3v"; }
		if (folio.startsWith("7v")) { file = "f7v"; }
		if (folio.startsWith("138")) { file = "f138"; }
	}
	if (manuscript.toUpperCase().equals("V")) {
		if (folio.startsWith("1r")) { file = "1r"; }
		if (folio.startsWith("1v")) { file = "1v"; }
	}
	if (manuscript.toUpperCase().equals("X")) {
		if (folio.startsWith("1r")) { file = "back"; }
		if (folio.startsWith("1v")) { file = "front"; }
	}
	if (manuscript.toUpperCase().equals("A")) {
		if (folio.startsWith("1v")) { file = "l01"; }
		if (folio.startsWith("52v")) { file = "l02"; }
		if (folio.startsWith("56v")) { file = "l03"; }
	}
	if (manuscript.toUpperCase().equals("P")) {
		if (folio.startsWith("8v")) { file = "l1a"; }
		if (folio.startsWith("15r")) { file = "l1b"; }
		if (folio.startsWith("29v")) { file = "l1c"; }
	}
	if (manuscript.toUpperCase().equals("G")) {
		if (folio.startsWith("140r")) { file = "l2a"; }
		if (folio.startsWith("141r")) { file = "l2b"; }
		if (folio.startsWith("168v")) { file = "l2c"; }
	}
	if (manuscript.toUpperCase().equals("F")) {
		if (folio.startsWith("1r")) { file = "l3a"; }
		if (folio.startsWith("1v")) { file = "l3b"; }
	}
	if (manuscript.toUpperCase().equals("L")) {
		if (folio.startsWith("38v")) { file = "l4a"; }
		if (folio.startsWith("64r")) { file = "l4b"; }
		if (folio.startsWith("75v")) { file = "l4c"; }
		if (folio.startsWith("87v")) { file = "l4d"; }
	}
        return file;
}
%>

<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><script type="text/javascript" src="http://www.hrionline.ac.uk/partonopeus/a.js"></script><link rel="stylesheet" href="http://www.hrionline.ac.uk/partonopeus/a.css" type="text/css"><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>Partonopeus de Blois</title></head><body>
<%
// Let's set how many lines were going to try and auto-generate
int lines = 200;

// Get all the variables submitted to the page, and in some cases make sure they're not null
String di = request.getParameter("di");
if (di == null) { di = "A"; }
%>
<img style="position:absolute; right:10px; top:50px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:250px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:450px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:650px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:850px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:1050px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:1250px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:1450px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:1650px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:1850px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<img style="position:absolute; right:10px; top:2050px; z-index:-1;" src="http://www.hrionline.ac.uk/partonopeus/i/<%=di%>.gif">
<%
String typecode = request.getParameter("typecode");
if (typecode == null) { typecode = "ma"; }

String line_system = "meta_string";
if (typecode.substring(0,1).equals("p")) { line_system = "number"; }

int sml = 1;
String start_ml = request.getParameter("l");
if (start_ml == null) { start_ml = ""; }
try { sml = Integer.parseInt(start_ml); } catch (Exception e) { sml = 1; }

int eml = sml + lines;

// Here we have to do some database meddling
// First we just set up the database
try {
	Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception E) {
	out.println("Unable to load driver.");
	E.printStackTrace(new PrintWriter(out));
}
Connection Conn = null;
try {
	Conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/part?user=part&password=blackout&DBname=part");
}
catch (SQLException E) {
	out.println("SQLException: " + E.getMessage());
	out.println("SQLState:     " + E.getSQLState());
	out.println("VendorError:  " + E.getErrorCode());
}
Statement Stmt = Conn.createStatement();
// Now, let's get our lines
ResultSet RS = Stmt.executeQuery("SELECT subdoctype_3, meta_string, number, " + typecode.substring(1,2) + "_html FROM m_lines WHERE meta >= " + sml + " AND meta <= " + eml + " AND subdoctype_1 LIKE \"" + di + "\" ORDER BY meta, meta_string, subdoctype_1;");
// Now, a big and clever loop does the rest
String current_meta = "" + sml;
int di_pos = 0;
String[] notes = new String[100];
int notecode = 0;
// This next bit is horrible..
int back_dest = sml - lines;
int back_number = lines; 
if (back_dest < 0) { back_dest = 0; back_number = (sml - 1); }
if (back_number > 0) {
	%>
	<a target="_self" href="part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di%>&amp;l=<%=back_dest%>">View previous <%=back_number%> lines ...</a>
	<br/><br/>
	<%
}
%>
<table>
<tr>
<%
String old_folio = "";
boolean first_line_hack = true;
while (RS.next()) {
	String ms = di;
	String body_html = RS.getString(typecode.substring(1,2) + "_html");
	String line_string = RS.getString(line_system);
	String meta_string = RS.getString("meta_string");
	String folio = RS.getString("subdoctype_3");
	
	// The problem here probably stems from me doing things in this loop in the wrong order, but I don't have time to fix it now.
	if (first_line_hack) {
		%>
		<td>
		<%
		if (line_system.equals("meta_string")) {
			try {
				int cm = Integer.parseInt(line_string);
				if ((cm % 5) == 0) {
					%>&#160;<a href="javascript:collate(<%=current_meta%>)"><%=line_string%><%
				}
			} catch (Exception e) {
				// if it's not int-able we'd better print it anyway
				%>&#160;<%=line_string%><%
			}
		}
		%>
		</td>
		<%
		first_line_hack = false;
	}
	// End of nasty hack.
			
	boolean cycling = true;
	while (cycling) {
		if (di_pos >= di.length()) { di_pos = 0; }
		String expected_ms = di.substring(di_pos, di_pos + 1);
			
		if (ms.equals(expected_ms) && line_string.equals(current_meta)) {
			%><%=body_html%>
			<td>
			<%
			if (folio.equals(old_folio)) {
			} else {
				%>
				<%=folio%>
				<%
				String folio_file = get_folio_image(di,folio);
				if (folio_file != null) {
					%>
					<a target="_blank" href="http://www.hrionline.ac.uk/partonopeus/facsimile_html/<%=di%>_<%=folio_file%>.html"><img border="0" src="http://www.hrionline.ac.uk/partonopeus/i/facsimile.gif" alt="Click here to view a facsimile of this page"/></a>
					<%
				}
				old_folio = folio;
			}
			%>
			</td>
			<%
			cycling = false;
			di_pos++;
		} else {
			if (line_string.equals(current_meta) ? false : true) {
				if (di_pos < di.length() && di_pos > 0) {
					for (int i = 0; i < (di.length() - di_pos); i++) {
					%><td>&#160;</td><%
					}
				}
				String last_meta = current_meta;
				current_meta = line_string;
				di_pos = 0;
				%></tr>
				<%
				// Now we need a few bits of code to decide how to display
				// the start of the next line
				try {
					int lm = Integer.parseInt(last_meta);
					int cm = Integer.parseInt(current_meta);
					for (int i = 0; i < ((cm - lm) - 1); i++) {
						%><tr><td>&#160;<%
						if (((lm + i + 1) % 5) == 0) {
							%><a href="javascript:collate(<%=(lm + i + 1)%>)"><%=(lm + i + 1)%></a><%
						}
						%></td></tr><%
					}
				} catch (Exception e) {
					// if they're not int-able we just wont bother	
				}
				%><tr><td>&#160;<%
				try {
					int cm = Integer.parseInt(current_meta);
					if ((cm % 5) == 0) {
						%><a href="javascript:collate(<%=meta_string%>)"><%=cm%></a><%
					}
				} catch (Exception e) {
					// if it's not int-able we'd better print it anyway
					%><%=current_meta%><%
				}
				%></td><%
			} else {
				if (ms.equals(expected_ms) ? false : true) {
				%><td>&#160;</td><%
					di_pos++;
				}
			}
		}
	}
}
%>
</tr>
</table>
<br/><br/>
<a target="_self" href="part_generate.jsp?typecode=<%=typecode%>&amp;di=<%=di%>&amp;l=<%=(eml + 1)%>">View next <%=lines%> lines ...</a>
</body>
</html>