﻿// new code for incorporating notes files

function handleXMLResponse () { 
	if (request.readyState == 4) {
		if (request.status == 200) { 
			var notesfile = request.responseXML;
			var divs = notesfile.getElementsByTagName('div'); 
			var storagediv = document.getElementById('notescontainer');
			var append = false;
			if (_notesLoaded) {
				append = confirm('Loading notes into the application.\nClick OK to append to any notes loaded earlier, or Cancel to replace them.');
				}
			if (!append) { 
				var newDiv = document.createElement( 'div' );
				newDiv.setAttribute( 'id', 'notescontainer' );
				var removedNode = storagediv.parentNode.replaceChild( newDiv, document.getElementById('notescontainer') );
				storagediv = document.getElementById('notescontainer');
				}
//	var listDiv = document.getElementById( 'charOutput' );
//	var oldContent = document.getElementById('charInfo');
//
//	var newContent = document.createElement( 'div' );
//			newContent.className = 'charInfo';
//			newContent.setAttribute( 'id', 'charInfo' );
//	var removedNode = listDiv.replaceChild( newContent, oldContent );

			if (divs.length > 0) {
				for (var i=0; i<divs.length; i++) {
					if (divs[i].className == 'notes') {
						var clonedNode = divs[i].cloneNode(true);
						var note = storagediv.appendChild( clonedNode );
						}
					}
				} 
			else { 
				storagediv.appendChild(document.createTextNode('None.'));
				}
			_notesLoaded = true; 
			}
		}
	}	


function reproduceTree (node) {
	if (node.nodeType == 1) { 
		var children = node.childNodes;
		for (var i=0; i<children.length; i++) {
			}
		}
	}


function copyNodes (ajaxnode, copiednode) {
	//alert(ajaxnode.nodeName+' '+copiednode.nodeName);
	for (var node=ajaxnode.firstChild; node != null; node = node.nextSibling) {
		if (node.nodeType == 3){ //text
			copiednode.appendChild(document.createTextNode(node.data));
			//alert('added text node: '+node.data);
			}
		if (node.nodeType == 1){ //element
			//alert('sibling '+node.nodeName);
			var subnode = document.createElement(node.nodeName);
			var attlist = node.attributes;
			if (attlist != null) {  
				for (var i=0; i<attlist.length; i++){
					//alert(attlist[i].name+' '+attlist[i].value);
					subnode.setAttribute(attlist[i].name, attlist[i].value);
					}
				}
			copiednode.appendChild(subnode);
			copyNodes(node, subnode);
			}
		}
	}
	

function printDBInfo () { 
	// When a request is sent to get data from the DB, and a response
	// received, this function processes the data immediately and writes
	// to the bottom of the right side.  Only the data for a single character
	// is retrieved at any time, so no need to hang on to xmlDocument.
	if (request.readyState == 4 && request.status == 200) { 
		var dbResponse = request.responseXML;
		var nodename; var newnode;
		var body = dbResponse.getElementsByTagName('body')[0];
		var outputdiv = document.getElementById('dboutput');
		if (body.childNodes.length > 0) {
			copyNodes(body, outputdiv);
			} 
		else { 
			outputdiv.appendChild(document.createTextNode('None.'));
			}
		// attach a function to each .ex example in db notes so that on
		// click it shows the characters
		var examples = outputdiv.getElementsByTagName('span'); 
		for (i=0; i<examples.length; i++) {
			if (examples[i].className == 'ex') {
				//examples[i].title = examples[i].firstChild.data;
				var uri = '/rishida/tools/analysestring/index.php?list='+examples[i].firstChild.data+'&smallgraphics=on&compact=on&nounicode=on&nonotes=on';
				setlinkonclick(examples[i], uri);
				//examples[i].onclick = function () { detail = window.open(uri, 'detail'); detail.focus(); }
				examples[i].title = examples[i].firstChild.data;
				}
			}

		}
	}
	
function setlinkonclick (node, uri) {
	node.onclick = function(){ detail = window.open(uri, 'detail'); detail.focus() };
	}
	
