/**
  * Return a XmlHttpRequest
  */
function getXMLHttpRequest() {
    if (typeof(XMLHttpRequest) != 'undefined') {
        return new XMLHttpRequest();
    } else if (typeof(ActiveXObject) != 'undefined') {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
}
var NORMAL_STATE = 4;
var STATUS_OK = 200;

/**
 * Loads an url assynchronously.
 * The callback receives 2 parameters: the response text and DOM document
 */
function loadAsync(body, url, callback) {
	return loadAjax(true, body, url, callback);
}

/**
 * Loads an url synchronously.
 * The callback receives 2 parameters: the response text and DOM document
 */
function loadSync(body, url, callback) {
	return loadAjax(false, body, url, callback);
}

/**
 * Loads an url using the XmlHttpRequest.
 * The callback receives 2 parameters: the response text and DOM document
 */
function loadAjax(async, body, url, callback) {
    var xmlHttpRequest = getXMLHttpRequest();
    //VERIFICA SE OBJETO XMLHTTPREQUEST FOI CRIADO
	if (!xmlHttpRequest) {
        alert("Your browser does not support this operation.\nPlease, use Firefox, Internet Explorer or a W3C compilant browser.");
        return;
    }
    var isPost = body != null;
    xmlHttpRequest.open(isPost ? "POST" : "GET", url, true);
    if (isPost) {
        xmlHttpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=ISO-8859-1');
    }
    
    var cb = function() {
        if (xmlHttpRequest.readyState == NORMAL_STATE) {
            if (xmlHttpRequest.status == STATUS_OK) {
                //REPONSVEL PELO RETORNO DA INFORMAÇÃO PAGA A FUCNTION
				callback(xmlHttpRequest.responseText, xmlHttpRequest.responseXML);
            } else {
                alert("An error has occurred while processing your request");
            }
        }
    };
    
    if (async) {
    	xmlHttpRequest.onreadystatechange = cb;
    }
    xmlHttpRequest.send(body);
    if (!async) {
    	cb();
    }
}