$(document).ready(function()
{
	
	$("#cpf").blur(function()
	{
		if($('#cpf').val() !== "")
		{		
			if(!document.getElementById('img-confirmacao-cpf'))
				$(this).parent().append("<img id='img-confirmacao-cpf' src='img/loading.gif' />");
				
			$("#cpf").ajaxStart(function() {						 
				$(this).attr('src', 'img/loading.gif');				 
			});
			
			$.get("../Controller.Request/PessoaFisica.php",{action: 'check-cpf', cpf: $('#cpf').val()}, function(data) {
				
				//alert(data);
				
				var resposta  = data.getElementsByTagName("response")[0];
				
				var message = resposta.getElementsByTagName("message")[0].firstChild.nodeValue.replace(/^\s+|\s+$/g,"");
				
				if(message !== "NULL")
				{
					$("#img-confirmacao-cpf").attr('src', 'img/no.png');	
					//alert(message);
					$("#rg").focus();
				} else if(message == "NULL")
					$("#img-confirmacao-cpf").attr('src', 'img/ok.png');			
			});
		} else
		{
			if(document.getElementById('img-confirmacao-cpf'))
				$('#img-confirmacao-cpf').remove();
		}
	});
	
	
	$("#cnpj").blur(function()
	{
		if($('#cnpj').val() !== "")
		{			
			if(!document.getElementById('img-confirmacao-cnpj'))
				$(this).parent().append("<img id='img-confirmacao-cnpj' src='img/loading.gif' />");
				
			$("#cnpj").ajaxStart(function() {						 
				$(this).attr('src', 'img/loading.gif');				 
			});
			
			$.get("../Controller.Request/PessoaJuridica.php",{action: 'check-cnpj', cnpj: $('#cnpj').val()}, function(data) {
				
				//alert(data);
				
				var resposta  = data.getElementsByTagName("response")[0];
				
				var message = resposta.getElementsByTagName("message")[0].firstChild.nodeValue.replace(/^\s+|\s+$/g,"");
				
				if(message !== "NULL")
				{
					$("#img-confirmacao-cnpj").attr('src', 'img/no.png');	
					//alert(message);
				} else if(message == "NULL")
					$("#img-confirmacao-cnpj").attr('src', 'img/ok.png');		
			});
		} else {
			if(document.getElementById('img-confirmacao-cnpj'))
				$('#img-confirmacao-cnpj').remove();
		}
	});
	
		
	$("#email").blur(function()
	{
		if($('#email').val() !== "")
		{
			if(!document.getElementById('img-confirmacao-email'))
				$(this).parent().append(" <img id='img-confirmacao-email' src='img/ok.png' />");
				
			if(!document.getElementById('img-confirmacao-email'))
				$(this).parent().append("<img id='img-confirmacao-email' src='img/loading.gif' />");
				
			$("#email").ajaxStart(function() {						 
				$(this).attr('src', 'img/loading.gif');				 
			});
			
			$.get("../Controller.Request/PessoaJuridica.php",{action: 'check-email', email: $('#email').val()}, function(data) {
				
				//alert(data);
				
				var resposta  = data.getElementsByTagName("response")[0];
				
				var message = resposta.getElementsByTagName("message")[0].firstChild.nodeValue.replace(/^\s+|\s+$/g,"");
				
				if(message !== "NULL")
				{
					$("#img-confirmacao-email").attr('src', 'img/no.png');	
					//alert(message);
					$("#sobrenome").focus();
				} else if(message == "NULL")
					$("#img-confirmacao-email").attr('src', 'img/ok.png');			
			});
		} else {
			if(document.getElementById('img-confirmacao-email'))
				$('#img-confirmacao-email').remove();
		}
			
	});
});

