
function searchLoad() {
	/* Set up AJAX */
	var requestObj = new XMLHttpRequest();
	var url = "library.xml";
	requestObj.open("GET", url, true);
	requestObj.send(null);
	
	/* Get reply */
	requestObj.onreadystatechange = function() {
		if (requestObj.readyState == 4) {
			if (requestObj.status == 200) {
				AJAXresponse = requestObj.responseXML;
				addResults("");
			}
			else
				alert(requestObj.status);
		}
	}
	
	data = "title";
	
	/* Attach event listener for search box */
	document.getElementById("textname").addEventListener("keyup", textSearch, false);
	document.getElementById("dataspan").addEventListener("click", changeData, false);
}

function textSearch() {
	var textName = document.getElementById("textname");
	var dataDiv = document.getElementById("datadiv");
	
	/* Remove all old results */
	removeChildren(dataDiv);
	
	/* Create a new table */
	var newtable = document.createElement("table");
	newtable.setAttribute("id", "datatable");
	newtable.setAttribute("class", "datatable");
	dataDiv.appendChild(newtable);
	
	addResults(textName.value);
}

function addResults(text) {
	var xml = AJAXresponse.getElementsByTagName("song");
	
	/* Create regexp */
	var pattern = text;
	var nameRegExp = new RegExp(pattern, "i");
	
	for (var i = 0; i < xml.length; i++) {
		/* Check search criteria */
		var mysearch;
		if (data == "title")
			mysearch = xml[i].getAttribute("title");
		else if (data == "arranger")
			mysearch = xml[i].getAttribute("arranger");
		else if (data == "original")
			mysearch = xml[i].getAttribute("original");
		
		/* Check to see the string match */
		if (mysearch.match(nameRegExp))
			addEntry(xml[i]);
	}
	
	highlightTable();
}

function changeData() {
	var dataform = document.getElementById("dataform");
	for (var i = 0; i < dataform.searchdata.length; i++) {
		if (dataform.searchdata[i].checked)
			data = dataform.searchdata[i].value;
	}
	
	textSearch();
}

function addEntry(xmldata) {
	/* Get relevant data from xml */
	var title = xmldata.getAttribute("title");
	var original = xmldata.getAttribute("original");
	var gleek = xmldata.getAttribute("gleek");
	
	/* Special case to remove template */
	if (title == "sampleTitle")
		return;
		
	/* Make a row for the new entry */
	var newrow = document.createElement("tr");
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	td1.setAttribute("class", "titles");
	td2.setAttribute("class", "originals");
	
	/* Fill in actual data */
	var anchor = document.createElement("a");
	anchor.setAttribute("href", "title.php?title=" + title);
	anchor.setAttribute("class", "datalink");
	
	/* Insert the new elements into the document */
	if (gleek == "false")
		title = title + "*";
	anchor.appendChild(document.createTextNode(title));
	td1.appendChild(anchor);
	td2.appendChild(document.createTextNode(original));
	newrow.appendChild(td1);
	newrow.appendChild(td2);
	document.getElementById("datatable").appendChild(newrow);
}

function removeChildren(place) {
	while (place.hasChildNodes()) {
		removeChildren(place.firstChild);
		place.removeChild(place.firstChild);
	}
}

function highlightTable() {
	var mytable = document.getElementById("datatable");
	// Iterate over the rows
	var rows = mytable.getElementsByTagName("tr");
	for (var counter = 0; counter < rows.length; counter++)
	{
		if (!(counter % 2)) {
			rows[counter].style.background = "#CCCCCC";
		}
	}
}
