

/* AJAX Functionality for Research Resource Database */

/* Started by Greg Engel, July 2006 */


///////////////////////////////////////////////////////////////////////////////////////////////////////
/// BASIC AJAX FUNCTIONS //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////

// Fix for IE
if (!window['Node']) {
    window.Node = new Object();
    Node.ELEMENT_NODE = 1;
    Node.ATTRIBUTE_NODE = 2;
    Node.TEXT_NODE = 3;
    Node.CDATA_SECTION_NODE = 4;
    Node.ENTITY_REFERENCE_NODE = 5;
    Node.ENTITY_NODE = 6;
    Node.PROCESSING_INSTRUCTION_NODE = 7;
    Node.COMMENT_NODE = 8;
    Node.DOCUMENT_NODE = 9;
    Node.DOCUMENT_TYPE_NODE = 10;
    Node.DOCUMENT_FRAGMENT_NODE = 11;
    Node.NOTATION_NODE = 12;
}


// Specify the Requested File, method
// imgslot and container are the corresponding id's of where the content and loadimg should go
// renderer is the function which will recieve the XML data and container id upon return
function makeRequest(url, method, imgslot, renderer) {

	// Initiate AJAX stuff... use different cases for different browsers
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	// Set event handler
	http_request.onreadystatechange = function() {
		handleChange(http_request, imgslot, renderer);
	};

	http_request.open(method, url, true);
	http_request.send(null);
	
	return http_request;
}




// The event handler
function handleChange(http_request, imgslot, renderer) {

	// A nice loading picture while we wait
	var loadpic = document.createElement('img');
	loadpic.setAttribute('src','http://www.oberlin.edu/library/reftmp/loading.gif');

	if(http_request.readyState > 0 && http_request.readyState < 4) {
		if(document.getElementById(imgslot).childNodes.length < 1)
			document.getElementById(imgslot) . appendChild(loadpic);
	} else if(http_request.readyState == 4) {
		document.getElementById(imgslot).innerHTML = "";
		//alert(http_request.responseText);
		renderer(http_request.responseXML);
	} 

}





//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////* SOME USEFUL DEFAULTS *////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////



// Clears container and begins the rendering, calling renderXMLSegment, below.
function renderAllXML(xml, container, special) {

	var container = document.getElementById(container);

	// Clear container of any data
	while(container.childNodes.length > 0) 
		container.removeChild(container.firstChild);

	// Do the recursion!
	renderXMLSegment(xml, container, special);
	
}


// The main work - this takes the XML data sent by the server and renders it as DOM objects.
// For the most part, we just generate the appropriate div's and give them classes. (Letting CSS
// take over.)  However, we do need to lay things out correctly and provide some special functionality
// for hiding, etc.  (This function is recursive.)

// XML - the chunk of XML ( as a tree, not a string) to be parsed and rendered
// render - the object in the XHTML DOM to which we can write what we render
// specialCases - a map from XML Tag Names ("group", "resource", etc.) to JS functions that should be
//				  used to render those tags, bypassing the normal renderXMLSegment operations

function renderXMLSegment(xml, render, specialCases) {

	
	// Run special case if available
	if(specialCases[xml.tagName]) {
		
		var handler = specialCases[xml.tagName];
		handler(xml, render, specialCases);

	} else {
		// Create a div for this element
		var div = document.createElement('div');
		div.className = xml.tagName;
		render.appendChild(div);

		recurXML(xml, div, specialCases);
		return div;
	}

}


// Handles the recursion.  This function is split from renderXMLSegment
// so that special cases can still call it if they so desire.
function recurXML(xml, render, specialCases) {

	// For each child of the given root node...
	for(var i = 0; i < xml.childNodes.length; i++) {

		// Recur on Elements
		if(xml.childNodes[i].nodeType == Node.ELEMENT_NODE)
			renderXMLSegment(xml.childNodes[i], render, specialCases);


		if(	xml.nodeType == Node.ELEMENT_NODE && 
			xml.getAttribute('label') != 'false' ) {

			if(xml.childNodes[i].nodeType == Node.TEXT_NODE)
				render.appendChild( 
					document.createTextNode(
						xml.childNodes[i].nodeValue) );
		}

	}
}





