// FUNCAO PARA PESQUISAR EMPRESA ESTRANGEIRA
// -----------------------------------------
function outrasAtividades(campoNome){
	document.forms[0][campoNome].value=1;
	document.forms[0].submit;
}

// FUNCAO PARA MARCAR ENTIDADES COMPONENTES ESCOLHIDAS
// ---------------------------------------------------
function marcaEntidadesComponentes(campoNome, qtdRes){
	alert("Rotina de cadastramento de entidades componentes em desenvolvimento...");
//	i=0;
//	while (i!=qtdRes) {
//		i++;
//		window.opener.document.forms[0][campoNome].value=valor;
//	}

}


// FUNCAO PARA ABRIR JANELA DE PESQUISA ENTIDADES COMPONENTES
// ----------------------------------------------------------
function pesquisarEntidadesComponentes(frm, nrCnpjPesq, regAncinePesq, razaoSocialPesq, campoNome){
	if (nrCnpjPesq!="" || regAncinePesq!="" || razaoSocialPesq!=""){
		mostraTbl(tblResultadoPesq);
		window.open("../pesquisarEntidadesComponentes.jsp?frm="+frm+"&nrCnpjPesq="+nrCnpjPesq+"&regAncinePesq="+regAncinePesq+"&razaoSocialPesq="+razaoSocialPesq+"","pesquisarEntidadesComponentes","status,width=625,height=350");
	} else {
		window.alert("Escolha e preencha um critério de pesquisa.");
	}
}

// FUNCAO PARA MARCAR AS ENTIDADES COMPONENTES
// -------------------------------------------
function marcaMunicipio(campoNome,
						campoCd,
						mun,
						cdMun){

	window.opener.document.forms[0][campoNome].value=mun;
	window.opener.document.forms[0][campoCd].value=cdMun;
	window.close();
}

// FUNCAO PARA CANCELAR PREENCHIMENTO DE DADOS PARA CORRESPONDÊNCIA
// ----------------------------------------------------------------
function cancelaJanela(tbl){
	escondeTbl(window.opener[tbl]);
	window.close();
}
// FUNCAO PARA CANCELAR PREENCHIMENTO DE DADOS PARA CORRESPONDÊNCIA
// ----------------------------------------------------------------
function cancelaEntidadesComponentes(){
	escondeTbl(window.opener.tblResultadoPesq);
	window.close();
}

// FUNCAO PARA MARCAR O MUNICÍPIO ESCOLHIDO
// ----------------------------------------
function marcaMunicipio(campoNome,
						campoCd,
						mun,
						cdMun){

	window.opener.document.forms[0][campoNome].value=mun;
	window.opener.document.forms[0][campoCd].value=cdMun;
	window.close();
}

// FUNCAO PARA MARCAR OS DADOS PARA CORRESPONDENCIA MUNICÍPIO ESCOLHIDO
// --------------------------------------------------------------------
function marcaDadosParaCorrespondenciaEmpresa(nomeFrm,
												nmEnderecoCorrespondencia, valor1, 
												deComplementoCorrespondencia, valor2, 
												nmBairroCorrespondencia, valor3, 
												sgUfCorrespondencia, valor4, 
												cdMunicipioIbgeCorrespondencia, valor5, 
												nmMunicipioIbgeCorrespondencia, valor6, 
												nrCepCorrespondencia, valor7, 
												deEmailCorrespondencia, valor8){

	emBranco="Campos em branco:"+('\n\n');
	
	if (frmDadosCorrespondenciaEmpresa.nmEndereco.value==""){
		emBranco = emBranco+"- Endereço"+('\n');
	}
	if (frmDadosCorrespondenciaEmpresa.deComplemento.value==""){
		emBranco = emBranco+"- Complemento"+('\n');
	}
	if (frmDadosCorrespondenciaEmpresa.nmBairro.value==""){
		emBranco = emBranco+"- Bairro"+('\n');
	}
	if (frmDadosCorrespondenciaEmpresa.nrCep.value==""){
		emBranco = emBranco+"- CEP"+('\n');
	}
	if (frmDadosCorrespondenciaEmpresa.sgUf.options.value==""){
		emBranco = emBranco+"- UF"+('\n');
	}
	if (frmDadosCorrespondenciaEmpresa.nmMunicipioIbge.value==""){
		emBranco = emBranco+"- Município"+('\n');
	}
	if (frmDadosCorrespondenciaEmpresa.deEmail.value==""){
		emBranco = emBranco+"- E-mail"+('\n');
	}
	if (emBranco=="Campos em branco:"+('\n\n')){
		window.opener.document.forms[nomeFrm][nmEnderecoCorrespondencia].value=valor1;
		window.opener.document.forms[nomeFrm][deComplementoCorrespondencia].value=valor2;
		window.opener.document.forms[nomeFrm][nmBairroCorrespondencia].value=valor3;
		window.opener.document.forms[nomeFrm][sgUfCorrespondencia].value=valor4;
		window.opener.document.forms[nomeFrm][cdMunicipioIbgeCorrespondencia].value=valor5;
		window.opener.document.forms[nomeFrm][nmMunicipioIbgeCorrespondencia].value=valor6;
		window.opener.document.forms[nomeFrm][nrCepCorrespondencia].value=valor7;
		window.opener.document.forms[nomeFrm][deEmailCorrespondencia].value=valor8;
		window.close();
	} else {
		window.alert(emBranco+('\n')+"* Preenchimento obrigatório.");
	}
}

