﻿function ComposeSOAPMessage(Method,Params)
{
    var SOAPMessage = '<?xml version="1.0" encoding="utf-8"?>';
    SOAPMessage += '<soap:Envelope ';
    SOAPMessage += 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
    SOAPMessage += 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ';
    SOAPMessage += 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
    SOAPMessage += '<soap:Body>'
    SOAPMessage += '<' + Method + ' xmlns="http://tempuri.org/">';
    for(x=0; x<Params.length; x++)
    {
        SOAPMessage += '<' + Params[x].split('|')[0] + '>' + Params[x].split('|')[1] + '</' + Params[x].split('|')[0] + '>';
    }
    SOAPMessage += '</' + Method + '></soap:Body></soap:Envelope>';
    return SOAPMessage;
}
 
function ToQuery(Params)
{
    var first = true;
    var res = '';
    for(x=0; x<Params.length; x++)
    {
        if(first)
        {
            first = false;
        }
        else
        {
            res += '&';
        }
        
        res += Params[x].split('|')[0] + '=' + Params[x].split('|')[1];
    }
    
    return res;
}
 
 var SOAPMessageTable = '<?xml version="1.0" encoding="utf-8"?>';
 SOAPMessageTable += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
 SOAPMessageTable += '  <soap:Body>'
 SOAPMessageTable += '    <GetDataTable xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>';

function CrearProxy()
{ 
    var obj; 
    if(window.XMLHttpRequest)
    {
        // no es IE 
        obj = new XMLHttpRequest(); 
    }
    else
    {
        // Es IE o no tiene el objeto 
        try
        { 
            obj = new ActiveXObject("Microsoft.XMLHTTP"); 
        }
        catch (e)
        { 
            alert('El navegador utilizado no está soportado');
        }
    } 
    return obj; 
} 

function connect(sURL, sProtocol, sMethod, sVars, fnDone, bAsynch)
{
	if (!xmlhttp) 
	{
        alert('No se pudo establacer conexión asíncrona con el servidor');
		return false;
	}
	
	bComplete = false;
	sProtocol = sProtocol.toUpperCase();
		
	if (bAsynch == null)
	{
	    bAsynch = true; //treat asynch as an optional argument
	}
	
	try
	{
		if (sProtocol == 'GET')
		{
		    sVars = ToQuery(sVars);
			xmlhttp.open('GET', sURL + '?' + sVars, (bAsynch == true));
		}
		if (sProtocol == 'POST')
		{
			xmlhttp.open('POST', sURL, (bAsynch == true));
			xmlhttp.setRequestHeader('Method', 'POST ' + sURL + ' HTTP/1.1');
			xmlhttp.setRequestHeader('Content-Type', 'text/xml');
		}
		if (sProtocol == 'SOAP')
		{
            sVars=ComposeSOAPMessage(sMethod,sVars);
			xmlhttp.open('POST', sURL, (bAsynch == true));
			xmlhttp.setRequestHeader('SOAPAction', 'http://tempuri.org/'+sMethod);
			xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		}

		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4 && !bComplete)
			{
				bComplete = true;
				fnDone(xmlhttp);
			}
		};
		
		xmlhttp.send(sVars);

		/**
		 * Firefox <= 2.0.0 doesn't fire onreadystatechange for synchronous requests.
		 * See http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/
		 */
		var isGecko = (document.addEventListener) ? true : false;
		try {
			if (!bAsynch && isGecko && xmlhttp.onreadystatechange == null) {
				bComplete = true;
				fnDone(xmlhttp);
				return true;
			}
		}
		catch (e) { return false; }
		/**
		 * End synchronous request patch
		 */
	}
	
	catch(z)
	{
	    alert('Error de conexión asíncrona.');
	    weke();
	    return false;
	}
	
	return true;
};

function connectParallel(sURL, sProtocol, sMethod, sVars, fnDone, bAsynch, proxy)
{
	if (!proxy) 
	{
        alert('No se pudo establacer conexión asíncrona con el servidor');
		return false;
	}
	
	bComplete = false;
	sProtocol = sProtocol.toUpperCase();
		
	if (bAsynch == null)
	{
	    bAsynch = true; //treat asynch as an optional argument
	}
	
	try
	{
		if (sProtocol == 'GET')
		{
		    sVars = ToQuery(sVars);
			proxy.open('GET', sURL + '?' + sVars, (bAsynch == true));
		}
		if (sProtocol == 'POST')
		{
			proxy.open('POST', sURL, (bAsynch == true));
			proxy.setRequestHeader('Method', 'POST ' + sURL + ' HTTP/1.1');
			proxy.setRequestHeader('Content-Type', 'text/xml');
		}
		if (sProtocol == 'SOAP')
		{
            sVars=ComposeSOAPMessage(sMethod,sVars);
			proxy.open('POST', sURL, (bAsynch == true));
			proxy.setRequestHeader('SOAPAction', 'http://tempuri.org/'+sMethod);
			proxy.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
		}

		proxy.onreadystatechange = function()
		{
			if (proxy.readyState == 4 && !bComplete)
			{
				bComplete = true;
				fnDone(proxy);
			}
		};
		
		proxy.send(sVars);

		/**
		 * Firefox <= 2.0.0 doesn't fire onreadystatechange for synchronous requests.
		 * See http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/
		 */
		var isGecko = (document.addEventListener) ? true : false;
		try {
			if (!bAsynch && isGecko && proxy.onreadystatechange == null) {
				bComplete = true;
				fnDone(proxy);
				return true;
			}
		}
		catch (e) { return false; }
		/**
		 * End synchronous request patch
		 */
	}
	
	catch(z)
	{
	    alert('Error de conexión asíncrona.');
	    return false;
	}
	
	return true;
};

var xmlhttp=CrearProxy();
var xmlhttp1=CrearProxy();
var xmlhttp2=CrearProxy();
var xmlhttp3=CrearProxy();
