﻿
function FactoryXMLHttpRequest()
{
	if(window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var msxmls = new Array(
			'Msxml2.XMLHTTP.5.0',
			'Msxml2.XMLHTTP.4.0',
			'Msxml2.XMLHTTP.3.0',
			'Msxml2.XMLHTTP',
			'Microsoft.XMLHTTP');
		for (var i = 0; i < msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]);
			} catch (e) {
			}
		}
	}
	throw new Error("Could not instantiate XMLHttpRequest");
}

function Asynchronous( )
{
	this._xmlhttp = new FactoryXMLHttpRequest();
}

function Asynchronous_call(url, method, body, async)
{
	var instance = this;
	var b = true;
	if( typeof async != 'undefined' )
		b = async;
	if ( method )
		this._xmlhttp.open(method, url, b);
	else
		this._xmlhttp.open('GET', url, b);
	this._xmlhttp.onreadystatechange = function()
	{
		switch(instance._xmlhttp.readyState)
		{
			case 1:
				instance.loading();
				break;
			case 2:
				instance.loaded();
				break;
			case 3:
				instance.interactive();
				break;
			case 4:
			if ( instance.complete )
				instance.complete(instance._xmlhttp.responseText, instance._xmlhttp.responseXML,
					instance._xmlhttp.status, instance._xmlhttp.statusText);
					
			break;
		}
	}
	if ( body && body != null )
	{
		this._xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		this._xmlhttp.send(body);
	}
	else
		this._xmlhttp.send(null);
}
function Asynchronous_loading() {
}
function Asynchronous_loaded() {
}
function Asynchronous_interactive() {
}
function Asynchronous_complete(responseText, responseHTML,status, statusText) {
}
Asynchronous.prototype.loading = Asynchronous_loading;
Asynchronous.prototype.loaded = Asynchronous_loaded;
Asynchronous.prototype.interactive = Asynchronous_interactive;
Asynchronous.prototype.complete = Asynchronous_complete;
Asynchronous.prototype.call = Asynchronous_call;

function AjaxCall( url, callback, body, async )
{
	var asy = new Asynchronous();
	if ( body && body!=null)
	{
		asy.complete=callback;
		asy.call(url,"POST",body,async);
	}
	else {
		asy.complete=callback;
		asy.call(url,"GET",null,async);
	}
}

