function Transport_ajax(debug)
{
    this.callback = null;
    this.onError = function (statusCode) {};
    this.debug = typeof(debug) == 'undefined' ? false : debug;

    if (window.ActiveXObject) {
    	this.transport = new ActiveXObject('Microsoft.XMLHTTP');
    }
    else if (window.XMLHttpRequest) {
    	this.transport = new XMLHttpRequest();
    }
    else {
    	throw "Your browser does not support XMLHttpRequest object.";
    }

    if (this.debug) {
        var console = document.createElement("div");
        console.style.border = "1px solid black";
        console.style.width = "100%";
        console.style.height = "200px";
        console.style.overflow = "auto";
        document.body.appendChild(console);
        this.console = console;
    }

    this._inProgress = false;
}

Transport_ajax.prototype.send = function (url, callback) {
    this.sendGet(url, callback);
}

Transport_ajax.prototype.onStateChange = function () {
    this._inProgress = false;

    if (this.transport.readyState == 4) {
        try {
            var status = this.transport.status;
        }
        catch (e) {
            this.onError(null);
            this.transport.onreadystatechange = function(){};
            return;
        }

        if (status == 200) {
            this.transport.onreadystatechange = function(){};

            if (this.debug) {
                this._toConsole("RESULT: " + this.transport.responseText);
            }

            try {
                eval('var res = ' + this.transport.responseText + ';');
            }
            catch (e) {
                this.onError(null);
                return;
            }

            if (this.callback) {
                this.callback(res);
                this.callback = null;
            }
        }
        else {
            this.onError(this.transport.status);
        }
    }
}

Transport_ajax.prototype.sendGet = function (url, callback) {
    if (this._inProgress) {
        throw "You are trying to send GET request through already used transport";
        return false;
    }

    this._inProgress = true;

    if (this.debug) {
        this._toConsole("GET: " + url);
    }

    this.transport.open('get', url, true);
    this.transport.onreadystatechange = this.onStateChange.bind(this);
    this.callback = callback;
    this.transport.send('');
}

Transport_ajax.prototype.sendPost = function (url, params, callback) {
    if (this._inProgress) {
        throw "You are trying to send POST request through already used transport";
        return false;
    }

    this._inProgress = true;

    this.transport.open('post', url, true);
    this.transport.onreadystatechange = this.onStateChange.bind(this);
    this.callback = callback;

    var postBody = '';
    for (i in params) {
        postBody += encodeURIComponent(i) + '=' + encodeURIComponent(params[i]) + '&';
    }

    this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');

    if (this.debug) {
        this._toConsole("POST: " + url + "<br>" + postBody);
    }

    this.transport.send(postBody);
}

Transport_ajax.prototype.isBusy = function () {
    return this._inProgress;
}

Transport_ajax.prototype._toConsole = function (text) {
    var s = document.createElement('div');
    s.style.paddingBottom = "5px";
    s.style.borderBottom = "1px solid gray";
    s.innerHTML = text;
    this.console.appendChild(s);
}
