/// ==========================================================================
/// Clase Ajax
/// ==========================================================================
function Ajax()
{
  // Variables privadas
  var isExplorer = (typeof window.ActiveXObject != 'undefined');
  var isMozilla  = (typeof document.implementation != "undefined") && (typeof document.implementation.createDocument != "undefined");
  var xmlhttp    = false;
  
  ///--------------------------------------------------------------------------
  /// Crea el objeto XmlHttp
  ///--------------------------------------------------------------------------
  this.createXmlHttp = function()
  {

    // Soporte nativo...
    try { xmlhttp = new XMLHttpRequest(); } 
    // Soporte ActiveX
    catch ( e ) 
    { 

      xmlhttp = false;

      if ( window.ActiveXObject ) 
      {
 	      try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } 
 	      catch ( e ) 
 	      {
  	      try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
  	      catch ( e ) { xmlhttp = false; }
  	    }
      }
    }
  }

  ///--------------------------------------------------------------------------
  /// Solicita una respuesta a la pagina indicada, con los parametros indicados,
  /// e informando de la funcion a la que debe llamar cuando el objeto XmlHttp 
  /// haya terminado de cargar los datos solicitados
  ///--------------------------------------------------------------------------
  this.request = function( url, params, callbackFunction )
  {
  
    // Pagina que devuelve la informacion XML (En este caso, la misma pagina)
    var strUrlPage = url;
    // Parametros que admite la pagina
    var strUrlParams = "xmlhttp=true";

		if ( params != null && params != "" )
      strUrlParams += "&" + params;

    // Crea el objeto XmlHttp
    this.createXmlHttp();
  
    // Prepara el objeto
    if ( xmlhttp )
    {    
      // De manera sincronica si no funcion de callback
      if ( callbackFunction == null || callbackFunction == "" )
      {
        // Solicita via post (siempre al pasar datos, por post, al obtener, por get)  
        xmlhttp.open("POST",strUrlPage,false);
      }
      // De manera asincronica si si la hay
      else
      {
        // Solicita via post (siempre al pasar datos, por post, al obtener, por get)  
        xmlhttp.open("POST",strUrlPage,true);
        // funcion a la que llamara al cambiar de estado
		    xmlhttp.onreadystatechange = callbackFunction;
      }
      xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		  // Solicita la informacion
			xmlhttp.send(strUrlParams);
    }
  }

  ///--------------------------------------------------------------------------
  /// Informa de si el objeto ya ha recibido todos los datos de manera segura
  ///--------------------------------------------------------------------------
  this.isReady = function()
  {

    return (xmlhttp.readyState == 4 && xmlhttp.status == 200);
  }
  
  ///--------------------------------------------------------------------------
  /// Devuelve una cadena con los datos solicitados
  ///--------------------------------------------------------------------------
  this.getResultXml = function() 
  {
  
    if ( this.isReady )
      return xmlhttp.responseText.replace(/\uFEFF/g,"");
      
    return "";
  }
}
