/* Function to create the HTML text for a sentence (or panel) of Japanese text.
 * This creates a table with three rows, each row having only one column.
 * The first row contains the Japanese text.  It is divided so that each word has it's own
 * div division (jtextset) with kanji, romaji, derivitive (deriv), and a direct translation (dtrans).
 * The second row contains (possibly) a rough translation (rtrans) of the whole sentence.
 * The third row contains a clear translation into natural English (ctrans).
 * In the end, it should have this format:
 *
 * <div class="jtext">
 *	<table>
 *		<tr>
 *			<td>
 *				<div class=jtextset (or jtextsetkey)>
 *					<p class=kanji>KANJI TEXT</p>
 *					<p class=romaji>ROMANJI TEXT</p>
 *					<p class=deriv>DERIVATIVES</p>
 *					<p class=dtrans>DIRECT TRANSLATION</p>
 *				</div>
 * 				....repeated for however many kanji sets there are ....
 *			</td>
 *		</tr>
 *		<tr>
 *			<td>
 *				<div class=rtans>
 *					<p>ROUGH TRANSLATION TEXT</p>
 *				</div>
 *			</td>
 *		</tr>
 *		<tr>
 *			<td>
 *				<div class=ctrans>
 *					<p>CLEAR TRANSLATION TEXT</p>
 *				</div>
 *			</td>
 *		</tr>
 *	</table>
 *</div>
 */


function jtexttoHTML(sentence) {
	var HTMLText = "<div class=\"jtext\"><table><tr><td>";
	
	/* Check to see if there is any japanese text associated with this translation and print it if there is */
	var isJText = (sentence.getElementsByTagName("jtext")[0] != null && sentence.getElementsByTagName("jtext")[0].firstChild != null);
	if (isJText) {
		for (var j=0; j < sentence.getElementsByTagName("jtextset").length; j++) {
	
			/* First, check to see if the element is a "key" element.  If so, give is a different
			* div classification so that it can be formatted differently in the CSS
			*/
	
			if (sentence.getElementsByTagName("jtextset")[j].getAttribute("id") == "key") {
				HTMLText += "<div class=\"jtextsetkey\">";
			} else {
				HTMLText += "<div class=\"jtextset\">";
			}
	
			var currentJtextset = sentence.getElementsByTagName("jtextset")[j];
			
			HTMLText += "<p class=\"kanji\">";
			HTMLText += currentJtextset.getElementsByTagName("kanji")[0].firstChild.nodeValue;
			HTMLText += "</p>";
	
			HTMLText += "<p class=\"romaji\">";
			/* Check to see if there is a link attribute in the romaji tag.  If so, create a link for it */
			if(sentence.getElementsByTagName("jtextset")[j].getElementsByTagName("link")[0] != null) {
				HTMLText += "<a href=\"javascript:grammarWindow('" + currentJtextset.getElementsByTagName("link")[0].getAttribute("name") + "')\">";
				HTMLText += currentJtextset.getElementsByTagName("romaji")[0].firstChild.nodeValue + "</a>";
			} else {
				HTMLText += currentJtextset.getElementsByTagName("romaji")[0].firstChild.nodeValue;
			}
			HTMLText += "</p>";
			
			/* Check to see if there is a deriv tag.  If so, add it to the HTMLText */
			var isDeriv = (sentence.getElementsByTagName("jtextset")[j].getElementsByTagName("deriv")[0] != null && sentence.getElementsByTagName("jtextset")[j].getElementsByTagName("deriv")[0].firstChild != null);
			if(isDeriv) {
				HTMLText += "<p class=\"deriv\">";
				HTMLText += "(" + currentJtextset.getElementsByTagName("deriv")[0].firstChild.nodeValue + ")";
				HTMLText += "</p>";
			}
	
			HTMLText += "<p class=\"dtrans\">";
			HTMLText += currentJtextset.getElementsByTagName("dtrans")[0].firstChild.nodeValue;
			HTMLText += "</p>";
	
			/* Insert an extra space if there is no deriv element to balance everything out */
			if(!isDeriv) {
				HTMLText += "<p class=\"deriv\">&nbsp;</p>";
			}
	
			HTMLText += "</div>"; // Close div for jtextset or jtextsetkey
		}
	}

	HTMLText += "</td></tr>"; // Close table for top text portion.

	/* Check to see if there is a rtans tag, if so, add it to HTMLText */
	
	var isRtrans = (sentence.getElementsByTagName("rtrans")[0] != null && sentence.getElementsByTagName("rtrans")[0].firstChild != null);
	if(isRtrans) {
		HTMLText += "<tr><td><div class=\"rtrans\">";
		HTMLText += "<p>" + sentence.getElementsByTagName("rtrans")[0].firstChild.nodeValue + "</p>";
		HTMLText += "</div></tr></td>";
	}
	
	var isCtrans = (sentence.getElementsByTagName("ctrans")[0] != null && sentence.getElementsByTagName("ctrans")[0].firstChild != null);
	if(isCtrans) {
		HTMLText += "<tr><td><div class=\"ctrans\">";
		HTMLText += "<p>" + sentence.getElementsByTagName("ctrans")[0].firstChild.nodeValue + "</p>";
		HTMLText += "</div></td></tr>";
	}
	
	HTMLText += "</table></div>";

	return HTMLText;
}
