/**
 * @author Nicolas D
 */

Delegate = {
    create : function(o,f){
        return function(){ return f.apply(o,arguments); };
    }
};


/**
 * Classe qui g la communication avec le serveur
 *
 * METHODES
 * reset()
 * setAttribute(key, value)
 * setData(data)
 * addListener(listener)
 * sendAndLoad(url)
 */
XML = function(){				
    this._listener = [];	
    this.reset();
}

XML.prototype.reset = function(){
    this._params = null;
    this._method = "GET";
}

XML.prototype.setAttribute = function(/*String*/ key, /*String*/ value){		
    if(this._params == null){
        this._params = {};
    }		
    this._params[key] = escape(value);	
}

XML.prototype.setData = function(/*String*/ data){		
    this._params = data;
    this._method = "POST";
}

XML.prototype.addListener = function(/*Function*/ listener){
    this._listener.push(listener);
}

XML.prototype.sendAndLoad = function(/*String*/ url) {
    var conn = null;
    
    try {
        conn = new XMLHttpRequest();		
    }
    catch (error) {        
        try {
            conn = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (error) {
            try {
                conn = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (error) {}
        }
    }
    
    var ls = this._listener;
    
    conn.onreadystatechange = function() {
        try{        
            if (conn.readyState == 4 && conn.status == 200){             
                var i = ls.length;				
                while(--i > -1) ls[i](conn);			
            }
        }catch (error) {}
    }
    
    var params = "";
    
    if(this._params != null) {                	
        for(var i in this._params) {
            params += "&"+ i + "=" + this._params[i];
        }		
        
        if(this._method == "GET") {
            url += "?" + params.substring(1);
            this.reset();	
            params = null;
        }	
    }	
    
    conn.open(this._method, url);
    
    if(this._method == "POST") {
        conn.setRequestHeader("Content-Type","application/x-www-form-urlencoded");		
    }	
    
    conn.send(params);   
}	