var xmlhttp_submissions = 0;

function create_xmlhttp() {

	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	  xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function filldiv (resource, element_id, show_waiting, is_response_value, ignore_response, final_focus_element_id) {
	var current_submission = xmlhttp_submissions++;
	if (show_waiting != null && show_waiting != '') { document.getElementById(element_id).innerHTML = show_waiting; }
	var xmlhttp = create_xmlhttp();
        var rand2 = Math.round(Math.random()*10000);
	xmlhttp.open ('GET', resource+'&rand1='+(current_submission)+'&rand2='+rand2, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {	
			if (xmlhttp.status == 200) {
				if (ignore_response == true) {
				} else {
					if (is_response_value == true) {
						var results = _trim(xmlhttp.responseText);
						if (results == 'NULL') {
							results = '';
						}
						document.getElementById(element_id).value = results;
					} else {
						document.getElementById(element_id).innerHTML = xmlhttp.responseText;
					}
				}
				if (final_focus_element_id != null && final_focus_element_id != '') {
					document.getElementById(final_focus_element_id).focus();
				}
			} else {
				alert ('Request failed');
			}
		}
	}
	xmlhttp.setRequestHeader("Content-type", "text/html");
	xmlhttp.send(null);
}

function _trim(chkString) {
	var startString = 0;
	var endString = 0;
	var startFound = false;
	for (i = 0; i < chkString.length; i++) {
		if (chkString.charAt(i) == ' ' || chkString.charAt(i) == '\n') {
			if (startFound == false) {
				startString = i + 1;
			}
		} else {
			startFound = true;
			endString = i;
		}
	}
	return chkString.substring (startString, endString+1);	
}