// FUNCAO PARA ABRIR JANELA DE PESQUISA DE MUNICÍPIOS
// --------------------------------------------------------------------------
function pesquisaMunicipio(frm, cdUf, campoNome, campoCd){
	window.open("../pesquisarMunicipio.jsp?frm="+frm+"&cdUf="+cdUf+"&campoNome="+campoNome+"&campoCd="+campoCd+"","pesquisarMunicipio","status,width=450,height=400");
}

// FUNCAO PARA CONFIRMAR CANCELAMENTO DE CADASTRO DE ATIVIDADE
// --------------------------------------------------------------------------
function confirmaCancelamento(frm){
	if (confirm("Deseja cancelar o cadastramento dessa atividade?")){
		document.forms[frm].reset();
	}
}

// FUNCAO PARA MASCARAR DADOS
// --------------------------------------------------------------------------
/***
* Descrição.: formata um campo do formulário de
* acordo com a máscara informada...
* Parâmetros: 
* - campo (this)
* - sMask (mascara que define o
* formato que o dado será apresentado,
* usando o algarismo "9" para
* definir números e o símbolo "!" para
* qualquer caracter...
* - evtKeyPress (evento)
*
* Uso.......: <input type="textbox"
* name="xxx".....
* onkeypress="return Mascara(this, '99999-999', event);">
* Observação: As máscaras podem ser representadas
* como os exemplos abaixo:
* CEP -> 99999-999
* CPF -> 999.999.999-99
* CNPJ -> 99.999.999/9999-99
* C/C -> 999999-!
* Tel -> (99) 9999-9999
* Hora -> 99:99:99 ou 99:99
* Número -> R$ 15.000,00
* Formato do jeito que quiser usando e abusando dos exemplos acima.
***/
function Mascara(campo, sMask, evt) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	objForm = document.forms[0];

	evt = (evt) ? evt : (window.event) ? window.event : ""

	if(evt.which) {
		nTecla = evt.which;
	}else {
		nTecla = evt.keyCode;
	}

	if(nTecla == 8){
		campo.value = campo.value;
		return true;
	}

	if(nTecla == 35) return true;
	if(nTecla == 36) return true;
	if(nTecla == 37) return true;
	if(nTecla == 39) return true;

	sValue = campo.value;

	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	var characters = new Array(10);
	characters[0] = '.';
	characters[1] = '-';
	characters[2] = '/';
	characters[3] = '(';
	characters[4] = ')';
	characters[5] = ' ';
	characters[6] = ':';
	characters[7] = ',';
	characters[8] = '$';
	characters[9] = 'R';

	for(i=0;i<characters.length;i++){
		sValue = replaceAll(sValue, characters[i], "");
	}

	fldLen = sValue.length;
	mskLen = sMask.length; // = 5

	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;

	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"));
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "));
		bolMask = bolMask || ((sMask.charAt(i) == ":") || (sMask.charAt(i) == "$") || (sMask.charAt(i) == "R") || (sMask.charAt(i) == ","));

		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++; }
		else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}

		i++;
	}

	campo.value = sCod;

	if (sMask.charAt(i-1) == "9") { // apenas números...
		return ((nTecla > 47) && (nTecla < 58));
	} // números de 0 a 9
	else { // qualquer caracter...
		return true;
	}
}

