function Ajax(args) {
    this.url = args.url || "";
    this.params = args.params || "";
    this.mime = args.mime || "text/html";
    this.onComplete = args.onComplete || this.defaultOnCompleteFunc;
    this.onLoading= args.onLoading || this.defaultOnLoadingFunc;
    this.onError = args.onError || this.defaultOnErrorFunc;
    this.method = args.method || "post";
    this.loadData();
}

Ajax.prototype = {
    READY_STATE_COMPLETE : 4,
    getRequest : function () {
	var funcs = [
	    function() {return new ActiveXObject('Msxml2.XMLHTTP')},
	    function() {return new ActiveXObject('Microsoft.XMLHTTP')},
	    function() {return new XMLHttpRequest()},
	];

	var req = null;
	for (var i = 0; i < funcs.length; i++) {
	    var f = funcs[i];
	    try {
		req = f();
		break;
	    } catch (e) {}
	}

	return req || false;
    },

    loadData : function () {
	this.req = this.getRequest();
	
	if (this.req) {
		this.onLoading();
	    try {
		var loader = this;
		this.req.onreadystatechange = function () {
			if (loader.req.readyState == loader.READY_STATE_COMPLETE) {
			    loader.onComplete.call(loader, loader.req);
			}
		}
		this.req.open(this.method, this.url, true);
		if (this.req.overrideMimeType) {
		    this.req.overrideMimeType(this.mime);
		}
		this.req.send(this.method == "post" ? this.params : null);
	    } catch (e) {
		// throw e
		this.onError.call(this, e);
	    }
	}
    },

    defaultOnCompleteFunc : function () {
	alert(this.req.responseText);
    },

    defaultOnLoadingFunc : function () {
    },

    defaultOnErrorFunc : function (error) {
	alert(error);
    }
}


