var xmlHttp = criaXmlHttpRequestObject( );

//cria o objeto XML HTTL REQUEST 		
function criaXmlHttpRequestObject( ) 
{
  
  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 buscaPessoa(obj){
	var cpf = limpaMascara(obj.value);
	xmlHttp.open("GET", "buscaPessoas.php?cpf="+cpf, true);
	var imagem = document.createElement("img");
	imagem.setAttribute("src","imagens/loading.gif");
	noImagem =document.getElementById("imgAguardando");
	noImagem.style.textAlign="left";
	if(!noImagem.lastChild)
		noImagem.appendChild(imagem);
   		xmlHttp.onreadystatechange = function(){
    	if(xmlHttp.readyState==4) { 
	    	if (xmlHttp.status == 200) {
	    		var dadosXML = document.createElement("div");
	    		alert(xmlHttp.responseText);
	    		dadosXML.innerHTML=xmlHttp.responseText;
	    		
	    		noImagem.removeChild(noImagem.lastChild);	    		
				itens = dadosXML.childNodes;
				alert(itens)
	    		for (i=0; i<itens.length; i++){
	    			if (itens[i].tagName=="ERRO"){
		    			alert(itens[i].firstChild.nodeValue);
		    			limpaCampos();
	    			} else if (itens[i].tagName=="PESSOA"){
			    		alert('teste');	
	    				pessoa = itens[i].childNodes;
	    				document.getElementById("dadosFormulario").style.display="block";
						for(k=0; k<pessoa .length; k++){
							//remove os elemento txt
							if (pessoa[k].nodeName!="#text"){
								alert(document.getElementById(pessoa[k].nodeName).tagName +' - '+document.getElementById(pessoa[k].nodeName).type)
								if (pessoa[k].firstChild){
									if(document.getElementById(pessoa[k].nodeName).type=='radio'){
										document.getElementById(pessoa[k].nodeName).setAttribute("checked","checked");
									}
									document.getElementById(pessoa[k].nodeName).value =  pessoa[k].firstChild.nodeValue
								}
							}
						}
	    			}
	    		}
	    	} else {
	    		alert("Erro: "+xmlHttp.statusText);
	    	}
	    }
    }
	xmlHttp.send(null);
}

// o nome dos campos do formulario devem ser correspondente as TAGS XML criadas no arquivo resposta
function preencheFormulario(dadosXML, tagPai){
	dados = dadosXML.getElementsByTagName(tagPai);
	if (dados.length>0){
		for (var j=0; j<dados.length; j++){
			var item = dados[j].childNodes;
			for(k=0; k<item.length; k++){
				//remove os elemento txt
				if (item[k].nodeName!="#text"){
					document.getElementById(item[k].nodeName).value =item[k].firstChild.nodeValue;
				}
			}
		}
	}
}

function limpaCampos(){
    var cpf = document.getElementById('CPF').value;
	for(i=0; i<document.forms[0].elements.length; i++){
		document.forms[0].elements[i].value="";
	}
	document.getElementById("dadosFormulario").style.display="block";
	document.getElementById('CPF').value = aplicaMascara('###.###.###-##',cpf);
}   

function buscaAutor(cpf, campo){
	xmlHttp.open("GET", "buscaPessoas.php?cpf="+cpf, true);
	xmlHttp.onreadystatechange = function(){
	   	if(xmlHttp.readyState==4) { 
	    	if (xmlHttp.status == 200) {
	    		document.getElementById(campo).innerHTML=xmlHttp.responseText;
	    	} else {
	    		alert("Erro: "+xmlHttp.statusText);
	    	}
	    }
	}
	xmlHttp.send(null);
}
