function ajax1(url, containerid){
    page_request=false;
    if(window.XMLHttpRequest)page_request=new XMLHttpRequest();
    else if(window.ActiveXObject){
        try{
            page_request=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e){
            try{
                page_request=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){}
        }
    }
    else return false
    page_request.onreadystatechange=function(){
        loadpage(page_request,containerid);
    }
    page_request.open('GET',url,true);
    page_request.send(null);
}
function loadpage(page_request,containerid){
    if(page_request.readyState==4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))document.getElementById(containerid).innerHTML=page_request.responseText;
}

// -- combobox

function ajaxComboBox(url, sub, comboBox){
    //dest_combo = comboBox;
    //indice = document.getElementById('estados').selectedIndex;
    //sigla = document.getElementById('estados').options[indice].getAttribute('value');
    url = url + '?cat=' + sub;
    if (document.getElementById) { //Verifica se o Browser suporta DHTML.
        if (window.XMLHttpRequest) {
            HttpReq = new XMLHttpRequest();
            HttpReq.onreadystatechange = function() {
                XMLHttpRequestChange(comboBox);
            }
            HttpReq.open("GET", url, true);
            HttpReq.send(null);
        } else if (window.ActiveXObject) {
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            if (HttpReq) {
                HttpReq.onreadystatechange = function() {
                    XMLHttpRequestChange(comboBox);
                }
                HttpReq.open("GET", url, true);
                HttpReq.send();
            }
        }
    }
}

function XMLHttpRequestChange(combo) {
    if (HttpReq.readyState == 4 && HttpReq.status == 200){
        result = HttpReq.responseXML;
        sub = result.getElementById(combo);
        document.getElementById(combo).innerHTML = "";
        for (i = 0; i < sub.length; i++) {
            new_opcao = create_opcao(sub[i]);
            document.getElementById(combo).appendChild(new_opcao);
        }
    }
}

function create_opcao(cidade) {
    new_opcao = document.createElement("option");
    texto = document.createTextNode(cidade.childNodes[0].data);
    new_opcao.setAttribute("value",cidade.getAttribute("id"));
    new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
    return new_opcao; // Retorna a nova OPTION.
}

// -- combobox

function ajax2(url,idDiv,callback){
    loading(true);
    req = null;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {
            processReqChange(idDiv,callback);
        }
        req.open('GET',url,true);
        req.send(null);
    // Procura por uma versão ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject('Microsoft.XMLHTTP');
        if (req) {
            req.onreadystatechange = function() {
                processReqChange(idDiv,callback);
            }
            req.open('GET',url,true);
            req.send();
        }
    }
}

function processReqChange(idDiv,callback){
    // apenas quando o estado for "completado"
    if (req.readyState == 4){
        // apenas se o servidor retornar "OK"
        if (req.status ==200){
            // procura pela div id="pagina" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById(idDiv).innerHTML = req.responseText;
            if(callback) eval(callback);
            loading(false);
        }else{
            alert("Houve um problema ao obter os dados:n" + req.statusText);
        }
    }
}

function loading(acao) {
    if(acao){
            document.getElementById('LOADING').innerHTML='aguarde...';
    }else{
        document.getElementById('LOADING').innerHTML=' ';
    }
}



function ajaxSemLoading(url,idDiv,callback){ 
    req = null;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() {
            processReqChangeSemLoading(idDiv,callback);
        }
        req.open('GET',url,true);
        req.send(null);
    // Procura por uma versão ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject('Microsoft.XMLHTTP');
        if (req) {
            req.onreadystatechange = function() {
                processReqChangeSemLoading(idDiv,callback);
            }
            req.open('GET',url,true);
            req.send();
        }
    }
}

function processReqChangeSemLoading(idDiv,callback){
    // apenas quando o estado for "completado"
    if (req.readyState == 4){
        // apenas se o servidor retornar "OK"
        if (req.status ==200){
            // procura pela div id="pagina" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById(idDiv).innerHTML = req.responseText;
            if(callback) eval(callback);
        }else{
            alert("Houve um problema ao obter os dados:n" + req.statusText);
        }
    }
}






/**
 * Inicializando o HTTPRequest
 *
 * Verifica o tipo e a versão do objeto nativo
 */
function inicializaHTTPRequest() {
    req = null;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) return new XMLHttpRequest();
    // Procura por uma versão ActiveX (IE)
    else if (window.ActiveXObject) {
        try{
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }
    }
}