// JavaScript Document

// Funções de limitação de digitação
function verificaNumeros(e) {
	if (document.all) // Internet Explorer
		var tecla = e.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
	if  (!((tecla == 8) || (tecla > 47 && tecla < 58))) // numeros de 0 a 9
		e.keyCode = 0;
	return true;
}

function  verificaLetras(e) {
	var	resultadoValidacao = false
	if (document.all) // Internet Explorer
		var tecla = e.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
	switch (tecla) {
		case 8:   // backspace
		case 32:  // espaço
		case 39:  // '
		case 127: // delete
			resultadoValidacao = true;
	}
	if (tecla > 64 && tecla < 91) //  letras maiusculas e minusculas
		resultadoValidacao = true;
	if (tecla > 96 && tecla < 123) {
		e.keyCode = e.keyCode - 32;
		resultadoValidacao = true;
	}
	if (resultadoValidacao == false)
		e.keyCode = 0; //resultadoValidacao = false;		
	return true;
}





// funções de máscaras de digitação do cnpj
function mascara_cnpj(evento, cnpj) {
	if (verificaNumeros(evento)) {
	    var mycnpj = '';
	    mycnpj = mycnpj + cnpj;
	    if (mycnpj.length == 2) {
	        mycnpj = mycnpj + '.';
	        document.form1.cnpj.value = mycnpj;
	    }
	    if (mycnpj.length == 6) {
	        mycnpj = mycnpj + '.';
	        document.form1.cnpj.value = mycnpj;
	    }
	    if (mycnpj.length == 10) {
	        mycnpj = mycnpj + '/';
	        document.form1.cnpj.value = mycnpj;
	    }
		if (mycnpj.length == 15) {
	        mycnpj = mycnpj + '-';
	        document.form1.cnpj.value = mycnpj;
	    }
	}
}


function mascara_cep(evento, varCep) {
	if (verificaNumeros(evento)) {
	    var cep = '';
	    cep = cep + varCep;
	    if (cep.length == 5) {
	        cep = cep + '-';
	        document.form1.cep.value = cep;
	    }
	}
}


function mascara_telefone(evento, varTel) {
	if (verificaNumeros(evento)) {
	    var tel = '';
	    tel = tel + varTel;
	    if (tel.length == 4) {
	        tel = tel + '-';
	        document.form1.telefone.value = tel;
	    }
	}
}


function mascara_fax(evento, varTel) {
	if (verificaNumeros(evento)) {
	    var tel = '';
	    tel = tel + varTel;
	    if (tel.length == 4) {
	        tel = tel + '-';
	        document.form1.fax.value = tel;
	    }
	}
}




function verificaEmail() {
	var regstr = "^[a-zA-Z0-9_\\-\\.]+\\@([a-zA-Z0-9_\\-\\.]+\\.){1,4}[a-zA-Z]{2,4}$";
	var reg = new RegExp(regstr);
	if (!reg.test(document.form1.email.value))
		return false
	else
		return true;
}




// Funções de validação de CPF
function validaCPF() {
	var cpfTXT = document.form1.cnpj.value;
	var resultadoValidacao = true;
	var a = [];
	var b = new Number;
	var c = 11;

	if (cpfTXT.length != 14)
		resultadoValidacao = false;
	else {
		//remove a máscara de digitação
		myCpf = cpfTXT.substring(0, 3) + cpfTXT.substring(4, 7) + cpfTXT.substring(8, 11) + cpfTXT.substring(12,14);
		
		if (myCpf == "00000000000" || myCpf == "11111111111" || myCpf == "22222222222" || myCpf == "33333333333" || myCpf == "44444444444" || myCpf == "55555555555" || myCpf == "66666666666" || myCpf == "77777777777" || myCpf == "88888888888" || myCpf == "99999999999")
			resultadoValidacao = false;

		for (i=0; i<11; i++) {
			a[i] = myCpf.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; 

		if ((myCpf.charAt(9) != a[9]) || (myCpf.charAt(10) != a[10]))
			resultadoValidacao = false;
	}
	
	return(resultadoValidacao);		
}




// Funções de validação de CNPJ
function validaCNPJ() {
	CNPJ = document.form1.cnpj.value;
	
	//Verifica se tem todos os dígitos
	if (CNPJ.length < 18) return false;
	//Verifica a posição dos caracteres especiais na máscara de CNPJ
	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")) return false;
	
	//substituir os caracteres que não são números
	if(document.layers && parseInt(navigator.appVersion) == 4){
		   x = CNPJ.substring(0,2);
		   x += CNPJ. substring (3,6);
		   x += CNPJ. substring (7,10);
		   x += CNPJ. substring (11,15);
		   x += CNPJ. substring (16,18);
		   CNPJ = x;
	} else {
		   CNPJ = CNPJ. replace (".","");
		   CNPJ = CNPJ. replace (".","");
		   CNPJ = CNPJ. replace ("-","");
		   CNPJ = CNPJ. replace ("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CNPJ)) return false;
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		   a[i] = CNPJ.charAt(i);
		   b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
		   b += (a[y] * c[y]);
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])) return false;
	
	return true;
}









