/* 022809b.js.
   This version works.   It pulls the announcements   And the events. */

window.onload=doEverything;
var xhr = false;

function doEverything() // Runs all functions.
				 {
				 choosePic();
				 makeRequest();
				 insertInfo();
				 }

function choosePic () // Randomizes the splash pic.
				 {
				 var splashPics = new Array ("bells", "chasuble", "crosses", "cruets"); // When I have more class names (and therefore more pictures), add them to this array.
				 var randomNum = Math.floor((Math.random()*4)); // multiply "Math.random()" by however many images are in the array above.
				 document.getElementById("splash").className = splashPics[randomNum];
				 }
				 
function makeRequest () // Sets up xhr as and XMLHttpRequest object, or its IE equivalent.
				 {
				 if (window.XMLHttpRequest)
				 		{
						xhr = new XMLHttpRequest();
						}
				 else
				 		{
						if (window.ActiveXObject)
							 {
							 try
							 		{
									xhr = newActiveXObject ("Microsoft.XMLHTTP");
									}
							 catch (e) {}
							 }
					  }
				 
				 if (xhr)
				 		{
						xhr.onreadystatechange = checkReadiness;
						xhr.open("GET", "master.xml"+"?cachebuster="+new Date().getTime(), true);  // This "cachebuster" nonsense is to make sure it retrieves a live, rather than cached, copy of the XML file.
						xhr.send(null);
						}
				 else
				 		{
						alert ("Couldn't create the XMLHttpRequest object.");
						}
				 }
				 
function checkReadiness ()
				 {
				 if (xhr.readyState==4)
				 		{
						if (xhr.status==200)
							 {
							 if (xhr.responseXML) //  && xhr.responseXML.contentType=="text/xml"  //That used to be a condition.  But for some reason, when I include it, we don't get to this code block.  And it seems to work fine without this condition...
							 		{
  							  var newTitle1 = xhr.responseXML.getElementsByTagName("title")[0].firstChild.nodeValue;
									}
							 else
							 		{
									alert("We're sorry, but your browser is not fully supported.  Some elements may not work correctly.");
									}
							 }
						else
						 	 {
							 alert("We're sorry.  Your browser is not fully supported.  Some elements may not work correctly.");
							 }
						// And here's the meat: if everything's ready, run my AJAX functions.
						insertBooks();						}


					}	 				 

				 
function insertBooks()
	{

	// Needed: (1)item page url (2)img file (3)Title (4)Price
	// Begin by doing this for just one book, and statically; i.e. no XML yet.
	// Points of insertion: bookLink0, bookPic0, bookTitle0, bookPrice0.
	// Now that this works, loop it.
	// Now that this works, the scary bit: rather than getting the info statically, get it from the XML file.
	for (var i=0; i<16; i++)
		
		{
		var urlBlock = "http://www.trinstore.com/ecom_2/item_view.cfm?inventoryid="; // This bit goes on every item page url.
		var picBlock = "http://www.trinstore.com/ecom_2/inventoryImages/"; // This bit goes on every img url (with a ".jpg" suffix).
		var newItemNumber = xhr.responseXML.getElementsByTagName("item")[i].firstChild.nodeValue;
		document.getElementById("bookLink"+i).href = (urlBlock + newItemNumber);
		document.getElementById("bookPic"+i).src = (picBlock + newItemNumber + "_t.jpg");
		var newBookTitle = xhr.responseXML.getElementsByTagName("title")[i].firstChild.nodeValue;
		document.getElementById("bookTitle"+i).innerHTML = newBookTitle;
		var newBookPrice = xhr.responseXML.getElementsByTagName("price")[i].firstChild.nodeValue;
		document.getElementById("bookPrice"+i).innerHTML = newBookPrice;
		}
	}