function newAjaxObject(){
    var objAjax;

    try{
        objAjax     = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch(e){
        try{
            objAjax = new ActiveXObject('Msxml2.XMLHTTP');
            objAjax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        }
        catch(ex){
            try{
                objAjax = new XMLHttpRequest();
            }
            catch(exc){
                objAjax = null;
            }
        }
    }
    return objAjax;
}

function RunAjaxRequest(stMethod, stUrl, stParameters, objFunction, idElement, stTitle){
    objXmlRequest       = newAjaxObject()
    process             = objFunction;

    objXmlRequest.onreadystatechange = function(){
        try{
            if(!idElement){
                /* Sem par?metros de Retorno */
                process(objXmlRequest);

            }else{
                /* Com par?metros de Retorno */
                process(objXmlRequest, idElement, stTitle);
            }
        }
        catch(ex){
            alert('Um erro ocorreu ao tentar carregar os dados. Tente novamente.\nDescricao: ' + ex.message + '\nArquivo: \t' + ex.fileName + '\nStack: \t' + ex.stack + '\nLInha: \t' + ex.lineNumber )
        }
    }

    stMethod = stMethod.toUpperCase();
    objXmlRequest.open(stMethod, stUrl, true);

    if(stMethod == 'POST'){
        objXmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        objXmlRequest.setRequestHeader('Method', 'POST ' + stUrl + 'HTTP/1.1');
        objXmlRequest.setRequestHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
        objXmlRequest.setRequestHeader('Cache-Control', 'post-check=0, pre-check=0');
        objXmlRequest.setRequestHeader('Pragma', 'no-cache');
        /*
        objXmlRequest.setRequestHeader('charset','ISO-8859-1');
        objXmlRequest.setRequestHeader('encoding','ISO-8859-1');
        */
    }
    objXmlRequest.send(stParameters);
    return objXmlRequest;
}

function buscaCep(nmCep){
    if(nmCep.length == 8){
        RunAjaxRequest('POST', '../xml/logradouro.php'    , 'nmCep='+nmCep+'', retornaXmlCep);
    }else{
        limpaCampoCep();
    }
}

function limpaCampoCep(){
    var obj_Cep                 = document.getElementById("id_Cep");
    var obj_stLogradouro        = document.getElementById("id_stLogradouro");
    var obj_Endereco            = document.getElementById("id_stEndereco");
    var obj_Bairro              = document.getElementById("id_Bairro");
    var obj_Cidade              = document.getElementById("id_Cidade");
    var obj_stUf                = document.getElementById("id_stUf");

    obj_Endereco.value        = '';
    obj_Bairro.value        = '';
    obj_Cidade.value        = '';
//    obj_Cep.value            = '';

    obj_stLogradouro.options[0].selected    = true;
    obj_stUf.options[0].selected            = true;
}

function retornaXmlCep(objXmlRequest){
    var obj_Cep     = document.getElementById("id_Cep");

    if(objXmlRequest.readyState == 4){
        if(objXmlRequest.status == 200){

            var stLogradouro, obj_stLogradouro;
            var stEndereco, obj_Endereco;
            var stBairro, obj_Bairro;
            var stCidade, obj_Cidade;
            var stUf, obj_stUf;


            nmTotalResults         = objXmlRequest.responseXML.getElementsByTagName("logradouro").length
            objXml                 = objXmlRequest.responseXML

            obj_stLogradouro     = document.getElementById("id_stLogradouro");
            obj_Endereco        = document.getElementById("id_stEndereco");
            obj_Bairro            = document.getElementById("id_Bairro");
            obj_Cidade            = document.getElementById("id_Cidade");
            obj_stUf              = document.getElementById("id_stUf");

            if(nmTotalResults > 0){
                for(i = 0 ; i < nmTotalResults ; i++){

                    stLogradouro = (objXml.getElementsByTagName("logradouro")[i].getElementsByTagName("stLogradouro")[0].firstChild.nodeValue)
                    stEndereco   = (objXml.getElementsByTagName("logradouro")[i].getElementsByTagName("stEndereco")[0].firstChild.nodeValue)
                    stBairro     = (objXml.getElementsByTagName("logradouro")[i].getElementsByTagName("stBairro")[0].firstChild.nodeValue)
                    stCidade     = (objXml.getElementsByTagName("logradouro")[i].getElementsByTagName("stCidade")[0].firstChild.nodeValue)
                    stUf         = (objXml.getElementsByTagName("logradouro")[i].getElementsByTagName("stUf")[0].firstChild.nodeValue)

                    for(nmInc = 0; nmInc < obj_stLogradouro.length; nmInc ++){
                        if(Trim(obj_stLogradouro[nmInc].value.toUpperCase()) == Trim(stLogradouro.toUpperCase())) obj_stLogradouro[nmInc].selected = true
                    }

                    for(nmInc = 0; nmInc < obj_stUf.length; nmInc ++){
                        if(Trim(obj_stUf[nmInc].value.toUpperCase()) == Trim(stUf.toUpperCase())) obj_stUf[nmInc].selected = true
                    }

                    obj_Endereco.value    = stEndereco;
                    obj_Bairro.value      = stBairro;
                    obj_Cidade.value      = stCidade;
                }
            }else{
                limpaCampoCep();
            }
        }
    }
}