// FUNCÕES PARA MOSTRAR E ESCONDER TABELAS <TABLES>
//-------------------------------------------------
function mostraTbl(tbl){
	tbl.style.display="block";
}
function escondeTbl(tbl){
	tbl.style.display="none";
}

// VALIDA E-MAIL
//-------------------------------------------------
function validaEmail(nomeCampo) {
	if (document.forms[0][nomeCampo].value == "") {
/*		alert("Digite seu e-mail.");
		document.forms[0][nomeCampo].focus();
		document.forms[0][nomeCampo].select();
*/
	} else {
		prim = document.forms[0][nomeCampo].value.indexOf("@")
		if(prim < 2) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("@",prim + 1) != -1) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf(".") < 1) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf(" ") != -1) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("zipmeil.com") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("hotmeil.com") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf(".@") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("@.") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf(".com.br.") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("/") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("[") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("]") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("(") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf(")") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
		if(document.forms[0][nomeCampo].value.indexOf("..") > 0) {
			alert("E-mail incorreto.");
			document.forms[0][nomeCampo].focus();
			document.forms[0][nomeCampo].select();
			return false;
		}
	}
		return true;
}


// FUNCÕES PARA VALIDAR CNPJ
//--------------------------------------------
function TESTACNPJ(nomeCampo) {
	numCnpj = document.forms[0][nomeCampo].value;
	if (numCnpj.length == 0){
		return true;
	} else {
		if (numCnpj.length < 18){
			alert("Digite o CNPJ no formato correto - 00.000.000/0000-00");
			document.forms[0][nomeCampo].select();
			return true;
		}
	}
	if (numCnpj < 1){
		return true;
	}
	if(VerifyCNPJ(numCnpj) == 1) {
		return true
	} else {
		alert("O CNPJ digitado não é válido ou está vazio.");
		document.forms[0][nomeCampo].select();
	}
	return;
}
function isNUMB(c) {
	if((cx=c.indexOf(","))!=-1) {
		c = c.substring(0,cx)+"."+c.substring(cx+1);
	}
	if((parseFloat(c) / c != 1)) {
		if(parseFloat(c) * c == 0) {
			 return(1);
		} else {
			return(0);
		}
	} else {
		return(1);
	}
}

function LIMP(c)  {
	while((cx=c.indexOf("-"))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("/"))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(","))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("."))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("("))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(")"))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(" "))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	return(c);
}

function VerifyCNPJ(CNPJ){
	CNPJ = LIMP(CNPJ);
	if(isNUMB(CNPJ) != 1) {
		return(0);
	} else {
		if(CNPJ == 0) {
			return(0);
		} else {
			g=CNPJ.length-2;
			if(RealTestaCNPJ(CNPJ,g) == 1) {
				g=CNPJ.length-1;
				if(RealTestaCNPJ(CNPJ,g) == 1) {
					return(1);
				} else {
					return(0);
				}
		    } else {
				return(0);
			}
		}
	}
}

function RealTestaCNPJ(CNPJ,g) {
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--) {
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8) {
			ind=2;
		} else {
			ind++;
		}
	}
	VerCNPJ%=11;
	if(VerCNPJ==0 || VerCNPJ==1) {
		VerCNPJ=0;
	} else {
		VerCNPJ=11-VerCNPJ;
	}
	if(VerCNPJ!=parseInt(CNPJ.charAt(g))) {
		return(0);
	} else {
		return(1);
	}
}


// VALIDA CPF
// --------------------------------------------------------------
function limpaCPF(c) {
	while((cx=c.indexOf("-"))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("/"))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(","))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("."))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf("("))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(")"))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	while((cx=c.indexOf(" "))!=-1) {
		c = c.substring(0,cx)+c.substring(cx+1);
	}
	return(c);
}
 
