var _SetaAjax = [];

function SetaAjax(url, async) {
    this.n = _SetaAjax.length;
    _SetaAjax[this.n] = this;
    this.data = {};
    this.url = url;
    this.async = typeof async == 'boolean' ? async : true;
    
    this._createRequestObject = function() {
        try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
        try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
        try { return new XMLHttpRequest(); } catch(e) {}
        return null;
    }

    this._http = this._createRequestObject();
    //this._http.ajax = this;
    
    this.HandleCallback = function(http) { 
        alert('No callback function defined!'); 
    }
    
    this.SendGET = function() {
        eval('this._http.onreadystatechange = function() {_SetaAjax['+this.n+']._handleResponse(); };');
        this._http.open('get', this.url+'?'+this._SerializeData(this.data), this.async);
        this._http.send(null);
    }
    
    this.SendPOST = function() {
        eval('this._http.onreadystatechange = function() {_SetaAjax['+this.n+']._handleResponse(); };');
        this._http.open('post', this.url, this.async);
        this._http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
        this._http.send(this._SerializeData(this.data));
    }

    this._handleResponse = function() {
        if (this._http.readyState == 4) {
            this.HandleCallback(this._http);
        }
    }
    
    this.SetCallback = function(Callback) {
        this.HandleCallback = Callback;
    }
    
    this.SetData = function(data) {
        this.data = data;
    }
    
    this._SerializeData = SetaSerializeData;
    
}

var SetaSerializeData = function(data,prefix) {
    var s = [];
    if (typeof data != 'object')
        return '';
    else {
        for (var n in data) {
            if (typeof data[n] != 'object') {
                if (typeof prefix == 'undefined')
                    s.push(n+'='+encodeURIComponent(data[n]));
                else
                    s.push(prefix+'['+n+']='+encodeURIComponent(data[n]));
            } else {
                if (typeof prefix == 'undefined')
                    s.push(SetaSerializeData(data[n], n));
                else
                    s.push(SetaSerializeData(data[n], prefix+'['+n+']'));
            }
        }
    }
    return s.join('&');
}
