/**
  * 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();
    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) {
                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();
    }
}
function url_decode(str) {
    var n, strCode, strDecode = "";

    for (n = 0; n < str.length; n++) {
        if (str.charAt(n) == "%") {
            strCode = str.charAt(n + 1) + str.charAt(n + 2);
            strDecode += String.fromCharCode(parseInt(strCode, 16));
            n += 2;
        } else {
            strDecode += str.charAt(n);
        }
    }

    return strDecode;
} 