function Ajax ( connection , method,  objXmlHTTP , sincrono )
{

	this.SAFECHARS = "áéíóúÁÉÍÓÚñÑ¿?'àèìòùÀÈÌÒÙçÇ·äëïöüÄËÏÖÜâêîôûÂÊÎÔÛºª¡!€+";
	this.HEX = "0123456789ABCDEF";



	this.Connection   = connection.toLowerCase();
	this.Method       = method.toUpperCase();
	this.oXmlHTTP     = objXmlHTTP;
	this.AddHostToUrl = true;


	if ( sincrono )
	{
		this.Sincrono   = true;
		this.ASincrono  = false;
	}else
	{
		this.Sincrono   = false;
		this.ASincrono  = true;
	}

	this.fnCallBack = "";
	this.fnWaiting = "";


	this.AJAX_TIMEOUT 		  = false;
	this.AJAX_TIMEOUT_DNS     = "";
	this.AJAX_TIMEOUT_OPEN    = "";
	this.AJAX_TIMEOUT_SEND    = "";
	this.AJAX_TIMEOUT_RECEIVE = "";

	this.WAITING_STATUS = 1;
	this.READY_STATUS 	= 4;
	this.OK_STATUS 	  	= 200;


	this.TipoContenido = "Texto";	// Texto o Binario


	Ajax.prototype.DefineCallBack = function ( fn )
	{
		this.fnCallBack = fn;
	}
	Ajax.prototype.DefineWaitingCallBack = function ( fn )
	{
		this.fnWaiting = fn;
	}

	Ajax.prototype.GetWaitingState = function ( )
	{
		return this.WAITING_STATUS ;
	}
	Ajax.prototype.GetReadyState = function ( )
	{
		return this.READY_STATUS ;
	}
	Ajax.prototype.GetStatusOK = function ( )
	{
		return this.OK_STATUS;
	}


	Ajax.prototype.DefineTimeOut = function ( dns, open, send, receive )
	{
		this.AJAX_TIMEOUT 		  = true;
		this.AJAX_TIMEOUT_DNS     = dns;
		this.AJAX_TIMEOUT_OPEN    = open;
		this.AJAX_TIMEOUT_SEND    = send;
		this.AJAX_TIMEOUT_RECEIVE = receive;
	}


	Ajax.prototype.ConnectToHost = function ( host , puerto , url )
	{

		this.fnWaiting ( );

		if ( this.AddHostToUrl )
		{
			url.Url = this.Connection + "//"+ host + ":" + puerto + url.Url;
		}

		if ( this.Sincrono )
		{
			var mXml = this.loadXMLDoc ( url , this.Method );
			return mXml;
		}else
		{
			this.loadXMLDoc ( url , this.Method );
		}

	}


	Ajax.prototype.loadXMLDoc = function ( url , metodo )
	{

		var output = "";
		var rc = true;


		try {

			if ( this.AJAX_TIMEOUT )
			{
				this.oXmlHTTP.setTimeouts( this.AJAX_TIMEOUT_DNS, this.AJAX_TIMEOUT_OPEN, this.AJAX_TIMEOUT_SEND, this.AJAX_TIMEOUT_RECEIVE );

			}



			if ( metodo == "GET" )
			{

				var remoteUrl = url.Url;
				if ( url.Datos != "" )
				{
					remoteUrl += "?" + this.URL_Encode ( url.Datos );
				}
				
				if ( this.ASincrono )
				{
					this.oXmlHTTP.open( metodo , remoteUrl ,true);

					this.oXmlHTTP.onreadystatechange = this.ControladorRetornoAjax;
				}else
				{
					this.oXmlHTTP.open( metodo , remoteUrl ,false);
				}


				this.oXmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=ISO-8859-1");
				this.oXmlHTTP.setRequestHeader("Pragma", "no-cache");


				this.oXmlHTTP.send( "" );


			}else if ( metodo == "POST" )
			{
				if ( this.ASincrono )
				{
					this.oXmlHTTP.open( metodo , url.Url ,true)
					this.oXmlHTTP.onreadystatechange = this.ControladorRetornoAjax;
				}else
				{
					this.oXmlHTTP.open( metodo , url.Url ,false)
				}

				this.oXmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.oXmlHTTP.setRequestHeader("Pragma", "no-cache");

				this.oXmlHTTP.send( this.URL_Encode ( url.Datos ) );

			}


		}catch ( e )
		{
			rc = false;
		}

		if ( this.Sincrono )
		{
			if ( rc )
			{
				output = this.ControladorRetornoAjax ( );
			}
			return output;
		}
	}

	Ajax.prototype.ControladorRetornoAjax = function ( )
	{


		var contenido = "";

		// var estado = objAjax.readyState;
		if ( objAjax.readyState == XConn.GetReadyState() )
		{

			// var status = objAjax.status;
			if ( objAjax.status == XConn.GetStatusOK() )
			{
				contenido = objAjax.responseText;

			}

			if ( XConn.Sincrono )
			{
				return contenido;
			}else
			{
				XConn.fnCallBack ( contenido );
			}


		}
	}
	Ajax.prototype.URL_Encode = function ( plaintext )
	{
		var encoded = "";
		var ch = "";
		var charCode = "";

		for(var i=0; i<plaintext.length; i++) 
		{
			ch = plaintext.charAt(i);
			if( this.SAFECHARS.indexOf(ch) == -1) 
			{
				encoded += ch;
			}else 
			{
				charCode = ch.charCodeAt(0);
				if(charCode > 255) 
				{
					switch(ch) 
					{
						case "€":
							encoded += "E";
							break;
						default: 
							break;
					}
				}else 
				{
					encoded += "%";
					encoded += this.HEX.charAt((charCode >> 4) & 0xF);
					encoded += this.HEX.charAt(charCode & 0xF);
				}
			}
		}

		return encoded;

	}




};


