// JavaScript Document
// Scripts for loading XML files and checking the state

//Gloabal Vars
	var Ebooks;
	var xmlDoc;

function state_change() {
	//check the state change to see if the XML doc is loaded
	if (xmlDoc.readyState==4) { //done loading
		if (xmlDoc.status<300) {
			document.body.style.cursor='default';
//			displayResults.innerHTML = "Ready!";
					window.status="Done!!";
		}
//				else alert ("some problem loading");
	}
	else document.body.style.cursor='wait';
}


//********************************************************
//This is the old code which worked great as long as you 
//weren't using a Mac or Safari--oops
//********************************************************
function old_LoadXMLDoc(dname) {
	// code for IE
	if (window.ActiveXObject) {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument) {
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else {
		//show error message with link to old Ebooks page(s)
		result_text = 'Sorry, it looks like your browser cannot handle this script. \n Please update your browser to the latest version. \nIn the meantime, try our old <a href="http://bcd.tamhsc.edu/library/ebooks/abc.html">Ebooks page</a> which is no loanger being updated as of 10/30/07. \n\n Thank you, and sorry for the inconveneience.';
	}
	xmlDoc.async=false;
	xmlDoc.load(dname);
	xmlDoc.onreadystatechange=state_change;
	
	//check the status of the download
	//set the cursor and statusbar message to indicate that it's still loading
		//document.body.style.cursor='wait';
	//window.status="Loading Ebook Database. Please wait."
		//displayEbookResults.innerHTML = "Loading Ebook Database. Please wait."

	return(xmlDoc);
}


function old_LoadHTMLDoc(dname) {
	// code for IE
	if (window.ActiveXObject) {
		htmlDoc=new ActiveXObject("Microsoft.HTMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument) {
		htmlDoc=document.implementation.createDocument("","",null);
	}
	else {
		//show error message with link to old Ebooks page(s)
		result_text = 'Sorry, it looks like your browser cannot handle this script. \n Please update your browser to the latest version. \nIn the meantime, try our old <a href="http://bcd.tamhsc.edu/library/ebooks/abc.html">Ebooks page</a> which is no loanger being updated as of 10/30/07. \n\n Thank you, and sorry for the inconveneience.';
	}
	htmlDoc.async=false;
	htmlDoc.load(dname);
	htmlDoc.onreadystatechange=state_change;
	
	//check the status of the download
	//set the cursor and statusbar message to indicate that it's still loading
		//document.body.style.cursor='wait';
	//window.status="Loading Ebook Database. Please wait."
		//displayEbookResults.innerHTML = "Loading Ebook Database. Please wait."

	return(htmlDoc);
}



//********************************************************
//Got this code from http://www.howtocreate.co.uk/jslibs/script-importxml
//to fix problems with loading on Macs and with Safari browser.
//********************************************************
function loadXMLDoc( oURL, oNoRand, oDelay ) {
	
	//note: in XML importing event handlers, 'this' refers to window
	if( !oNoRand ) { oURL += ( ( oURL.indexOf('?') + 1 ) ? '&' : '?' ) + ( new Date() ).getTime(); } //prevent cache

	// code for IE 
	if( !navigator.__ice_version && window.ActiveXObject ) {
		//the Microsoft way - IE 5+/Win (ICE produces errors and fails to use try-catch correctly)
		var activexlist = ['Microsoft.XMLHTTP','Microsoft.XMLDOM','Msxml2.XMLHTTP'], tho; //add extra progids if you need specifics
		for( var i = 0; !tho && i < activexlist.length; i++ ) {
			try { tho = new ActiveXObject( activexlist[i] ); } catch(e) {}
		}
		if( tho ) {
			xmlDoc = tho;
			if( xmlDoc.load ) {
				xmlDoc.load(oURL);
				xmlDoc.onreadystatechange = state_change;
				XD = xmlDoc;
			} 
			else {
				xmlDoc.open('GET', oURL, false);
				xmlDoc.send(null);
				xmlDoc.onreadystatechange = state_change;
				XD = xmlDoc.responseXML;
			}
			return XD;
		}
	}
	
	// code for Mozilla, Firefox, Opera, etc.
	if (document.implementation && document.implementation.createDocument) {
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.async=false;
		if (xmlDoc.load) {
			xmlDoc.load(oURL);
			xmlDoc.onreadystatechange=state_change;

		return xmlDoc;
		}
	}
	
	// This is the original version of the Mozilla engine loader, but it didn't work with Firefox, so used the old 
	// version above
	//	if( document.createElement && document.childNodes ) {
	//		//load the XML in an iframe
	//		var ifr = document.createElement('DIV');
	//		ifr.style.visibility = 'hidden'; ifr.style.position = 'absolute'; ifr.style.top = '0px'; ifr.style.left = '0px';
	//		//onload only fires in Opera so I use a timer for all
	//		if( !window.MWJ_XML_timer ) { window.MWJ_XML_timer = window.setInterval('checkXMLLoad();',100); }
	//		ifr.innerHTML = '<iframe src="'+oURL+'" name="MWJ_XML_loader_'+'" height="0" width="0"><\/iframe>';
	//		xmlDoc = state_change+'MWJ_SPLIT'+(oDelay?oDelay:1)+'';
	//		document.body.appendChild(ifr);
	//		return true;
	//	}

	// catch-all code for browsers that fall through the creack--Safari
	if( window.XMLHttpRequest ) {
		//alternate XMLHTTP request - Gecko, Safari 1.2+ and Opera 7.6+
		xmlDoc = new XMLHttpRequest();
		xmlDoc.onreadystatechange = state_change;
		xmlDoc.open("GET", oURL, false);
		xmlDoc.overrideMimeType('text/xml');
		xmlDoc.send(null);

		return xmlDoc.responseXML;
	}

	return false;
}


//this function is used by the iframe code that commented out above. 
//function checkXMLLoad() {
	//check if each imported file is available (huge files may not have loaded completely - nothing I can do - use the delay to help)
//	if( xmlDoc && window.frames['MWJ_XML_loader_'] ) {
//		setTimeout( xmlDoc.split('MWJ_SPLIT')[0] + '(window.frames.MWJ_XML_loader_'+'.window.document);', parseInt(xmlDoc.split('MWJ_SPLIT')[1]) );
//		xmlDoc = false;
//	}
//}


//I don't think this is called anymore, mjc 10/10/08
function init() {
	Ebooks = xmlDoc;
alert("made it--INIT");
}