function validaCPF(nomeCampo) {
	var i;
	s = document.forms[0][nomeCampo].value;
	s = limpaCPF(s);
	if (s.length > 1) {
		if (s==11111111111 || s==22222222222 || s==33333333333 || s==44444444444 || 
			s==55555555555 || s==66666666666 || s==77777777777 || s==88888888888 || 
			s==99999999999 || s==00000000000) {
			alert("CPF Invalido");
			document.forms[0][nomeCampo].select();
			return false;
		}
	}
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(10-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1) {
		alert("CPF Invalido");
		document.forms[0][nomeCampo].select();
		return false;
	}
	d1 *= 2;
	for (i = 0; i < 9; i++) {
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)	{
		alert("CPF Invalido");
		document.forms[0][nomeCampo].select();
		return false;
	}
	return true;
}

// FUNCAO PARA VALIDAR DATA
// ----------------------------------------------------------------
function validaData(nomeCampo){
	time = new Date();
	if (document.forms[0][nomeCampo].value.length < 10 && document.forms[0][nomeCampo].value.length >= 1){
		document.forms[0][nomeCampo].value="";
		alert("Data inválida.");
		document.forms[0][nomeCampo].select();
	}

	dia = document.forms[0][nomeCampo].value.substring(0,2);
	mes = document.forms[0][nomeCampo].value.substring(3,5);
	ano = document.forms[0][nomeCampo].value.substring(6);

	var v_dia;
	var v_mes;
	var v_ano;
	v_dia = dia;
	v_mes = mes;
	v_ano = ano;

	if (v_dia.length < 2) {
		return(false);
	}
	if (v_mes.length < 2) {
		return(false);
	}
	if (v_ano.length < 4) {
		return(false);
	}

	if (((v_ano < 1900) || (v_ano > time.getYear())) && (v_ano.length != 0)) {
		document.forms[0][nomeCampo].select();
		alert("Data inválida.");
		document.forms[0][nomeCampo].focus();
		return(false);
	}
	if (v_dia > 31) {
		document.forms[0][nomeCampo].select();
		alert("Data inválida.");
		document.forms[0][nomeCampo].focus();
		return(false);
	}
	if (v_mes > 12) {
		document.forms[0][nomeCampo].select();
		alert("Data inválida.");
		document.forms[0][nomeCampo].focus();
		return(false);
	}
	if (v_dia == "31") {
		if ((v_mes == "04") || (v_mes == "06") || (v_mes == "09") || (v_mes == "11")) {
			return(false);
		}
	}
	if (v_mes == "02") {
		if (!(v_ano%4)) {
			if (v_dia > 29) {
				document.forms[0][nomeCampo].select();
				alert("Data inválida.");
				document.forms[0][nomeCampo].focus();
				return(false);
		    }
		} else if (v_dia > 28) {
			document.forms[0][nomeCampo].select();
			alert("Data inválida.");
			document.forms[0][nomeCampo].focus();
			return(false);
		}
	}
  
  //o -if- abaixo testa se algum campo foi preenchido e outro deixado em branco deixando a data incompleta

	if (((v_dia != "") || (v_mes != "") || (v_ano != "")) && ((v_dia == "") || (v_mes == "") || (v_ano == ""))) {
		return(false);
	}
	return(true);
}


// FUNCÕES PARA VALIDAR CAMPOS DEPESQUISA DE ENTIDADES COMPONENTES
//-------------------------------------------------------------------
function validaFrmPesqEntComponentes(pag){
	if (frmGeral.cnpjPesq.value=="" && frmGeral.razaosocialPesq.value=="" && frmGeral.regancinePesq.value=="") {
		window.alert("Escolha e preencha uma forma de pesquisa.");
	} else {
		window.open("/protRegistro/jsp/cadastro/EntidadesEncontradas.jsp?pag="+pag+"","entidadesEncontradas","width=580,height=150")
	}	
}

function validaFrmPesqEntComponentes1(pag){
	if (frmCadDeEmp.cnpjPesq.value=="" && frmCadDeEmp.razaosocialPesq.value=="" && frmCadDeEmp.regancinePesq.value=="") {
		window.alert("Escolha e preencha uma forma de pesquisa.");
	} else {
		window.open("/protRegistro/jsp/cadastro/EntidadesEncontradas.jsp?pag="+pag+"","entidadesEncontradas","width=580,height=150")
	}	
}

