// JavaScript Document
function DadosData(valor) {
      //verifica se o browser tem suporte a ajax
	  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 tem recursos para uso do Ajax");
               ajax = null;
            }
         }
      }
	  //se tiver suporte ajax
	  if(ajax) {
	     //deixa apenas o elemento 1 no option, os outros são excluídos
		 document.frmAjaxData.txtCategoria.options.length = 1;
	     
		 idOpcao  = document.getElementById("txtTable");
		 idOpcao1  = document.getElementById("txtCategoria");
		 
	     ajax.open("POST", "_include_ajax_seleciona_data.php", true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		 ajax.onreadystatechange = function() {
            //enquanto estiver processando...emite a msg de carregando
			if(ajax.readyState == 1) {
			   idOpcao.innerHTML = "Loading...!";   
	        }
			//após ser processado - chama função processXML que vai varrer os dados
            if(ajax.readyState == 4 ) {
			   if(ajax.responseXML) {
			      processXML(ajax.responseXML);
			   }
			   else {
			       //caso não seja um arquivo XML emite a mensagem abaixo
				   idOpcao.innerHTML = "Select the table";
			   }
            }
         }
		 //passa o código do estado escolhido
	     var params = "categoria="+valor;
         ajax.send(params);
      }
   }
   
   function processXML(obj){
      //pega a tag cidade
      var dataArray   = obj.getElementsByTagName("segmentos");
      
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
	     //percorre o arquivo XML paara extrair os dados
         for(var i = 0 ; i < dataArray.length ; i++) {
            var item = dataArray[i];
			//contéudo dos campos no arquivo XML
			var tipo    =  item.getElementsByTagName("tipo")[0].firstChild.nodeValue;
			var subtipo =  item.getElementsByTagName("subtipo")[0].firstChild.nodeValue;
			
	        idOpcao.innerHTML = "Select the table";
			idOpcao1.innerHTML = "categoria";
			
			//cria um novo option dinamicamente  
			var novo = document.createElement("option");
			var novo1 = document.createElement("option");
			    //atribui um ID a esse elemento
			    novo.setAttribute("id", "txtTable");
				novo1.setAttribute("id", "txtCategoria");
				//atribui um valor
			    novo.value = subtipo;
				novo1.value = tipo;
				//atribui um texto
			    novo.text  = subtipo;
				novo1.text  = tipo;
				//finalmente adiciona o novo elemento
				document.frmAjaxData.txtTabela.options.add(novo);
		 }
	  }
	  else {
	    //caso o XML volte vazio, printa a mensagem abaixo
		idOpcao.innerHTML = "Select the table";
	  }	 
	  document.frmAjaxData.txtCategoria.options.add(novo1);
   }