/**
 * Javascript XMLHTTP sample with pool control
 * Pool control reference at URL: http://birdshome.cnblogs.com/archive/2004/12/27/82274.html
 * 
 * Author: Li Hou Yu (Karajan Lee), Shanghai, China.
 * Modified for domain lookup
 */

function jsXmlHttp()
{
	this.xmlhttp_pool_length = 100;
	this.xmlhttp_pool = new Array();

	this.query_entity = new Array();

	this.getXmlHttpObj = function()
	{
		var xmlhttp = null;

		for (var i=0; i<this.xmlhttp_pool.length; i++)
		{
			if (this.xmlhttp_pool[i].readyState == 4 || this.xmlhttp_pool[i].readyState == 0)
			{
				xmlhttp = this.xmlhttp_pool[i];
				break;
			}
		}

		if (xmlhttp == null)
		{
			return this.loadXmlHttpObj();
		}

		return xmlhttp;
	}

	this.loadXmlHttpObj = function()
	{
		if (this.xmlhttp_pool.length < this.xmlhttp_pool_length)
		{
			var xmlhttp = null;

			if (document.all)
			{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			else if (XMLHttpRequest)
			{
				xmlhttp = new XMLHttpRequest();
			}
			else
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			if (xmlhttp)
			{
				this.xmlhttp_pool[this.xmlhttp_pool.length] = xmlhttp;
				return xmlhttp;
			}
		}

		return false;
	}

	this.setQueryEntity = function(key, val)
	{
		if (!this.query_entity[key])
		{
			this.query_entity[key] = new Array();
		}
		this.query_entity[key].push(val);
	}

	this.buildQuery = function()
	{
		var query_string = "";
		
		for (var key in this.query_entity)
		{
			for (var i=0; i<this.query_entity[key].length; i++)
			{
				query_string += key + "=" + encodeURIComponent(this.query_entity[key][i]) + "&";
			}
		}
		
		return query_string.substring(0, query_string.length - 1);
	}

	this.sendRPC = function(url, callback, rsctrlid)
	{
		var xmlhttp = null;
		
		xmlhttp = this.getXmlHttpObj();
		
		if (xmlhttp && xmlhttp != null)
		{
			xmlhttp.open("POST", url);
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4)
				{
					callback(xmlhttp.responseText, rsctrlid);
				}
			}
			xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlhttp.send(this.buildQuery());
		}

		this.query_entity = new Array();
	}
}


