/***********************************************************************************************
* Example Useage:

	<a href="#" onclick="ajaxit('targetid','targetfile.php','get');">link</a>
	<a href="#" onclick="ajaxit('targetid','targetfile.php');">link</a> <!-- POST, default -->

*
***********************************************************************************************/

function ajaxit(target_id, target_url, method) {
	res = ajaxit_engine(target_id, target_url, {
		meth:method,
		func_pre:"ajaxit_loading('show')",
		func_post:"ajaxit_loading('hide')",
		func_forbid:"ajaxit_forbid()",
		func_unauth:"ajaxit_unauth()",
		func_notfound:"ajaxit_notfound()",
		func_error:"ajaxit_error()" }
	);
	return false;
}

function ajaxit2formvalue(targetformelement_id, target_url, method) {
	res = ajaxit_engine2formvalue(targetformelement_id, target_url, {
		meth:method,
		func_forbid:"ajaxit_forbid()",
		func_unauth:"ajaxit_unauth()",
		func_notfound:"ajaxit_notfound()",
		func_error:"ajaxit_error()" }
	);
	return false;
}

function ajaxit_loading(action) {

}

function ajaxit_forbid() {
	alert("Authentication failed.");
}

function ajaxit_unauth() {
	alert("Access is denied.");
}

function ajaxit_notfound() {
	alert("Target not found.");
}

function ajaxit_error(err) {
	alert("Unknown error: "+err);
}

/**********************************************************************************
* The "options" parameter is an anonymous object which includes the following
* available options:
*
* meth:      		The request method. Can be "get" or "post". Default is "post".
* async:     		Toggles asynchronous mode. Default is true.
* func_pre: 		A function or list of functions to be called before the AJAX request is made. A list of functions must be separated by the semi-colon like this: "showLoad(); animateText(); hideDiv('bob')".
* func_post:   	A function or list of functions to be called after a successful AJAX request. Uses the same format as "func_pre".
* func_forbid: 	A function or list of functions to be called when the AJAX request has not authenticated (http status code 401 is encountered). Uses the same format as "func_pre".
* func_unauth: 	A function or list of functions to be called when the AJAX request is not authorized (http status code 403 is encountered). Uses the same format as "func_pre".
* func_notfound: 	A function or list of functions to be called when the AJAX requested URL is not found (http status code 404 is encountered). Uses the same format as "func_pre".
* func_error: 		A function or list of functions to be called when the AJAX request is unsuccessful. Uses the same format as "func_pre".
*
* Function returns true on success and false on failure.
*
* Example Usage:

	ajaxit_engine("targetid", "targetfile.php", {
		meth:"post",
		async:true,
		func_pre:"show('loading')",
		func_post:"hide('loading'); show('target')",
		func_forbid:"ajaxit_forbid()",
		func_unauth:"ajaxit_unauth()",
		func_notfound:"ajaxit_notfound()",
		func_error:"ajaxit_error()"
		}
	);

*
*******************************************************************************/

function ajaxit_engine(target_id, target_url, options) {
	var req 					= false;
	var showalert 			= false;
	var meth 				= options.meth 				|| "post";
	var async 				= options.mode 				|| true;
	var func_pre 			= options.func_pre 			|| "";
	var func_post 			= options.func_post 			|| "";
	var func_forbid 		= options.func_forbid 		|| "";
	var func_unauth		= options.func_unauth 		|| "";
	var func_notfound 	= options.func_notfound 	|| "";
	var func_error 		= options.func_error			|| "";

	if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); }
	else {
		eval(func_err);
		return false;
	}

	if (func_pre != "") { eval(func_pre); }

	if (meth == "post") {
		if (target_url.indexOf("?") >= 0) {
			var url_part = target_url.split("?");
			target_url = url_part[0];
			data = url_part[1];
		}
		else { data = ""; }
	}
	else { data = null; }

	req.open(meth, target_url, async);
	if (meth == "post") { req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); }
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				document.getElementById(target_id).innerHTML = req.responseText;
				if (func_post != "") { eval(func_post); }
				return true;
			}
			else {
				if (req.status == 401 && func_forbid != "") { eval(func_forbid); }
				else if (req.status == 403 && func_unauth != "") { eval(func_unauth); }
				else if (req.status == 404 && func_notfound != "") { eval(func_notfound); }
				else { if (func_error != "") { eval(func_error(req.status)); } }
				if (func_post != "") { eval(func_post); }
				return false;
			}
		}
	};
	req.send(data);
}

function ajaxit_engine2formvalue(targetformelement_id, target_url, options) {
	var req 					= false;
	var showalert 			= false;
	var meth 				= options.meth 				|| "post";
	var async 				= options.mode 				|| true;
	var func_pre 			= options.func_pre 			|| "";
	var func_post 			= options.func_post 			|| "";
	var func_forbid 		= options.func_forbid 		|| "";
	var func_unauth		= options.func_unauth 		|| "";
	var func_notfound 	= options.func_notfound 	|| "";
	var func_error 		= options.func_error			|| "";

	if (window.XMLHttpRequest) { req = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP"); }
	else {
		eval(func_err);
		return false;
	}

	if (func_pre != "") { eval(func_pre); }

	if (meth == "post") {
		if (target_url.indexOf("?") >= 0) {
			var url_part = target_url.split("?");
			target_url = url_part[0];
			data = url_part[1];
		}
		else { data = ""; }
	}
	else { data = null; }

	req.open(meth, target_url, async);
	if (meth == "post") { req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); }
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				document.getElementById(targetformelement_id).value = req.responseText;
				if (func_post != "") { eval(func_post); }
				return true;
			}
			else {
				if (req.status == 401 && func_forbid != "") { eval(func_forbid); }
				else if (req.status == 403 && func_unauth != "") { eval(func_unauth); }
				else if (req.status == 404 && func_notfound != "") { eval(func_notfound); }
				else { if (func_error != "") { eval(func_error(req.status)); } }
				if (func_post != "") { eval(func_post); }
				return false;
			}
		}
	};
	req.send(data);
}