// +----------------------------------------------------------------------+
// | ajax.js                                                              |
// +----------------------------------------------------------------------+
// | FUNCTIONS                                                            |
// | = doAjax(eid, src, req)                                              |
// |   - Peform AJAX request type (GET or POST) on provided source file   |
// |     to update the element with specified ID                          |
// | = stateChanged(eid)                                                  |
// |   - Change inner HTML content of element with specified ID           |
// |   - Executes every time state of the XMLHTTP object changes          |
// | = getXmlHttpObject()                                                 |
// |   - Create XMLHTTP object based on browser                           |
// +----------------------------------------------------------------------+

var xmlHttp=null; // Defines that xmlHttp is a new variable.

function doAjax(eid, src, req) {
  if (typeof req == "undefined") {
    req = "get";
  }
	xmlHttp=getXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	xmlHttp.onreadystatechange=function() {
		stateChanged(eid);
	}
	xmlHttp.open(req,src,true); // .open(Request Method, Source File);
	xmlHttp.send(null); // Since there is no supplied form, null takes its place as a new form.
}

function stateChanged(eid) {
	if (xmlHttp.readyState==4) {
		try { // In some instances, status cannot be retrieved and will produce an error (e.g. Port is not responsive)
			if (xmlHttp.status == 200) {
				 // Set the element's inner HTML content to the info provided by the AJAX Request
				 document.getElementById(eid).innerHTML = xmlHttp.responseText;
			}
		}
		catch (e) {
			// Set element's inner HTML content to the generated error
			document.getElementById(eid).innerHTML = "Error on Ajax return call : " + e.description;
		}
	}
}

function getXmlHttpObject() {
	// Try to get the right object based on browser
	try {
		// Firefox, Opera 8.0+, Safari, IE7+
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	// Return object
	return xmlHttp;
}