﻿/////////////////////////////////////////////////
//	TABLE OF 214 KANGXI SERIES		//
/////////////////////////////////////////////////

function gShowKangxi214 ()
{
	// Clear perfs
	gPerfsClear ();

	// Set the layout if not done
	gCheckLayout ();
	
	// Set the info page
	JmvcSetPage ('frameInfo', 'pageKangxi214');
	
	// Set the info loader
	gSetInfoLoader ();
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Load data
	gLoadData ('series_kangxi214.php', OnKangxi214, 'get', null);
}

function OnKangxi214 (url, transport)
{
	// Time stamp
	gPerfsResponse (transport.responseText.length);
	
	// Update local data
	top.info = transport.responseText;
	top.bKangxi214Changed = true;
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Show perfs
	gPerfsShow ();
}

function OnWriteKangxi214 (view, page)
{
	// Ignore if not relevant
	if (!top.bKangxi214Changed)
		return;
	
	// Get a string buffer
	var buf = new StringBuffer ();

	// Begin main header table
	buf.append ("<table  class='ht1'><tr><td>");
	buf.append ("<table><tr style='overflow:hidden;'><td class='hh1'>214 " + (top.sLangSource == 'cn' ? "Kangxi keys" : "Radicals") + "</td></tr></table>");
	
	// If a text is available add the link to the word lists	
	if (top.oParsedText)
	{
		buf.append ("</td><td class='serBack'><a href='javascript:gSetMainList();'>Main list</a>");
		
		if (top.sLangSource == 'cn')
			buf.append ("</td><td class='serBack'><a href='javascript:gSetSimplTradList();'>Simpl/trad. list</a>");
	}
	
	// End main header table
	buf.append ("</td></tr></table>");
	
	// Write the header
	JGetFrameHeader (view).innerHTML = buf.toString();
	
	// Write server info in the frame inner content
	JGetFrameContent (view).innerHTML = top.info + gXitiAjax (top.sLangSource + "_kangxi_214");
}

/////////////////////////////////////////////////
//		EACH KANGXI SERIES		//
/////////////////////////////////////////////////

function KX (id)
{
	gShowKangxiSeries (id);
}

function KXRS (krs)
{
	/*
	 * Unexplained: krs, initially a string, is received as a number.
	 * To be done: special cases for krs (see precise definition in Unihan)
	 */
	
	// Show the Kangxi series
	KX (parseInt(krs));
}

function gShowKangxiSeries (id)
{
	// Clear perfs
	gPerfsClear ();
	
	// Set the layout if not done
	gCheckLayout ();
	
	// Set the info page
	JmvcSetPage ('frameInfo', 'pageKangxiSeries');

	// Set the info loader
	gSetInfoLoader ();
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Load data
	gLoadData ('series_kangxi.php', OnKangxiSeries, 'get', 'id=' + id);
}

function OnKangxiSeries (url, transport)
{
	// Time stamp
	gPerfsResponse (transport.responseText.length);
	
	// Update local data
	top.info = transport.responseXML.documentElement;
	top.bKangxiSeriesChanged = true;
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Show perfs
	gPerfsShow ();
}

