﻿function HttpConnector()
{
    var oThis=this;
    var connector = null; //socket
    var oTimeOut; // timer para timeout de la petición
    var timeoutTime=3000; //tiempo de espera para timeout por defecto
    var userObj;//datos de usuario
    var errorLink="Error! No se pudo adquirir conector de red del navegador";
    var hasTimeOut = true;
    var uri="";
    var isBinaryRequest = false;
    var isProcessing=false;
    
    this.init = function (timeout, binaryRequest)
    {
        if (timeout==null) 
        {
            hasTimeOut = false;
        }
        else
        {
            timeoutTime = timeout;
        }
        isBinaryRequest = binaryRequest == null || binaryRequest==false ? false : true;
        connector   = null;
        if(window.XMLHttpRequest) 
        {
            try
            { 
                connector = new XMLHttpRequest();
            }
            catch(ex) 
            { 
                alert(errorLink);
            }
        }
        else 
            if(window.ActiveXObject)
            {
                var ids = ["Msxml2.XMLHTTP.7.0", "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<ids.length; i++) 
                {
                    try
                    { 
                        connector = new ActiveXObject(ids[i]);
                        break; 
                    }
                    catch(ex) 
                    { 
                        //Se continua probando
                    }
                }
             }
        if( connector == null ) alert(errorLink);
    }
    
    //asigna datos de usuario
    this.setTag = function (obj)
    {
        userObj = obj;
    }
    
    //retorna datos de usuario
    this.getTag = function ()
    {
        return userObj;
    }
    
    this.getURL=function()
    {
        return uri;
    }
    
    this.connect=function (url, cbk, cbkError)
    {
        if (connector!=null)
        {
            try
            {
                uri = url;
                this.sendEvent=cbk;
                this.sendEventError=cbkError;
                connector.onreadystatechange = connectorCallback;
                connector.open("GET", url, true);
                if (hasTimeOut) oTimeOut=setTimeout(this.abort, timeoutTime);
                isProcessing = true;
                connector.send("");
            }
            catch(e)
            {
                alert("Error al ejecutar conexión: " + e);
            }
        }
    }
    
    this.abort=function()
    {
        if (hasTimeOut) clearTimeout(oTimeOut);
        if (isProcessing)
        {
            try
            {
                connector.abort();
            }
            catch(e){}
            isProcessing = false;
        }
    }
    
    function connectorCallback()
    {
        if(connector.readyState == 4 )
        {
            isProcessing = false;
            if (hasTimeOut) clearTimeout(oTimeOut);
            if(connector.status == 200)
            {
                oThis.sendEvent(!isBinaryRequest ? connector.responseText : null, oThis);
	        }
	        else
	        {
	            oThis.sendEventError();
	        }
        }
    }
}

HttpConnector.prototype.sendEvent = function (arg) {};
HttpConnector.prototype.sendEventError = function () {};

//****************** peticion AJAX
function AJAXTileRequest(url, callback, obj) 
{
    var req = init();
        
    function init() {
      if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    
    function processRequest () {
      if (req.readyState == 4) {
        if (req.status == 200) {
          if (callback) callback(obj);
        }
      }
    }

    this.doGet = function() {
      req.open("GET", url, true);
      req.onreadystatechange = processRequest;
      req.send(null);
    }
    
    this.doPost = function(body) {
      req.open("POST", url, true);
      req.onreadystatechange = processRequest;
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      req.send(body);
    }
}