﻿var xmlHttp = ajaxInit();

function ajaxInit() {
  var xmlHttp;
  // trabalha com todos os browsers, menos IE
  try
  {
	// tenta criar o objeto XMLHttpRequest
	xmlHttp = new XMLHttpRequest( );
  }
  catch(e)
  {
	var XmlHttpVersoes = [ 
		"MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.5.0",
		"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
		"MSXML2.XMLHttp","Microsoft.XMLHttp"
		];

	// procura a melhor versão para o IE
	for (var i=0; i< XmlHttpVersoes.length; i++) 
	{
	  try 
	  { 
		//tenta criar o objeto XMLHttpRequest 
		xmlHttp = new ActiveXObject(XmlHttpVersoes[i]);
	  } 
	  catch (e) {}
	}
  }
  //retorna o objeto criado ou um erro
  if (!xmlHttp)
	alert("Erro ao tentar criar um objeto XMLHttpRequest.");
  else 
	return xmlHttp;
}

function buscaPesquisadores(cpf){

	var idElemento=cpf;
	var insereDetalhe = document.getElementById(idElemento);
	var divPesquisador;
	
	if(document.getElementById("divDetalhe")){
		divDetalhe =document.getElementById("divDetalhe");
		noPai = divDetalhe.parentNode
		alert(noPai.nodeName)
		while (noPai.hasChildNodes()){
			noPai.removeChild(noPai.lastChild);
		}
	}

	divDetalhe= document.createElement("div");
	divDetalhe.setAttribute("id", "divDetalhe");
		
	imagem = document.createElement("img");
	imagem.setAttribute("src","loading.gif");
		
	noImagem =document.getElementById("img"+idElemento);
	noImagem.appendChild(imagem);
	if(xmlHttp){
		xmlHttp.open("POST","obterPesquisador.php",true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
		var dados = "cpf="+cpf;
		xmlHttp.onreadystatechange=function() { 
			if(xmlHttp.readyState==4) { 
	   			if (xmlHttp.status == 200) {
	   				noImagem.removeChild(noImagem.lastChild);
	   				divDetalhe.innerHTML=xmlHttp.responseText; 
					insereDetalhe.appendChild(divDetalhe);
				} else {
	   				
   				}
     		}
 		}	
		xmlHttp.send(dados);
	}
}

