//Javascript

//Função criaRequest
function criaRequest(){
	try {
      request = new XMLHttpRequest();
	} catch (trymicrosoft) {
    
	    try {
    	  request = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (othermicrosoft) {
    
    		try {
		        request = new ActiveXObject("Microsoft.XMLHTTP");
		    } catch (failed) {
       			request = false;
    		}
    	}
  	}

  if (!request)
    alert("Error initializing XMLHttpRequest!");
  else
  	return request;
}

//Função enviaDados
function enviaDadosCpf(){
	//Novo Request
	linkReq = criaRequest();
	
	if(linkReq != undefined){
		//Pegar dados
		var cpf = document.getElementById('tx_cpf');
		
		//Montar requisição
		linkReq.open("POST","ajax/verifica_ajax.php",true);
		linkReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		linkReq.onreadystatechange = recebeDadosCpf;
		
		var params = "msg="+cpf.value;
		
		//Carregar DIV de "carregando"
		document.getElementById('pesquisando_cpf').style.display = 'block';
		
		//Enviar
		linkReq.send(params);
		
	}
	
}

//Função recebeDados
function recebeDadosCpf(){
	
	//alert(linkReq.readyState);
	
	//Verificar pelo estado "4" de pronto
	if (linkReq.readyState == '4'){ // if #
		
		//alert(linkReq.responseText);
		
		//Pegar dados da resposta XML
		var xmlRes = linkReq.responseXML;
		
		//Verificar erro
		var erro = xmlRes.getElementsByTagName('erro');
		
		if (erro[0].firstChild.nodeValue == '1'){ // if ##
			alert("Erro no retorno "+erro[0].firstChild.nodeValue);
		} else {
						
			//Pegar mensagem
			var msg = xmlRes.getElementsByTagName('item');
			
			//Se o retorno for igual a 1 eu mostro a div
			if(msg[0].id == 1){  // if ###
		
			//Pegar DIV destino
			var targetDiv = document.getElementById('resultado_cpf');
			
			//Montar Nova msg
			var mDiv = document.createElement('div');
			mDiv.id = "msg_"+msg[0].id;
			mDiv.innerHTML = msg[0].firstChild.nodeValue;
			
			//Adicionar ao destino
			targetDiv.appendChild(mDiv);
			
			//Remove loading
			document.getElementById('pesquisando_cpf').style.display = 'none';
			
			//Deixa visivel a div com o conteudo			
			document.getElementById('resultado_cpf').style.display = 'block';
			
			} else {
			
			//Remove loading
			document.getElementById('pesquisando_cpf').style.display = 'none';
				
		   }  // fim if ###
			
		}  // fim if ##
		
	}  // fim if #
	
} // fim function recebeDados

//============================================================================================//

//Função enviaDados
function enviaDadosCnpj(){
	//Novo Request
	linkReq = criaRequest();
	
	if(linkReq != undefined){
		//Pegar dados
		var cnpj = document.getElementById('tx_cnpj');
		
		//Montar requisição
		linkReq.open("POST","ajax/verifica_ajax.php",true);
		linkReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		linkReq.onreadystatechange = recebeDadosCnpj;
		
		var params = "msg="+cnpj.value;
		
		//Carregar DIV de "carregando"
		document.getElementById('pesquisando_cnpj').style.display = 'block';
		
		//Enviar
		linkReq.send(params);
		
	}
	
}

//Função recebeDados
function recebeDadosCnpj(){
	
	//alert(linkReq.readyState);
	
	//Verificar pelo estado "4" de pronto
	if (linkReq.readyState == '4'){ // if #
		
		//alert(linkReq.responseText);
		
		//Pegar dados da resposta XML
		var xmlRes = linkReq.responseXML;
		
		//Verificar erro
		var erro = xmlRes.getElementsByTagName('erro');
		alert(erro);
		
		if (erro[0].firstChild.nodeValue == "2"){ // if ##
			alert("CNPJ inválido!");
		} else if (erro[0].firstChild.nodeValue != "0"){ // if ##
			alert("Erro no retorno "+erro[0].firstChild.nodeValue);
		} else {
						
			//Pegar mensagem
			var msg = xmlRes.getElementsByTagName('item');
			
			//Se o retorno for igual a 1 eu mostro a div
			if(msg[0].id == 1){  // if ###
		
			//Pegar DIV destino
			var targetDiv = document.getElementById('resultado_cnpj');
			
			//Montar Nova msg
			var mDiv = document.createElement('div');
			mDiv.id = "msg_"+msg[0].id;
			mDiv.innerHTML = msg[0].firstChild.nodeValue;
			
			//Adicionar ao destino
			targetDiv.appendChild(mDiv);
			
			//Remove loading
			document.getElementById('pesquisando_cnpj').style.display = 'none';
			
			//Deixa visivel a div com o conteudo			
			document.getElementById('resultado_cnpj').style.display = 'block';
			
			} else {
			
			//Remove loading
			document.getElementById('pesquisando_cnpj').style.display = 'none';
				
		   }  // fim if ###
			
		}  // fim if ##
		
	}  // fim if #
	
} // fim function recebeDadosCnpj