function validaFrmPesqEntComponentes2(pag){
	if (frmGeralGrupo.cnpjPesq.value=="" && frmGeralGrupo.razaosocialPesq.value=="" && frmGeralGrupo.regancinePesq.value=="") {
		window.alert("Escolha e preencha uma forma de pesquisa.");
	} else {
		window.open("/protRegistro/jsp/cadastro/EntidadesEncontradas.jsp?pag="+pag+"","entidadesEncontradas","width=580,height=150")
	}	
}

function validaFrmPesqEntComponentesComplexo(pag){
	if (frmGeralGrupo.cnpjPesq.value=="" && frmGeralGrupo.razaosocialPesq.value=="" && frmGeralGrupo.regancinePesq.value=="") {
		window.alert("Escolha e preencha uma forma de pesquisa.");
	} else {
		window.open("/protRegistro/jsp/cadastro/EntidadesEncontradas.jsp?pag="+pag+"","entidadesEncontradas","width=580,height=150")
	}	
}

function getElement(id){
	if(document.all){
		return document.all(id)
	}else{
		return document.getElementById(id)
	}
}

var primeiraTecla = false;
var shift = false;
function formatarValor(valor,tammax,pattern,teclapres) {
	var tecla = teclapres.keyCode;

	if(tecla == 16){
		shift = true
	}

	if(!shift){
	if(primeiraTecla && ((tecla>=48 && tecla<=57) || (tecla>=96 && tecla<=105) || tecla==8 || tecla==46)){
			valor.value = '';
			primeiraTecla = false;
	}

	if(pattern.indexOf("%")>-1){
		pattern = pattern.substr(0,pattern.length-2);
	}
	numCasasDecimais = pattern.replace(".", "").length-1

	valorSemFormato = valor.value;
	valorSemFormato = replaceAll(valorSemFormato, ",", "");
	valorSemFormato = replaceAll(valorSemFormato, ".", "");
	valorSemFormato = replaceAll(valorSemFormato, "%", "");

	do{
		esquerda = valorSemFormato.substr(0,1);
		if(esquerda == '0'){
			valorSemFormato = valorSemFormato.substr(1, valorSemFormato.length);
		}
	}while(esquerda == '0');

	if(valorSemFormato==0){
		valorSemFormato = ""
		valor.value = ""
	}

	tamanho = valorSemFormato.length;

	if (tecla == 46) {
		valor.value = valor.value;
	}

	if (tamanho < tammax && tecla != 8) {
		tamanho = valorSemFormato.length + 1;

	}if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) {
		if (tecla == 8) {
			tamanho = tamanho - 1;
		}

		if(numCasasDecimais>0){
			if (tamanho <= numCasasDecimais) {
				valor.value = valorSemFormato;
			}else if ((tamanho > numCasasDecimais) && (tamanho <= 3+numCasasDecimais)) {
				valor.value = valorSemFormato.substr(0, tamanho - numCasasDecimais) + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 4+numCasasDecimais) && (tamanho <= 6+numCasasDecimais)) {
				valor.value = valorSemFormato.substr(0, tamanho - 3 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3) + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 7+numCasasDecimais) && (tamanho <= 9+numCasasDecimais)) {
				valor.value = valorSemFormato.substr(0, tamanho - 6 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3) + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 10+numCasasDecimais) && (tamanho <= 12+numCasasDecimais)) {
				valor.value = valorSemFormato.substr(0, tamanho - 9 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 9 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3)  + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 13+numCasasDecimais) && (tamanho <= 15+numCasasDecimais)) {
				valor.value = valorSemFormato.substr(0, tamanho - 12 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 12 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 9 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3)  + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);
			}else if ((tamanho >= 16+numCasasDecimais) && (tamanho <= 18+numCasasDecimais)) {
				valor.value = valorSemFormato.substr(0, tamanho - 15 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 15 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 12 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 9 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3)  + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);
			}
		}
		else{
			if ((tamanho > 0) && (tamanho <= 3)) {
				valor.value = valorSemFormato.substr(0, tamanho+1);

			}else if ((tamanho >= 4) && (tamanho <= 6)) {
				valor.value = valorSemFormato.substr(0, tamanho - 3) + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho)

			}else if ((tamanho >= 7) && (tamanho <= 9)) {
				valor.value = valorSemFormato.substr(0, tamanho - 6) + '.' +
							valorSemFormato.substr(tamanho - 6, 3) + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho)

			}else if ((tamanho >= 10) && (tamanho <= 12)) {
				valor.value = valorSemFormato.substr(0, tamanho - 9) + '.' +
							valorSemFormato.substr(tamanho - 9, 3) + '.' +
							valorSemFormato.substr(tamanho - 6, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho);

			}else if ((tamanho >= 13) && (tamanho <= 15)) {
				valor.value = valorSemFormato.substr(0, tamanho - 12) + '.' +
							valorSemFormato.substr(tamanho - 12, 3) + '.' +
							valorSemFormato.substr(tamanho - 9, 3) + '.' +
							valorSemFormato.substr(tamanho - 6, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho);
			}else if ((tamanho >= 16) && (tamanho <= 18)) {
				valor.value = valorSemFormato.substr(0, tamanho - 15) + '.' +
							valorSemFormato.substr(tamanho - 15, 3) + '.' +
							valorSemFormato.substr(tamanho - 12, 3) + '.' +
							valorSemFormato.substr(tamanho - 9, 3) + '.' +
							valorSemFormato.substr(tamanho - 6, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho);
			}
		}
	}
	}
}

