// JavaScript Document

/* submitForm function - Modified for updating content in one element only.
 * Receives two mandatory, and one optional argument from an eventHandler on the calling page.
 * xmlFile = The name of an xml file to be loaded into the page container.
 * The filename convention is [pageName][id].xml (i.e., supportmainContent.xml, supportsidebar1.xml).
 * parseForm checks for the number of parameters - 2 parameters (the xmlFile and location) indicate
 * only one section to update, 3 parameters means two sections. parseForm will then call submitForm
 * the appropriate number of times, passing along the filename and section(s) to update.
 *
 * TODO - Make this dynamic enough to handle any number of arguments.
 * TODO - Calling with only two parameters leaves anything that was in the non-used
 * element there. Two options - 1) leave it there, 2) create a dummy xmlFile to
 * load in its place.
 */ 

/*
function parseForm(xmlFile,id1,id2) {
	if (id2.length != 0) { // We have id2, so call submitForm twice.
		submitForm(xmlFile,id1);
		submitForm(xmlFile,id2);
	}

	if (id2.length==0) {
		submitForm(xmlFile,id1);
	}
}
*/

/* submitForm function
 * Receives two mandatory arguments from parseForm - xmlFile and contentid.
 * Updates the required section of the content with HTML in [xmlFile][id].xml
 */

function submitForm(xmlFile,contentid) 
{ 
	var req = null; 
	document.getElementById(contentid).innerHTML = "Started...";
 	if (window.XMLHttpRequest) // Non-IE browser
		{
 			req = new XMLHttpRequest();
			if (req.overrideMimeType) 
			{
				req.overrideMimeType('text/xml');
			}
		} 

		else if (window.ActiveXObject) // IE, in some form
		{
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)

			{
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { alert ("Your browser doesn't support AJAX!"); 
				return; } // Need to test this and add a link a for HTML only option
		}
	}

	req.onreadystatechange = function() { 
		document.getElementById(contentid).innerHTML = "Wait server...";
		if(req.readyState == 4)
		{
			if(req.status == 200)
				{
					document.getElementById(contentid).innerHTML  = req.responseText;	
				}	
			else	
				{
					document.getElementById(contentid).innerHTML="Error: returned status code " + req.status + " " + req.statusText;
				}	
		} 
	}; 

	req.open("GET", xmlFile + contentid + ".html?" + Date(), true); // Append date to request URL to prevent cache issues.
	req.send(null); 
} 
