// as the library is loaded, create global variables

try {

	var AJAX_RESPONSE = 'AJAX_NO_RESPONSE_YET'; // 'string buffer' for the server response
	var AJAX_CLIENT = AJAX_createRequestObject(); // link to the js http request object

	// for each state of the js client a callback is created
	// you need to use these objects to register methods from your own app handlers
	// example: as soon as a response comes you can alert or parse it
	// see the example of the handler here futher

	// registering just like  AJAXCM_COMPLETE.register (your_invoke_function);
	var AJAXCM_COMPLETE     = new AJAXCallbackManager();  // status code 4
	var AJAXCM_INTERACTIVE  = new AJAXCallbackManager();  // status code 3
	var AJAXCM_LOADED       = new AJAXCallbackManager();  // status code 2
	var AJAXCM_LOADING      = new AJAXCallbackManager();  // status code 1
	var AJAXCM_INIT         = new AJAXCallbackManager();  // status code 0

} catch (error) {

	AJAX_handle_exception (error);

}


/**
 *
 * This is a better callback manager which allows many callbacks
 *
 */
function AJAXCallbackManager() 
{
	var callback_counter =0;
	var callbacks = new Array ();

	/** this one iterates over registered callbacks */
	this.callback=function callback(){
		for (var i=0;i<callbacks.length;i++){
			try{
				callbacks[i](AJAX_RESPONSE);
			} catch (error) {
				AJAX_handle_exception (error);
			}
		}
	}

	this.register=function(callbackFunction) {
		callbacks[callback_counter++]=callbackFunction;
	}
}


/**
 * this one is (c) Rasmus Lerdorf, maybe some browser adjustments are needed
 */

function AJAX_createRequestObject() {
	var ro;
	if (document.all){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}


/**
 *  send a get query
 */
function AJAX_get(url, async) {
	if (async){
		AJAX_CLIENT.open('GET', url);
		AJAX_CLIENT.onreadystatechange = AJAX_handleResponse;
		AJAX_CLIENT.send(null);
	} else {
		AJAX_CLIENT.open('GET', url ,false);
		AJAX_CLIENT.send(null);
		return AJAX_CLIENT.responseText;
	}
}


/**
 *   send a post query
 */
function AJAX_post(url, request, async) {
	try {
		if (async) {
			AJAX_CLIENT.open("POST",url,true);
			AJAX_CLIENT.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			AJAX_CLIENT.send(request);
			AJAX_CLIENT.onreadystatechange = AJAX_handleResponse;
		} else {
			AJAX_CLIENT.open("POST",url,false);
			AJAX_CLIENT.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			AJAX_CLIENT.send(request);
			return AJAX_CLIENT.responseText;
		}
	} catch (error) {
		AJAX_handle_exception (error);
	}
}


/**
 * this is the extended the server response handler for async requests
 */   
function AJAX_handleResponse() {
	
	if(AJAX_CLIENT.readyState == 4){
		AJAX_internal_response_handler ('complete (4).');
		AJAX_RESPONSE = AJAX_CLIENT.responseText;
		AJAXCM_COMPLETE.callback ();
	}
	
	if(AJAX_CLIENT.readyState == 3){
		AJAX_internal_response_handler ('interactive (3)');
		AJAXCM_INTERACTIVE.callback ();
	}
	
	if(AJAX_CLIENT.readyState == 2){
		AJAX_internal_response_handler ('loaded (2).');
		AJAXCM_LOADED.callback ();
	}
	
	if(AJAX_CLIENT.readyState == 1){
		AJAX_internal_response_handler ('loading.... (1)');
		AJAXCM_LOADING.callback ();
	}
	
	if(AJAX_CLIENT.readyState == 0){
		AJAX_internal_response_handler ('uninitialized (0)');
		AJAXCM_INIT.callback ();
	}
}


/**
 * feel free to replace this function with whatever you want
 * the idea is hier is debugging made mostly simple ;-)
 */
function AJAX_internal_response_handler (status)
{
	//document.title = 'js client status: ' + status;
}

/**
 *  feel free to overwrite this function the way you like it
 */


function AJAX_handle_exception (error)
{
	alert("[Exception|creating AJAX_ro()]: "+error.name+ "; "+error.message + "; "+error.description+"; "+error.number);  
}