var HttpReq = null;
var dest_combo = null;

function ajaxComboBoxCidades(url, comboBox){
  dest_combo = comboBox;
  var indice = document.getElementById('USU_ESTADO').selectedIndex;
  var sigla = document.getElementById('USU_ESTADO').options[indice].getAttribute('value');
  url = url + '?UF=' + sigla;
  if (document.getElementById) { //Verifica se o Browser suporta DHTML.
    if (window.XMLHttpRequest) {
      HttpReq = new XMLHttpRequest();
      HttpReq.onreadystatechange = XMLHttpRequestChangeCidade;
      HttpReq.open("GET", url, true);
      HttpReq.send(null);
    } else 
    if (window.ActiveXObject) {
      HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
      if (HttpReq) {
        HttpReq.onreadystatechange = XMLHttpRequestChangeCidade;
        HttpReq.open("GET", url, true);
        HttpReq.send();
      }
    }
  }
}

function XMLHttpRequestChangeCidade() {
  if (HttpReq.readyState == 4 && HttpReq.status == 200){  //Verifica se o arquivo foi carregado com sucesso.
    var result = HttpReq.responseXML;
    var cidades = result.getElementsByTagName("nome");
    document.getElementById(dest_combo).innerHTML = "";
    for (var i = 0; i < cidades.length; i++) {
      new_opcao = create_opcao_cidade(cidades[i]);
      document.getElementById(dest_combo).appendChild(new_opcao);
    }
  }
}

function create_opcao_cidade(cidade) { //Cria um novo elemento OPTION.
  //return opcao.cloneNode(true);
  var new_opcao = document.createElement("option"); //Cria um OPTION.
  var texto = document.createTextNode(cidade.childNodes[0].data); //Cria um texto.
  new_opcao.setAttribute("value",cidade.getAttribute("id")); //Adiciona o atributo de valor a nova opção.
  var cid=document.getElementById('cidade_atual').value;
  if (cid==cidade.getAttribute("id")) {
    new_opcao.setAttribute("selected",true);
  }
  new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
  return new_opcao; // Retorna a nova OPTION.
}
