//
//  Librairie by The Rubik's Man
//          © 2005-2006
//

var Ajax_request =  function() {
    this.request.apply(this, arguments);
}
Ajax_request.prototype = {
    
    request: function (url, options) {  
        this.getObject();
            
        if ( typeof(options) == 'undefined' ) options = new Array();
        if ( typeof(options) == 'object' ) {
            method = options['method'] || 'post';
            successEnd = options['onSuccess'] || this.debugSuccessMessage.bind(this);
            errorEnd = options['onError'] || this.defaultErrorMessage.bind(this);
            paramString = typeof(options['params']) == 'undefined' ? '' : (options['method'] == 'get' ? '?'+options['params'] : options['params']);
                        
            if ( typeof(options['async']) != 'undefined' && typeof(options['async']) != 'boolean' ) {
                alert('si la valeur de \'async\' est definie, elle doit etre de type booleen');
                return;
            }
            else async = typeof(options['async']) == 'undefined' || typeof(options['async']) != 'boolean'? true : options['async'];
                                    
            this.setProperties(method,successEnd,errorEnd,paramString,async);
        }
                                
        if ( typeof(url) != 'string' ) {
            alert('url de format invalide ( l\'url est obligatoire )');
            return;
        }
        else this.properties['url'] = url;
                            
        this.setReadyProcess();
        this.processRequest();
    },
    
    
    getObject: function() {
        if(window.XMLHttpRequest) this.Ajax_object = new XMLHttpRequest();
        else if (window.ActiveXObject) { 
            try {
                this.Ajax_object = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    this.Ajax_object = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e1) {
                    this.Ajax_object = null;
                }
            }
        }
        else  {
            alert("AJAX impossible sur votre navigateur");
        }
        this.properties = new Array();
    },
    
    setProperties: function() {
        this.properties = {
            method : arguments[0],
            successEnd : arguments[1],
            errorEnd : arguments[2],
            paramString : arguments[3],
            async : arguments[4]
        }
    },

    setReadyProcess: function() {
        this.Ajax_object.onreadystatechange = function() {
            if (this.Ajax_object.readyState == 4 ) {
                responseText = this.Ajax_object.responseText;
                responseXML = this.Ajax_object.responseXML;
                this.responseText = this.Ajax_object.responseText;
                this.responseXML = this.Ajax_object.responseXML;
                //if ( this.Ajax_object.status == 200 )
                this.properties['successEnd'](this.Ajax_object);
                //else this.properties['errorEnd'](this.Ajax_object);
            }
        }.bind(this);
    },
    
    defaultErrorMessage: function(xhr)  {
        alert('Error ' + xhr.status + ' -- ' + xhr.statusText);
    },
    
    debugSuccessMessage: function(xhr) {
        alert('Reponse texte\n\n'+xhr.responseText);
    },
    
    processRequest: function() {
        this.Ajax_object.open(
            this.properties['method'],
            this.properties['method'] == 'post' ?
                this.properties['url'] : this.properties['url']+this.properties['paramString'],
            this.properties['async']
        );
        if ( this.properties['method'] == 'post' )
            this.Ajax_object.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        this.Ajax_object.send(this.properties['method'] == 'post' ? this.properties['paramString'] : null);
    }
    
}
Function.prototype.bind = function(object) {
    var __method = this;
    return function() {
        return __method.apply(object, arguments);
    }
}

var responseText = new String();
var responseXML = new Object();

//
//  Librairie by The Rubik's Man
//          © 2005-2006
//
