function contarCaracteres(maximo,campo,resultado){
	var total=maximo;
	var obj = document.getElementById(campo);
	var digitado=obj.value.length;
	if(digitado>total){
		alert('Número máximo de caracteres excedido.');
		obj.value=obj.value.slice(0,total) //exibe os primeiros caracteres
	}
	digitado=obj.value.length;
	var restante=total-digitado;
	document.getElementById(resultado).innerHTML=restante.toString() + ' restantes';//exibe o texto na div
}

// função para aceitar apenas letras e números
function letras_num(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode != 127 && charCode !=8 && charCode !=95) {
		if (charCode < 45 || charCode == 47 || (charCode > 57 && charCode < 64) || (charCode > 90 && charCode < 97) || charCode > 122) {
			return false
		}
	}
	return true;
}

// função para tornar maiusculas a string
function maiusculas(evt) {
	evt.toUpperCase();
	return true;
}

// função para aceitar apenas números
function sonumeros(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		return false;
	}
	return true;
}

function autotab(current,_to){
	var to = document.getElementById(_to);
    if (current.getAttribute && current.value.length==current.getAttribute("maxlength")) {
        to.focus();
		to.select();
    }
}

function checa_mail(objname) {
	var obj=document.getElementById(objname);
	var ok = true;
	var email = obj.value;
	var tamanho = email.length;
	if (tamanho>10){ 
		var arroba = email.indexOf("@"); 
		var ponto = email.indexOf(".",arroba);
		// Tem arroba e ponto no texto ??
		if (arroba<1 || ponto<1 || ponto==(tamanho-1))
			ok = false;
	} else
		ok = false;
	if (!ok) {
		alert("E-mail inválido.");
		obj.value='';
		obj.focus();
		return false;
	} else
		return true;
}

function formataCEP(Campo, teclapres){

   if(window.event){
    var tecla = teclapres.keyCode;
   }else  tecla = teclapres.which;

   var vr = new String(Campo.value);
   vr = vr.replace(".", "");
   vr = vr.replace(".", "");
   vr = vr.replace("/", "");
   vr = vr.replace("-", "");

   tam = vr.length + 1;

   
   if (tecla != 9 && tecla != 8){
      if (tam > 2 && tam < 6)
         Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
      if (tam >= 6 && tam < 9)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '-' + vr.substr(5,tam-5);
   }
}

function formataTel(Campo, teclapres){

   if(window.event){
    var tecla = teclapres.keyCode;
   }else  tecla = teclapres.which;

   var vr = new String(Campo.value);
   vr = vr.replace("(", "");
   vr = vr.replace(")", "");
   vr = vr.replace("-", "");

   tam = vr.length + 1;

   
   if (tecla != 9 && tecla != 8){
      if (tam > 2 && tam < 7)
         Campo.value = '(' + vr.substr(0,2) + ')' + vr.substr(2, tam);
      if (tam >= 7 && tam < 10)
         Campo.value = '(' + vr.substr(0,2) + ')' + vr.substr(2,4) + '-' + vr.substr(6,tam-6);
   }
}

