/* Description: A short and simple, target language independent implementation of AJAX. */
/* Supported browser platforms: Tested successfully in  Firefox,  Mozilla, Opera,  Safari, and
Internet Explorer 5.5 through 7. Will try other browsers as problems are presented. */
/* Note: Earlier version did output internally, all that was supplied was the ID of the output
container. However I've since added support for calling external functions. This is only risky
if the user written program that uses it is, so use it with care. */
/* Note: I should write a replacement for innerHTML soon as well that will interpret DOM based
languages and manipulate DOM  objects  using DOM only, so as  not to support the proprietarily
introduced innerHTML method. */

/*
	function: ajax_create_request_object();
	usage: var new_object = ajax_create_request_object();
	description: Create a request object.
*/
function ajax_create_request_object() {
	/* Set initially to null. */
	var descriptor = "";
	try {
		/* Attempt to create standard request object from standard function. */
		descriptor = new XMLHttpRequest();
	} catch (try_current_microsoft) {
		try {
			/* Attempt to create microsoft request object from non-standard function. */
			descriptor = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (try_generic_microsoft) {
			try {
				/* Attempt to create old microsoft request object from non-standard function. */
				descriptor = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (unsupported) {
				/* Reset the descriptor to null, this point is complete failure. */
				descriptor = "";
				/* Return descriptor for evaluation. */
				return descriptor;
			}
		}
	}
	/* Return descriptor for evaluation. */
	return descriptor;
}

/*
	function: ajax_handle_request(method,target,payload,output);
	usage: ajax_handle_request('POST','process.php',payload_var,'output_div');
	description: Handle request and request response.
*/
function ajax_handle_request(method,target,payload,func,funcargs) {
	/* If no method is sent, die.. */
	if ( !method ) { alert("Error: No method."); return 1; }
	/* If no target is sent, die. */
	if ( !target ) { alert("Error: No target."); return 1; }
	/* If no handler is sent, die. */
	if ( !func ) { alert("Error: No handler."); return 1; }
	/* Create request object. */
	var request_object = ajax_create_request_object();
	/* NULL object is a failure. */
	if ( !request_object ) { return 1; }
	/* IF METHOD IS GET, payload is tagged to the target URL. */
	if ( method == "GET" && payload ) { target = target + "?" + payload; }
	/* Open request object with supplied target and message in async mode. */
	request_object.open(method,target,true);
	/* Handle state changes on the ready-ness of the object. */
	request_object.onreadystatechange = function() {
		/* If request object in state 4 (finished) and return status 200 (a-okay), then deal with response text. */
		if ( request_object.readyState == 4 && request_object.status == 200) {
			if ( funcargs ) {
				/* If the argument array/var is set, then pass it to the function. */
				window[func](request_object.responseText,funcargs);
			} else {
				/* If not, then simply call he function, passing the response text alone to it. */
				window[func](request_object.responseText);
			}
		}
	}
	/* IF METHOD IS POST, then the payload is sent separately as the message response. */
	if ( method == "POST" ) {
		/* Set header content type. */
		request_object.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		/* Set header content length based on payload. */
		request_object.setRequestHeader("Content-length",payload.length);
		/* Send (POST) payload. */
		request_object.send(payload);
	}
	/* IF METHOD IS GET, then the payload is sent with the URL, and doesn't require a response. */
	else if ( method == "GET" ) {
		/* Respond with a null message since a response isn't required. */
		request_object.send(null);
	}
	/* Return 0, all OK. */
	return 0;
}

function ajax_data_by_vars(variable_name,variable_value) {
	/* Need at least 2 arguments. */
	if ( arguments.length % 2 != 0 || arguments.length < 2 ) {
		return 1;
	}
	/* Declare data and counter variables. */
	var i = 0;
	var data = "";
	/* Loop through arguments and output data string. */
	for ( i ; i < arguments.length ; i = i + 2 ) {
		data = data + arguments[i] + "=" + escape(arguments[i+1]);
		if ( i != arguments.length-2 ) {
			data = data + "&";
		}
	}
	/* Return data string. */
	return data;
}

function ajax_data_by_ids() {
	/* Need at least 1 argument. */
	if ( arguments.length < 1 ) {
		return 1;
	}
	/* Declare data and counter variables. */
	var i = 0;
	var data = "";
	/* Loop through arguments and output data string. */
	for ( i ; i < arguments.length ; i++ ) {
		data = data + arguments[i] + "=" + escape(document.getElementById(arguments[i]).value);
		if ( i != arguments.length-1 ) {
			data = data + "&";
		}
	}
	/* Return data string. */
	return data;
}


