function getHTTPObject(){
	var xmlhttp = false;
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(ex){
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

var http = getHTTPObject();

function funcaowebservicecep() 
{
	if(cadastro.cep.value==""){
		
		//document.cadastro.cep.focus();
		
	}else{
		
		http.open("GET", 'buscarendereco.php?cep='+document.cadastro.cep.value, true);
		http.onreadystatechange = handleHttpResponse;
		http.send(null);
	
		var arr; //array com os dados retornados
		function handleHttpResponse() 
		{
			if (http.readyState == 4) 
			{
				var response = http.responseText;
				eval("var arr = "+response); //cria objeto com o resultado
				document.cadastro.rua.value = arr.rua;
				document.cadastro.bairro.value = arr.bairro;
				document.cadastro.cidade.value = arr.cidade;
				document.cadastro.uf.value = arr.uf;
			}
		}
		document.cadastro.numero.focus();
		
	}
}



// JavaScript Document
function FormataCNPJ(campo, teclapres)
{
	var tecla = teclapres.keyCode;
	var vr = new String(campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");
	tam = vr.length + 1;
	if (tecla != 14)
	{
		if (tam == 3)
			campo.value = vr.substr(0, 2) + '.';
		if (tam == 6)
			campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 5) + '.';
		if (tam == 10)
			campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(6, 3) + '/';
		if (tam == 15)
			campo.value = vr.substr(0, 2) + '.' + vr.substr(2, 3) + '.' + vr.substr(6, 3) + '/' + vr.substr(9, 4) + '-' + vr.substr(13, 2);
	}
}

function Formatar(mascara, documento)
{
  var i = documento.value.length;
  var saida = mascara.substring(0,1);
  var texto = mascara.substring(i)
  
  if (texto.substring(0,1) != saida){
            documento.value += texto.substring(0,1);
  }
  
}
function SomenteNumero()
{
	if (event.keyCode<48 || event.keyCode>57)
	{
	return false;
	}
} 
function ValidaCNPJ(cnpj)
{

  var i = 0;
  var l = 0;
  var strNum = "";
  var strMul = "6543298765432";
  var character = "";
  var iValido = 1;
  var iSoma = 0;
  var strNum_base = "";
  var iLenNum_base = 0;
  var iLenMul = 0;
  var iSoma = 0;
  var strNum_base = 0;
  var iLenNum_base = 0;

  if (cnpj == ""){
        return false;
		$(".ok_cnpj").hide();
		$(".erro_cnpj").show();
  }

  l = cnpj.length;
  for (i = 0; i < l; i++) {
        caracter = cnpj.substring(i,i+1)
        if ((caracter >= '0') && (caracter <= '9'))
           strNum = strNum + caracter;
  };

  if(strNum.length != 14)
        return false;
		$(".ok_cnpj").hide();
		$(".erro_cnpj").show();

  strNum_base = strNum.substring(0,12);
  iLenNum_base = strNum_base.length - 1;
  iLenMul = strMul.length - 1;
  for(i = 0;i < 12; i++)
        iSoma = iSoma +
                        parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) *
                        parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10);

  iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);
  if(iSoma == 11 || iSoma == 10)
        iSoma = 0;

  strNum_base = strNum_base + iSoma;
  iSoma = 0;
  iLenNum_base = strNum_base.length - 1
  for(i = 0; i < 13; i++)
        iSoma = iSoma +
                        parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) *
                        parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)

  iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);
  if(iSoma == 11 || iSoma == 10)
        iSoma = 0;
  strNum_base = strNum_base + iSoma;
  if(strNum != strNum_base){
        return false;
		$(".ok_cnpj").hide();
		$(".erro_cnpj").show();
  }else{
  		$(".erro_cnpj").hide();
		$(".ok_cnpj").show();
  }
  return (true);

}

