
window.onload = loadIndex;
function loadIndex() { // load indexfile
	var xml = document.getElementById("db").value;

 // most current browsers support document.implementation
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.load(xml);
	}
// MSIE uses ActiveX
	else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
 		xmlDoc.load(xml);
	}
}


function searchIndex() { // search the index (duh!)
	if (!xmlDoc) {
		loadIndex();
	}
	// get the search term from a form field with id 'searchme'

	var searchterm = document.getElementById("searchme").value;
	var allitems = xmlDoc.getElementsByTagName("subtitle");
	results = new Array;
	if (searchterm.length < 2) {
		alert("Enter at least two characters");
	} else {
		for (var i=0;i<allitems.length;i++) {
// see if the XML entry matches the search term,
// and (if so) store it in an array
			var name = allitems[i].lastChild.nodeValue;
			var exp = new RegExp(searchterm,"i");
			if ( name.match(exp) != null) {
				results.push(allitems[i]);
			}
		}
// send the results to another function that displays them to the user
	showResults(results, searchterm);
	}
}


// The following is just an example of how you
// could handle the search results
function showResults(results, searchterm) {

	if (results.length > 0) {
// if there are any results, put them in a list inside the "resultshere" div
		var resultshere = document.getElementById("resultshere");
		var header = document.createElement("h5");
		var list = document.createElement("ul");
		var searchedfor = document.createTextNode("You've searched for "+searchterm);
		resultshere.appendChild(header);
		header.appendChild(searchedfor);
		resultshere.appendChild(list);
		for (var i=0;i<results.length;i++) {


			var item = document.createTextNode(results[i].lastChild.nodeValue);
			var link = '#' + results[i].lastChild.nodeValue;
			var listitem = document.createElement("li");

			list.appendChild(listitem);
			var listlink = document.createElement("a");
			listitem.appendChild(listlink);
			listlink.setAttribute('href', link);
			listlink.appendChild(item);			


//			listitem.appendChild(item);
			
			
//		newlink = document.createElement('a');
//		newlink.setAttribute('href', ''+item);
//		div.appendChild(newlink);
// div = document.getElementById('foo');
// newlink = document.createElement('a');
// div.appendChild(newlink);
		}
	} else {
// else tell the user no matches were found
		var resultshere = document.getElementById("resultshere");
		var para = document.createElement("h5");
		var notfound = document.createTextNode("Sorry, I couldn't find anything like "+searchterm +"!");
		resultshere.appendChild(para);
		para.appendChild(notfound);
	}
}

