// JavaScript Document

 var existeUsuario = true;
 var existeEmail   = true;
 var existeCPF     = true;

  function check_cpf(numcpf)
  {
	  x = 0;
	  soma = 0;
	  dig1 = 0;
	  dig2 = 0;
	  texto = "";
	  numcpf1="";
	  len = numcpf.length; x = len -1;
	  // var numcpf = "12345678909";
	  for (var i=0; i <= len - 3; i++) {
		  y = numcpf.substring(i,i+1);
		  soma = soma + ( y * x);
		  x = x - 1;
		  texto = texto + y;
	  }
	  dig1 = 11 - (soma % 11);
	  if (dig1 == 10) dig1=0 ;
	  if (dig1 == 11) dig1=0 ;
	  numcpf1 = numcpf.substring(0,len - 2) + dig1 ;
	  x = 11; soma=0;
	  for (var i=0; i <= len - 2; i++) {
		  soma = soma + (numcpf1.substring(i,i+1) * x);
		  x = x - 1;
	  }
	  dig2= 11 - (soma % 11);
	  if (dig2 == 10) dig2=0;
	  if (dig2 == 11) dig2=0;
	  //alert ("Digito Verificador : " + dig1 + "" + dig2);
	  if ((dig1 + "" + dig2) == numcpf.substring(len,len-2)) {
		  return true;
	  }
	  //alert ("Numero do CPF invalido !!!");
	  return false;
  }

  function checkMail(mail){
	  var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	  if(typeof(mail) == "string"){
		  if(er.test(mail)){
			  return true;
		  }
	  }else if(typeof(mail) == "object"){
		  if(er.test(mail.value)){
			  return true;
		  }
	  }else{
		  return false;
	  }
  }

  function validarData(data) {
	  var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	  if ((data.match(expReg)) && (data!='')) {
		  return true;
	  } else {
		  return false
	  }
  }

function formcadastro(){
	  
	var flag = true;
	validaemail(document.FormCadastro.fix_cadastro_email);
	validacpf(document.FormCadastro.fix_cadastro_cpf);
	validausuario(document.FormCadastro.fix_cadastro_usuario);

	if(document.FormCadastro.fix_cadastro_comoconheceufix.value == ""){
		document.getElementById("cadastro_comoconheceufix").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;* Selecione uma op&ccedil;&atilde;o.</strong></span>"
		document.FormCadastro.fix_cadastro_comoconheceufix.focus();
		flag = false;
	}else{
		document.getElementById("cadastro_comoconheceufix").innerHTML = ""
	}
	  
	if(document.FormCadastro.fix_cadastro_estado.value == ""){
		document.getElementById("cadastro_estado").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;* Selecione um estado.</strong></span>"
		document.FormCadastro.fix_cadastro_cidade.focus();
		flag = false;
	}else{
		document.getElementById("cadastro_estado").innerHTML = ""
	}

	if(document.FormCadastro.fix_cadastro_cidade.value == ""){
		document.getElementById("cadastro_cidade").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		document.FormCadastro.fix_cadastro_cidade.focus();
		flag = false;
	}else{
		document.getElementById("cadastro_cidade").innerHTML = ""
	}
	
	if(document.FormCadastro.fix_cadastro_dtnascimento.value == ""){
		document.getElementById("cadastro_dtnascimento").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		document.FormCadastro.fix_cadastro_dtnascimento.focus();
		flag = false;
	}else{
		  if (!validarData(document.FormCadastro.fix_cadastro_dtnascimento.value)){
			  document.getElementById("cadastro_dtnascimento").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;Data inv&aacute;lida.";
			  document.FormCadastro.fix_cadastro_dtnascimento.focus();
			  flag = false;
		  } else {
			  document.getElementById("cadastro_dtnascimento").innerHTML = "";
		  }
	  }

	  if(document.FormCadastro.fix_cadastro_telefone.value == ""){
		  document.getElementById("cadastro_telefone").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		  document.FormCadastro.fix_cadastro_telefone.focus();
		  flag = false;
	  }else{
		  document.getElementById("cadastro_telefone").innerHTML = ""
	  }

	  if(document.FormCadastro.fix_cadastro_dddtelefone.value == ""){
		  document.getElementById("cadastro_telefone").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;* Preencher o DDD da regi&atilde;o.</strong></span>"
		  document.FormCadastro.fix_cadastro_dddtelefone.focus();
		  flag = false;
	  }else{
		  document.getElementById("cadastro_telefone").innerHTML = ""
	  }

	  if(document.FormCadastro.fix_cadastro_cpf.value == ""){
		  document.getElementById("cadastro_cpf").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		  document.FormCadastro.fix_cadastro_cpf.focus();
		  flag = false;
	  }else{
		  if (!check_cpf(document.FormCadastro.fix_cadastro_cpf.value)){
			  document.getElementById("cadastro_cpf").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;CPF Inv&aacute;lido</strong></span>"
			  document.FormCadastro.fix_cadastro_cpf.focus();
		  } else {
			  if (existeCPF == false){
			  	document.getElementById("cadastro_cpf").innerHTML = ""
			  }
		  }
	  }

	  if(document.FormCadastro.fix_cadastro_sobrenome.value == ""){
		  document.getElementById("cadastro_sobrenome").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		  document.FormCadastro.fix_cadastro_sobrenome.focus();
		  flag = false;
	  }else{
		  document.getElementById("cadastro_sobrenome").innerHTML = ""
	  }

	  if(document.FormCadastro.fix_cadastro_nome.value == ""){
		  document.getElementById("cadastro_nome").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		  document.FormCadastro.fix_cadastro_nome.focus();
		  flag = false;
	  }else{
		  document.getElementById("cadastro_nome").innerHTML = ""
	  }

	  if(document.FormCadastro.fix_cadastro_confirmasenha.value == ""){
		  document.getElementById("cadastro_confirmasenha").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		  document.FormCadastro.fix_cadastro_confirmasenha.focus();
		  flag = false;
	  } else {
		  document.getElementById("cadastro_confirmasenha").innerHTML = ""
	  }

	  if(document.FormCadastro.fix_cadastro_senha.value == ""){
		  document.getElementById("cadastro_senha").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		  document.FormCadastro.fix_cadastro_senha.focus();
		  flag = false;
	  } else {
		  document.getElementById("cadastro_senha").innerHTML = ""
	  }

	  if (document.FormCadastro.fix_cadastro_confirmasenha.value != "" && document.FormCadastro.fix_cadastro_senha.value != ""){
		  if(document.FormCadastro.fix_cadastro_confirmasenha.value != document.FormCadastro.fix_cadastro_senha.value){
			  document.getElementById("cadastro_confirmasenha").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;N&atilde;o confere com a senha.</strong></span>";
			  flag = false;
		  }
	  }

	  if(document.FormCadastro.fix_cadastro_email.value == ""){
		  document.getElementById("cadastro_email").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*</strong></span>"
		  document.FormCadastro.fix_cadastro_email.focus();
		  flag = false;
	  } else {
		  if (!checkMail(document.FormCadastro.fix_cadastro_email.value)){
			  document.getElementById("cadastro_email").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;E-mail inv&aacute;lido.</strong></span>";
			  flag = false;
		  } else {
			  if (existeEmail == false){
			  	document.getElementById("cadastro_email").innerHTML = "";
			  }
		  }
	  }

	if(document.FormCadastro.fix_cadastro_usuario.value == ""){
		document.getElementById("cadastro_usuario").innerHTML = "<span class='red'><strong>&nbsp;&nbsp;*"
		document.FormCadastro.fix_cadastro_usuario.focus();
		flag = false;
	} else {
		if (existeUsuario == false){
			document.getElementById("cadastro_usuario").innerHTML = ""
		}
	}

	if (flag == true && existeUsuario == false && existeEmail == false && existeCPF == false){
		document.FormCadastro.submit();
		return true
	} else {
		alert("Dados incompletos ou incorretos. Favor cadastrar novamente.");
		return false
	}
}

