//AJAX ////////////////////////////////////////////////////

function ajaxGet(url,elemento_retorno,exibe_carregando, tipo_retorno){
/******
* ajaxGet - Coloca o retorno de uma url em um elemento qualquer
* Use a vontade mas coloque meu nome nos créditos. Dúvidas, me mande um email.
* Versão: 1.1 - 10/04/2006
* Autor: Micox - micoxjcg@yahoo.com.br
* Parametros:
* url: string; elemento_retorno: object||string; exibe_carregando:boolean
*  - Se elemento_retorno for um elemento html (inclusive inputs e selects),
*    exibe o retorno no innerHTML / value / options do elemento
*  - Se elemento_retorno for o nome de uma variavel
*    (o nome da variável deve ser declarado por string, pois será feito um eval)
*    a função irá atribuir o retorno à variável ao receber a url.
*******/
    var ajax1 = pegaAjax();
    if(ajax1){
        url = antiCacheRand(url)
        ajax1.onreadystatechange = ajaxOnReady
        ajax1.open("GET", url ,true);
        ajax1.setRequestHeader("Cache-Control", "no-cache");
        ajax1.setRequestHeader("Pragma", "no-cache");
        if(exibe_carregando){ put('Carregando...')    }
        ajax1.send(null)
        return true;
    }else{
        return false;
    }
    function ajaxOnReady(){
        if (ajax1.readyState==4){
            if(ajax1.status == 200){
                var texto=ajax1.responseText;
                if(texto.indexOf(" ")<0) texto=texto.replace(/\+/g," ");
                //texto=unescape(texto); //descomente esta linha se tiver usado o urlencode no php ou asp
                put(texto);
            }else{
                if(exibe_carregando){put('Falha no Carregamento.' + httpStatus(ajax1.status));}
            }
            ajax1 = null
        }else if(exibe_carregando){//para mudar o status de cada carregando
                put('Carregando...' )
        }
    }
    function put(valor){ //coloca o valor na variavel/elemento de retorno
        if((typeof(elemento_retorno)).toLowerCase()=="string"){ //se for o nome da string
            if(valor!='Falha no Carregamento.'){
                eval(elemento_retorno + '= unescape("' + escape(valor) + '")')
            }
        }else if(elemento_retorno.tagName.toLowerCase()=="input"){
            valor = escape(valor).replace(/\%0D\%0A/g,"")
            elemento_retorno.value = unescape(valor);
        }else if(elemento_retorno.tagName.toLowerCase()=="select"){        
            //select_innerHTML(elemento_retorno,valor)
        }else if(elemento_retorno.tagName){
			trataResposta(valor, tipo_retorno, elemento_retorno);
        }    
    }
    function pegaAjax(){ //instancia um novo xmlhttprequest
        //baseado na getXMLHttpObj que possui muitas cópias na net e eu nao sei quem é o autor original
        if(typeof(XMLHttpRequest)!='undefined'){return new XMLHttpRequest();}
        var axO=['Microsoft.XMLHTTP','Msxml2.XMLHTTP','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];
        for(var i=0;i<axO.length;i++){ try{ return new ActiveXObject(axO[i]);}catch(e){} }
        return null;
    }
    function httpStatus(stat){ //retorna o texto do erro http
        switch(stat){
            case 400: return "400: Solicita&ccedil;&atilde;o incompreensível"; break;
            case 403: case 404: return "404: N&atilde;o foi encontrada a URL solicitada"; break;
            case 405: return "405: O servidor n&atilde;o suporta o m&eacute;todo solicitado"; break;
            case 500: return "500: Erro desconhecido de natureza do servidor"; break;
            case 503: return "503: Capacidade m&aacute;xima do servidor alcançada"; break;
            default: return "Erro " + stat + ". Mais informa&ccedil;&otilde;es em http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"; break;
        }
    }
    function antiCacheRand(aurl){
        var dt = new Date();
        if(aurl.indexOf("?")>=0){// já tem parametros
            return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
        }else{ return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());}
    }

	function trataResposta(resposta, tipo, elemento_retorno){
		if (tipo == "alert"){
			displayStaticMessage('<div class=tituloMensagem>ATEN&Ccedil;&Atilde;O!</div><div class=mensagem>' + resposta + '</div><p class=\'alignCenter\'><a href=\'#\' onclick=\'closeMessage();return false\'><img src=imagens/bt_ok.gif></a>',false,150,120);
		}
		if (tipo == "select"){
			elemento_retorno.innerHTML = resposta;
		}
	}
}

function cadastroRapido(){
    ajaxGet("includes/cadastroRapido.asp?email=" + document.getElementById('email').value,document.getElementById("respostaDIV"),true, "alert")
}

function listaCidades(){
    ajaxGet("includes/listaCidade.asp?estado=" + document.getElementById('filtroEstado').value,document.getElementById("cidadeDIV"),true, "select")
}

function listaRoteiros(){
    ajaxGet("includes/listaRoteiro.asp?nacionalidade=" + document.getElementById('buscaNacionalidade').value + "&tipo=" + document.getElementById('tipo').value,document.getElementById("roteiroDIV"),true, "select")
}

////////////////////////////////////////////////////