
if (navigator.appName.indexOf('Microsoft') != -1){  
    clientNavigator = "IE";  
}else{  
    clientNavigator = "Other";  
}  
 
function Verifica_Cep(cep, obrigatorio){  
//Se o parâmetro obrigatório for igual à zero, significa que elepode estar vazio, caso contrário, não  
    var cep    = document.getElementById(cep);  
    var strcep = cep.value;  
    if((obrigatorio == 1) || (obrigatorio == 0 && strcep != "")){  
        if (strcep.length != 9){  
            alert("CEP informado inválido.");  
            cep.focus();  
            return false  
        }else{  
            if (strcep.indexOf("-") != 5){  
                alert("Formato de CEP informado inválido.");  
                cep.focus();  
                return false  
            }else{  
                if (isNaN(strcep.replace("-","0"))){  
                    alert("CEP informado inválido.");  
                    cep.focus();  
                    return false  
                }  
            }  
        }  
    }       
}  


function formatar(src, mask){
//Formatação para qualquer mascara

  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)
if (texto.substring(0,1) != saida)
  {
    src.value += texto.substring(0,1);
  }
}

 
function Verifica_Email(email, obrigatorio){  
//Se o parâmetro obrigatório for igual à zero, significa que elepode estar vazio, caso contrário, não  
    var email = document.getElementById(email);  
    if((obrigatorio == 1) || (obrigatorio == 0 && email.value != "")){  
        if(!email.value.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi)){  
            alert("Informe um e-mail válido");  
            email.focus();  
            return false  
        }  
    }  
}  
   
function Bloqueia_Caracteres(evnt){  
//Função permite digitação de números  
    if (clientNavigator == "IE"){  
        if (evnt.keyCode < 48 || evnt.keyCode > 57){  
            return false  
        }  
    }else{  
        if ((evnt.charCode < 48 || evnt.charCode > 57) && evnt.keyCode == 0){  
            return false  
        }  
    }  
}  
 
function Ajusta_Data(input, evnt){  
//Ajusta máscara de Data e só permite digitação de números  
    if (input.value.length == 2 || input.value.length == 5){  
        if(clientNavigator == "IE"){  
            input.value += "/";  
        }else{  
            if(evnt.keyCode == 0){  
                input.value += "/";  
            }  
        }  
    }  
//Chama a função Bloqueia_Caracteres para só permitir a digitação de números  
    return Bloqueia_Caracteres(evnt);  
}  
 
function Ajusta_Cep(input, evnt){  
//Ajusta máscara de CEP e só permite digitação de números  
    if (input.value.length == 5){  
        if(clientNavigator == "IE"){  
            input.value += "-";  
        }else{  
            if(evnt.keyCode == 0){  
                input.value += "-";  
            }  
        }  
    }  
//Chama a função Bloqueia_Caracteres para só permitir a digitação de números  
    return Bloqueia_Caracteres(evnt);  
}  
function Valida_Cpf(cpf){
		cpf = document.cadastro.cpf.value;
		erro = new String;
		if (cpf.length < 11) {
			alert("Sao necessarios 11 digitos para verificacao do CPF!");
			document.cadastro.cpf.focus();
			return false;
			}
		var nonNumbers = /\D/;
		if (nonNumbers.test(cpf)) {
			alert("A verificacao de CPF suporta apenas numeros!");
			document.cadastro.cpf.focus();
			return false;
			}
		if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
			alert("Numero de CPF invalido!");
			document.cadastro.cpf.focus();
			return false;
			}
		var a = [];
		var b = new Number;
		var c = 11;
		for (i=0; i<11; i++){
			a[i] = cpf.charAt(i);
			if (i <  9) b += (a[i] *  --c);
		}
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0;
		c = 11;
		for (y=0; y<10; y++) b += (a[y] *  c--); 
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		status = a[9] + ""+ a[10]
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			alert("CPF - Digito verificador com problema!");
			document.cadastro.cpf.focus();
			return false;
			}
		return true;
	}