function ajaxDrawSelection () {
	// Captures database records and adds them to the U and desc arrays
	// Data display algorithms can then access the arrays as usual
	// Array records simply accumulate during the session - no reason to delete them.
	
	if (request.readyState == 4) {
		if (request.status == 200) { 
			var dbResponse = request.responseXML;
			var db = dbResponse.getElementsByTagName('db');
			for (var i=0; i<db.length; i++) {
				U[db[i].firstChild.firstChild.data]=db[i].childNodes[1].firstChild.data;
				var fields = db[i].childNodes[1].firstChild.data.split(';'); //U[db[i].firstChild.firstChild.data].split(';');
				if (fields.length < 10) { alert(db[i].firstChild.firstChild.data); }
				}
			var d = dbResponse.getElementsByTagName('desc');
			for (var i=0; i<d.length; i++) {
				desc[d[i].firstChild.firstChild.data]=d[i].childNodes[1].firstChild.data;
				}
			drawSelection(document.getElementById('customRange1').value);
			}
		else { 
			alert('Oops. Unexpected problem in ajaxDrawSelection.'); //outputdiv.appendChild(document.createTextNode('None.'));
			}
		}
	}
	
function ajaxDrawProperties () {
	// Captures database record for a character and  adds it to the U and desc arrays
	// Data display algorithms can then access the arrays as usual
	// Array records simply accumulate during the session - no reason to delete them.
	
	if (request.readyState == 4) {
		if (request.status == 200) { 
			var dbResponse = request.responseXML;
			var db = dbResponse.getElementsByTagName('db');
			for (var i=0; i<db.length; i++) {
				U[db[i].firstChild.firstChild.data]=db[i].childNodes[1].firstChild.data;
				}
			var d = dbResponse.getElementsByTagName('desc');
			for (var i=0; i<d.length; i++) {
				desc[d[i].firstChild.firstChild.data]=d[i].childNodes[1].firstChild.data;
				}
			drawProperties(parseInt(_newCodepoint, 16));
			}
		else { 
			alert('oops'); //outputdiv.appendChild(document.createTextNode('None.'));
			}
		}
	}
	
function ajaxDisplayUnorderedList () {
	// Captures database records for a list of characters and  adds it to the U and desc arrays
	// Data display algorithms can then access the arrays as usual
	// Array records simply accumulate during the session - no reason to delete them.
	// _newCharacterList: list of decimal code point values separated by :
	//	set when this was called
	
	if (request.readyState == 4) {
		if (request.status == 200) { 
			var dbResponse = request.responseXML;
			var db = dbResponse.getElementsByTagName('db');
			for (var i=0; i<db.length; i++) {
				U[db[i].firstChild.firstChild.data]=db[i].childNodes[1].firstChild.data;
				}
			var d = dbResponse.getElementsByTagName('desc');
			for (var i=0; i<d.length; i++) {
				desc[d[i].firstChild.firstChild.data]=d[i].childNodes[1].firstChild.data;
				}
			var characterList ='';
			var chars = _newCharacterList.split(':'); 
			for (var j=0; j<chars.length; j++){
				characterList += getCharFromInt(chars[j]); 
				}
			displayUnorderedList(characterList);
			}
		else { 
			alert('oops'); //outputdiv.appendChild(document.createTextNode('None.'));
			}
		}
	}

function ajaxGetSearchList () {
	// Captures database record for a character and  adds it to the U and desc arrays
	// Data display algorithms can then access the arrays as usual
	// Array records simply accumulate during the session - no reason to delete them.
	
	if (request.readyState == 4) {
		if (request.status == 200) { 
			var dbResponse = request.responseXML;
			var foundRecords = new Array;
			var db = dbResponse.getElementsByTagName('db');
			for (var i=0; i<db.length; i++) {
				U[db[i].firstChild.firstChild.data]=db[i].childNodes[1].firstChild.data;
				foundRecords[i] = db[i].firstChild.firstChild.data;
				}
			var d = dbResponse.getElementsByTagName('desc');
			for (var i=0; i<d.length; i++) {
				desc[d[i].firstChild.firstChild.data]=d[i].childNodes[1].firstChild.data;
				}

			var counter = 0; 
			for (var k=0; k<foundRecords.length; k++) {
				addLine(eval(foundRecords[k]), _newContent); counter++;
				} 
			drawSearchList(_newContent, _resultdiv, counter);
				
			}
		else { alert("No response from the server."); }
		} 
	}
	

function getn11n () {
	// Normalises text in the picker area
	
	if (request.readyState == 4) {
		if (request.status == 200) { 
			var normalizedText = request.responseText;
			
			document.getElementById('picker').value = normalizedText;
				
			}
		else { alert("No response from the server."); }
		} 
	}
	
