// Formata o campo valor
function formataValor(campo) 
{
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;

	if ( tam <= 2 ){
		campo.value = vr ; }
	if ( (tam > 2) && (tam <= 5) ){
		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 9) && (tam <= 11) ){
		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 15) && (tam <= 18) ){
		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
}

// Formata o campo valor
function formataNumerico(campo) 
{
	campo.value = filtraCampo(campo);
	vr = campo.value;
	tam = vr.length;
}

// limpa todos os caracteres especiais do campo solicitado
function filtraCampo(campo)
{
	var s = "";
	var cp = "";
	vr = campo.value;
	tam = vr.length;
	for (i = 0; i < tam ; i++) 
	{
		if (vr.substring(i,i + 1) != "/" && vr.substring(i,i + 1) != "-" && vr.substring(i,i + 1) != "." && vr.substring(i,i + 1) != "," )
		{
			s = s + vr.substring(i,i + 1);
		}
	}
	campo.value = s;
	return cp = campo.value
}

function pula(tamanho, e, campo, alvo)
{
	var key = capturaTecla(e);
	if (key == 9 || key == 16) return false;
	if (campo.value.length == tamanho) alvo.focus();
}
		
function validaEmail(email)
{
	emailRE = /^([a-zA-Z0-9_\-]+\.)*[a-zA-Z0-9_\-]+\@([a-zA-Z0-9_\-]+\.)+[a-zA-Z0-9]{2,4}$/;
	return emailRE.test(email);
}
		
function valida_data(dia,mes,ano)
{
	dias = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	if (ano % 4 == 0)	//ano bisexto
	{
		dias[1] = 29;
	}
	
	if (dia < 1 || dia > dias[mes-1])
	{
		return false;
	}
	
	if (mes < 1 || mes > 12)
	{
		return false;
	}
	
	if (ano < 1900)
	{
		return false;
	}
	
	return true;
}

function valida_data_nascimento(dia,mes,ano)
{
	if (!valida_data(dia,mes,ano)) return false;
	
	dtAtual = new Date();
	dtNascimento = new Date();
	dtNascimento.setFullYear(ano,mes,dia);
	
	return dtNascimento < dtAtual;
}



function valida_cpf(cpf) {
	var numeros, digitos, soma, i, resultado, digitos_iguais;
	digitos_iguais = 1;
	if (cpf.length < 11)
			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))
			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))
			return false;
		return true;
	} else {
		return false;
	}
}
function soNumeroSac(e){
	var tecla = capturaTecla(e);
	if (tecla == 8 || tecla == 0 || (tecla >= 48 && tecla<=57) || tecla == 118){
		
		return true;
	}
	return false;

}

function soNumero(e){
	var tecla = capturaTecla(e);
	
	if (tecla == 8 || tecla == 0 || (tecla >= 48 && tecla<=57) || tecla == 118){
		return true;
	}
	return false;
}

function soLetra(e)
{
	var tecla = capturaTecla(e);
	if (tecla == 8 || tecla == 0 || (tecla >= 65 && tecla <=90) || (tecla >=97 && tecla <= 122))
	{
		return true;
	}
	else
	{
		return false;
	}
}
function soLetrasEspaco(e) {
	var tecla =  capturaTecla(e);
	if (tecla == 32 || tecla == 8 || tecla == 0 || (tecla >= 65 && tecla <=90) || (tecla >=97 && tecla <= 122))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function soLetraNumero(e, num){
	var tecla =  capturaTecla(e);
	if ( tecla == 8 || tecla == 0 || (tecla >= 48 && tecla<=57) || (tecla >= 65 && tecla <=90) || (tecla >=97 && tecla <= 122))
	{
		ValidaCampos.numero(num)
		return true;
	}
	else
	{
		return false;
	}
}

function paraEmail(e) {
	var tecla = capturaTecla(e);
	switch(tecla) {
		case 0 :
		case 8 :
		case 45 : // -
		case 46 : // .
		case 64 : // @
		case 95 : // _
			return true;
	}
	return false;
}

function paraCpfCnpj(e) {
	var tecla = capturaTecla(e);
	switch(tecla) {
		case 0 :
		case 8 :
		case 46 : // .
		case 47 : // /
			return true;
	}
	return false;
}

function capturaTecla(e) {
	if(e.charCode != undefined)
		return e.charCode;
	
	if(e.which != undefined)
		return e.which;
	
	if(e.keyCode != undefined)
		return e.keyCode;
}

function isSequencia(str) {
	if(str != "") {
		init = str.charCodeAt(0);
		for(var i = 1; i < str.length; i++) {
			init++;
			if(str.charCodeAt(i) != init) {
				init = str.charCodeAt(0);
				for( var j = 1; j < str.length; j++) {
					init--; 
					if(str.charCodeAt(j) != init) {
						return false;
					}
				}
			}
		}
		return true;
	} 
	return false;
}

function isUnicoCaracter(str) {
	init = str.charCodeAt(0);
	for(var i = 0; i < str.length; i++) {
		if(init != str.charCodeAt(i)) 
			return false;
	}
	return true;
}

function valida_cnpj(cnpj)
{
    var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
    digitos_iguais = 1;
    if (cnpj.length < 14 && cnpj.length < 15)
        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))
            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))
            return false;
        return true;
    } else
        return false;
} 

