////////////////////////////////////////////////////////
// SEARCH //////////////////////////////////////////////
////////////////////////////////////////////////////////
var booksXMLHttp = null;
var booksXMLDoc = null;

// Loads the XML and preps the search display function.
function PrepSearch()
{
	var searchResults = document.getElementById('searchResults');
	try{
		searchResults.innerHTML = "<h1>Loading...</h1>";
		
		booksXMLHttp = LoadXMLDoc(booksXMLFile, ifReadyDisplay);
	}
	catch (ex){
		if (searchResults != null)
			searchResults.innerHTML = "<h3>An error occured while searching the catalog.</h3>";
	}
}

function ifReadyDisplay()
{
	if (booksXMLHttp.readyState == 4) {
		booksXMLDoc = booksXMLHttp.responseXML.documentElement;
		DisplayResults();
	}
}

// Searches through the XML and displays the matches.
function DisplayResults()
{
    var searchResults = document.getElementById('searchResults');
	try{
		var textSearch    = document.getElementById('textSearch');
		
		searchResults.innerHTML = "";
		var books = booksXMLDoc.getElementsByTagName('book');
		
		var resultsHeading = document.createElement('h2');
		resultsHeading.innerHTML = 'Results for "' + textSearch.value + '"';
		searchResults.appendChild(resultsHeading);   
		
		// The array that specifies where in the data we search.
		var searchFields = new Array();
		
		// Make it search through all of the important fields.
		searchFields.push('title');
		searchFields.push('subtitle');
		searchFields.push('editor');
		searchFields.push('author');
		searchFields.push('description.authorinfo');
		searchFields.push('description.shortdescription');
		searchFields.push('description.longdescription');
		
		// An array we temporarily store book data in for sorting.
		var bookSortData = new Array();	
		var curSortIndex = 0;
		
		// Add all matching books to our temporary sorting array.
		var matchFound = false;
		for (bookIndex = 0; bookIndex < books.length; bookIndex++){
			if ( BookContains( books[bookIndex], textSearch.value, searchFields ) ){
				matchFound = true;
				bookSortData[curSortIndex] = new BookType();
				bookSortData[curSortIndex].m_Title = valueOfTag(books[bookIndex], 'title');
				bookSortData[curSortIndex].m_Published = valueOfTag(books[bookIndex], 'published');
				bookSortData[curSortIndex].m_Category = valueOfTag(books[bookIndex], 'category');
				curSortIndex++;
			}
		}
		// Sort them based on publishing date.
		SortBookData(bookSortData, "published", "desc");
		
		// Add each book to the search results to display and seperate them by horizontal-rules.
		for (curSortIndex = 0; curSortIndex < bookSortData.length; curSortIndex++){
			searchResults.appendChild(CreateBookDiv(GetBookByTitle(books, bookSortData[curSortIndex].m_Title)));
			searchResults.appendChild(document.createElement('hr'));
		}
		
		// If no books matched, tell the user as much.
		if (!matchFound){
			var nothingFound = document.createElement('p');
			nothingFound.innerHTML = "No matches found.";
			searchResults.appendChild(nothingFound);
		}
	}
	catch (ex){
		if (searchResults != null)
			searchResults.innerHTML = "<h3>An error occured while displaying the search results.</h3>";
	}
}

// Determines whether or not the given book contains the search string in the specified fields.
function BookContains( curBook, searchStr, searchFields ){
	var searchArray = new Array();
	searchArray = searchStr.split(' ');
	
	var curSearchWord;
	while (searchArray.length != 0){
		var curWordFound = false;
		
		curSearchWord = searchArray.shift();
		for (curIndex in searchFields){
			fieldInQuestion = valueOfTag(curBook, searchFields[curIndex]).toLowerCase();
			if (fieldInQuestion.indexOf(curSearchWord.toLowerCase()) != -1){
				curWordFound = true;
				break;
			}
		}
		if (!curWordFound){
			return false;
		}
	}
	return true;
}

//Checks to see if the user pressed the ENTER key and submits the data if they did.
function CheckSubmit(e){
	var keynum;
	var keychar;
	var numcheck;
	
	if(window.event){ // IE	
		keynum = e.keyCode;
	}
	else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}
	
	// If the user pressed enter (the keycode of which is 13), submit the data.
	if (keynum == 13){
		PrepSearch();
	}
	
}