﻿function $(id) { return document.getElementById(id); }

if(typeof(XMLHttpRequest) == 'undefined') XMLHttpRequest = function()
{
    try{ return new ActiveXObject("MSXML3.XMLHTTP") }catch(e){}
    try{ return new ActiveXObject("MSXML2.XMLHTTP.3.0") }catch(e){}
    try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
    try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
    throw new Error("No se puede inicializar el objeto XMLHttpRequest");
};

function doRequest(url, async, callback, inText, params, post)
{	
    async = async ? true : false;
    if(!params) params = null;
    var method = post ? "POST" : "GET";
    
    var xmlHttp = new XMLHttpRequest();
      	
    xmlHttp.open(method,url,async);
	
    if(post)
    {
        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
        xmlHttp.setRequestHeader('Content-length', params ? params.length : 0);
        xmlHttp.setRequestHeader('Connection', 'close');
    }
	
    if(async) xmlHttp.onreadystatechange = f_rsc;
	
    xmlHttp.send(params.join('&'));
	
    if(!async)
    {
        return f_rsc();
    }
	
    function f_rsc()
    {
        if(xmlHttp.readyState == 4)
        {
            if (xmlHttp.status != 200)
            {
                throw new Error("Error al obtener el documento '" + url + "': " + xmlHttp.statusText);
            }
            if(callback)
                callback(inText ? xmlHttp.responseText : xmlHttp.responseXML);
            else
                return inText ? xmlHttp.responseText : xmlHttp.responseXML;	
        }
    }
}
