window.onload = loadIndex;

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


function searchEuropeanIndex() { // search the index
	if (!xmlDoc) {
		loadIndex();
	}
// get the search term from a form field with id 'search_glossary'
	var searchterm = document.getElementById("search_glossary").value;
	var allitems = xmlDoc.getElementsByTagName("Translation_EL");
	var returntranslation = xmlDoc.getElementsByTagName("Word");
	var returnlanguage = xmlDoc.getElementsByTagName("Language");
	var returnDescription = xmlDoc.getElementsByTagName("Description_EL");
	var returnHS_Chapter = xmlDoc.getElementsByTagName("HS_Chapter");
	var returnComments = xmlDoc.getElementsByTagName("Comments_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]);
			}
		}


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

	resultsLanguage = 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 name3 = allitems[i].lastChild.nodeValue;
				var exp3 = new RegExp(searchterm,"i");
				if (name3.match(exp3) != null) {
				resultsLanguage.push(returnlanguage[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 name4 = allitems[i].lastChild.nodeValue;
				var exp4 = new RegExp(searchterm,"i");
				if (name4.match(exp4) != null) {
				resultsDescription.push(returnDescription[i]);
			}
		}

	resultsHS_Chapter = 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 name5 = allitems[i].lastChild.nodeValue;
				var exp5 = new RegExp(searchterm,"i");
				if (name5.match(exp5) != null) {
				resultsHS_Chapter.push(returnHS_Chapter[i]);
			}
		}

	resultsComments = 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 name6 = allitems[i].lastChild.nodeValue;
				var exp6 = new RegExp(searchterm,"i");
				if (name6.match(exp6) != null) {
				resultsComments.push(returnComments[i]);
			}
		}

// send the results to another function that displays them to the user
	showEthnicWordResults(resultsWord, resultsTranslation, resultsLanguage, resultsDescription, resultsHS_Chapter, resultsComments, searchterm);
	}
}



function showEthnicWordResults(resultsWord, resultsTranslation, resultsLanguage, resultsDescription, resultsHS_Chapter, resultsComments, searchterm) {

	if (resultsWord.length > 0) {
// if there are any results, put them in a list inside the "results_native_search" div
		var results_native_search = document.getElementById("results_native_search");
		while(results_native_search.firstChild)results_native_search.removeChild(results_native_search.firstChild)
		var tableitem = document.createElement("table");
		tableitem.style.cssText = 'border-collapse:collapse; background-color:#FFFFFF; margin:0px; width:985px';
		var tbodyitem = document.createElement("tbody");
		tableitem.appendChild(tbodyitem);
		results_native_search.appendChild(tableitem);
		var row1 = document.createElement("tr");
		row1.style.cssText = 'font-weight:bold; background-color:#CCCCCC';
		var cell1 = document.createElement("td");
		cell1.style.cssText = 'width:150px; border-top:0px';
		var cell2 = document.createElement("td");
		cell2.style.cssText = 'width:90px; border-top:0px';
		var cell3 = document.createElement("td");
		cell3.style.cssText = 'width:150px; border-top:0px';
		var cell4 = document.createElement("td");
		cell4.style.cssText = 'border-top:0px';
		var cell5 = document.createElement("td");
		cell5.style.cssText = 'width:100px; border-top:0px';
		var cell6 = document.createElement("td");
		cell6.style.cssText = 'width:150px; border-top:0px';
		tbodyitem.appendChild(row1);
		row1.appendChild(cell1);
		cell1.appendChild(document.createTextNode("Word"));
		row1.appendChild(cell2);
		cell2.appendChild(document.createTextNode("Language"));
		row1.appendChild(cell3);
		cell3.appendChild(document.createTextNode("Translation"));
		row1.appendChild(cell4);
		cell4.appendChild(document.createTextNode("Description"));
		row1.appendChild(cell5);
		cell5.appendChild(document.createTextNode("HS_Chapter"));
		row1.appendChild(cell6);
		cell6.appendChild(document.createTextNode("Comments"));

		for (var i=0;i<resultsWord.length;i++) {
			var row2 = document.createElement("tr");
			var cell7 = document.createElement("td");
			cell7.style.cssText = 'font-weight:bold';
			var cell8 = document.createElement("td");
			var cell9 = document.createElement("td");
			var cell10 = document.createElement("td");
			var cell11 = document.createElement("td");
			var cell12 = document.createElement("td");

			var WordDiv = document.createElement("div");
			WordDiv.innerHTML = resultsWord[i].lastChild.nodeValue;
			var LanguageDiv = document.createElement("div");
			LanguageDiv.innerHTML = resultsLanguage[i].lastChild.nodeValue;
			var TranslationDiv = document.createElement("div");
			TranslationDiv.innerHTML = resultsTranslation[i].lastChild.nodeValue;
			var DescriptionDiv = document.createElement("div");
			DescriptionDiv.innerHTML = resultsDescription[i].lastChild.nodeValue;
			var HS_ChapterDiv = document.createElement("div");
			HS_ChapterDiv.innerHTML = resultsHS_Chapter[i].lastChild.nodeValue;
			var CommentsDiv = document.createElement("div");
			CommentsDiv.innerHTML = resultsComments[i].lastChild.nodeValue;

			tbodyitem.appendChild(row2);
			row2.appendChild(cell7);
			cell7.appendChild(WordDiv);
			row2.appendChild(cell8);
			cell8.appendChild(LanguageDiv);
			row2.appendChild(cell9);
			cell9.appendChild(TranslationDiv);
			row2.appendChild(cell10);
			cell10.appendChild(DescriptionDiv);
			row2.appendChild(cell11);
			cell11.appendChild(HS_ChapterDiv);
			row2.appendChild(cell12);
			cell12.appendChild(CommentsDiv);
			}}
	else {
// else tell the user no matches were found
		var results_native_search = document.getElementById("results_native_search");
		while(results_native_search.firstChild)results_native_search.removeChild(results_native_search.firstChild)
			var divitem = document.createElement("div");
			divitem.style.cssText = 'background-color:#FFFFFF; margin:0px; width:972px; border-style:solid; border-width: 0px 1px 1px 1px; padding:5px 5px 20px 5px';
			var notfound = document.createTextNode("Συγνώμη, δεν υπάρχουν λέξεις στην αναζήτηση που περιέχουν το κείμενο "+searchterm +"!");
			results_native_search.appendChild(divitem);
			divitem.appendChild(notfound);
		}
document.getElementById('SearchDiv').style.display = "block";
for(x=1;x<=9;x++){
ThisDiv = 'RadioGroupName' + x +'Div';
document.getElementById(ThisDiv).style.display = 'none';}
}


