/* Ajax Object; author: <peter@spi.com.pl> */

var	ajax =
{	
	engines:	new Array(),
	newEngine:	function()
			{	var objA = new this.AJAX();
				if(!objA.xmlHttpRequest) return -1;
				this.engines[this.engines.length] = objA; return this.engines.length - 1; },
	setEngine:	function()
			{	for(var i=0; i<this.engines.length; i++)
					if(this.engines[i].xmlHttpRequest.readyState == 0 || this.engines[i].free) { this.engines[i].free = false; return i; }
				return this.newEngine(); },
	busy:		function()
			{	var ret = 0;
				for(var i=0; i<this.engines.length; i++) if(!this.engines[i].free) ret++; return ret; },
	get:	function(args) 								// args.url: obligation
		{	var engine = this.setEngine(); if(engine == -1) return -1; args.method = "GET";
			return this.engines[engine].handleRequest(args); },
	post:	function(args)								// args.url: obligation; args.post: welcome (no file transfer)
		{	var engine = this.setEngine(); if(engine == -1) return -1; args.method = "POST";
			return this.engines[engine].handleRequest(args); },

	query:	function(form)
		{	var query = "";
			for(var i=0; i<form.elements.length; i++)
			{	if(form.elements[i].disabled) continue;
				switch(form.elements[i].type)
				{	case "text":
					case "password":
					case "hidden":
					case "textarea":	if(query.length > 0) query += "&";
								query += encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(form.elements[i].value);
								break;
					case "radio":
					case "checkbox":	if(form.elements[i].checked)
								{	if(query.length > 0) query += "&";
									query += encodeURIComponent(form.elements[i].name) + "=";
									query += encodeURIComponent(form.elements[i].value); }
								break;
					case "select-one":	if(form.elements[i].selectedIndex >= 0)
								{	if(query.length > 0) query += "&";
									query += encodeURIComponent(form.elements[i].name) + "=";
									query += encodeURIComponent(form.elements[i].options[form.elements[i].selectedIndex].value); }
								break;
					case "select-multiple":	for(var ii=0; ii<form.elements[i].options.length; ii++)
									if(form.elements[i].options[ii].selected)
									{	if(query.length > 0) query += "&";
										query += encodeURIComponent(form.elements[i].name) + "=";
										query += encodeURIComponent(form.elements[i].options[ii].value); }
								break; } } return query; },
	AJAX:	function()
		{	
			function create_xmlHttpRequest()
			{	if (window.XMLHttpRequest) return new XMLHttpRequest();	// firefox, safari, ...
				if (window.XDomainRequest) return new XDomainRequest(); // IE8 only ...
				if (window.ActiveXObject)				// IE ...
				{	var ActXMode = [ "MSXML2.XMLHttp", "Microsoft.XMLHttp", "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0" ];
					for (var i=0; i<ActXMode.length; i++)
					{	try
						{	return new ActiveXObject(ActXMode[i]); }
	   					catch(e)
						{					// suppress error here
						}
					}
				}
				else return null; };					// no coverage for XMLHttpRequest

			function process_xmlHttpRequest()
			{	switch (ajax.xmlHttpRequest.readyState) {
                			case 1: ajax._onLoading(); break;
                			case 2: ajax._onLoaded(); break;
                			case 3: ajax._onInteractive(); break;
                			case 4: ajax._onComplete(); break; }
			}

			function custom_xmlHttpRequest(args)
			{	ajax.onLoading = (typeof args.onLoading == "function")? args.onLoading : null;
				ajax.onLoaded = (typeof args.onLoaded == "function")? args.onLoaded : null;
				ajax.onInteractive = (typeof args.onInteractive == "function")? args.onInteractive : null;
				ajax.onInteractiveTimer = (typeof args.onInteractiveTimer != "undefined")? args.onInteractiveTimer : null;
				ajax.onComplete = (typeof args.onComplete == "function")? args.onComplete : null;
				ajax.onCompleteError = (typeof args.onCompleteError == "function")? args.onCompleteError : null;

				ajax.onCompleteLoad = (typeof args.onCompleteLoad == "string")? args.onCompleteLoad : "";
			}

		    var ajax = new Object();

			ajax._onLoading 	= function () { if (typeof ajax.onLoading == "function") ajax.onLoading(); };
			ajax._onLoaded 		= function () { if (typeof ajax.onLoaded == "function") ajax.onLoaded(ajax.xmlHttpRequest.getAllResponseHeaders()); };
			ajax._onInteractive 	= function () 
						{	if (ajax.xmlHttpRequest.readyState != 3) return;
							if (typeof ajax.onInteractive == "function") 
							{	var contentLength = ajax.xmlHttpRequest.getResponseHeader("Content-length");
								ajax.onInteractive({sent: ajax.xmlHttpRequest.responseText.length, value: contentLength});
								if (ajax.onInteractiveTimer != null) window.setTimeout(ajax._onInteractive,ajax.onInteractiveTimer);
							}
						};
			ajax._onComplete 	= function ()
						{	if (ajax.xmlHttpRequest.status == 200 || ajax.xmlHttpRequest.status == 0)
							{	var content = ajax.xmlHttpRequest.getResponseHeader("Content-Type").toLowerCase();
								if (content.indexOf("json") != -1) eval(ajax.xmlHttpRequest.responseText);
								else if (typeof ajax.onComplete == "function")
								{	if (content.indexOf("xml") != -1) ajax.onComplete(ajax.xmlHttpRequest.responseXML);
									else ajax.onComplete(ajax.xmlHttpRequest.responseText,ajax.onCompleteLoad);
								}
							}
							else
							{	if (typeof ajax.onCompleteError == "function") 
									ajax.onCompleteError(ajax.xmlHttpRequest.status,ajax.xmlHttpRequest.statusText,ajax.onCompleteLoad); }
							ajax.free = true;
						};

		    	ajax.xmlHttpRequest = create_xmlHttpRequest();
			ajax.free = false;

			ajax.handleRequest = function(args)						// should be defined: args.method,args.url
			{	if (typeof args == "undefined" || args == null) return -1;
				custom_xmlHttpRequest(args);
				if (ajax.xmlHttpRequest)
				{	ajax.xmlHttpRequest.open(args.method, args.url, true);
					ajax.xmlHttpRequest.onreadystatechange = process_xmlHttpRequest;
					switch(args.method)
					{	case "GET":	ajax.xmlHttpRequest.send("");
								break;
						case "POST":	ajax.xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
								ajax.xmlHttpRequest.send(args.post);	// to do : definition of arg.post
								break; } }
				else { alert("xmlHttpRequest is not supported"); }
			};

			return ajax;
		}
}