function formatarDecimal(campo, pattern) {
	if(campo.value==''){
		campo.value = '0';
	}

	if(pattern.indexOf("%")>-1){
	pattern = pattern.substr(0,pattern.length-2);
	}
	numCasasDecimais = pattern.replace(".", "").length-1

	if(numCasasDecimais == 0){
		return;
	}
	if(numCasasDecimais == 1){
		if (campo.value.length == 1) {
			campo.value = '0,' + campo.value;
		}
	}
	if(numCasasDecimais == 2){
		if (campo.value.length == 2) {
			campo.value = '0,' + campo.value;
		}else if (campo.value.length == 1)  {
			campo.value = '0,0' + campo.value;
		}
	}
	if(numCasasDecimais == 3){
		if (campo.value.length == 3) {
			campo.value = '0,' + campo.value;
		}else if (campo.value.length == 2)  {
			campo.value = '0,0' + campo.value;
		}else if (campo.value.length == 1)  {
			campo.value = '0,00' + campo.value;
		}
	}
	if(numCasasDecimais == 4){
		if (campo.value.length == 4) {
			campo.value = '0,' + campo.value;
		}else if (campo.value.length == 3)  {
			campo.value = '0,0' + campo.value;
		}else if (campo.value.length == 2)  {
			campo.value = '0,00' + campo.value;
		}else if (campo.value.length == 1)  {
			campo.value = '0,000' + campo.value;
		}
	}
}

function filtrar(campo, tipo, evt) {
	// tipo 1 = numerico
	// tipo 2 = alfabetico sem acentos
	// tipo 3 = email
    // tipo 4 =  conta
    // tipo 5 =  ALFA SEM ACENTOS E NÚMEROS    
    
    	var chars = '';
	var charsNumero = "0123456789";
	var charsAlfaSemAcentoMaiusculo = "QAZWSXEDCRFVTGBYHNUJMIKOLP";
	var charsAlfaSemAcentoMinusculo = "qazwsxedcrfvtgbyhnujmikolp";
	var charsAlfaComAcentoMinusculo = "áàãâäéèêëíìîïôõóòöúùûüç";
	var charsAlfaComAcentoMaiusculo = "ÁÀÃÂÄÉÈÊËÍÌÎÏÔÕÓÒÖÚÙÛÜÇ";
	var charsSpaces = " "
    
	var nTecla;
	evt = (evt) ? evt : (window.event) ? window.event : ""

	if(evt.which) {
		nTecla = evt.which;
	}else {
		nTecla = evt.keyCode;
	}

	switch(tipo)
	{
		case 1:
			if(nTecla!=39 && nTecla>31 && (nTecla<48 || nTecla>57))
			  
				return false;
				break;
		case 2:
			if(nTecla>31 && (nTecla<65 || nTecla>122) )
				return false;
				break;
	   case 3: 
	       if(!(nTecla==46 || nTecla==8 || nTecla==37 || nTecla==39 || (nTecla>=48 && nTecla<=57) || (nTecla>=64 && nTecla<=90) || (nTecla>=97 && nTecla<=122)))
				return false;
				break;
       case 4:
		   if(nTecla>31 && ((nTecla<48 || nTecla>57) && (nTecla!=120 && nTecla!=88 )))
		    	return false;
		  	    break;
       case 5:
       		   var c = charFromCharCode(nTecla);
       	       chars = charsNumero + charsAlfaSemAcentoMaiusculo + charsAlfaSemAcentoMinusculo;
     	       
       		   if(chars.indexOf(c)==-1){
 		    		return false;
		    	}
		  	    break;
	}
	return true;
}



