/* Generic functions for site
	Index:
		// Generic functions
		XmlDocument openXml (string uri)
		void printToPage (HtmlDocument document, string whereOnPage, string whatOnPage)
*/

// Constants
var XML_URI = "inventory.xml";

/* Document openXml (string uri)

	Regular AJAX function. Instantiates HTTP Request object for all modern
	browsers, connects it to a specified file URI (variable in case other
	XML databases are used) and, if all goes well, returns the data as a
	standard Document object. If the object fails to load an error message
	is presented.
	
	TODO:	Handle errors in try/catch structure
			Add handler for interim loading time */
	function openXml (uri) {
		var rXml; // XML document object
		// Determine which objects are available for instantiating HTTP Request
		if (window.XMLHttpRequest) {
			// Load XMLHttpRequest in non-MSIE browsers
			rXml = new XMLHttpRequest ();
 		} else if (window.ActiveXObject) {
 			// Load ActiveXObject for that IE thing
	 		rXml = new ActiveXObject("Microsoft.XMLHTTP");
			alert ("Microsoft");
		} else {
			// No known object available - Exit
			alert ("No known object available for HTTP Request.");
			return false;
		}
		// Open a synchronous connection to the URI passed, should be XML
		rXml.open ("GET", uri, false);
		rXml.send (null);		
		// If all went well, return XML from HTTP Request
		if (rXml.status == 200 && rXml.responseXML != null) {
			// Document has loaded without incident and may be returned
			return rXml.responseXML;
		} else {
			// Throw error message
			var errMes = "There was an error handling your request for ";
			errMes += uri;
			errMes += ". Status Code: ";
			errMes += rXml.status;
			errMes += ". Contents of Response are ";
			errMes += rXml.responseXML;
			alert (errMes);
			// Document failed to load
			return false;
		}
	}
	
/* void printToPage (HtmlDocument document, string whereOnPage, string whatOnPage)
	Replaces HTML content in a specified element. The element should be a <DIV>
	tag. NOTE: Erases previous content.

	Arguments:
		document = may be archaic; optionally extensible
		whereOnPage = string <id attribute> of <div element> where content goes
		whatOnPage = string of regularly formatted HTML
	
	TODO:	 */
 function printToPage (whereOnPage, whatOnPage, addToPage) {
 
 	// Reference specified element in DOM
 	var hereOnPage = document.getElementById (whereOnPage);
 	
 	if (hereOnPage == null) {
 		
 		// Escape if not found
 		window.alert("No such element " + whereOnPage + " exists.");
 		return false;
 		
 	} else {
 		
 		if (addToPage == 1) {
	 		// Appends HTML content
	 		hereOnPage.innerHTML += whatOnPage;
	 	} else if (addToPage == 2) {
	 		// Prepends HTML content
	 		var strPrep = hereOnPage.innerHTML;
	 		hereOnPage.innerHTML = whatOnPage + strPrep;
	 	} else {
	 		hereOnPage.innerHTML = whatOnPage;
	 		}
 	
 	}

 }