function Valida_Cnpj(cnpj) {
	 cnpj = document.cadastro.cnpj.value;
     var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
     digitos_iguais = 1;
    if (cnpj.length != 14)
            {
                 alert('CNPJ inválido, tem que ter 14 dígitos.');
                 document.cadastro.cnpj.focus();
                 return false;
                 }

     for (i = 0; i < cnpj.length - 1; i++)
           if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                 {
                 digitos_iguais = 0;
                 break;
                 }
     if (!digitos_iguais)
           {
           tamanho = cnpj.length - 2
           numeros = cnpj.substring(0,tamanho);
           digitos = cnpj.substring(tamanho);
           soma = 0;
           pos = tamanho - 7;
           for (i = tamanho; i >= 1; i--)
                 {
                 soma += numeros.charAt(tamanho - i) * pos--;
                 if (pos < 2)
                       pos = 9;
                 }
           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
           if (resultado != digitos.charAt(0))
				{
                 alert('CNPJ inválido');
                 document.cadastro.cnpj.focus();
                 return false;
                 }

           tamanho = tamanho + 1;
           numeros = cnpj.substring(0,tamanho);
           soma = 0;
           pos = tamanho - 7;
           for (i = tamanho; i >= 1; i--)
                 {
                 soma += numeros.charAt(tamanho - i) * pos--;
                 if (pos < 2)
                       pos = 9;
                 }
           resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
           if (resultado != digitos.charAt(1)){
                 alert('CNPJ inválido');
                 document.cadastro.cnpj.focus();
                 return false;
                 }
           else {
     // alert('CNPJ  OK !');
            return true;
            }
           }
     else{
           alert('CNPJ inválido');
           document.cadastro.cnpj.focus();
           return false;
           }
     } 
	 
function check(ev)  
  {  
   var key = ev.keyCode;  
	if ((key == 46) || (key < 48) || (key > 57) || (key == 44))  
		{ event.returnValue = false; }  
	return true;
  }  

function vercontato() { 

	d = document.contato;

	if (d.nome.value == ""){
		alert("O campo Nome deve ser preenchido!");
                     d.nome.focus();
					 return false;			 
           }
		   
	if (d.email.value == ""){
		alert("O campo email deve ser preenchido!");
                     d.email.focus();
					 return false;		 
           }
	   
    if(Verifica_Email("email", 1) == false){ return false }  
		   
	if (d.mensagem.value == ""){
		alert("O campo mensagem deve ser preenchido!");
                     d.mensagem.focus();
					 return false;		 
           }	
		   
	return;
} 
	
function verificarj() { 

	d = document.cadastro;

   if (d.nome.value == ""){
		alert("O campo Nome deve ser preenchido!");
                     d.nome.focus();
					 return false;
					 
           }
		   
	if (d.endereco.value == ""){
		alert("O campo Endereço deve ser preenchido!");
                     d.endereco.focus();
					 return false;
					 
           }	
		
//	if 	(d.cpfs1.value != "") {	
//		if(Valida_Cpf("cpfs1") == false){ return false}
//		}
	             
    if(Verifica_Email("email", 1) == false){ return false }  
	
	if(Verifica_Cep("cep", 1) == false){ return false }  		   
		   
	if 	(d.cnpj.value != "") {	
		if(Valida_Cnpj("cnpj") == false){ return false}
		}
	
	return;
} 

function verificarf() { 

	d = document.cadastro;

   if (d.nome.value == ""){
		alert("O campo Nome deve ser preenchido!");
                     d.nome.focus();
					 return false;
					 
           }
		   
	if (d.endereco.value == ""){
		alert("O campo Endereço deve ser preenchido!");
                     d.endereco.focus();
					 return false;
					 
           }	
		
//	if 	(d.cpfs1.value != "") {	
//		if(Valida_Cpf("cpfs1") == false){ return false}
//		}
	             
    if(Verifica_Email("email", 1) == false){ return false }  
	
	if(Verifica_Cep("cep", 1) == false){ return false }  		   
		   

	if 	(d.cpf.value != "") {	
		if(Valida_Cpf("cpf") == false){ return false}
		}	
	
	return;
} 