function searchGlossaryIndex() { // search the index
	if (!xmlDoc) {
		loadIndex();
	}
// get the search term from a form field with id 'search_glossary'
	var searchterm = document.getElementById("search_glossary").value;
	var allitems = xmlDoc.getElementsByTagName("Word");
	var returntranslation = xmlDoc.getElementsByTagName("Translation_EL");
	var returnlanguage = xmlDoc.getElementsByTagName("Language");
	var returnDescription = xmlDoc.getElementsByTagName("Description_EL");
	var returnHS_Chapter = xmlDoc.getElementsByTagName("HS_Chapter");
	var returnComments = xmlDoc.getElementsByTagName("Comments_EL");

	resultsWord = new Array;
		if (searchterm.length < 1) {
		} 
		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]);
			}
		}


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

	resultsLanguage = 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 name3 = allitems[i].lastChild.nodeValue;
				var exp3 = new RegExp(searchterm,"i");
				if (name3.match(exp3) != null) {
				resultsLanguage.push(returnlanguage[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 name4 = allitems[i].lastChild.nodeValue;
				var exp4 = new RegExp(searchterm,"i");
				if (name4.match(exp4) != null) {
				resultsDescription.push(returnDescription[i]);
			}
		}

	resultsHS_Chapter = 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 name5 = allitems[i].lastChild.nodeValue;
				var exp5 = new RegExp(searchterm,"i");
				if (name5.match(exp5) != null) {
				resultsHS_Chapter.push(returnHS_Chapter[i]);
			}
		}

	resultsComments = 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 name6 = allitems[i].lastChild.nodeValue;
				var exp6 = new RegExp(searchterm,"i");
				if (name6.match(exp6) != null) {
				resultsComments.push(returnComments[i]);
			}
		}

// send the results to another function that displays them to the user
	showKeywordResults(resultsWord, resultsTranslation, resultsLanguage, resultsDescription, resultsHS_Chapter, resultsComments, searchterm);
	}
}



