/* The Following code is written by Andy Ng <http://www.PCinvent.info>
 * All Rights Reserved
 */
//Written by andy@PCinvent.com
//Purpose: Search XML File generate from Church Presentation System

/*-------------------------------------- Songs --------------------------------------------------------*/
function loadsongsIndex() { // load file
// most current browsers support document.implementation
	if (document.implementation && document.implementation.createDocument) {
		SongsxmlDoc = document.implementation.createDocument("", "", null);
		SongsxmlDoc.load("../songs/xml/Export.xml");
	}
// MSIE uses ActiveX
	else if (window.ActiveXObject) {
		SongsxmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		SongsxmlDoc.async = "false";
		SongsxmlDoc.load("../songs/xml/Export.xml");
	}
}

function searchSongsIndex() {
	if (!SongsxmlDoc) {
		loadsongIndex();
	}
	// get the search term from a form field with id 'searchme'

	var searchterm = document.getElementById("searchme").value;
	var allitems = SongsxmlDoc.getElementsByTagName("Contents");//Contents array
	var allitems_t = SongsxmlDoc.getElementsByTagName("Title1");//Title1 array
	var allitems_s = SongsxmlDoc.getElementsByTagName("SongNumber");//SongNumber array

	songsresults = new Array;
	if (searchterm.length < 2) {
		var songsresultshere = document.getElementById("songsresultshere");
		songsresultshere.innerHTML = "<h5>Please Enter at least two characters !</h5>";
	} 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) {
				songsresults.push(allitems_t[i]);
			}
		}
// send the results to another function that displays them to the user
	showsongsResults(songsresults, searchterm);//andy:put title into reference
	}
}


// The following is just an example of how you
// could handle the search results
function showsongsResults(songsresults, searchterm) {

	if (songsresults.length > 0) {
// if there are any results, put them in a list inside the "resultshere" div
		var songsresultshere = document.getElementById("songsresultshere");
		songsresultshere.innerHTML = "";//andy:clean first
		var header = document.createElement("h5");
		var list = document.createElement("ul");
		var searchedfor = document.createTextNode(songsresults.length+" results found for \""+searchterm+"\"");
		songsresultshere.appendChild(header);
		header.appendChild(searchedfor);
		songsresultshere.appendChild(list);
		for (var i=0;i<songsresults.length;i++) {
			var listitem = document.createElement("li");
			
			//andy:set hyperlink
			var jump_link = document.createElement("a");	
			var item = document.createTextNode(songsresults[i].lastChild.nodeValue);
			
			list.appendChild(listitem);//ul -> li
			listitem.appendChild(jump_link);//li -> a
			jump_link.appendChild(item);//a -> content
			
			jump_link.setAttribute('class','jumper');
			jump_link.setAttribute('href','THIS_JUMPER_LINK_FEATURE_IS_NOT_COMPLETE_YET');
		}
	} else {
// else tell the user no matches were found
		var songsresultshere = document.getElementById("songsresultshere");
		songsresultshere.innerHTML = "";//andy:clean first
		var para = document.createElement("h5");
		var notfound = document.createTextNode("Sorry, I couldn't find any song that matches \""+searchterm +"\"");
		songsresultshere.appendChild(para);
		para.appendChild(notfound);
	}
}

function showSongNumber() {
	document.getElementById("song_numbers_loader").innerHTML = "We have Total "+document.getElementById("songs_saver").innerHTML+" Songs";
}


/*-------------------------------------- Sermons --------------------------------------------------------*/
function loadsermonsIndex() { // load file
// most current browsers support document.implementation
	if (document.implementation && document.implementation.createDocument) {
		SermonsxmlDoc = document.implementation.createDocument("", "", null);
		SermonsxmlDoc.load("../sermons/xml/Export.xml");
	}
// MSIE uses ActiveX
	else if (window.ActiveXObject) {
		SermonsxmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		SermonsxmlDoc.async = "false";
		SermonsxmlDoc.load("../sermons/xml/Export.xml");
	}
}

function searchSermonsIndex() {
	if (!SermonsxmlDoc) {
		loadsermonsIndex();
	}
	// get the search term from a form field with id 'searchme'

	var searchterm = document.getElementById("searchme").value;
	var allitems = SermonsxmlDoc.getElementsByTagName("Contents");//Contents array
	var allitems_t = SermonsxmlDoc.getElementsByTagName("Title2");//Title1 array
	var allitems_s = SermonsxmlDoc.getElementsByTagName("Title1");//SongNumber array

	sermonsresults = new Array;
	if (searchterm.length < 2) {
		var sermonsresultshere = document.getElementById("sermonsresultshere");
		sermonsresultshere.innerHTML = "<h5>Please Enter at least two characters !</h5>";
	} 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) {
				sermonsresults.push(allitems_t[i]);
			}
		}
// send the results to another function that displays them to the user
	showsermonsResults(sermonsresults, searchterm);//andy:put title into reference
	}
}


// The following is just an example of how you
// could handle the search results
function showsermonsResults(sermonsresults, searchterm) {

	if (sermonsresults.length > 0) {
// if there are any results, put them in a list inside the "resultshere" div
		var sermonsresultshere = document.getElementById("sermonsresultshere");
		sermonsresultshere.innerHTML = "";//andy:clean first
		var header = document.createElement("h5");
		var list = document.createElement("ul");
		var searchedfor = document.createTextNode(sermonsresults.length+" results found for \""+searchterm+"\"");
		sermonsresultshere.appendChild(header);
		header.appendChild(searchedfor);
		sermonsresultshere.appendChild(list);
		for (var i=0;i<sermonsresults.length;i++) {
			var listitem = document.createElement("li");
			
			//andy:set hyperlink
			var jump_link = document.createElement("a");	
			var item = document.createTextNode(sermonsresults[i].lastChild.nodeValue);
			
			list.appendChild(listitem);//ul -> li
			listitem.appendChild(jump_link);//li -> a
			jump_link.appendChild(item);//a -> content
			
			jump_link.setAttribute('class','jumper');
			jump_link.setAttribute('href','THIS_JUMPER_LINK_FEATURE_IS_NOT_COMPLETE_YET');
		}
	} else {
// else tell the user no matches were found
		var sermonsresultshere = document.getElementById("sermonsresultshere");
		sermonsresultshere.innerHTML = "";//andy:clean first
		var para = document.createElement("h5");
		var notfound = document.createTextNode("Sorry, I couldn't find any sermon that matches \""+searchterm +"\"");
		sermonsresultshere.appendChild(para);
		para.appendChild(notfound);
	}
}

function showSermonNumber() {
	document.getElementById("sermon_numbers_loader").innerHTML = "We have Total "+document.getElementById("sermons_saver").innerHTML+" Sermons";
}