function replaceAll(sValue, strOld, strNew){
	var indexof = sValue.indexOf(strOld);

	while(indexof > -1){
		sValue = sValue.replace( strOld, strNew );
		indexof = sValue.indexOf(strOld);
	}
	return sValue;
}

function testaShift(evt){
	var tecla = evt.keyCode;

	if(tecla == 16){
		shift = false;
	}
}

function formatarData(campo, evt)
{
	if(evt.keyCode>31 && (campo.value.length==2 || campo.value.length==5))
		campo.value = campo.value + "/";

	return true;
}

function isDate(dia, mes, ano){
	d = new Date(ano,mes-1,dia)

	if(d.getDate() != dia || (d.getMonth()+1) != mes || d.getFullYear() != ano){
		return false;
	}

	return true;
}

function obtemValue(value){
	if(value!=''){
		return 1.0*limpaValor(value);
	}
	else{
		return 0;
	}
}

function limpaValor(valor){
	valorSemFormato = valor;
	valorSemFormato = replaceAll(valorSemFormato, ".", "" );
	valorSemFormato = replaceAll(valorSemFormato, ",", "." );
	valorSemFormato = replaceAll(valorSemFormato, "%", "" );

	return 1.0*valorSemFormato;
}

function formataValor(valor, pattern){
	if(isNaN(valor) || !isFinite(valor)){
		valor = 0;
	}

	porcentagem = false
	if(pattern.indexOf("%")>-1){
		pattern = pattern.substr(0,pattern.length-1);
		porcentagem = true
	}

	numCasasDecimais = pattern.replace(".", "").length-1

	if(numCasasDecimais==0){
		valor = Math.round(valor);
	}
	else if(numCasasDecimais==1){
		valor = Math.round(valor*10)/10;
	}
	else if(numCasasDecimais==2){
		valor = Math.round(valor*100)/100;
	}
	else if(numCasasDecimais==3){
		valor = Math.round(valor*1000)/1000;
	}
	else if(numCasasDecimais==4){
		valor = Math.round(valor*10000)/10000;
	}


	valorSemFormato = "" + valor;

	if(valorSemFormato.indexOf(".")==-1){
		for(k=0;k<numCasasDecimais;k++){
			valorSemFormato = valorSemFormato + "0"
		}
	}
	else{
		var tamanhoDecimais = valorSemFormato.length - valorSemFormato.indexOf(".") - 1;

		for(z=0;z<numCasasDecimais-tamanhoDecimais;z++){
			valorSemFormato = valorSemFormato + "0"
		}
	}

	var negativo = false;
	if(valorSemFormato.indexOf("-")==0){
		negativo = true;
		valorSemFormato = valorSemFormato.substr(1, valorSemFormato.length-1)
	}

	if(valorSemFormato.indexOf(".")<0){
		if(valorSemFormato=="0"){
			if(numCasasDecimais==1){
				valorSemFormato = "0.0";
			}
			else if(numCasasDecimais==2){
				valorSemFormato = "0.00";
			}
			else if(numCasasDecimais==3){
				valorSemFormato = "0.000";
			}
			else if(numCasasDecimais==4){
				valorSemFormato = "0.0000";
			}
		}
	}
	else if(valorSemFormato.indexOf(".")==valorSemFormato.length-2){
//		valorSemFormato = valorSemFormato + "0"
	}

	valorSemFormato = valorSemFormato.replace(".", "");
	tamanho = valorSemFormato.length

	var valorFormatado = '';

		//alert(numCasasDecimais + ' - ' + valorSemFormato)

		if(numCasasDecimais>0){
			if (tamanho <= numCasasDecimais) {
				valorFormatado = valorSemFormato;
			}else if ((tamanho > numCasasDecimais) && (tamanho <= 3+numCasasDecimais)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - numCasasDecimais) + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 4+numCasasDecimais) && (tamanho <= 6+numCasasDecimais)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 3 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3) + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 7+numCasasDecimais) && (tamanho <= 9+numCasasDecimais)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 6 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3) + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 10+numCasasDecimais) && (tamanho <= 12+numCasasDecimais)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 9 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 9 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3)  + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);

			}else if ((tamanho >= 13+numCasasDecimais) && (tamanho <= 15+numCasasDecimais)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 12 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 12 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 9 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3)  + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);
			}else if ((tamanho >= 16+numCasasDecimais) && (tamanho <= 18+numCasasDecimais)) {
				valor.value = valorSemFormato.substr(0, tamanho - 15 - numCasasDecimais) + '.' +
							valorSemFormato.substr(tamanho - 15 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 12 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 9 - numCasasDecimais, 3) + '.' +
							valorSemFormato.substr(tamanho - 6 - numCasasDecimais, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3 - numCasasDecimais, 3)  + ',' +
							valorSemFormato.substr(tamanho - numCasasDecimais, tamanho);
			}
		}
		else{
			if ((tamanho > 0) && (tamanho <= 3)) {
				valorFormatado = valorSemFormato.substr(0, tamanho+1);

			}else if ((tamanho >= 4) && (tamanho <= 6)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 3) + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho)

			}else if ((tamanho >= 7) && (tamanho <= 9)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 6) + '.' +
							valorSemFormato.substr(tamanho - 6, 3) + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho)

			}else if ((tamanho >= 10) && (tamanho <= 12)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 9) + '.' +
							valorSemFormato.substr(tamanho - 9, 3) + '.' +
							valorSemFormato.substr(tamanho - 6, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho);

			}else if ((tamanho >= 13) && (tamanho <= 15)) {
				valorFormatado = valorSemFormato.substr(0, tamanho - 12) + '.' +
							valorSemFormato.substr(tamanho - 12, 3) + '.' +
							valorSemFormato.substr(tamanho - 9, 3) + '.' +
							valorSemFormato.substr(tamanho - 6, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho);
			}else if ((tamanho >= 16) && (tamanho <= 18)) {
				valor.value = valorSemFormato.substr(0, tamanho - 15) + '.' +
							valorSemFormato.substr(tamanho - 15, 3) + '.' +
							valorSemFormato.substr(tamanho - 12, 3) + '.' +
							valorSemFormato.substr(tamanho - 9, 3) + '.' +
							valorSemFormato.substr(tamanho - 6, 3)  + '.' +
							valorSemFormato.substr(tamanho - 3, tamanho);
			}
		}

	if(negativo){
		valorFormatado = "-" + valorFormatado;
	}

	return valorFormatado;
}

