window.onload = loadIndex;

function loadIndex() { // load indexfile
// Browsers other than MSIE
	if (window.XMLHttpRequest) {
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", "Glossary_Dictionary_EN.xml", false);
		xmlhttp.send(null);
		xmlDoc = xmlhttp.responseXML.documentElement;
	}
// MSIE uses ActiveX
	else if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.load("Glossary_Dictionary_EL.xml");
	}
}


function searchWordIndex() { // search the index
	if (!xmlDoc) {
		loadIndex();
	}
// get the search term from a form field with id 'search_Keyword'
	var searchterm = document.getElementById("search_Keyword").value;
	var allitems = xmlDoc.getElementsByTagName("Word");
	var returnDescription = xmlDoc.getElementsByTagName("Description_EL");

	resultsWord = new Array;
		if (searchterm.length < 1) {
			alert("Ξεχάσατε να βάλετε κάποιον όρο στην αναζήτηση!");
		} 
		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) {
				resultsWord.push(allitems[i]);
			}
		}


	resultsDescription = new Array;
			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 name2 = allitems[i].lastChild.nodeValue;
				var exp2 = new RegExp(searchterm,"i");
				if (name2.match(exp2) != null) {
				resultsDescription.push(returnDescription[i]);
			}
		}


// send the results to another function that displays them to the user
	showWordResults(resultsWord, resultsDescription, searchterm);
	}
}


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

	if (resultsWord.length > 0) {
// if there are any results, put them in a list inside the "results_word_search" div
		var results_word_search = document.getElementById("results_word_search");
		while(results_word_search.firstChild)results_word_search.removeChild(results_word_search.firstChild)
		var tableitem = document.createElement("table");
		tableitem.style.cssText = 'border-collapse:collapse; background-color:#FFFFFF';
		var tbodyitem = document.createElement("tbody");
		tableitem.appendChild(tbodyitem);
		results_word_search.appendChild(tableitem);
			for (var i=0;i<resultsWord.length;i++) {
				var row = document.createElement("tr");
				var cell1 = document.createElement("td");
				cell1.style.cssText = 'width:150px; font-weight:bold';
				var cell2 = document.createElement("td");
				var cell3 = document.createElement("td");
				cell3.style.cssText = 'width:680px';
				var WordDiv = document.createElement("div");
				WordDiv.innerHTML = resultsWord[i].lastChild.nodeValue;
				var DefinitionDiv = document.createElement("div");
				DefinitionDiv.innerHTML = resultsDescription[i].lastChild.nodeValue;
				tbodyitem.appendChild(row);
				row.appendChild(cell1);
				cell1.appendChild(WordDiv);
				row.appendChild(cell2);
				cell2.appendChild(DefinitionDiv);
			}}
	else {
// else tell the user no matches were found
		var results_word_search = document.getElementById("results_word_search");
		while(results_word_search.firstChild)results_word_search.removeChild(results_word_search.firstChild)
			var para = document.createElement("p");
			var notfound = document.createTextNode("Συγνώμη, δεν υπάρχει όρος ή ορισμός που περιέχει το κείμενο "+searchterm +"!");
			results_word_search.appendChild(para);
			para.appendChild(notfound);
		}
}

