// Generic AJAX functions.

function viewAjaxResponse() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var xmlDoc = request.responseText;
			alert(xmlDoc);
		}
	}
}

var request = null;
try {
	request = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			request = null;
		}
	}
}

if (request == null)
	alert("Error creating request object!");



function replaceText(el, text) {
	if (el != null) {
		clearText(el);
		var newNode = document.createTextNode(text);
		el.appendChild(newNode);
	}
}

function clearText(el) {
	if (el != null) {
		if (el.childNodes) {
			for (var i = 0; i < el.childNodes.length; i++) {
				var childNode = el.childNodes[i];
				el.removeChild(childNode);
			}
		}
	}
}

function getText(el) {
	var text = "";
	if (el != null) {
		if (el.childNodes) {
			for (var i = 0; i < el.childNodes.length; i++) {
				var childNode = el.childNodes[i];
				if (childNode.nodeValue != null) {
					text = text + childNode.nodeValue;
				}
			}
		}
	}
	return text;
}


// Multiple AJAX requests below.

MAX_SIMULTANEOUS_AJAX_REQUESTS = 200;
ajaxArray = new Array();
function updateSubLatLng(ID, Lat, Lng) {
	var index = 0;
	// Find an empty slot in the AJAX array
	while (ajaxArray[index] && index < MAX_SIMULTANEOUS_AJAX_REQUESTS)
	index++; 
	if (index < MAX_SIMULTANEOUS_AJAX_REQUESTS) {
		// We got one
		try {
			ajaxArray[index] = new XMLHttpRequest();
		}
		catch(e) {
			try {
				ajaxArray[index] = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e2) {
				try {
					ajaxArray[index] = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					// no AJAX support
				}
			}
		}
		var URL = "ff3.php?action=micro&update=unit&unitType=Sub&ID="+ID+"&Lat="+Lat+"&Lng="+Lng+"&destLat="+Lat+"&destLng="+Lng;
		URL = URL + URLstuff();
		//alert(URL);
		if (ajaxArray[index]) {
			ajaxArray[index].open('GET', URL, true);
			ajaxArray[index].setRequestHeader("Cache-Control", "no-cache");
			ajaxArray[index].setRequestHeader("Pragma", "no-cache");
			ajaxArray[index].onreadystatechange = updateSubLatLngResponse;
			ajaxArray[index].send(null);
		}
	}
} 	 

function updateSubLatLngResponse() {
	for (var index = 0; index < ajaxArray.length; index++) {
		if (ajaxArray[index] && ajaxArray[index].readyState == 4) { 
			if (ajaxArray[index].status == 200) {
				//var xmlDoc = ajaxArray[index].responseXML;
				//alert(xmlDoc);
				//document.getElementById(id).innerHTML = feed;
			} else {
				//alert(ajaxArray[index].status);
				//document.getElementById(id).innerHTML = message;
			} 		  
			ajaxArray[index] = null;
		}
	}
}



