﻿function CreateXMLHttpRequest() {
	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) {}			
		}
	}
	// could not instantiate XMLHttpRequest
}

function ajax() {
	this._xmlhttp = CreateXMLHttpRequest();	
}

function ajaxCall(url) {
	var instance = this;	
	this._xmlhttp.open('POST', url, true)
	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:
				instance.complete(instance._xmlhttp.status,
								instance._xmlhttp.statusText,
								instance._xmlhttp.responseText,
								instance._xmlhttp.responseXML);
				
				break;
			}
		}
	this._xmlhttp.send(null);
}

function ajaxCallGet(url) {
	var instance = this;	
	this._xmlhttp.open('GET', url, true)
	//this._xmlhttp.open('POST', url, true)
	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:
				instance.complete(instance._xmlhttp.status,
								instance._xmlhttp.statusText,
								instance._xmlhttp.responseText,
								instance._xmlhttp.responseXML);
				break;
			}
		}
	this._xmlhttp.send(null);
}

function ajaxLoading() {}
function ajaxLoaded() {}
function ajaxInteractive() {}
function ajaxComplete(status, statusText, responseText, responseXML) {}

ajax.prototype.loading = ajaxLoading;
ajax.prototype.loaded = ajaxLoaded;
ajax.prototype.interactive = ajaxInteractive;
ajax.prototype.complete = ajaxComplete;
ajax.prototype.call = ajaxCall;
ajax.prototype.callGet = ajaxCallGet;


