/*
 *  Ajax class by Laurent Doudon
 *  -----------------------------
 *  function exemple(){
 *  var ajx = new Ajax(file.php, 'GET','Text'); or
 *  var ajx = new Ajax(file.php, 'POST','XML');
 *  
 *  ajx.getResponse = function(result){
 *		document.getElementById('descriptionarea').innerHTML= result;
 *	}  	  
 *	ajx.run();  
 *  }
 *  see exemple.html for details
 */
function Ajax(url, method, mode) 
{
    this.url      = url;
    this.method   = (method) ? method : 'GET';
    this.params   = (method='GET') ? null : params;
    this.mode     = (mode) ? mode : 'Text';
	this.response = null;
    if(!this.params) params = null;
    if( this.mode != 'Text' && this.mode != 'XML') this.mode = 'Text';
};

Ajax.prototype.run = function(){	
		
		if( this.url == undefined || this.url == '') return ;
		
        // XHR = XMLHttpRequest abreviation
        this.XHR_object = null;
        
		// Gecko, KHTML/safari, opera
        if( window.XMLHttpRequest ){ 
            this.XHR_object = new XMLHttpRequest();
            if (this.XHR_object.overrideMimeType) this.XHR_object.overrideMimeType('text/xml');	
        // IE 
        } else if( window.ActiveXObject){
            try{
                this.XHR_object = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e){
                try{
                    this.XHR_object = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) { return; }
            }
        }
        if(this.XHR_object != null && this.XHR_object != undefined ){
            var obj = this;
            this.XHR_object.onreadystatechange = function(){ obj.handle_response.call(obj);};
            this.XHR_object.open(this.method,this.url, true);
			if	(this.method =='POST'){
				try {
					this.XHR_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				} catch (e) { }
			}	
            this.XHR_object.send(this.params);
        }     
};


Ajax.prototype.getResponse = function(val){
	
	if (this.XHR_object != null && val != null) return val;
};
           
Ajax.prototype.handle_response = function(){
	
        if( this.XHR_object.readyState == 4 ){
            if( this.XHR_object.status == 200 ){
				
                var resp = ( this.mode == 'Text' ) ? this.XHR_object.responseText : this.XHR_object.responseXML;
    		
                if( resp != null ){
					this.response = resp;
					this.getResponse(this.response);
                } else {
                    alert("error="+resp);
            	}
            } else {
                this.getError();
            }
        }
}; 
	      
Ajax.prototype.getError= function(){
        alert(this.XHR_object.status + ' : ' + this.XHR_object.statusText);
};          
