/* Ajax Funções */
   /* Autor: Junior Gobira - juniorgobira@gmail.com */
   /* Acesse: www.jnsa.com.br */

function Dados(valor) {
             try {
             ajax = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e) {
             try {
             ajax = new ActiveXObject("Msxml2.XMLHTTP");
             }
             catch(ex) {
             try {
             ajax = new XMLHttpRequest();
             }
             catch(exc) {
             alert("Esse browser não aceita scripts AJAX");
             ajax = null;
             }
             }
    }

    if(ajax) {
             document.inscricao.cidade.options.length = 1;
             idOpcao = document.getElementById("opcoes");
             ajax.open("POST", "var.cidades.php", true);
             ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
             ajax.onreadystatechange = function() {
             if(ajax.readyState == 1) {
                  idOpcao.innerHTML = "Carregando...";
             }
             if(ajax.readyState == 4 ) {
             if(ajax.responseXML) {
             processXML(ajax.responseXML);
             }
             else {
             idOpcao.innerHTML = "Primeiro selecione o estado";
             }
             }
             }
             var params = "estado="+valor;
             ajax.send(params);
    }
}

function processXML(obj){
var dataArray = obj.getElementsByTagName("cidade");
if(dataArray.length > 0) {
for(var i = 0 ; i < dataArray.length ; i++) {
var item = dataArray[i];
var codigo = item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
idOpcao.innerHTML = "Selecione um das cidades abaixo";
var novo = document.createElement("option");
    novo.setAttribute("id", "opcoes");
    novo.value = descricao;
    novo.text  = descricao;
    document.inscricao.cidade.options.add(novo);
}
}
else {
idOpcao.innerHTML = "Primeiro selecione o estado";
}
}


