// JavaScript Document

// inputArrayObj - this is an associative array that will be appended to the file name in $_GET syntex
// outputArrayObj - this is an associative array that will be returned from the function
// namePagetoGet - this is the page that this AJAX function calls
// there must be a function on the page called ProcessAJAXReturn(return array)

function getAJAXobject(InputArrayObj, namePageToGet) {	
	
	var xmlhttp = false;
		
	// construct file name
	var namePageWithGetData = namePageToGet + "?";
	
	var i=0;
	for(var index in InputArrayObj) {
		// check for bad characters
		
		if (i>0) {
			namePageWithGetData += '&';			
		}			
		InputArrayObj[index]=InputArrayObj[index].replace(/\W/,"A");		
		namePageWithGetData += index + '=' + InputArrayObj[index];
		++i;
	}
	
	// alert(namePageWithGetData);
	
	// the filename to call the XML page should now be namePageWithGetData
	// this should have all of the values and keys that you want to pass to the XML page
	
	if (window.XMLHttpRequest) { // Non-IE Browser
		xmlhttp = new XMLHttpRequest ();
		xmlhttp.onreadystatechange = function () {
			processStateChange (xmlhttp);
		}
		try {
			xmlhttp.open("GET", namePageWithGetData, true);
		} catch (e) {
			alert (e);
		}
		xmlhttp.send(null);
	} else if (window.ActiveXObject) { // IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp) {
			xmlhttp.onreadystatechange = function () {
				processStateChange (xmlhttp);
			}
			xmlhttp.open("GET", namePageWithGetData, true);
			xmlhttp.send();
		}
	}	
	
// return outputArrayObj;

}

// this processStateChange function goes with the getAJAXobject above

function processStateChange (xmlhttp) {	

	if (xmlhttp.readyState == 4) { // Complete
		if (xmlhttp.status == 200) { // OK response
		
			deliminateXMLHttpData(xmlhttp.responseText);
						
		} else {
			alert ("Problem: " + xmlhttp.statusText);
		}
	}
}


// function to convert break up xmlhttp return string into associative array
function deliminateXMLHttpData(xmlhttpResponse) { 
	var outputArrayObj = new Array();  // this array will have names and values for each
	// user format <key1> text for key1 </key1><key2> text for key2 </key2>
	// this format is not true XML it ignores everything except the outermost tags.
	// for this set up you may not have more than one tag with the same key.
	var keyStart; 
	var key;
	var keyEnd;
	var keyString;
	var testString = xmlhttpResponse;
	var keepGoing = 1;
	var i = 0;
	var testStringSplit = new Array();
	var EndingPosition;
	while (keepGoing>0 && /<[A-Za-z_]{1,}>/.test(testString))
		{
		testArray = /<[A-Za-z_]{1,}>/.exec(testString);
		keyStart = testArray[0];
		testArray = /[A-Za-z_]{1,}/.exec(testArray);
		key = testArray[0];
		keyEnd = '</' + key + '>';	
		if (keyEnd.length == 3) {
			keepGoing=0;
		} else {
			// alert(keyStart + ' ' + key + ' ' + keyEnd);
			// determine the value inside
			// remove front tag and everything in front of it.
			// testStringSplit = new Array();
			testStringSplit = testString.split(keyStart);
			testString = testStringSplit[1];
			testStringSplit = testString.split(keyEnd);
			testString = testStringSplit[1];
			keyString = testStringSplit[0];
			
			outputArrayObj[key] = keyString;
			++i;
			// alert(key + ' ' + keyString);			
		
		} // end of else
		} // end of while loop	
		
	// now that array is complete what do you do with it?
	// decide what function to run to handle the data string
	// note: there is a delay between the time that the original function is called and this function runs
	
	ProcessAJAXReturn(outputArrayObj);
	
}

