window.onload = loadIndex;

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


function searchHSCodeIndex() { // search the index
	if (!xmlDoc) {
		loadIndex();
	}
// get the search term from a form field with id 'searchme'
	var searchterm = document.getElementById("search_HS_Codes").value;
	var allitems = xmlDoc.getElementsByTagName("HS_Chapter");
	var returnitemSubnumber = xmlDoc.getElementsByTagName("HS_SubNumber");
	var returnitemTitle = xmlDoc.getElementsByTagName("HS_SubNumber_Title_EL");

	results = new Array;
		if (searchterm.length != 4) {
			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(returnitemSubnumber[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(returnitemTitle[i]);
			}
		}


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


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

	if (results.length > 0) {
// if there are any results, put them in a list inside the "results_HS_Codes" div
		var results_HS_Codes = document.getElementById("results_HS_Codes");
		while(results_HS_Codes.firstChild)results_HS_Codes.removeChild(results_HS_Codes.firstChild)
			var header = document.createElement("h5");
			var list = document.createElement("div");
			list.style.cssText = 'margin-left:20px';
			var searchedfor = document.createTextNode("Έγγραφα με Κωδικός ΕΣ "+searchterm+":");
			results_HS_Codes.appendChild(header);
			header.appendChild(searchedfor);
			results_HS_Codes.appendChild(list);
				for (var i=0;i<results.length;i++) {
					var listitem = document.createElement("div");
					listitem.style.cssText = 'font-weight:bold';
					var Chapteritem = document.createTextNode(results[i].lastChild.nodeValue);
					var textitem = document.createTextNode(": ");
					var SubtitleDiv = document.createElement("SPAN");
					SubtitleDiv.innerHTML = results2[i].lastChild.nodeValue;
					SubtitleDiv.style.cssText = "font-weight:normal";
					list.appendChild(listitem);
					listitem.appendChild(Chapteritem);
					listitem.appendChild(textitem);
					listitem.appendChild(SubtitleDiv);
		}
	} 
	else {
// else tell the user no matches were found
		var results_HS_Codes = document.getElementById("results_HS_Codes");
		while(results_HS_Codes.firstChild)results_HS_Codes.removeChild(results_HS_Codes.firstChild)
		var para = document.createElement("p");
		var notfound = document.createTextNode("Συγνώμη, δεν υπάρχουν έγγραφα που είναι διαθέσιμα με τον κωδικό ΕΣ "+searchterm +"!");
		results_HS_Codes.appendChild(para);
		para.appendChild(notfound);
	}
}