function showKeywordResults(resultsWord, resultsTranslation, resultsLanguage, resultsDescription, resultsHS_Chapter, resultsComments, searchterm) {

	if (resultsWord.length > 0) {
// if there are any results, put them in a list inside the "results_ethnic_search" div
		var results_ethnic_search = document.getElementById("results_ethnic_search");
		while(results_ethnic_search.firstChild)results_ethnic_search.removeChild(results_ethnic_search.firstChild)
		var tableitem = document.createElement("table");
		tableitem.style.cssText = 'border-collapse:collapse; background-color:#FFFFFF; margin:0px; width:984px';
		var tbodyitem = document.createElement("tbody");
		tableitem.appendChild(tbodyitem);
		results_ethnic_search.appendChild(tableitem);
		var row1 = document.createElement("tr");
		row1.style.cssText = 'font-weight:bold; background-color:#CCCCCC';
		var cell1 = document.createElement("td");
		cell1.style.cssText = 'width:150px; border-top:0px';
		var cell2 = document.createElement("td");
		cell2.style.cssText = 'width:90px; border-top:0px';
		var cell3 = document.createElement("td");
		cell3.style.cssText = 'width:150px; border-top:0px';
		var cell4 = document.createElement("td");
		cell4.style.cssText = 'border-top:0px';
		var cell5 = document.createElement("td");
		cell5.style.cssText = 'width:100px; border-top:0px';
		var cell6 = document.createElement("td");
		cell6.style.cssText = 'width:150px; border-top:0px';
		tbodyitem.appendChild(row1);
		row1.appendChild(cell1);
		cell1.appendChild(document.createTextNode("Word"));
		row1.appendChild(cell2);
		cell2.appendChild(document.createTextNode("Language"));
		row1.appendChild(cell3);
		cell3.appendChild(document.createTextNode("Translation"));
		row1.appendChild(cell4);
		cell4.appendChild(document.createTextNode("Description"));
		row1.appendChild(cell5);
		cell5.appendChild(document.createTextNode("HS_Chapter"));
		row1.appendChild(cell6);
		cell6.appendChild(document.createTextNode("Comments"));

		for (var i=0;i<resultsWord.length;i++) {
			var row2 = document.createElement("tr");
			var cell7 = document.createElement("td");
			cell7.style.cssText = 'font-weight:bold';
			var cell8 = document.createElement("td");
			var cell9 = document.createElement("td");
			var cell10 = document.createElement("td");
			var cell11 = document.createElement("td");
			var cell12 = document.createElement("td");

			var WordDiv = document.createElement("div");
			WordDiv.innerHTML = resultsWord[i].lastChild.nodeValue;
			var LanguageDiv = document.createElement("div");
			LanguageDiv.innerHTML = resultsLanguage[i].lastChild.nodeValue;
			var TranslationDiv = document.createElement("div");
			TranslationDiv.innerHTML = resultsTranslation[i].lastChild.nodeValue;
			var DescriptionDiv = document.createElement("div");
			DescriptionDiv.innerHTML = resultsDescription[i].lastChild.nodeValue;
			var HS_ChapterDiv = document.createElement("div");
			HS_ChapterDiv.innerHTML = resultsHS_Chapter[i].lastChild.nodeValue;
			var CommentsDiv = document.createElement("div");
			CommentsDiv.innerHTML = resultsComments[i].lastChild.nodeValue;

			tbodyitem.appendChild(row2);
			row2.appendChild(cell7);
			cell7.appendChild(WordDiv);
			row2.appendChild(cell8);
			cell8.appendChild(LanguageDiv);
			row2.appendChild(cell9);
			cell9.appendChild(TranslationDiv);
			row2.appendChild(cell10);
			cell10.appendChild(DescriptionDiv);
			row2.appendChild(cell11);
			cell11.appendChild(HS_ChapterDiv);
			row2.appendChild(cell12);
			cell12.appendChild(CommentsDiv);
			}}
	else {
// else tell the user no matches were found
		var results_ethnic_search = document.getElementById("results_ethnic_search");
		while(results_ethnic_search.firstChild)results_ethnic_search.removeChild(results_ethnic_search.firstChild)
			var divitem = document.createElement("div");
			divitem.style.cssText = 'background-color:#FFFFFF; margin:0px; width:972px; border-style:solid; border-width: 0px 1px 1px 1px; padding:5px 5px 20px 5px';
			var notfound = document.createTextNode("Συγνώμη, δεν υπάρχουν λέξεις στην αναζήτηση που περιέχουν το κείμενο "+searchterm +"!");
			results_ethnic_search.appendChild(divitem);
			divitem.appendChild(notfound);
		}
document.getElementById('SearchDiv').style.display = "block";
for(x=1;x<=9;x++){
ThisDiv = 'RadioGroupName' + x +'Div';
document.getElementById(ThisDiv).style.display = 'none';}
}