function MaskDdd(objeto, evt) { return Mascara(objeto, evt, '##'); }
function MaskFone(objeto, evt) { return Mascara(objeto, evt, '####-####');}
function MaskData(objeto, evt) { return Mascara(objeto, evt, '##/##/####');}
function MaskCpf(objeto, evt) { return Mascara(objeto, evt, '###########');}
  
function habilitaCorretora(habilita){
	if(habilita == 1){
		document.FormCadastro.fix_cadastro_corretora_nome.disabled = false;
		document.FormCadastro.fix_cadastro_corretora_nome.value    = "Nomes separados por (,)";
	} else {
		document.FormCadastro.fix_cadastro_corretora_nome.disabled = true;
		document.FormCadastro.fix_cadastro_corretora_nome.value   = "";				
	}
}

function corretoraNome(obj){
	if (obj.value == "Nomes separados por (,)"){
		obj.value = "";
	} 
}


// FUNCOES AJAX PARA VERIFICAR SE JÁ EXISTE CADASTRADOS
// - CPF
// - EMAIL
// - USUARIO

function GetXmlHttpObject(){
	try {
		// Firefox, Opera, Safari
		return new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try	{
			return new ActiveXObject("Msxml2.XMLHTTP"); // 6.0+
		} catch (e)	{
			return new ActiveXObject("Microsoft.XMLHTTP"); // 5.5+
		}
	}
	return null
}

// VERIFICA USUARIO

function validausuario(obj){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="valida-usuario.php";
	url = url + "?user=" + obj.value;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=retornausuario;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function retornausuario(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
		var retorno = xmlhttp.responseText;
		if (retorno != '0'){
			document.getElementById('cadastro_usuario').innerHTML = "<span class='red'><strong>&nbsp;&nbsp;Usu&aacute;rio j&aacute; cadastrado.</strong></span>"
			existeUsuario = true;
		} else {
			document.getElementById('cadastro_usuario').innerHTML = ""
			existeUsuario = false;
		}
	}
}

// VERIFICA CPF

function validacpf(obj){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="valida-cpf.php";
	url = url + "?cpf=" + obj.value;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=retornacpf;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function retornacpf(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
		var retorno = xmlhttp.responseText;
		if (retorno != '0'){
			document.getElementById('cadastro_cpf').innerHTML = "<span class='red'><strong>&nbsp;&nbsp;CPF j&aacute; cadastrado.</strong></span>"
			existeCPF = true;
		} else {
			document.getElementById('cadastro_cpf').innerHTML = ""
			existeCPF = false;
		}
	}
}

// VERIFICA EMAIL

function validaemail(obj){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="valida-email.php";
	url = url + "?email=" + obj.value;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=retornaemail;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function retornaemail(){
	if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
		var retorno = xmlhttp.responseText;
		if (retorno != '0'){
			document.getElementById('cadastro_email').innerHTML = "<span class='red'><strong>&nbsp;&nbsp;E-mail j&aacute; cadastrado.</strong></span>"
			existeEmail = true;
		} else {
			document.getElementById('cadastro_email').innerHTML = ""
			existeEmail = false;
		}
	}
}



