// see globals/init code at bottom of file.

function mistakeWord(location, word, suggestions, len) {
  this.location = location;
	this.word = word;
	this.suggestions = suggestions;
	this.len = len;
}

function documentLoaded(e) {
dvo = "";
jsdata = new Array();

for (sc = 0; sc < xmlDoc.getElementsByTagName('struct').length; sc++)
{
  s = xmlDoc.getElementsByTagName('struct').item(sc);

	errLocation = -1;
  errSuggest = -1;
	errWord = -1;
	errLen = -1; // length of misspelled word
		
  for (mc = 0; mc < s.getElementsByTagName('member').length; mc++)
	{
	  m = s.getElementsByTagName('member').item(mc);
    n = m.getElementsByTagName('name').item(0).firstChild.nodeValue; // get name    		 
        
    switch (n)
		{
		  case "suggestions":
			  DOMerrSuggest = m.getElementsByTagName('string');
				errSuggest = new Array();
				for (es = 0; es < DOMerrSuggest.length; es++)
				{
				  errSuggest[es] = DOMerrSuggest.item(es).firstChild.nodeValue;
				} 
			break;

			case "location":
			  errLocation = m.getElementsByTagName('i4').item(0).firstChild.nodeValue; 
			break;

			case "length": /* This is an optional extension added by GED July 20, 2001 */
			  errLen = m.getElementsByTagName('i4').item(0).firstChild.nodeValue			   
			break;
			
			case "word":
			  errWord = m.getElementsByTagName('string').item(0).firstChild.nodeValue;
			break;			
			default:
			alert(" ["+n+"] is not a recognised response member") 
		}	    
  }
  /* we must build js arrays based on location of spelling error
	   as it is the only piece of data that is for sure unique. */

  if (errLocation != -1 && errSuggest != -1 && errWord != -1)
	{   /* if we set all of those then we are ready to build a JS array entry... */
      // alert("ok to build data for node " + errLocation +" "+ errSuggest + " " + errWord);
			errLocation = errLocation + "";
	    jsdata[errLocation] = new mistakeWord(errLocation, errWord, errSuggest, errLen);  
	}	 
}
callSpellerUpdate();
return;
}

function check()
{
  dvo = "";

  for (a in jsdata)
  {
    alert("location: " + jsdata[a].location + " # of suggestions: "+ jsdata[a].suggestions.length);
		dvo = dvo + "<hr>Word: <b>" + jsdata[a].word + "</b>"  
		          + "<br>Location: " + jsdata[a].location 
							+ "<br>Length: "+ jsdata[a].len
							+ "<br>Suggestions: ";
							
		for (i in jsdata[a].suggestions)
    {
		  dvo = dvo + "<br>" + jsdata[a].suggestions[i];
		}
	}

  document.getElementById("output").innerHTML = dvo;
	return;
}

function loadNextDoc(e) {
  if (document.all && xmlDoc.readyState != 4) return; 

  d = xmlDoc.documentElement;

	while (d.nodeName != "struct") 
	{
	  if (!d.firstChild) { 
		  /* alert("no struct found"); */ 			
		  callSpellerUpdate();
			return; 
		}  
		d = d.firstChild;
	}
  d = d.parentNode.parentNode;
//	alert(d.childNodes.length);
	
	for (sc = 0; sc < d.childNodes.length; sc++)
  {

    s = d.childNodes[sc].firstChild;

    errLocation = -1;
    errSuggest = -1;
    errWord = -1;
    errLen = -1; // length of misspelled word

 		//alert(s.nodeName);
//    alert(s.nodeName + " has # of children: "  + s.childNodes.length);
    for (mc = 0; mc < s.childNodes.length; mc++)
  	{ 
		    m = s.childNodes[mc].firstChild;
				
        n = m.firstChild.nodeValue; // get name    		 

//        alert(n);

        switch (n)
          {
            case "suggestions":
							DOMerrSuggest = m.nextSibling.firstChild.firstChild;
							errSuggest = new Array();
							es = -1;
//							alert(n + " = " + DOMerrSuggest.nodeName);
							if (DOMerrSuggest.firstChild)
							{
							  DOMerrSuggest = DOMerrSuggest.firstChild;
// 							 alert(n + " -> " + DOMerrSuggest.nodeName);
  							while (DOMerrSuggest.nextSibling)
  							{
  							  // alert(n + " ==> " + DOMerrSuggest.firstChild.firstChild.nodeValue); // + " has # of children: " + DOMerrSuggest.childNodes.length);
									es++;
									errSuggest[es] = DOMerrSuggest.firstChild.firstChild.nodeValue;
									DOMerrSuggest = DOMerrSuggest.nextSibling;
  							}
							}
						break;

      			case "location":
						    errLocation = m.nextSibling.firstChild.firstChild.nodeValue;
//							  alert("errLocation = " + errLocation);
      			break;
      
      			case "length": /* This is an optional extension added by GED July 20, 2001 */
      			    errLen = m.nextSibling.firstChild.firstChild.nodeValue;
      			break;
      			case "word":
       			    errWord = m.nextSibling.firstChild.firstChild.nodeValue;
      			break;			

      			default:
      			alert(" ["+n+"] is not a recognised response member") 
				  }			
				
  	} 

    if (errLocation != -1 && errSuggest != -1 && errWord != -1)
  	{   /* if we set all of those then we are ready to build a JS array entry... */
  			errLocation = errLocation + "";
  	    jsdata[errLocation] = new mistakeWord(errLocation, errWord, errSuggest, errLen);  
  	}	 

	}
callSpellerUpdate();
return;
}

function LoadDocument(url)
{
  xmlDoc.load(url);
}


var jsdata = new Array();

// Netscape 6 and IE5
if (document.implementation && document.implementation.createDocument)
{
  var xmlDoc = document.implementation.createDocument("", "test", null);
  xmlDoc.addEventListener("load", documentLoaded, false);
}
else if (document.all && document.getElementById)
{
  var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
  xmlDoc.async = true;
  xmlDoc.onreadystatechange = loadNextDoc;
}
