// JavaScript Document
function dateMask(inputData, e){
	if(document.all) // Internet Explorer
		var tecla = event.keyCode;
	else //Outros Browsers
		var tecla = e.which;

	if(tecla >= 47 && tecla < 58){ // numeros de 0 a 9 e "/"
		var data = inputData.value;
		if (data.length == 2 || data.length == 5){
			data += '/';
			inputData.value = data;
		}
	}else if(tecla == 8 || tecla == 0) // Backspace, Delete e setas direcionais(para mover o cursor, apenas para FF)
		return true;
		else return false;
}

function fone(obj, e) {
	if(document.all) // Internet Explorer
		var tecla = event.keyCode;
	else //Outros Browsers
		var tecla = e.which;
		
	if(tecla >= 47 && tecla < 58){ // numeros de 0 a 9 e "/"
		var fone = obj.value;
		if(fone.length == 0){ 
			fone = '(';
			obj.value = fone;
		}
		if(fone.length == 3){ 
			fone += ') ';
			obj.value = fone;
		}
		if(fone.length == 9){ 
			fone += '-';
			obj.value = fone;
		}
	}else if(tecla == 8 || tecla == 0) return true; // Backspace, Delete e setas direcionais(para mover o cursor, apenas para FF)
		else return false;
}

function MascaraCep(obj, e){
	if(document.all) // Internet Explorer
		var tecla = event.keyCode;
	else //Outros Browsers
		var tecla = e.which;

	if(tecla >= 47 && tecla < 58){ // numeros de 0 a 9 e "/"
		var cep = obj.value;
		if(cep.length == 2){ 
			cep += '.';
			obj.value = cep;
		}
		if(cep.length == 6){ 
			cep += '-';
			obj.value = cep;
		}
	}else if(tecla == 8 || tecla == 0) return true; // Backspace, Delete e setas direcionais(para mover o cursor, apenas para FF)
		else return false;		   
}

function MascaraCNPJ(obj, e){
	if(document.all) var tecla = event.keyCode; // Internet Explorer 
	else var tecla = e.which;                   //Outros Browsers
		
	if(tecla >= 47 && tecla < 58){              // numeros de 0 a 9 e "/"
		var cpf = obj.value;
		if(cpf.length == 2){ 
			cpf += '.';
			obj.value = cpf;
		}
		if(cpf.length == 6){ 
			cpf += '.';
			obj.value = cpf;
		}
		if(cpf.length == 10){ 
			cpf += '/';
			obj.value = cpf;
		}
		if(cpf.length == 15){ 
			cpf += '-';
			obj.value = cpf;
		}
	}else if(tecla == 8 || tecla == 0) return true; // Backspace, Delete e setas direcionais(para mover o cursor, apenas para FF)
		else return false;
}

function MascaraCPF(obj, e){
	if(document.all) var tecla = event.keyCode; // Internet Explorer 
	else var tecla = e.which;                   //Outros Browsers
		
	if(tecla >= 47 && tecla < 58){              // numeros de 0 a 9 e "/"
		var cpf = obj.value;
		if(cpf.length == 3){ 
			cpf += '.';
			obj.value = cpf;
		}
		if(cpf.length == 7){ 
			cpf += '.';
			obj.value = cpf;
		}
		if(cpf.length == 11){ 
			cpf += '-';
			obj.value = cpf;
		}
	}else if(tecla == 8 || tecla == 0) return true; // Backspace, Delete e setas direcionais(para mover o cursor, apenas para FF)
		else return false;
}

function ValidarCPF(Objcpf){
 var cpf = Objcpf.value;
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   
   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");
    
   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	  window.alert("CPF inválido. Tente novamente.");
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     window.alert("CPF inválido. Tente novamente.");
	 return false;
   }
   return true;
 };

function remove(str, sub) {
i = str.indexOf(sub);
r = "";
if (i == -1) return str;
r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
return r;
}


