/**
 * Ce fichier est la propri�t� de Private Outlet
 * Copyright (c) 2007 Private Outlet
 * @url   http://www.privateoutlet.com
 *
 * Pour plus d'informations : olivier@privateoutlet.com
 *
 */

/**
 * @name Class PrivateXHR
 * @abstract Class pour les echanges Ajax
 * @author   Nicolas Lev�e <nicolas@privateoutlet.com>
 * @version  v 1.0.0 30/11/2006
 */

function PrivateXHR () {
	
	//init des vars
	var _this = this;
	_this.param = "";
	_this.fonction;
	_this.param_fonction = "";
	_this.text = true;
	
	
	//connection Ajax selon navigateur
	try {  
    	var xhr = new ActiveXObject('Msxml2.XMLHTTP');   
    }catch (e){
		try {   
        	var xhr = new ActiveXObject('Microsoft.XMLHTTP');    
        }catch (e2){
			try {  
          		var xhr = new XMLHttpRequest();     
          	}catch (e3){
				var xhr = false;  
			}
		}
	}
	
	//verif du statut
	xhr.onreadystatechange  = function(){ 
		if(xhr.readyState  == 4){
			if(xhr.status  == 200 || xhr.status  == 500){
				 if (_this.text)
				 	_this.fonction(xhr.responseText,_this.param_fonction);
				 else
				 	_this.fonction(xhr.responseXML.documentElement,_this.param_fonction);
			}
		}
	}; 
	
	
	//modification du type de recuperation
	_this.outToText = function (statu){
		_this.text = statu;
	};
	
	
	//ajout de param pour envoi
	_this.addParam = function (param, value){
		if (_this.param != "")
			_this.param += "&";
		
		value = new String(value);
		//value = value.replace(/\\("|'|\\)/g, "$1");
		value = encodeURI(value);
		value = value.replace(/\&/g,"%26");
		_this.param += param+"="+value;
	};
	
	_this.addParamFunction = function (value){
		_this.param_fonction = value;
	};
		
	//envoi des donnée en Ajax
	_this.sendToPhp = function (fonction, tofile, type){
		_this.fonction = fonction;
		if (type == "POST"){
			xhr.open("POST", tofile, true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr.send(_this.param);
		}else if (type == "GET"){
			xhr.open("GET", tofile+"?"+_this.param, true); 
			xhr.send(null);
		} 
		
	
	};
}

