////////////////////////////////////////////////////////
// CATALOG DISPLAY /////////////////////////////////////
////////////////////////////////////////////////////////

var gCategory = "";
var booksXMLHttp;

// This function will load the xml and then call the CreateCatalogList function.
function LoadCatalog(Category){
	try{
		gCategory = Category;
		booksXMLHttp = LoadXMLDoc(booksXMLFile, onResponse);
		/*if (window.XMLHttpRequest)	{	// code for IE7, Firefox, Mozilla, etc.
			booksXMLHttp=new XMLHttpRequest();
		}
		else if (window.ActiveXObject)  	{	// code for IE5, IE6
			booksXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (booksXMLHttp!=null){
			gCategory = Category;
			booksXMLHttp.onreadystatechange=onResponse;
			booksXMLHttp.open("GET",booksXMLFile,true);
			booksXMLHttp.send(null);
		}
		else{
		  alert("Your browser does not support XMLHTTP.");
		  throw "blast!";
		}*/
	}
	catch(ex){
			document.getElementById('contentRegion').innerHTML += ex;
		if (document.getElementById('contentRegion') != null)
			document.getElementById('contentRegion').innerHTML += "<h1>Latest Books</h1><h3>An error occured while loading the catalog.</h3>";
	}
}
function onResponse(){
	if (xmlHttp != null){
		if (xmlHttp.readyState == 4){
			booksXMLDoc = xmlHttp.responseXML.documentElement
			CreateCatalogList(gCategory);
		}
	}
}

/* 	Loops through all of the books and outputs the ones that pertain to the specified category.
*  	Possible categories are:
*		"ALL" : Display all of the books.
*		"Latest" : Displays the last 10 books.
*		"Studies on East Asia" or "East Asian Research Aids and Translations": Displays every book within that category
*	For all of these, the output is sorted in descending order based on publishing year.                                */
function CreateCatalogList(CategoryToDisplay){
	try{
		document.getElementById('contentRegion').innerHTML = "";
		
		var books = booksXMLDoc.getElementsByTagName('book');
		
		// An array we temporarily store book data in for sorting.
		var bookSortData = new Array(books.length);				
		
		var catalogHead = document.createElement('h1');
		
		// Set the main header based on the category.
		if (CategoryToDisplay == "ALL")
			catalogHead.innerHTML = 'All Books';
		else if (CategoryToDisplay == "Latest")
			catalogHead.innerHTML = 'Latest Books';
		else
			catalogHead.innerHTML = CategoryToDisplay;
		document.getElementById('contentRegion').appendChild(catalogHead); 
		
		
		// Print a checkout button at the top.
		document.getElementById('contentRegion').appendChild(makePaypalCheckoutBtn());
		
		document.getElementById('contentRegion').appendChild(document.createElement('hr'));
		
		
		// Load in the needed book data into the sorting array.
		for (bookIndex = 0; bookIndex < books.length; bookIndex++){
			bookSortData[bookIndex] = new BookType();
			bookSortData[bookIndex].m_Title = valueOfTag(books[bookIndex], 'title');
			bookSortData[bookIndex].m_Published = valueOfTag(books[bookIndex], 'published');
			bookSortData[bookIndex].m_Category = valueOfTag(books[bookIndex], 'category');
		}
		
		// Sort the books by publishing date.
		SortBookData(bookSortData, "published", "desc");
	
		// Print out the books we want.
		for (bookIndex = 0; bookIndex < bookSortData.length; bookIndex++){
			if (CategoryToDisplay == "ALL" || bookSortData[bookIndex].m_Category == CategoryToDisplay){
				document.getElementById('contentRegion').innerHTML += CreateBookDiv(GetBookByTitle(books, bookSortData[bookIndex].m_Title)).innerHTML + "<hr />";
			}
			if (CategoryToDisplay == "Latest"){
				if (bookIndex > 9)
					break;
				else{
					document.getElementById('contentRegion').innerHTML += CreateBookDiv(GetBookByTitle(books, bookSortData[bookIndex].m_Title)).innerHTML + "<hr />";
				}
			}
		}
		
		// Print a checkout button at the bottom.
		document.getElementById('contentRegion').appendChild(makePaypalCheckoutBtn());
		
	}
	catch(ex){
		if (document.getElementById('contentRegion') != null)
			document.getElementById('contentRegion').innerHTML = "<h1>Latest Books</h1><h3>An error occured while123 loading the catalog.</h3>";
	}
}