function ValidarCNPJ(ObjCnpj){
        var cnpj = ObjCnpj.value;
        var valida = new Array(6,5,4,3,2,9,8,7,6,5,4,3,2);
        var dig1= new Number;
        var dig2= new Number;
        
        exp = /\.|\-|\//g
        cnpj = cnpj.toString().replace( exp, "" ); 
		if (cnpj == "00000000000000" || 
			cnpj == "11111111111111" || 
			cnpj == "22222222222222" || 
			cnpj == "33333333333333" || 
			cnpj == "44444444444444" || 
			cnpj == "55555555555555" || 
			cnpj == "66666666666666" || 
			cnpj == "77777777777777" || 
			cnpj == "88888888888888" || 
			cnpj == "99999999999999") {
			
		}
        var digito = new Number(eval(cnpj.charAt(12)+cnpj.charAt(13)));
                
        for(i = 0; i<valida.length; i++){
                dig1 += (i>0? (cnpj.charAt(i-1)*valida[i]):0);  
                dig2 += cnpj.charAt(i)*valida[i];       
        }
        dig1 = (((dig1%11)<2)? 0:(11-(dig1%11)));
        dig2 = (((dig2%11)<2)? 0:(11-(dig2%11)));
        
        if(((dig1*10)+dig2) != digito)  return false;                
}

function ValidaFormulario1( ){
	var erros = 0;
	var erro = "Preencha os campos abaixo que estão vazios:\n";
	if(document.cadastro.nome.value == ""){
		erro += "- Nome: Campo obrigatorio Vazio\n";
		erros++;
		}
	if(document.cadastro.CPF.value == ""){
		erro += "- CPF: Campo Obrigatorio Vazio\n";
		erros++;
	}
	else if(ValidarCPF(document.cadastro.CPF)!= true){
		erro += "- CPF: Não valido\n";
		erros++;
	}
	if(document.cadastro.telefone.value == ""){
		erro += "- Telefone ou Celular: Um dos dois tem que ser preenchido\n";
		erros++;
	}
	if(document.cadastro.nascimento.value == ""){
		erro += "- Data de Nascimento: Campo Obrigatorio Vazio\n";
		erros++;	
	}
	if(document.cadastro.email.value == ""){
		erro += "- Email: Campo Obrigatorio Vazio\n";
		erros++;
	}
	if(document.cadastro.endereco.value == ""){
		erro += "- Rua: Campo Obrigatorio Vazio\n";	
		erros++;
	}
	if(document.cadastro.bairro.value == ""){
		erro += "- Bairro: Campo Obrigatorio Vazio\n";	
		erros++;
	}
	if((document.cadastro.senha.value == "") && (document.cadastro.consenha.value == "")){
		erro += "- Senha: Campo Vazio\n";
		erros++;
	}
	else if(document.cadastro.senha.value != document.cadastro.consenha.value){
		erro += "- Senha não Comfere com a Confimação de senha\n";
		erros++;
	}
	if (erros > 0) {
		var totalErros = "Total de erros encontrados: "+erros; 
		erro+= "\n"+totalErros;
		alert(erro);
		return false;
	}
}

function Verifica( ){
	var erros = 0;
	var erro = "Corrija os campos abaixo: \n";
	if(document.form1.CPF.value == ""){
		erro+= "- Campo CPF Vazio \n";
		erros++;
	}
	else if(ValidarCPF(document.form1.CPF)!= true){
		erro += "- CPF: Não valido\n";
		erros++;
	}
	if(document.form1.senha.value == ""){
		erro+= "- Campo Senha Vazio \n";
		erros++;
	}
	if (erros > 0) {
		var totalErros = "Total de erros encontrados: "+erros; 
		erro+= "\n"+totalErros;
		alert(erro);
		return false;
	}
};

function Verifica_Senha( ){
	var erros = 0;
	var erro = "Corrija os campos abaixo: \n";
	if(document.form.senha_nova.value == ''){
		erro+= "- Campo senha nova esta vazio \n";
		erros++;
	}
	if(document.form.con_senha.value == ''){
		erro+= "- Campo confirmação de senha esta vazio \n";
		erros++;
	}
	if(document.form.con_senha.value != document.form.senha_nova.value){
		erro+= "- Confirmação de senha não confere com a nova senha\n";
		erros++;
	}
	if (erros > 0) {
		var totalErros = "Total de erros encontrados: "+erros; 
		erro+= "\n"+totalErros;
		alert(erro);
		return false;
	}
};
