window.onload = function(){
  var curso = document.getElementById('cod_curso');
  curso.onchange = function() { mostraDatas(curso); }
}

function mostraDatas(curso){
  if(curso == null) { return; }
  aviso(1);
  var escolha = curso.options[curso.selectedIndex].value;
  var url = "mostraDatas.php?curso=" + encodeURIComponent(escolha);
  requisicaoHTTP("GET", url, true);
}

function trataDados(){
  var info = ajax.responseText;
  var arrayOpcoes = eval(info);

  var listaNova = document.createElement('select');
  listaNova.setAttribute('name', 'cod_agenda');
  criaOpcoes(listaNova, arrayOpcoes);

  var camada = document.getElementById('exibir');
  limpa(camada);
  camada.appendChild(listaNova);
  aviso(0);
}

function criaOpcoes(lista, opcoes){
  if(opcoes == null || opcoes.length == 0){
    return;
  }
  var op = null;
  for(var i = 0;i < opcoes.length;i++){
    op = document.createElement('option');
    op.setAttribute('value',opcoes[i]);
    op.appendChild(document.createTextNode(opcoes[i]));
    lista.appendChild(op);
  }
}

function limpa(elemento){
  if(elemento != null && elemento.hasChildNodes()){
    for(var i = 0;i < elemento.childNodes.length;i++){
      elemento.removeChild(elemento.firstChild);
    }
  }
}

function aviso(exibir){
  var saida = document.getElementById("avisos");
  if(exibir){
    saida.className = "aviso";
    saida.innerHTML = "Aguarde... processando!";
  } else {
    saida.className = "";
    saida.innerHTML = "";
  }  
}