var req;
function validaEmail(email){
	// Verificando Browser
	if(window.XMLHttpRequest) {
	   req = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) {
	   req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// Aqui vai o valor e o nome do campo que pediu a requisição.
	var url = "verificaUsuario.php?email="+email;
	 
	// Chamada do método open para processar a requisição
	req.open("Get", url, true); 
	 
	// Quando o objeto recebe o retorno, chamamos a seguinte função;
	req.onreadystatechange = function() {
	 
		// Exibe a mensagem "Verificando" enquanto carrega
		if(req.readyState == 1) {
			document.getElementById('mensagemErroEmail').innerHTML = '<p class="destaque" style="line-height: 100%"><font face="verdana" size="1" color="#FF0000">Verificando Cadastro...</font></p>';
		}
	 
		// Verifica se o Ajax realizou todas as operações corretamente (essencial)
		if(req.readyState == 4 && req.status == 200) {
		// Resposta retornada pelo validacao.php
		var resposta = req.responseText;
		d = document.frmContato;
		var erro = 0;
		
			if((d.email.value.length < 5) || ((d.email.value.indexOf("@") < 1) || (d.email.value.indexOf('.') < 3))){
				erro = 1;
				document.getElementById('mensagemErroEmail').style.display= 'block';
				document.getElementById('mensagemErroEmail').innerHTML = '<p class="erroEmail"><font face="verdana" size="1" color="#FF0000">Informe um e-mail válido</font></p>';
				return false;
			}
			
			// Abaixo colocamos a resposta na div
			if(resposta == "E-mail já cadastrado!" && erro == 0){
			document.getElementById('mensagemErroEmail').style.display= 'block';
			document.getElementById('mensagemErroEmail').innerHTML = '<p class="erroEmail"><font face="verdana" size="1" color="#FF0000">'+resposta+'</font></p>';
			return false;
			}
			else if(resposta == "Disponível!" && erro == 0){
			document.getElementById('mensagemErroEmail').style.display= 'block';
			document.getElementById('mensagemErroEmail').innerHTML = '<p class="okEmail"><font face="verdana" size="1" color="#009933">'+resposta+'</font></p>';
			}
			else{
			document.getElementById('mensagemErroEmail').style.display= 'none';
			}
		}
	 
	}
req.send(null);
}

function validacao(){

	d = document.frmContato;
	var texto = "";
	var erro = 0;

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.nome.value == ""){
		erro = 1;
		texto = texto + "O campo <strong>Nome</strong> &eacute; de preencimento obrigat&oacute;rio.<br />";
		document.getElementById('nome').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('nome').style.background = '#FFFFFF';
	}
	
	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.sobrenome.value == ""){
		erro = 1;
		texto = texto + "O campo <strong>Sobrenome</strong> &eacute; de preencimento obrigat&oacute;rio.<br />";
		document.getElementById('sobrenome').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('sobrenome').style.background = '#FFFFFF';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.endereco.value == "" || d.num_endereco.value == ""){
		erro = 1;
		texto = texto + "O campo <strong>Endereço e Número</strong> são de preencimento obrigat&oacute;rio.<br />";
		document.getElementById('endereco').style.background = '#FFA6A6';
		document.getElementById('num_endereco').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('endereco').style.background = '#FFFFFF';
		document.getElementById('num_endereco').style.background = '#FFFFFF';
	}
	
	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.bairro.value == ""){
		erro = 1;
		texto = texto + "O campo <strong>Bairro</strong> &eacute; de preencimento obrigat&oacute;rio.<br />";
		document.getElementById('bairro').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('bairro').style.background = '#FFFFFF';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.cep.value == ""){
		erro = 1;
		texto = texto + "O campo <strong>CEP</strong> &eacute; de preencimento obrigat&oacute;rio.<br />";
		document.getElementById('cep').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('cep').style.background = '#FFFFFF';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.senha.value == ""){
		erro = 1;
		texto = texto + "O campo <strong>Senha</strong> &eacute; de preencimento obrigat&oacute;rio.<br />";
		document.getElementById('senha').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('senha').style.background = '#FFFFFF';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.senha.value != d.repete_senha.value){
		erro = 1;
		texto = texto + "Você deve repetir a mesma <strong>Senha</strong> para confirmar.<br />";
		document.getElementById('repete_senha').style.background = '#FFA6A6';
		document.getElementById('senha').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('repete_senha').style.background = '#FFFFFF';
		document.getElementById('senha').style.background = '#FFA6A6';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	if(d.fone.value.substring(0,1) == "8" || d.fone.value.substring(0,1) == "9" || d.fone.value.substring(0,1) == "" || d.ddd.value.substring(0,1) == ""){
		erro = 1;
		texto = texto + "Você deve informar um<strong> Telefone Fixo</strong>. Voip ou Móvel não são permitidos.<br />";
		document.getElementById('fone').style.background = '#FFA6A6';
		document.getElementById('ddd').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('fone').style.background = '#FFFFFF';
		document.getElementById('ddd').style.background = '#FFFFFF';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.cpfEscolhido.checked == true && d.cpf_cliente.value == ""){
		erro = 1;
		texto = texto + "Preencha o campo <strong>CPF</strong> corretamente.<br />";
		document.getElementById('cpf_cliente').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('cpf_cliente').style.background = '#FFFFFF';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	if(d.cnpjEscolhido.checked == true && d.cnpj_cliente.value == ""){
		erro = 1;
		texto = texto + "Preencha o campo <strong>CNPJ</strong> corretamente.<br />";
		document.getElementById('cnpj_cliente').style.background = '#FFA6A6';
	}
	else{
		texto = texto + "";
		document.getElementById('cnpj_cliente').style.background = '#FFFFFF';
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	if(erro == 1){
		document.getElementById('mensagemErroContato').style.display= 'block';
		document.getElementById('mensagemErroContato').innerHTML = "<p class='destaque' style='line-height: 100%'><font face='verdana' size='1' color='#FF0000'>" + texto + "</font></p>";
		return false;
	}
	else{
		document.getElementById('mensagemErroContato').style.display= 'none';
	}

}


	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



function focusForm(teste){
	document.getElementById(teste).style.background = '#FFFFFF';
}

function blurForm(teste){
	document.getElementById(teste).style.background = '#FFFFFF';
}

