var cnxs = new Conexiones();

function Conexiones()
{
   this.metodo = [];
   this.url = [];
   this.parametrosPost = [];
   this.asincronico = [];
   this.conexion = [];
   this.callback = [];
   this.parametrosHeader = [];
   this.respuesta = [];
   this.errorNro = [];
   this.errorMsj = [];

   this.crearConexion = function(p_nombre, p_metodo, p_url, p_parametrosPost, p_asincronico)
   {
      if ((p_nombre != undefined) && (p_nombre != ''))
      {
         if ((p_metodo == undefined) || ((p_metodo != 'GET') && (p_metodo != 'POST') && (p_metodo != 'HEAD')))
         {
            this.errorNro[p_nombre] = 1;
            this.errorMsj[p_nombre] = 'Método HTTP no válido.';
         }
         else if ((p_url == undefined) || (p_url == ''))
         {
            this.errorNro[p_nombre] = 2;
            this.errorMsj[p_nombre] = 'URL no especificado.';
         }
         else
         {
            var cnxTemp = this.instanciarXMLHttpRequest();
            if (cnxTemp)
            {
               this.metodo[p_nombre] = p_metodo;
               this.url[p_nombre] = p_url;
               this.parametrosPost[p_nombre] = p_parametrosPost || undefined;
               this.asincronico[p_nombre] = p_asincronico || false;
               this.conexion[p_nombre] = cnxTemp || undefined;
               this.callback[p_nombre] = new Function('', 'if (cnxs.conexion["' + p_nombre + '"].readyState == 4)' +
                                                          '{' +
                                                          '   if (cnxs.conexion["' + p_nombre + '"].status == 200)' +
                                                          '   {' +
                                                          '      cnxs.respuesta["' + p_nombre + '"] = cnxs.conexion["' + p_nombre + '"].responseText;' +
                                                          '   }' +
                                                          '   else' +
                                                          '   {' +
                                                          '      cnxs.errorNro["' + p_nombre + '"] = cnxs.conexion["' + p_nombre + '"].status;' +
                                                          '      cnxs.errorNro["' + p_nombre + '"] = cnxs.conexion["' + p_nombre + '"].statusText;' +
                                                          '   }' +
                                                          '}');
               this.errorNro[p_nombre] = 0;
               this.errorMsj[p_nombre] = undefined;
               return true;
            }
            else
            {
               this.errorNro[p_nombre] = 3;
               this.errorMsj[p_nombre] = 'No se pudo instanciar XMLHttpRequest.';
            }
         }
      }
      return false;
   }

   this.consultar = function(p_nombre)
   {
      if (this.conexion[p_nombre])
      {
         this.errorNro[p_nombre] = 0;
         this.errorMsj[p_nombre] = undefined;
         this.conexion[p_nombre].onreadystatechange = this.callback[p_nombre];
         this.conexion[p_nombre].open(this.metodo[p_nombre], this.url[p_nombre], this.asincronico[p_nombre]);
         if (this.parametrosHeader[p_nombre] != undefined)
         {
            for (var i = 0; i < this.parametrosHeader[p_nombre].length; i++)
            {
               this.conexion[p_nombre].setRequestHeader(this.parametrosHeader[p_nombre][i][0], this.parametrosHeader[p_nombre][i][1]);
            }
         }
         var parametrosPostEfectivos = null;
         if (this.metodo[p_nombre] == 'POST')
         {
            this.conexion[p_nombre].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            $parametrosPostEfectivos = ((this.parametrosPost[p_nombre] != undefined)? this.parametrosPost[p_nombre]: null);
         }
         this.conexion[p_nombre].send((this.parametrosPost[p_nombre] != undefined)? this.parametrosPost[p_nombre]: null);
         if (! this.asincronico[p_nombre])
         {
            this.callback[p_nombre]();
         }
         return true;
      }
      else
      {
         this.errorNro[p_nombre] = 4;
         this.errorMsj[p_nombre] = 'Error al inicial la consulta HTTP.';
         return false;
      }
   };

   this.getRespuesta = function(p_nombre)
   {
      return (this.respuesta[p_nombre] != undefined? this.respuesta[p_nombre]: undefined);
   };

   this.getErrorNro = function(p_nombre)
   {
      return (this.errorNro[p_nombre] != undefined? this.errorNro[p_nombre]: undefined);
   };

   this.getErrorMsj = function(p_nombre)
   {
      return (this.errorMsj[p_nombre] != undefined? this.errorMsj[p_nombre]: undefined);
   };

   this.huboError = function(p_nombre)
   {
      return ((this.errorNro[p_nombre] != undefined) && (this.errorNro[p_nombre] != 0));
   };

   this.instanciarXMLHttpRequest = function()
   {
      if (window.XMLHttpRequest)
      {
         return new XMLHttpRequest();
      }
      else if (window.ActiveXObject)
      {
         var msxmls = new Array('Msxml2.XMLHTTP.6.0',
                                'Msxml2.XMLHTTP.5.0',
                                'Msxml2.XMLHTTP.4.0',
                                'Msxml2.XMLHTTP.3.0',
                                'Msxml2.XMLHTTP',
                                'Microsoft.XMLHTTP');
         for (var i = 0; i < msxmls.length; i++)
         {
            try
            {
               return new ActiveXObject(msxmls[i]);
            }
            catch (e)
            {
            }
         }
      }
      return false;
   };

}