function ValidaCPF(cpf){
	
	FullCPF = cpf;
	
	cpf = cpf.replace(".", "");
	cpf = cpf.replace(".", "");
	cpf = cpf.replace("-", "");
      
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	
	if (cpf.length < 11){
		$(".ok_cpf").hide();
		$(".erro_cpf").show();
		return false;
	}
	for (i = 0; i < cpf.length - 1; i++){
		if (cpf.charAt(i) != cpf.charAt(i + 1)){
			digitos_iguais = 0;
			break;
		}
	}
	if (!digitos_iguais){
		
		numeros = cpf.substring(0,9);
		digitos = cpf.substring(9);
		soma = 0;
		
		for (i = 10; i > 1; i--){
			  soma += numeros.charAt(10 - i) * i;
		}
		
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		
		if (resultado != digitos.charAt(0)){
			$(".ok_cpf").hide();
			$(".erro_cpf").show();
			return false;
		}
		
		numeros = cpf.substring(0,10);
		soma = 0;
		
		for (i = 11; i > 1; i--){
			  soma += numeros.charAt(11 - i) * i;
		}
		
		resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
		
		if (resultado != digitos.charAt(1)){ 
			$(".ok_cpf").hide();
			$(".erro_cpf").show();
			return false; 
		}
		
		$(".erro_cpf").hide();
		$(".ok_cpf").show();
		//$(".resposta").load("verificaCPF.php", { 'cpf': FullCPF } );
		document.cadastro.validaCPF.value = 'ok';
		
		return true;

		
	}else{
		$(".ok_cpf").hide();
		$(".erro_cpf").show();
		return false;
		
	}
	  
}

function retorna(texto)
{	
	$("#naoEnvia").show();
	$("#naoEnvia p").text(texto);
}

function valida_cadastro(){

	if(cadastro.profissional.value==""){
		
		retorna("O seu nome não foi preenchido.");
		cadastro.profissional.focus();
		
	/*}else if(cadastro.nome_empresa.value==""){
		
		retorna("O nome da empresa não foi preenchido.");
		cadastro.nome_empresa.focus();
		
	*/}else if (cadastro.dia.value=="" || cadastro.mes.value=="" || cadastro.ano.value==""){
		
		retorna("O data de nascimento não foi preenchida corretamente.");
		if (cadastro.dia.value==""){
			cadastro.dia.focus();
		}else if (cadastro.mes.value==""){
			cadastro.mes.focus();
		}else if (cadastro.ano.value==""){
			cadastro.ano.focus();
		}
	
	}else if (cadastro.cpf.value=="" || cadastro.validaCPF.value!="ok"){
		
		retorna("O CPF não foi preenchido corretamente.");
		cadastro.cpf.focus();
	
	}else if (cadastro.registroGeral.value==""){
		
		retorna("O número do RG não foi preenchido.");
		cadastro.registroGeral.focus();
	
	}else if (cadastro.cep.value==""){
		
		retorna("O CEP não foi preenchido.");
		cadastro.cep.focus();

	}else if (cadastro.rua.value=="" || cadastro.numero.value=="" || cadastro.bairro.value=="" || cadastro.cidade.value=="" || cadastro.uf.value==""){
		
		retorna("O endereço não foi preenchido corretamente. Verifique o nome da rua, número, bairro, cidade ou estado.");
		cadastro.rua.focus();
	
	}else if (cadastro.telefone.value==""){
		
		retorna("O número do telefone não foi preenchido.");
		cadastro.telefone.focus();
		
	}else if (cadastro.celular.value==""){
		
		retorna("O número do celular não foi preenchido.");
		cadastro.celular.focus();
		
	}else if (cadastro.email_contato.value==""){
		
		retorna("O endereço de e-mail não foi preenchido.");
		cadastro.email_contato.focus();
		
	}else if(cadastro.email_contato.value.indexOf(".") == -1 || cadastro.email_contato.value.indexOf("@") == -1 || cadastro.email_contato.value.indexOf("www.") > -1 || cadastro.email_contato.value.indexOf("@.") > -1 || cadastro.email_contato.value.indexOf(".@") > -1){
		
		retorna("Informe um e-mail válido para efetuar o cadastro.");
		cadastro.email_contato.focus();
		
	}else{
		cadastro.cadastrar.value="ok"; 
		cadastro.submit();
	}

}
