
var limitCount = 0;
var unProcessed;
var xmldoc;
   
      if(typeof(DOMParser) == 'undefined') {
   
       DOMParser = function() {}
   
       DOMParser.prototype.parseFromString = function(str, contentType) {
   
        if(typeof(ActiveXObject) != 'undefined') {
   
         var xmldata = new ActiveXObject('MSXML.DomDocument');
   
         xmldata.async = false;
   
         xmldata.loadXML(str);
   
         return xmldata;
  
        } else if(typeof(XMLHttpRequest) != 'undefined') {
  
         var xmldata = new XMLHttpRequest;
  
         if(!contentType) {
  
          contentType = 'application/xml';
  
         }
  
         xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
  
         if(xmldata.overrideMimeType) {
  
          xmldata.overrideMimeType(contentType);
  
         }
 
         xmldata.send(null);
  
         return xmldata.responseXML;
  
        }
 
       }
  
      }
function disp(name, toggle) {
	var dispId = document.getElementById(name);
	if (toggle) {
		dispId.style.display = "block";
	} else {
		dispId.style.display = "none";
	}
}
function loadXML(url) {
    //From JavaScript:  The Definitive Guide (4th Edition)
    // http://www.webreference.com/programming/javascript/definitive/chap17/11.html
    // Use the standard DOM Level 2 technique, if it is supported
    if (document.implementation && document.implementation.createDocument) {
        // Create a new Document object
        xmldoc = document.implementation.createDocument("", "", null);
        // Specify what should happen when it finishes loading
        //xmldoc.onload = function(  ) { handler(xmldoc); }
        // And tell it what URL to load
      
        xmldoc.load(url);
    }
    // Otherwise, use Microsoft's proprietary API for Internet Explorer
    else if (window.ActiveXObject) { 
        xmldoc = new ActiveXObject("Microsoft.XMLDOM");   // Create doc
       // xmldoc.onreadystatechange = function(  ) {              // Specify onload
       //     if (xmldoc.readyState == 4) handler(xmldoc, url);
       // }
        xmldoc.load(url);                                     // Start loading!
    }
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
		
function getElementByAttribute(source,element,attName,attValue){


    allElements = source.getElementsByTagName(element);

    for (var i=0;i<allElements.length;i++){

        if (allElements[i].getAttribute(attName)==attValue){
            return allElements[i];
        } 
    }
    return null;
}
function expandSearch() {
	disp('expander',0);
	disp('advancedText',1);
	addLimiter();
	disp('addAnother',1);
}

function addLimiter() {
	var searchSpace = document.getElementById("advancedSearch");
	
	limiterDiv = document.createElement("div");
	limiterDiv.className = "limiterEntry";

	limiterDiv.innerHTML += "<span class='search_type' id='type_0'><select name='limit_"+limitCount+"_type' onchange='placeholder(this)'><option value='note'>Notes</option><option value='bibl'>Bibliography</option><option value='date'>Date</option><option value='name'>Name</option></select></span>";
	limiterDiv.innerHTML += "<span class='search_value' id='value_0'><input name='limit_"+limitCount+"_value' type='text' /></span>";
    
    //limiterDiv.innerHTML += "<span style='cursor: pointer' onclick='toggleOperator(this)'>OR</span>";
	
	limiterDiv.innerHTML += " <a style='cursor: pointer' onclick='removeMe(this)'><img style='vertical-align: text-top' src='images/btn_remove.jpg' /></a>";
	
	searchSpace.appendChild(limiterDiv);
	
	limitCount ++;
}
function toggleOperator(opNode){
    var curValue = opNode.innerHTML;
    if (curValue=="OR"){
        opNode.innerHTML = "AND";
    }
    else {
        opNode.innerHTML = "OR";
    }
}
function placeholder(aSelect) {

    if (aSelect.value=="name") {
       
    
        input = aSelect.parentNode.nextSibling;
        
        
        input.innerHTML = "";
        sText = getNameSelect();
      
        input.innerHTML=sText;
    }
    else {
        old = aSelect.parentNode.nextSibling;
        old.innerHTML="";
    
        newnode = document.createElement("input");
        newnode.setAttribute("type","text");
        

        old.appendChild(newnode);
    }
    
}
function getNameSelect(){

    req = GetXmlHttpObject();
	req.open('GET','./getNameSelect.php',false);
	req.send(null);
    
    return req.responseText;
}
function removeMe(node){
	node.parentNode.parentNode.removeChild(node.parentNode);
	limitCount --;
	
	if (limitCount == 0) {
		revertSearchBox();
	}
	
}

function revertSearchBox() {
	disp("advancedText", 0);
	disp("addAnother",0);
	disp("expander",1);
}

function showArray() {
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function importXML()
{
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = createTable;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createTable()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	xmlDoc.load("emperors.xml");
}




function processSearch () {

	var params = new Array();
	
	me = document.getElementById('advancedSearch');
	
	list = document.getElementById("sectionsList");
	lis = list.getElementsByTagName("li");
	for (i = 0; i < lis.length; i++) {
		if (lis[i].firstChild.checked) {
			var j = params.length;
		
		params[j] = new Array();
		params[j][0] = ("section");
		params[j][1] = lis[i].firstChild.id;
		}
	}
	for (i = 0; i < me.childNodes.length; i++) {
		var j = params.length;
		
		params[j] = new Array();
		params[j][0] = (me.childNodes[i].firstChild.firstChild.value);
		if (params[j][0]!="name"){
		params[j][1] = (me.childNodes[i].childNodes[1].firstChild.value);
		}
		else{
			
		   var svalue = me.childNodes[i].childNodes[1].firstChild.value;
			
		if (svalue==undefined){
		   svalue = me.childNodes[i].childNodes[1].childNodes[1].value;
		   }
		   params[j][1] = svalue;
}		
	}
    var terms = document.getElementById("searchstring").value;
    
	var req = GetXmlHttpObject();
	req.open('POST','./processSearch.php',false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	var query = params.join("_");
     
	req.send("terms="+terms+"&"+"limits="+query);
	
	xmlstring = req.responseText;


	   var resultBox = document.getElementById("content");
	   resultBox.innerHTML = xmlstring;
	
	 findAllOccurences(terms);
	 
}
function prevSection(){
    var book = document.getElementById("books").value;
    var sections = document.getElementById("sections");
    var curSec = sections.selectedIndex;
   // var totalBooks = document.getElementById("books").getElementsByTagName("option").length;
       
    if (curSec>0){
        curSec--;
        sections.selectedIndex = curSec;
    
    section = sections.value;
   
    getSection(book,section);
    }
    else if (document.getElementById("books").selectedIndex>0){
        
        document.getElementById("books").selectedIndex--;
        book = document.getElementById("books").value;
        getSections(book);
        document.getElementById("sections").selectedIndex = document.getElementById("sections").getElementsByTagName("option").length-1;
   startSection = document.getElementById("sections").getElementsByTagName("option")[0].getAttribute("value");
//   alert(startSection.getAttribute("value"));
    getSection(book,startSection);
        
    }
}
function nextSection(){
    var book = document.getElementById("books").value;
    var sections = document.getElementById("sections");
    var curSec = sections.selectedIndex;
    var totalBooks = document.getElementById("books").getElementsByTagName("option").length;
       
    if (curSec<sections.length-1){
        curSec++;
        sections.selectedIndex = curSec;
    
    section = sections.value;
   
    getSection(book,section);
    }
    else if (document.getElementById("books").selectedIndex<totalBooks-1){
      
        document.getElementById("books").selectedIndex++;
        book = document.getElementById("books").value;
        getSections(book);
        
   startSection = document.getElementById("sections").getElementsByTagName("option")[0].getAttribute("value");
//   alert(startSection.getAttribute("value"));
    getSection(book,startSection);
        
    }
}

function nodeText(node){
    if (!(node.textContent)){
        tc = node.firstChild.nodeValue;
        tc = tc.replace(/\s+/g," ");
        return node.firstChild.nodeValue;
    }
    else{
        return node.textContent;
    }
}
/*function processXML(xmlstring){
      var resultBox = document.getElementById("content");
    
    if (xmlstring.length>0){
  
 unProcessed = (new DOMParser()).parseFromString(xmlstring, "text/xml");   
   
  //alert(xmlstring);

    var allresults = unProcessed.firstChild.childNodes;
     resultBox.innerHTML = "";
    // alert(allresults.length);
   for (var k=0;k<allresults.length;k++){
    
    var allkids = allresults.item(k).childNodes;
   // alert(allkids.length);
   
    
    for (var i=0;i<allkids.length;i++){
  
    
        switch (allkids.item(i).nodeName.toLowerCase()) {
            case "div0":
              //       var bookheader = document.createElement("div");
              //          bookheader.className="bookHeader";
              //   bookheader.style.fontWeight="bold";
              //     bookheader.appendChild(document.createTextNode(allkids.item(i).getAttribute("id")+": "));
              //  resultBox.appendChild(bookheader);
                thisNode = document.createElement("div");
                thisNode.className = allkids.item(i).getAttribute("type");
                processDiv0(allkids.item(i),thisNode);
                resultBox.appendChild(thisNode);
                break;
            case "div1":
              //   var spanheader = document.createElement("div");
              //    spanheader.className="spanHeader";
              //   spanheader.style.fontWeight="bold";
              //     spanheader.appendChild(document.createTextNode("Section "+allkids.item(i).getAttribute("n")+": "));
                   
              //  resultBox.appendChild(spanheader);
               
                 thisNode = document.createElement("div");
                thisNode.className = allkids.item(i).getAttribute("type");
                processDiv1(allkids.item(i),thisNode);
                resultBox.appendChild(thisNode);
                break;
            case "p":
                var thisNode = document.createElement("p");
                processP(allkids.item(i),thisNode);
                resultBox.appendChild(thisNode);   
                break;
           case "name":
                //("NAME: "+allkids[i].textContent);
                var thisNode = document.createElement("span");
                thisNode.setAttribute("onclick","alert('"+allkids.item(i).getAttribute("reg")+"')");
                thisNode.appendChild(document.createTextNode(nodeText(allkids.item(i))));
                resultBox.appendChild(thisNode);   
                break; 
             case "ref":
                var thisNode = document.createElement("span");
                thisNode.setAttribute("onclick","alert('"+allkids.item(i).getAttribute("targets")+"')");
                processLink(allkids.item(i),thisNode);
                resultBox.appendChild(thisNode);   
                break; 
             case "head":
                var thisNode = document.createElement("h4");
                thisNode.className="header";
                thisNode.appendChild(document.createTextNode(allkids.item(i).nodeValue)); 
                resultBox.appendChild(thisNode);
                break;  
                   case "foreign":
                thisNode = document.createElement("span");
           
               thisNode = document.createTextNode(nodeText(allkids.item(i)));
                resultBox.appendChild(thisNode);   
                break; 
             case "#text":
                 var thisNode = document.createTextNode(allkids.item(i).nodeValue);
                 resultBox.appendChild(thisNode);
                 break;
        }
    }
  if (allresults.length>1)
  {resultBox.appendChild(document.createElement("hr")); 
   }
  
    }
   
  
   }
   else{
       resultBox.innerHTML="<span style='color: red'>Not found</span>";
   }
    return;
}
function processDiv0(source, div0container){
    var allkids = source.childNodes;

    for (var i=0;i<allkids.length;i++){
        switch (allkids.item(i).nodeName.toLowerCase()) {
            
            case "div1":

                 thisNode = document.createElement("div");
                thisNode.className = allkids.item(i).getAttribute("type");
                processDiv1(allkids.item(i),thisNode);
                div0container.appendChild(thisNode);
                break;
                   case "foreign":
                thisNode = document.createElement("span");
           
               thisNode = document.createTextNode(nodeText(allkids.item(i)));
                div0container.appendChild(thisNode);   
                break; 
              case "#text":
                 thisNode = document.createTextNode(allkids.item(i).nodeValue);
                 div0container.appendChild(document.createTextNode(thisNode.nodeValue));
                 break;
       
        }

}
return;
}

function processDiv1(source, div1container){
    var allkids = source.childNodes;
//resultBox = document.getElementById("content");
//(allkids.length);
    for (var i=0;i<allkids.length;i++){
       //(i);
        switch (allkids.item(i).nodeName.toLowerCase()) {
            
             case "p":
                var thisNode = document.createElement("p");
                processP(allkids.item(i),thisNode);
                div1container.appendChild(thisNode);   
                break;
             case "name":
                var thisNode = document.createElement("span");
                thisNode.setAttribute("onclick","alert('"+allkids.item(i).getAttribute("reg")+"')");
                thisNode.appendChild(document.createTextNode(nodeText(allkids.item(i))));
             
                div1container.appendChild(thisNode);   
                break; 
             case "ref":
                var thisNode = document.createElement("span");
           
                processLink(allkids.item(i),thisNode);
                div1container.appendChild(thisNode);   
                break; 
             case "#text":
          
                 var thisNode = document.createTextNode(allkids.item(i).nodeValue.replace(/\t/g,""));
                
                 div1container.appendChild(thisNode);
                 break;
              case "foreign":
                thisNode = document.createElement("span");
           
               thisNode = document.createTextNode(nodeText(allkids.item(i)));
                div1container.appendChild(thisNode);   
                break; 
             case "head":
                 
                var thisNode = document.createElement("h4");
                thisNode.className="header";
                //(allkids.item(i).firstChild.nodeValue);
                thisNode.appendChild(document.createTextNode(allkids.item(i).firstChild.nodeValue));
                //(thisNode);
                div1container.appendChild(thisNode);   
                break;
       
        }

}
//("done");
return;
}
function processP(source, pcontainer){
    //resultBox = document.getElementById("content");
    var allkids = source.childNodes;
    //alert("P kids: "+allkids.length);
    for (var i=0;i<allkids.length;i++){
 //alert("PPP: "+allkids[i].nodeName);
        switch (allkids.item(i).nodeName.toLowerCase()) {
              case "name":
            
                var thisNode = document.createElement("span");
                thisNode.setAttribute("onclick","alert('"+allkids.item(i).getAttribute("reg")+"')");
                thisNode.appendChild(document.createTextNode(nodeText(allkids.item(i))));
             
                pcontainer.appendChild(thisNode);   
                break; 
             case "ref":
           //  alert(ref);
                var thisNode = document.createElement("span");
                
                processLink(allkids.item(i),thisNode);
                pcontainer.appendChild(thisNode);   
                break; 
             case "#text":
          
                 var thisNode = document.createTextNode(allkids.item(i).nodeValue.replace(/\t/g,""));
                 pcontainer.appendChild(thisNode);
                 break;
             case "head":
                var thisNode = document.createElement("span");
                thisNode.className="header";
                pcontainer.appendChild(document.createTextNode(thisNode.nodeValue));   
                break;
             case "foreign":
                var thisNode = document.createElement("span");
                
               thisNode = document.createTextNode(nodeText(allkids.item(i)));
                pcontainer.appendChild(thisNode);   
                break; 
       
        }

}

return;
}
function processBibl(source, biblcontainer){
    //resultBox = document.getElementById("content");
    var allkids = source.childNodes;
    //alert("P kids: "+allkids.length);
    for (var i=0;i<allkids.length;i++){
 //alert("PPP: "+allkids[i].nodeName);
        switch (allkids.item(i).nodeName.toLowerCase()) {
              case "anaytic":
            
                var thisNode = document.createElement("span");
                thisNode.setAttribute("onclick","alert('"+allkids.item(i).getAttribute("reg")+"')");
                thisNode.appendChild(document.createTextNode(allkids.item(i).textContent));
             
                biblcontainer.appendChild(thisNode);   
                break; 
             case "ref":
           //  alert(ref);
                var thisNode = document.createElement("span");
                
                processLink(allkids.item(i),thisNode);
                biblcontainer.appendChild(thisNode);   
                break; 
             case "#text":
          
                 var thisNode = document.createTextNode(allkids.item(i).nodeValue);
                 biblcontainer.appendChild(thisNode);
                 break;
             case "head":
                var thisNode = document.createElement("span");
                thisNode.className="header";
                biblcontainer.appendChild(document.createTextNode(thisNode.nodeValue));   
                break;
             case "foreign":
                var thisNode = document.createElement("span");
                
               thisNode = document.createTextNode(nodeText(allkids.item(i)));
                biblcontainer.appendChild(thisNode);   
                break; 
       
        }

}

return;
}
function processLink(source, refcontainer){

                  linktype = source.getAttribute("type");
                  if (linktype=="note"){
    
                    var notenum = source.getAttribute("target");
                   refcontainer.id="note_"+notenum.substring(2);
                   refcontainer.className="noteholder";
                
             
                var thisNode = document.createElement("sup");
                thisNode.appendChild(document.createTextNode("["));
                var noteref = document.createElement("a");
               
                noteref.setAttribute("onclick","createNote('"+notenum+"')");
                noteref.appendChild(document.createTextNode(notenum.substring(2)));
                thisNode.appendChild(noteref);
                thisNode.appendChild(document.createTextNode("]"));
                refcontainer.appendChild(thisNode);   
                  }
                  else{
                      var bibref = source.getAttribute("target");
                      var xmlstring = getBib(bibref);
                      
                        
                      var thisNode = document.createElement("span");
                      thisNode.className="bibref";
                      var bibText = getBib(bibref);
                      thisNode.setAttribute("onmouseover","alert("+bibText+")");
                      //alert(nodeText(source));
                      thisNode.appendChild(document.createTextNode(nodeText(source)));
                      refcontainer.appendChild(thisNode);
                  }
                 return;
                 
   
       
        


}

function processAnalytic(analytic){
       /* var bibstring = "";
          authors = analytic.getElementsByTagName("author");
       
        for (var i=0;i<authors.length;i++){
            bibstring = bibstring + nodeText(authors[i])+", ";
        }
        if (bibstring.length>0){
            bibstring = bibstring.substring(0,bibstring.length-2)+". ";
            
        }
     
        titles = analytic.getElementsByTagName("title")
        if (titles){
            titleparts = titles[0].childNodes;
        }
        for (var m=0;m<titleparts.length;m++){
            if (titleparts.nodeName == "emph"){
                
            }
        } 
        alert(title);
        title = title.replace(/emph/g,"i");
        bibstring = bibstring+title;
        alert(bibstring);
        return;
        //bibstring; 
}*/
function processSingle(mono){
    var monostring = "";
     authors = analytic.getElementsByTagName("author");
       
        for (var i=0;i<authors.length;i++){
            authorstring = authorstring + authors[i]+", ";
        }
        if (authorstring.length>0){
            authorstring = authorstring.substring(0,authorstring.length-2)+". ";
            
        }
    var title = mono.getElementsByTagName("title")[0];
    if (!title){
        title = "";
    }
    else {
        title = "<i>"+title+"</i>.  ";
    }
    var vol = getElementsByAttributes(mono,"bibScope","type","vol");
    if (!vol){
        vol = "";
    }
   
    var pages = getElementsByAttributes(mono,"bibScope","type","pages");
    if (!pages){
        pages="";
    }
    else{
        pages = ": "+pages+".";
    }
    var date = getElementsByTagName("date")[0];
       if (!date){
        date = "";
    }
    else {
        date = date+".  ";
    }
    monostring = monostring+date+authorstring+title+vol+pages;
     
    return monostring;
    
    
}
function processMono(mono){
    var monostring = "";
    var title = mono.getElementsByTagName("title")[0];
    if (!title){
        title = "";
    }
    else {
        title = "<i>"+title+"</i>.  ";
    }
    var vol = getElementsByAttributes(mono,"bibScope","type","vol");
    if (!vol){
        vol = "";
    }
   
    var pages = getElementsByAttributes(mono,"bibScope","type","pages");
    if (!pages){
        pages="";
    }
    else{
        pages = ": "+pages+".";
    }
    var date = getElementsByTagName("date")[0];
       if (!date){
        date = "";
    }
    else {
        date = date+".  ";
    }
    monostring = monostring+date+title+vol+pages;
     
    return monostring;
    
    
}
function init(){
    startBook = document.getElementById("books").getElementsByTagName("option")[0].value;
  document.getElementById("books").selectedIndex=0;
  //alert(startBook.value);
   getSections(startBook);
  // alert(startBook);
   startSection = document.getElementById("sections").getElementsByTagName("option")[0].getAttribute("value");

//   alert(startSection.getAttribute("value"));
    getSection(startBook,startSection);

	
	

    
}
  function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
function getSections(book){
        scselect = document.getElementById("sectionSelect");
        
    req = GetXmlHttpObject();
	req.open('GET','./getSections.php?book='+book,false);
	req.send(null);
    scselect.innerHTML = req.responseText;
}
function changeBook(){
    var book = document.getElementById("books").value;
    //book = document.getElementById("books").options[index].value;

    getSections(book);
    scene = document.getElementById("sections").value;
    getSection(book,scene);
    
}
function changeSection(){
    var book = document.getElementById("books").value;

    section = document.getElementById("sections").value;
    getSection(book,section);
    
}

function showBib(bibContainer) {
  
    book = getContainingBook(bibContainer);
    //alert(book);
        var req = GetXmlHttpObject();
	req.open('GET','./getBib.php?bibref='+bibContainer.id+'&book='+book,false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	

	req.send(null);
	var bibtarget = document.getElementById("bibtarget");
	moveHereY = findPosY(bibContainer)+20;
	moveHereX = findPosX(bibContainer)+20;
	
	if ((moveHereX + 300) > 800) {
		moveHereX = 500;
	}
	
	bibtarget.style.top = moveHereY+"px";
	bibtarget.style.left = moveHereX+"px";
	bibtarget.innerHTML = req.responseText;
    bibtarget.style.display="block";
               
}
function hideBib(bibContainer) {
    var bibtarget = document.getElementById("bibtarget");
    bibtarget.style.display="none";
                     
}
function getSection(book,section){

    var req = GetXmlHttpObject();
	req.open('POST','./getSection.php',false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	
	

	req.send("book="+book+"&section="+section);
	
	secText = req.responseText;

	resultBox = document.getElementById("content");
	resultBox.innerHTML = "<div class='textContainer' id='"+book+"'>"+secText+"</div>";


}
function getContainingBook(node){
  
    while ((node.parentNode) && (node.className!="textContainer")){
        node = node.parentNode;
      
    }
    if (node.className=="textContainer"){
        //alert(node.id);
        return node.id;
        
    }
    else{
        return null;
    }
}
function createNote(thenode,notenum) {
  
    book = getContainingBook(thenode);
    
     
    var req = GetXmlHttpObject();
	req.open('GET','./getNotePopup.php?notenum='+notenum+'&book='+book,false);
	req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	

	req.send(null);
	
	notetext = req.responseText;
     
    
     num = notenum.substring(2);

	if (openNotes[num] != true) {
		
		
		
	
		notePopup(notetext, num);
		openNotes[num] = true;
	}
}

function findWord(word){
    resultBox = document.getElementById("content");
    var reg = new RegExp("\\W"+word+"\\W");
   
    //(resultBox.textContent.match(reg).index);
    //offset = resultBox.textContent.indexOf(word);
    offset = resultBox.textContent.match(reg).index+1;
    
    
    thislength = word.length;
    highlightOffset(offset,thislength,resultBox);
    
}
function findAllOccurences(word){
    resultBox = document.getElementById("content");
    var reg = new RegExp("(>[^<]+\\s)("+word+")(\\W)","gi");
    var haystack = resultBox.innerHTML;
    haystack = haystack.replace(reg,"$1<span class='highlight'>$2</span>$3");
    resultBox.innerHTML = haystack;
    //(resultBox.textContent.match(reg).index);
    //offset = resultBox.textContent.indexOf(word);
   // offset = resultBox.textContent.match(reg).index+1;
   
   /* while (offset>=0){
        
    thislength = word.length;
    highlightOffset(offset,thislength,resultBox);
    text = resultBox.textContent.substring(offset+word.length);
    if (text.match(reg)){
    offset = offset+text.match(reg).index+word.length+1;
    }
    else{
        offset=-1;
    }
   //// offset = resultBox.textContent.indexOf(word,offset+word.length);
    }*/
    window.scroll(0,0);
}
function highlightOffset(offset,wordlength,original){
     
    /*
    var result = getLocOfTextOffset(offset,rootNode,0);

    alert("offset "+offset+" wordlength: "+wordlength);
    var endoff = parseInt(offset)+parseInt(wordlength);
    alert("endoff: "+endoff);
    var endresult = getLocOfTextOffset(endoff,rootNode,0);
   // alert(endresult[0]);
    alert(result[0]+" *** "+result[1]);
    var myRange = document.createRange();
    alert("still good");
    alert(result[0].childNodes.length);
    alert("first child "+result[0].firstChild.length);
    myRange.setStart(result[0],result[1]);
    
    alert("Start is: "+ result[1]);
    myRange.setEnd(endresult[0],endresult[1]);
    highlight = document.createElement("span");
    highlight.style.backgroundColor="yellow";
    myRange.surroundContents(highlight);*/
}

function getLocOfTextOffset(offset,rootNode,initialPos){
    var rootKids = rootNode.childNodes;
    
    if (rootKids.length>0){
    var counter = initialPos;
   // alert("counter: "+counter);
    var i = 0;
    goOn = true;

    while (goOn){
        rootText = rootKids.item(i).textContent;
        counter = counter+rootText.length;
        i++;

        if ((i>=rootKids.length) || (counter>=offset)){
            goOn=false;
        }
    
    }
      lastKid = rootKids.item(i-1);  
    }
    else{
        rootText = rootNode.textContent;
        counter = initialPos+rootText.length;
        lastKid = rootNode;
    }
    if (counter<offset){
        return false;
    }
    else if (counter==offset){
        //alert(counter);
        //alert("exact match");
        result = new Array(rootKids.item(i),0);
        return result;
    }
    else {

 
        amountOver = counter-offset;
      
        if ((rootKids.length==1) || (rootKids.length==0)){
            lastKidText = lastKid.textContent;
          result = new Array(lastKid,(lastKidText.length-amountOver));
    
          return result;
        }
        else {
            //alert("recurse");
            return getLocOfTextOffset(offset,lastKid,counter-lastKid.textContent.length);
        }
    }
    
}
