// JavaScript Document

// Função da pagina comprar_credito.php

	// Mostra e oculta formularios de cadastro PF,PJ,Login
	function trocaForm(check,visivel,invisivel,invisivel2){
		document.form_compra.reset();
		document.getElementById(visivel).style.display='block';
		document.getElementById(invisivel).style.display='none';
		document.getElementById(invisivel2).style.display='none';
		document.getElementById('prepago').style.display='block';
		document.getElementById('pospago').style.display='none';
		document.getElementById(check.id).checked="checked";
		document.getElementById('pre').checked="checked";
	}
	
	// Mostra e oculta planos pré e pós pagos
	function trocaPlano(visivel,invisivel){
		document.getElementById(visivel).style.display='block';
		document.getElementById(invisivel).style.display='none';
	}
	
	// Mostra e oculta consultas
	function trocaDiv(visivel,invisivel){
		document.getElementById(visivel).style.display='block';
		document.getElementById(invisivel).style.display='none';
	}
	
	
	function Limpar(valor, validos) {
		// retira caracteres invalidos da string
		var result = "";
		var aux;
		for (var i=0; i < valor.length; i++) {
			aux = validos.indexOf(valor.substring(i, i+1));
			if (aux>=0) {
				result += aux;
			}
		}
		return result;
	}
	
	//Formata número tipo moeda usando o evento onKeyDown
	
	function Formata(campo,tammax,teclapres,decimal) {
		var tecla = teclapres.keyCode;
		vr = Limpar(campo.value,"0123456789");
		tam = vr.length;
		dec=decimal
		
		if (tam < tammax && tecla != 8){ 
			tam = vr.length + 1 ; 
		}
		
		if (tecla == 8 ){ 
			tam = tam - 1 ; 
		}
		
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		
			if ( tam <= dec ){ 
				campo.value = vr ; 
			}
			
			if ( (tam > dec) && (tam <= 5) ){
				campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; 
			}
			
			if ( (tam >= 6) && (tam <= 8) ){
				campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, 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 - dec, tam ) ;
			}
			
			if ( (tam >= 15) && (tam <= 17) ){
				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 ) ;
			}
		}
	
	}
	
// Muda de um campo para outro automaricamente
function trocaCampo(campo,proximo,length){
	if (campo.value.length==length){
		document.getElementById(proximo).focus();
	}
}


function eNumeroValido(caracter){
	var reTipo = /^[0-9 a-z A-Z]$/;
	return reTipo.test(caracter);
}

function emailValido(caracter){
	var reTipo = /^[0-9 a-z A-Z _ \- \. @ &nbsp;]$/;
	return reTipo.test(caracter);
}

function verificaCaracteresTexto(objTexto){
	var vl = new String(objTexto.value);
	for(i=0; i<vl.length; i++){
		if(!eNumeroValido(vl.charAt(i))){
			alert("Para este campo são permitidos apenas números e letras.");
			objTexto.value = "";
			objTexto.focus();
			break;
		}
	}	

}

function mascaraNumerica(objTexto){
	var vl = new String(objTexto.value);
	var ultimo = vl.charAt(vl.length-1);
	var penultimaLetra = "";
	
	if(ultimo == "."){
		  objTexto.value = vl.replace(".", ",");
		  ultimo = ",";
	}
	
	if(!eNumeroValido(ultimo)){
		  objTexto.value = vl.substr(0, vl.length-1);
		 
	}
	lower(objTexto);
}

function mascaraNome(objTexto){
	var vl = new String(objTexto.value);
	var ultimo = vl.charAt(vl.length-1);
	var penultimaLetra = "";
	
	if(ultimo == "."){
		  objTexto.value = vl.replace(".", ",");
		  ultimo = ",";
	}
	
	if(!eNumeroValido(ultimo)){
		  objTexto.value = vl.substr(0, vl.length-1);
		 
	}
}

function mascaraEmail(objTexto){
	var vl = new String(objTexto.value);
	var ultimo = vl.charAt(vl.length-1);
	var penultimaLetra = "";
		
	if(!emailValido(ultimo)){
		  objTexto.value = vl.substr(0, vl.length-1);
	}
	lower(objTexto);
}

function lower(objTexto){
	document.getElementById(objTexto.id).value = document.getElementById(objTexto.id).value.toLowerCase().replace(" ", "");
}