function verificaSenha(senha) {
	if(senha.indexOf(" ") >= 0)
		return false;
	
	if(!hasLetra(senha) || !hasNumero(senha))
		return false;

	return true;
}

function hasNumero(str) {
	var tamanho = str.length;
	var codigo;
	for(var i = 0; i < tamanho; i++) {
		codigo = str.charCodeAt(i);
		if(codigo >= 48 && codigo <= 57)
			return true;
	}
	return false;
}

function hasLetra(str) {
	var tamanho = str.length;
	var codigo;
	for(var i = 0; i < tamanho; i++) {
		codigo = str.charCodeAt(i);
		if((codigo >= 65 && codigo <=90) || (codigo >=97 && codigo <= 122))
			return true;
	}
	return false;
}

function checaEstado(str) {
	var arrEstados = new Array();
	arrEstados[0] = "AC";
	arrEstados[1] = "AL";
	arrEstados[2] = "AM";
	arrEstados[3] = "AP";
	arrEstados[4] = "BA";
	arrEstados[5] = "CE";
	arrEstados[6] = "DF";
	arrEstados[7] = "ES";
	arrEstados[8] = "GO";
	arrEstados[9] = "MA";
	arrEstados[10] = "MG";
	arrEstados[11] = "MS";
	arrEstados[12] = "MT";
	arrEstados[13] = "PA";
	arrEstados[14] = "PB";
	arrEstados[15] = "PE";
	arrEstados[16] = "PI";
	arrEstados[17] = "PR";
	arrEstados[18] = "RJ";
	arrEstados[19] = "RN";
	arrEstados[20] = "RO";
	arrEstados[21] = "RR";
	arrEstados[22] = "RS";
	arrEstados[23] = "SC";
	arrEstados[24] = "SE";
	arrEstados[25] = "SP";
	arrEstados[26] = "TO";

	for(var i = 0; i < arrEstados.length; i++) {
		if(arrEstados[i].toString() == str.toUpperCase()) {
			return true;
		}
	}
	return false;
}

var ValidaCampos = {
	numero: function (num)
  {
   var nums = "0123456789"
   var valor;
   
   for (var i=0;i<num.value.length;i++)
   {
    valor=num.value.substring(i,i+1)
    if (nums.indexOf(valor) == -1)
    {
     num.value = num.value.substring(0,i);
     break;
    }
   }
   
  },
 
 letra: function (let)
  {
   var lets = "0123456789" // "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   var valor;
   
   for (var i=0;i<let.value.length;i++)
   {
    valor = let.value.substring(i,i+1).toUpperCase()
    if (lets.indexOf(valor) != -1)
    {
     let.value = let.value.substring(0,i).toUpperCase();
     break;
    }
   }
  },
  ativaDesativa: function () 
  {
	 if(!document.FormEmail.cliente[0].checked)
	  {
		//document.FormEmail.numero.style.display = document.FormEmail.letra.style.display = "none"; 
		document.getElementById('tb_placa').style.display = "none";
	  }else{
		document.getElementById('tb_placa').style.display = "";
		//document.FormEmail.numero.style.display = document.FormEmail.letra.style.display = "";
	  }
		 document.FormEmail.letra.value = document.FormEmail.numero.value = "";
  }
	
};
