
var ajax_req;

function ajax_is_available()
{
	var tmp = null;
	
	if (window.XMLHttpRequest)     tmp = new XMLHttpRequest();
	else if (window.ActiveXObject) tmp = new ActiveXObject("Microsoft.XMLHTTP");
	return (typeof(tmp.open) == "object" || typeof(tmp.open) == "function");
}

function ajax_send(request, target, call_back_func)
{
	ajax_req = null;
	
	if (window.XMLHttpRequest)     ajax_req = new XMLHttpRequest();
	else if (window.ActiveXObject) ajax_req = new ActiveXObject("Microsoft.XMLHTTP");
	if (typeof(ajax_req.open) != "object" && typeof(ajax_req.open) != "function") ajax_req = new SimpleAJAX(); // ie6 fail
	
	if (ajax_req)
	{
		ajax_req.request = request;
		ajax_req.onreadystatechange = function() { ajax_receive(request, call_back_func); }
		ajax_req.open("POST", target, true);
		ajax_req.send(to_server(request));
	}
}

function SimpleAJAX()
{
	this.readyState = 0;
	this.status_check_wait = 256;
	
	this.open = function(method, url, is_asynchronous)
	{
		this.method = method;
		this.url = url;
		this.is_asynchronous = is_asynchronous;
	}
	
	this.send = function(request)
	{
		var elem, tmp_url, _this;
		
		_this = this;
		elem = doc_elem("simple_ajax_loc");
		if (empty(elem))
		{
			elem = document.createElement("iframe");
			elem.setAttribute("id", "simple_ajax_loc");
			elem.style.border = "0";
			elem.style.width = "0";
			elem.style.height = "0";        
			document.body.appendChild(elem);
		}
		
		elem.src = this.url + "?POST="+escape(request);
		setTimeout(function() { _this.check_status(); }, this.status_check_wait);
	}
	
	this.check_status = function()
	{
		var elem, _this;
		
		_this = this;
		elem = doc_elem("simple_ajax_loc");
		if (elem.getAttribute("readyState") == "complete")
		{
			this.responseText = window["simple_ajax_loc"].document.body.innerHTML;
			this.readyState = 4;
			this.onreadystatechange();
			window["simple_ajax_loc"].document.body.innerHTML = "";
		}
		else
		{
			this.status_check_wait += 256;
			setTimeout(function() { _this.check_status(); }, this.status_check_wait);
		}
	}
}

function ajax_receive(request, call_back_func)
{
	if (ajax_req.readyState == 4)
	{
		if (call_back_func) call_back_func(request, ajax_req.responseText);
	}
}
