window.onload = loadIndex;

function loadIndex() { // load indexfile
// Browsers other than MSIE
	if (window.XMLHttpRequest) {
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", "./XML_Data/LegalDocs.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("./XML_Data/LegalDocs.xml");
	}
}


function searchKeywordIndex() { // 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("text_string_EL");
	var returnitemdocnumber = xmlDoc.getElementsByTagName("DocNumber");

	results = 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) {
				results.push(returnitemdocnumber[i]);
			}
		}


	results2 = 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) {
				results2.push(allitems[i]);
			}
		}

// send the results to another function that displays them to the user
	showKeywordResults(results, results2, searchterm);
	}
}


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

	if (results.length > 0) {
// if there are any results, put them in a list inside the "results_keyword_search" div
		var results_keyword_search = document.getElementById("results_keyword_search");
		while(results_keyword_search.firstChild)results_keyword_search.removeChild(results_keyword_search.firstChild)
			var header = document.createElement("h5");
			var list = document.createElement("ul");
			var searchedfor = document.createTextNode("Έγγραφα με τη λέξη-κλειδί "+searchterm);
			results_keyword_search.appendChild(header);
			header.appendChild(searchedfor);
			results_keyword_search.appendChild(list);
				for (var i=0;i<results.length;i++) {
					var listitem = document.createElement("li");
					var item = document.createTextNode(results[i].lastChild.nodeValue + ": ");
					var itemTitle = document.createTextNode(results2[i].lastChild.nodeValue);
					list.appendChild(listitem);
					listitem.appendChild(item);
					listitem.appendChild(itemTitle);
				}
	}
	else {
// else tell the user no matches were found
		var results_keyword_search = document.getElementById("results_keyword_search");
		while(results_keyword_search.firstChild)results_keyword_search.removeChild(results_keyword_search.firstChild)
			var para = document.createElement("p");
			var notfound = document.createTextNode("Συγνώμη, δεν υπάρχουν έγγραφα με τη λέξη-κλειδί "+searchterm +"!");
			results_keyword_search.appendChild(para);
			para.appendChild(notfound);
		}
}