function truncValor(valor, numCasasDecimais){
	var multi = Math.pow(10, numCasasDecimais);
	return (Math.floor(valor*multi))/multi;
}

function diffDiasDataAtual(date1){
	var dtAtualHoras = new Date();
	var diff = date1-(new Date(dtAtualHoras.getYear(), dtAtualHoras.getMonth(), dtAtualHoras.getDate()));

	var dias = Math.floor(diff/86400000)

	return dias;
}


function gerarDigitoVerificadorBB(configuracao) {

                var pesos = [ 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9 ];
                if(configuracao.length!=16)
                {
                  alert("Formato da configuração errado. Deve ter 16 caracteres");
                  return -1;
                }



			   		var values = [ ];
			   		for ( i = 0; i < 16; i++) {
			   	 		values[i] = configuracao.charAt(i);
			   		}

			   		soma = 0;
			   		for ( i = 0; i < values.length; i++) {
			   			soma += pesos[i] * values[i];
			   		}
                 	digito = soma % 11;
			   		if (digito=="10") {
			   			digito = "X";
			   		}

		    return digito;

}

function rtrim(mvar)
{
  if (mvar.substring(mvar.length-1,mvar.length)==" ") mvar = rtrim(mvar.substring(0,mvar.length-1));
  return mvar;
}

function ltrim(mvar)
{
    if (mvar.substring(0,1)== " ") mvar = ltrim(mvar.substring(1,mvar.length));
    return mvar;
}

function trim(mvar)
{
   return rtrim(ltrim(mvar));
}

function charFromCharCode(charCode) {
	return unescape('%' + charCode.toString(16));
}


