/*

Class: ajax

Depends: general

Description: Implements asynchronous javascript 

*/

function yagi_ajax () {

	this.objGeneral = new yagi_general();
	this.debug = false;

	this.start = function () {

		var xmlhttp;

		if (window.XMLHttpRequest) {

			xmlhttp = new XMLHttpRequest();

		}
		else {

			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

		}

		xmlhttp.debug = this.debug;

		return xmlhttp;

	}

	this.callLocation = function (newLocation, functionName) {

		var xmlhttp = this.start();
		var paramList = [];

		xmlhttp.onreadystatechange = function () {

			if (this.readyState == 4 && this.status == 200) {

				var debugString = "/*debug*/";

				if (this.responseText.substr(0, debugString.length) == debugString) {

					this.debug = true;

				}

				if (this.debug) {

					alert(this.responseText);

				}

				eval(this.responseText);

			}

		}

		paramList[paramList.length] = "yagiAjax=1";
		paramList[paramList.length] = "functionName=" + functionName;

		for (var i = 2; i < arguments.length; i++) {

			paramList[paramList.length] = "param[]=" + encodeURIComponent(arguments[i]);

		}

		xmlhttp.open("POST", newLocation, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(paramList.join("&"));

	}

	this.call = function (functionName) {

		var xmlhttp = this.start();
		var paramList = [];

		xmlhttp.onreadystatechange = function () {

			if (this.readyState == 4 && this.status == 200) {

				var debugString = "/*debug*/";

				if (this.responseText.substr(0, debugString.length) == debugString) {

					this.debug = true;

				}

				if (this.debug) {

					alert(this.responseText);

				}

				eval(this.responseText);

			}

		}

		paramList[paramList.length] = "yagiAjax=1";
		paramList[paramList.length] = "functionName=" + functionName;

		for (var i = 1; i < arguments.length; i++) {

			paramList[paramList.length] = "param[]=" + encodeURIComponent(arguments[i]);

		}

		xmlhttp.open("POST", document.location, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(paramList.join("&"));

	}

	this.getReturn = function (functionName) {

		var xmlhttp = this.start();
		var paramList = [];

		paramList[paramList.length] = "yagiAjax=1";
		paramList[paramList.length] = "functionName=" + functionName;

		for (var i = 1; i < arguments.length; i++) {

			paramList[paramList.length] = "param[]=" + encodeURIComponent(arguments[i]);

		}

		xmlhttp.open("POST", document.location, false);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(paramList.join("&"));

		var debugString = "/*debug*/";

		if (xmlhttp.responseText.substr(0, debugString.length) == debugString) {

			xmlhttp.debug = true;

		}

		if (xmlhttp.debug) {

			alert(xmlhttp.responseText);

		}

		return eval(xmlhttp.responseText);

	}

	this.post = function (objForm) {

		var fieldNames = [];
		var formHasFiles = false;

		for (var i = 0; i < objForm.elements.length; i++) {

			fieldNames[objForm.elements[i].name] = true;

			if ((objForm.elements[i].type) && (objForm.elements[i].type == "file")) {

				formHasFiles = true;

			}

		}

		if (!formHasFiles) {

			var xmlhttp = this.start();
			var paramList = [];

			paramList[paramList.length] = "yagiAjax=1";
			paramList[paramList.length] = "functionName=submit";

			for (var i in fieldNames) {

				if (i == "") {

					continue;

				}

				paramList[paramList.length] = i + "=" + encodeURIComponent(this.objGeneral.getValue(i));

			}

			xmlhttp.onreadystatechange = function () {

				if (this.readyState == 4 && this.status == 200) {

					var debugString = "/*debug*/";

					if (this.responseText.substr(0, debugString.length) == debugString) {

						this.debug = true;

					}

					if (this.debug) {

						alert(this.responseText);

					}

					eval(this.responseText);

				}

			}

			xmlhttp.open("POST", objForm.action, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.send(paramList.join("&"));

		}
		else {

			var iframeName = "yagi_ajax_iframe_" + new Date().getTime();

			var iframe = document.createElement("iframe");
			iframe.name = iframeName;

			iframe.style["display"] = "none";

			objForm.appendChild(iframe);

			objForm.enctype = "multipart/form-data";
			objForm.method = "POST";
			objForm.target = iframeName;

			var hiddenInput1 = document.createElement("input");
			hiddenInput1.name = "yagiAjax";
			hiddenInput1.type = "hidden";
			hiddenInput1.value = "1";

			objForm.appendChild(hiddenInput1);

			var hiddenInput2 = document.createElement("input");
			hiddenInput2.name = "functionName";
			hiddenInput2.type = "hidden";
			hiddenInput2.value = "submit";

			objForm.appendChild(hiddenInput2);

			objForm.submit();

		}

	}

	this.open = function (newLocation, target) {

		var xmlhttp = this.start();

		xmlhttp.target = target;

		xmlhttp.onreadystatechange = function () {

			if (this.readyState == 4 && this.status == 200) {

				var debugString = "/*debug*/";

				if (this.responseText.substr(0, debugString.length) == debugString) {

					this.debug = true;

				}

				if (this.debug) {

					alert(this.responseText);

				}

				var scriptString = "/*script*/";

				if (this.responseText.substr(0, scriptString.length) == scriptString) {

					eval(this.responseText);

				}

				new yagi_general().getObject(this.target).innerHTML = this.responseText;

			}

		}

		xmlhttp.open("GET", newLocation + ((newLocation.indexOf("?") >= 0) ? "&": "?") + "yagiAjax=1", true);
		xmlhttp.send();

	}

	this.getXML = function (newLocation) {

		var xmlhttp = this.start();

		xmlhttp.open("GET", newLocation + ((newLocation.indexOf("?") >= 0) ? "&": "?") + "yagiAjax=1", false);
		xmlhttp.send();

		var responseText = xmlhttp.responseText;

		if (this.debug) {

			alert(responseText);

		}

		var scriptString = "/*script*/";

		if (responseText.substr(0, scriptString.length) == scriptString) {

			eval(responseText);

			return false;

		}

		return xmlhttp.responseXML;

	}

}

