function Tecla(e) {
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
		if (tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else {
			if (tecla != 8) // backspace
				event.keyCode = 0;
				//return false;
			else
				return true;
		}
}

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 );
		} 
	}

}

var NUM_DIGITOS_CPF  = 11;
var NUM_DIGITOS_CNPJ = 14;
var NUM_DGT_CNPJ_BASE = 8;

String.prototype.lpad = function(pSize, pCharPad) {
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) 
		str = ch + str;
	return (str);
}

String.prototype.trim = function() {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function unformatNumber(pNum) {
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
}  

function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj) {
	if (pIsCnpj==null) 
		pIsCnpj = false;
	if (pUseSepar==null) 
		pUseSepar = true;
	
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);
	numero = numero.lpad(maxDigitos, '0');
	
	if (!pUseSepar) 
		return numero;
	if (pIsCnpj) {
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	} else {
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	
	return numero;
}

function dvCpfCnpj(pEfetivo, pIsCnpj) {
	if (pIsCnpj==null) 
		pIsCnpj = false;
		
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? NUM_DGT_CNPJ_BASE: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pEfetivo, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";
	
	for(j = 1; j <= 2; j++) {
		k = 2;
		soma = 0;
		
		for (i = calculado.length-1; i >= 0; i--) {
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		
		dv = 11 - soma % 11;
		if (dv > 9) 
			dv = 0;
			
		calculado += dv;
		result += dv
	}

	return result;
}

function isCpf(pCpf) {
	var numero = formatCpfCnpj(pCpf, false, false);
	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;
	// Valida dígitos verificadores
	if (numero != base + digitos) 
		return false;
	/* Não serão considerados válidos os seguintes CPF:
	 * 000.000.000-00, 111.111.111-11, 222.222.222-22, 333.333.333-33, 444.444.444-44,
	 * 555.555.555-55, 666.666.666-66, 777.777.777-77, 888.888.888-88, 999.999.999-99.
	 */
	algUnico = true;
	for (i=1; i<NUM_DIGITOS_CPF; i++) {
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	return (!algUnico);
} 

function isCnpj(pCnpj) {
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, NUM_DGT_CNPJ_BASE);
	var ordem = numero.substring(NUM_DGT_CNPJ_BASE, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;
	
	// Valida dígitos verificadores
	if (numero != base + ordem + digitos) 
		return false;
	/* Não serão considerados válidos os CNPJ com os seguintes números BÁSICOS:
	 * 11.111.111, 22.222.222, 33.333.333, 44.444.444, 55.555.555,
	 * 66.666.666, 77.777.777, 88.888.888, 99.999.999.
	 */
	algUnico = numero.charAt(0) != '0';
	
	for (i=1; i<NUM_DGT_CNPJ_BASE; i++) {
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	
	if (algUnico) 
		return false;

	/* Não será considerado válido CNPJ com número de ORDEM igual a 0000.
	 * Não será considerado válido CNPJ com número de ORDEM maior do que 0300
	 * e com as três primeiras posições do número BÁSICO com 000 (zeros).
	 * Esta crítica não será feita quando o no BÁSICO do CNPJ for igual a 00.000.000.
	 */
	if (ordem == "0000") 
		return false;
	
	return (base == "00000000" || parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
}


function doSubmitSacado(pEvent, pForm) {
	var val = document.bolete.cpfSacado.value;
	var base = val.substring(0, val.length-2);
	
	if (base.length >11) {
		if (isCnpj(val) == false) {
			alert("Digite corretamente o CNPJ do Sacado.") ;
			return false;
		} else { 
			return true; 
		}
	} else {
		if (isCpf(val)==false) {
			alert("Digite corretamente o CPF do Sacado.");
			return false;
		} else { 
			return true;
		}
	}
}

function submitAto() {
	var iselectAto = document.bolete.atoEscolhido.value;
	if(iselectAto == 1) {
		document.bolete.action = "faces/jsp/boletoGraficaForm2.jsp?ato=" + iselectAto;
	} else if(iselectAto == 13) {
		document.bolete.action = "faces/jsp/boletoGraficaOrgaosForm.jsp?ato=" + iselectAto;
	}	
	
	document.bolete.submit();
}

function validate() {	
	if(document.bolete.atoEscolhido.value=="-1" || document.bolete.atoEscolhido.value == null) {
		alert("Escolha lauda ou tipo de assinatura que deseja pagar!");
		document.bolete.atoEscolhido.focus();
		return false;
	}	
	if(document.bolete.valorCausa.value==null || document.bolete.valorCausa.value=="" ||  
		document.bolete.valorCausa.value==0) {	
		alert("Informe o valor!" );
		document.bolete.valorCausa.focus();
		return false;
	}
	if(document.bolete.nomeSacado.value=="" || document.bolete.nomeSacado.value==null) {
		alert("Informe o nome do sacado com o nome completo do contribuinte ou interessado.");
		document.bolete.nomeSacado.focus();
		return false;
	}
}