function OnWriteKangxiSeries (view, page)
{
	// Ignore if not relevant
	if (!top.bKangxiSeriesChanged)
		return;
	
	// debug
	// alert ("OnWriteKangxiSeries");

	// Get a string buffer
	var buf = new StringBuffer ();
	
	// Begin main header table
	buf.append ("<table  class='ht1'><tr><td>");
	
	// Get the series data and ID
	var oSeries = top.info;
	var nId = parseInt (oSeries.getAttribute ("i").substr(1));
	
	// Add the series header
	buf.append("<table><tr>");
	buf.append ("<td class='hh1'>" + (top.sLangSource == 'cn' ? "Kangxi key" : "Radical") + " " + nId + "<span style='padding-left:3em;'>"
		+ oSeries.getElementsByTagName ('base')[0].getElementsByTagName('c')[0].getAttribute('h')
		+ "</span></td>");
	buf.append ("<td class='serNext'>" + ((nId > 1) ? ("<a href='javascript:KX(" + (nId-1) +");'>Previous</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId > 2) ? ("<a href='javascript:KX(" + (nId-2) +");'>" + (nId-2) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId > 1) ? ("<a href='javascript:KX(" + (nId-1) +");'>" + (nId-1) + "</a>") : "&nbsp;") + "</td>");
	buf.append("<td class='serCurrent'>" + nId + "</td>");
	buf.append ("<td class='serOther'>" + ((nId < 214) ? ("<a href='javascript:KX(" + (nId+1) +");'>" + (nId+1) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId < 213) ? ("<a href='javascript:KX(" + (nId+2) +");'>" + (nId+2) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serNext'>" + ((nId < 214) ? ("<a href='javascript:KX(" + (nId+1) +");'>Next</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serNext'><a href='javascript:gShowKangxi214();'>214 " + (top.sLangSource == 'cn' ? "keys" : "radicals") + "</a></td>");
	buf.append ("</tr></table>");
	
	// If a text is available add the link to the word lists	
	if (top.oParsedText)
	{
		buf.append ("</td><td class='serBack'><a href='javascript:gSetMainList();'>Main list</a>");
		
		if (top.sLangSource == 'cn')
			buf.append ("</td><td class='serBack'><a href='javascript:gSetSimplTradList();'>Simpl/trad. list</a>");
	}
	
	// End main header table
	buf.append ("</td></tr></table>");
	
	// debug
	JGetFrameHeader(view).innerHTML = buf.toString();
	
	// debug
	buf = new StringBuffer ();
	
	// Add the CJK BASE part
	buf.append (kangxiWritePart (oSeries.getElementsByTagName ("base")[0]));
	buf.append ("<div class='comment' style='padding-top:12pt;'>");
	buf.append ("<p>In \""+ nId + ".x\" (for instance " + nId + ".3), " + nId + " is for radical "
		+ (top.sLangSource == 'cn' ? "(Kangxi key)" : "")
		+ " " + nId + ". \"3\" means that there are three additional strokes besides the radical."
		+ " Due to practical reasons, the radical may differ from the series number in some exceptional cases.</p>"
		+ "<p>Please note that when the radical has a simplified variant, the characters with the simplified variant are found AFTER those with the traditional variant.</p>");
	buf.append ("</div>");
	
	// Add the Extension A part if any
	if (oSeries.getElementsByTagName ("exta").length)
	{
		buf.append ("<div class='hd1'>Extension A</div>");
		buf.append ("<div class='comment' style='padding-bottom:6pt;'>");
		buf.append ("The characters below belong to the first Unicode extension for Chinese - Japanese - Korean characters (see www.unicode.org for details)."
			+ " These uncommon characters may or may not display correctly depending upon which fonts are installed on the user PC.");
		buf.append ("</div>");
		buf.append (kangxiWritePart (oSeries.getElementsByTagName ("exta")[0]));
	}
	
	// Update the Info DIV
	JGetFrameContent('frameInfo').innerHTML = "<div id='pageKangxi214' class='fixedWidth'>" + buf.toString() + gXitiAjax (top.sLangSource + "_series_kangxi") + "</div>";
}

function kangxiWritePart (ePart)
{
	var eListChars = ePart.getElementsByTagName ("c");
	
	var buf = new StringBuffer ();
	var kX, hz, kM, kD;
	
	buf.append ("<table class='serTable'>");
	
	var nChars = eListChars.length;
	
	for (i = 0; i < nChars; i++)
	{
		eChar = eListChars[i];
		buf.append ("<tr><td class='serKangxi'>");
		
		kX = eChar.getAttribute ('kX');
		if (kX)
			buf.append (kX);
			
		// buf.append ("</td><td lang='zh' class='serHanzi zh'>");
		buf.append ("</td><td" + top.sCLang + " class='serHanzi zh'>");
		
		hz = eChar.getAttribute ('h');
		if (hz)
			buf.append ("<a href='javascript:HZ(" + hz.charCodeAt(0) + ");' class='serHanzi'>" + hz + "</a>");
			
		buf.append ("</td><td class='serPinyin'>");
			
		kM = eChar.getAttribute ('kM');
		if (kM)
			buf.append (kM);
		
		buf.append ("</td><td class='serDef'>");
		
		kD = eChar.getAttribute ('kD');
		if (kD)
			buf.append (kD);
		
		buf.append ("</td></tr>");
	}
	
	buf.append ("</table>");
	
	return buf.toString();
}

/////////////////////////////////////////////////
//		EACH ETYM SERIES		//
/////////////////////////////////////////////////

function ES (id)
{
	gShowEtymSeries (id);
}

function gShowEtymSeries (id)
{
	// Clear perfs
	gPerfsClear ();
	
	// Set the layout if not done
	gCheckLayout ();
	
	// Set the info page
	JmvcSetPage ('frameInfo', 'pageEtymSeries');
	
	// Set the info loader
	gSetInfoLoader ();
	
	// Update display
	JmvcUpdateAllViews ();	
	
	// Load data
	gLoadData ('series_etym.php', OnEtymSeries,  'get', 'id=' + id);
}

function OnEtymSeries (url, transport)
{
	// Time stamp
	gPerfsResponse (transport.responseText.length);
	
	// Update local data
	top.info = transport.responseXML.documentElement;
	top.bEtymSeriesChanged = true;
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Show perfs
	gPerfsShow ();
}

function OnWriteEtymSeries (view, page)
{
	// Ignore if not relevant
	if (!top.bEtymSeriesChanged)
		return;
	
	// Get a string buffer
	var buf = new StringBuffer ();
	
	// Begin main header table
	buf.append ("<table  class='ht1'><tr><td>");
	
	// Get the series data and ID
	var oSeries = top.info;
	var nId = parseInt (oSeries.getAttribute ("i").substr(1));
	
	// Add the series header
	buf.append("<table><tr>");
	buf.append ("<td class='hh1'>Wieger etymological lesson " + nId + "</td>");
	buf.append ("<td class='serNext'>" + ((nId > 1) ? ("<a href='javascript:ES(" + (nId-1) +");'>Previous</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId > 2) ? ("<a href='javascript:ES(" + (nId-2) +");'>" + (nId-2) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId > 1) ? ("<a href='javascript:ES(" + (nId-1) +");'>" + (nId-1) + "</a>") : "&nbsp;") + "</td>");
	buf.append("<td class='serCurrent'>" + nId + "</td>");
	buf.append ("<td class='serOther'>" + ((nId < 177) ? ("<a href='javascript:ES(" + (nId+1) +");'>" + (nId+1) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId < 176) ? ("<a href='javascript:ES(" + (nId+2) +");'>" + (nId+2) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serNext'>" + ((nId < 177) ? ("<a href='javascript:ES(" + (nId+1) +");'>Next</a>") : "&nbsp;") + "</td>");
	buf.append ("</tr></table>");
	
	// If a text is available add the link to the word lists	
	if (top.oParsedText)
	{
		buf.append ("</td><td class='serBack'><a href='javascript:gSetMainList();'>Main list</a>");
		
		if (top.sLangSource == 'cn')
			buf.append ("</td><td class='serBack'><a href='javascript:gSetSimplTradList();'>Simpl/trad. list</a>");
	}
	
	// End main header table
	buf.append ("</td></tr></table>");
	
	// Write the header
	JGetFrameHeader(view).innerHTML = buf.toString();
	
	// Write the content
	JGetFrameContent(view).innerHTML = 
		"<div style='font-size:smaller; font-style:italic; text-align:right;'>Please refer to Wieger's book for the content of the lessons</div>"
		+ etymWritePart (oSeries) + gXitiAjax (top.sLangSource + "_series_etym");
}

function etymWritePart (ePart)
{
	var eListChars = ePart.getElementsByTagName ("c");
	
	var buf = new StringBuffer ();
	var kX, section, hz, kM, kD;
	
	buf.append ("<table class='serTable'>");
	
	var nChars = eListChars.length;
	
	for (i = 0; i < nChars; i++)
	{
		eChar = eListChars[i];
		buf.append ("<tr><td class='serKangxi'>");
		
		kX = eChar.getAttribute ('kX');
		if (kX)
			buf.append ("<a href='javascript:KXRS(" + kX + ");' class='serKangxi'>" + kX + "</a>");
		
			
		buf.append ("</td><td class='serSection'>");
		section = eChar.getAttribute ('s');
		if (section)
			buf.append (section);
			
		// buf.append ("</td><td lang='zh' class='serHanzi zh'>");
		buf.append ("</td><td" + top.sCLang + " class='serHanzi zh'>");
		
		hz = eChar.getAttribute ('h');
		if (hz)
			buf.append ("<a href='javascript:HZ(" + hz.charCodeAt(0) + ");' class='serHanzi'>" + hz + "</a>");
			
		buf.append ("</td><td class='serPinyin'>");
			
		kM = eChar.getAttribute ('kM');
		if (kM)
			buf.append (kM);
		
		buf.append ("</td><td class='serDef'>");
		
		kD = eChar.getAttribute ('kD');
		if (kD)
			buf.append (kD);
		
		buf.append ("</td></tr>");
	}
	
	buf.append ("</table>");
	
	return buf.toString();
}

/////////////////////////////////////////////////
//	TABLE OF 858 PHONETIC ELEMENTS	//
/////////////////////////////////////////////////

function gShowPhon858 ()
{
	// Clear perfs
	gPerfsClear ();

	// Set the layout if not done
	gCheckLayout ();
	
	// Set the info page
	JmvcSetPage ('frameInfo', 'pagePhon858');
	
	// Set the info loader
	gSetInfoLoader ();
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Load data
	gLoadData ('series_phon858.php', OnPhon858, 'get', null);
}

function OnPhon858 (url, transport)
{
	// Time stamp
	gPerfsResponse (transport.responseText.length);
	
	// Update local data
	top.info = eval ("(" + transport.responseText + ")");
	top.bPhon858Changed = true;
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Show perfs
	gPerfsShow ();
}

function OnWritePhon858 (view, page)
{
	// Ignore if not relevant
	if (!top.bPhon858Changed)
		return;
	
	var x = 0;
	var y = 0;
	
	// Get a string buffer
	var buf = new StringBuffer ();
	
	// 1) HEADER
	
	// Begin main header table
	buf.append ("<table  class='ht1'><tr><td>");
	buf.append ("<table><tr style='overflow:hidden;'><td class='hh1'>858 phonetic elements");
	if (top.sLangSource == 'cn')
		buf.append ("(traditional characters)");
	buf.append ("</td></tr></table>");
	
	// If a text is available add the link to the word lists	
	if (top.oParsedText)
	{
		buf.append ("</td><td class='serBack'><a href='javascript:gSetMainList();'>Main list</a>");
		
		if (top.sLangSource == 'cn')
			buf.append ("</td><td class='serBack'><a href='javascript:gSetSimplTradList();'>Simpl/trad. list</a>");
	}
	
	// End main header table
	buf.append ("</td></tr></table>");
	
	// Write the header
	JGetFrameHeader (view).innerHTML = buf.toString();
	
	// 2) CONTENT
	
	// debug
	buf = new StringBuffer ();
	
	// Comments
	buf.append (top.info.comments);
	
	// Open a DIV for the table
	buf.append ("<div id='divPhon858'>");
	
	// debug
	oContent = top.info.content;
	for (iStrokes = 0; iStrokes < oContent.length; iStrokes++)
	{
		var sStrokes = oContent[iStrokes][0];
		var oListPhon = oContent[iStrokes][1];
		x = 0;
		
		// Strokes
		buf.append ("<div class='title' style='top:" + y + "px;'>" + sStrokes + "</div>");
		x = 50; y+= 9;
		
		// Series
		for (var iPhon = 0, iCol = 0; iPhon < oListPhon.length; iPhon++, iCol++)
		{
			if (iCol ==15)
			{
				iCol = 0;
				x = 50;
				y += 45;
			}
			
			var iSeries = oListPhon[iPhon][0];
			var cRef = oListPhon[iPhon][1];
			
			buf.append ("<div class='phcell' style='left:" + x + "px; top:" + y + "px;'>"
				+ "<a href='javascript:PS(" + iSeries + ");'" + ( cRef == '¿' ? "style='color:gray;'" : top.sCLang) + ">" + cRef + "</a>"
				+ "<p class='phn'>" + iSeries + "</p>"
				+ "</div>");
			
			x += 50;
		}
		
		y += 45;
	}
	
	// Scrolling
	y+= 100;
	buf.append ("<div class='phcell' style='left:" + x + "px; top:" + y + "px;'>&nbsp</div>");
	
	buf.append("</div>");
		
	// Write server info in the frame inner content
	JGetFrameContent (view).innerHTML = buf.toString() + gXitiAjax (top.sLangSource + "_phon_858");
}
	
/////////////////////////////////////////////////
//		EACH PHONETIC SERIES		//
/////////////////////////////////////////////////

function PS (id)
{
	gShowPhonSeries (id);
}

function gShowPhonSeries (id)
{
	// Clear perfs
	gPerfsClear ();
	
	// Set the layout if not done
	gCheckLayout ();

	// Set the info page
	JmvcSetPage ('frameInfo', 'pagePhonSeries');
	
	// Set the info loader
	gSetInfoLoader ();
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Load data
	gLoadData ('series_phon.php', OnPhonSeries, 'get', 'id=' + id);
}

function OnPhonSeries (url, transport)
{
	// Time stamp
	gPerfsResponse (transport.responseText.length);
	
	// Update local data
	// top.info = transport.responseXML.documentElement;
	top.info = eval ("(" + transport.responseText + ")");
	
	top.bPhonSeriesChanged = true;
	
	// Update display
	JmvcUpdateAllViews ();
	
	// Show perfs
	gPerfsShow ();
}

function OnWritePhonSeries (view, page)
{
	// Ignore if not relevant
	if (!top.bPhonSeriesChanged)
		return;
	
	// Get a string buffer
	var buf = new StringBuffer ();
	
	// Begin main header table
	buf.append ("<table  class='ht1'><tr><td>");
	
	// Get the series data
	var oSeries = top.info;
	var nId = oSeries.n;
	var nEtym = oSeries.e;
	var sSection = oSeries.s;
	var cRef = oSeries.c;
	
	// HEADER
	
	// Begin table
	buf.append("<table><tr>");
	
	// Title
	buf.append ("<td class='hh1'>")
	if (cRef)
		buf.append ("<span style='font-size:20pt;' lang='zh'>" + cRef + "</span>")
	buf.append ("<span style='padding-right:40px;'> Phonetic element " + nId + "</span>");
	
	// Show the etym. lesson if any
	if (nEtym)
	{
		buf.append ("<a href='javascript:ES(" + nEtym + ");' style='font-size:10pt;padding-left:10px;'>Wieger lesson " + nEtym);
		if (sSection)
			buf.append (" " + sSection);
		buf.append ("</a>");
	}
	
	buf.append ("</td>");
	buf.append ("<td class='serNext'>" + ((nId > 1) ? ("<a href='javascript:PS(" + (nId-1) +");'>Previous</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId > 2) ? ("<a href='javascript:PS(" + (nId-2) +");'>" + (nId-2) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId > 1) ? ("<a href='javascript:PS(" + (nId-1) +");'>" + (nId-1) + "</a>") : "&nbsp;") + "</td>");
	buf.append("<td class='serCurrent'>" + nId + "</td>");
	buf.append ("<td class='serOther'>" + ((nId < 858) ? ("<a href='javascript:PS(" + (nId+1) +");'>" + (nId+1) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serOther'>" + ((nId < 857) ? ("<a href='javascript:PS(" + (nId+2) +");'>" + (nId+2) + "</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serNext'>" + ((nId < 858) ? ("<a href='javascript:PS(" + (nId+1) +");'>Next</a>") : "&nbsp;") + "</td>");
	buf.append ("<td class='serNext'><a href='javascript:gShowPhon858();'>858 phon.</a></td>");
	buf.append ("</tr></table>");
	
	// If a text is available add the link to the word lists	
	if (top.oParsedText)
	{
		buf.append ("</td><td class='serBack'><a href='javascript:gSetMainList();'>Main list</a>");
		
		if (top.sLangSource == 'cn')
			buf.append ("</td><td class='serBack'><a href='javascript:gSetSimplTradList();'>Simpl/trad. list</a>");
	}
	
	// End table
	buf.append ("</td></tr></table>");
	
	// Write the header
	JGetFrameHeader (view).innerHTML = buf.toString ();
	
	// CONTENT
	JGetFrameContent(view).innerHTML = 
		"<div style='font-size:smaller; font-style:italic; text-align:right;'>The phonetic series refer to the French edition of Wieger's book</div>"
		+ oSeries.text  + gXitiAjax (top.sLangSource + "_series_phon");
}
