function aceitaNumero(e, decimal)
{
	if (decimal == null)
		decimal = false
		
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;

	if (tecla==8 || tecla==9 || tecla==16) {
		return;
	}else if (tecla>=35 && tecla<=40){
		return;
	}
	if(decimal && (tecla==44 || tecla==46)){
		return true;
	}
	if (tecla > 47 && tecla < 58) // numeros de 0 a 9
		return true;
	else{
		if (tecla != 8) // backspace
			event.keyCode = 0;
		else
			return true;
	}
}
//Função que abre popup
function popup(destino, nome,a,l,c,t)
{
 
 open(destino, nome, "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,menubar=no,width=l,height=a,left=c,top=t")

}
//Função que abre popup com scrollbars
function popup2(destino, nome,l,a,c,t)
{
 
 open(destino, nome, "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,menubar=no,width="+l+",height="+a+",left=c,top=t")

}

//Função que abre por meio de link o div oculto da página
function OnOff(nome)
{
if (document.all[nome].style.display == "none") 
	document.all[nome].style.display = ""
else
	document.all[nome].style.display = "none"
}

//impede a digitação caracteres de campos numéricos
//caso queira . e , colocar as opçoes  || (tecla == 44) || (tecla == 46)
function inibe_char(event)
	{
		tecla = event.which;
		if (tecla == null)
		   tecla = event.keyCode;
		if ((tecla >= 48 && tecla <= 57))
		   return true;
		return false;
	}

//retorna true se ok
function valida_email(email) {
  var objRegExp  = /^[A-Za-z]([\w\.]*)@([A-Za-z0-9\.]*)\.(([A-Za-z]{3}\.[A-Za-z]{2}$)|([A-Za-z]{3}$)|([a-z]{2}$))/i ;
  return objRegExp.test(email);
}

//retira caracteres de campos numéricos
function retira_char(campo, teclapres)
{
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	tam = vr.length;
	if (isNaN(vr.substr( tam - 1,tam )))
		document.form[campo].value = vr.substr( 0, tam - 1 )

}

function valido_periodo(periodo){

	periodo_valido = true

	if (periodo == ""){
		 return(true)
	}
 
        mes = periodo.substring(0,2)
        //ano = periodo.substring(2,6)


        if ( periodo.length == 4)
        {
                if(periodo.substring(2,4) < 30)
			ano = "20" + periodo.substring(2,4)
		else
			ano = "19" + periodo.substring(2,4)
        }
        else
                ano = periodo.substring(2,6)

	if (periodo.length > 6){
		 periodo_valido = false
	}

	if (isNaN(mes)){
		periodo_valido = false }

	if (mes.length < 1 || mes.length > 2 ){ 
		periodo_valido = false }
 
	if (isNaN(ano)){
 		periodo_valido = false }
 
	if (ano.length < 2 || ano.length > 4){ 
 		periodo_valido = false } 

	if (mes < 1 || mes > 12 || ano < 1931 ||ano > 2030){
		periodo_valido = false }

	return(periodo_valido)
}


function valida_placa(placa)
{
	if(placa.length == 7)
	{
		 if(!isNaN(placa.substring(4,4)) && isNaN(placa.substring(1,1)) && isNaN(placa.substring(2,1)) && isNaN(placa.substring(3,1)))
			return(true)
	}
	return(false)	

}

function valida_data(Data){

	Data_valida = true

	if (Data == ""){
		 return(true)
	}
 
        dia = Data.substring(0,2)
        mes = Data.substring(2,4)


        if ( Data.length == 6)
        {
                if(Data.substring(4,6) < 30)
			ano = "20" + Data.substring(4,6)
		else
			ano = "19" + Data.substring(4,6)
        }
        else
                ano = Data.substring(4,8)

	if (Data.length > 10){
		 Data_valida = false
	}

	if (isNaN(dia)){
 		Data_valida = false  }

	if (dia.length < 1 || dia.length > 2){ 
 		Data_valida = false }
 
	if (isNaN(mes)){
		Data_valida = false }

	if (mes.length < 1 || mes.length > 2 ){ 
		Data_valida = false }
 
	if (isNaN(ano)){
 		Data_valida = false }
 
	if (ano.length < 2 || ano.length > 4){ 
 		Data_valida = false } 

	if (dia < 1 || dia > 31 || mes < 1 || mes > 12 || ano < 1931 ||ano > 2030){
		Data_valida = false }

	if ( mes == 4 || mes == 6 || mes == 9 || mes == 11){
		if(dia> 30) Data_valida = false
	}

	if ( mes == 2 && ( ano == 2004 || ano == 2008 || ano == 2012)){
		if(dia> 29){ 
			Data_valida = false}
	}
	else
	{
		if ( mes == 2){
			if(dia> 28){ Data_valida = false}
		}
	}

	return(Data_valida)
}

function verifica_data(campo)
{
    if(document.form[campo].value != "")
    {
	document.form[campo].value = tira_char(document.form[campo].value,"/")
        if (!valida_data(document.form[campo].value))
        {
            // DATA INVÁLIDA.
            alert("Data Inválida")
            document.form[campo].value = ""   //MOVE PARA O CONTROLE.
            document.form[campo].focus()
        }
        else
        {
            // FORMATA A DATA.
            Dia = document.form[campo].value.substring(0,2)
            mes = document.form[campo].value.substring(2,4)
            if (document.form[campo].value.length == 6)
            {
                if(document.form[campo].value.substring(4,6) < 30)
					ANO = "20" + document.form[campo].value.substring(4,6)
				else
					ANO = "19" + document.form[campo].value.substring(4,6)
            }
            else
                ANO = document.form[campo].value.substring(4,8)
            
            document.form[campo].value = Dia + "/" + mes + "/" + ANO
        }
    }

}

function VerificaIE(num)
{    
    var i, j, soma
    soma = 0
    if (num.length == 0) return(false)
    
	if (num.length != 0 && num.length == 9)
	{
   		j = 9
		//faz o somatório
	    for (i = 1;i < num.length;i++)
		{
	        soma = soma + parseInt(num.substr(i-1,1)) * j
		    j = j - 1
	    }
        soma = soma % 11
        if(soma > 1)
	            soma = 11 - soma
        else
	            soma = 0


	//faz a comparação  
	if (parseInt(num.substr(8,1)) != parseInt(soma))
            return(false)
	else
            return(true)
	}
	else
	{
	       return(false)
		exit()
	}
}



function FormataValor2() {
	vr = document.frmbarra[campo].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

			
	if ( tam <= 2 ){ 
		document.frmbarra[campo].value = vr ; }
	if ( (tam > 2) && (tam <= 5) ){
		document.frmbarra[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
		document.frmbarra[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 9) && (tam <= 11) ){
		document.frmbarra[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) ){
		document.frmbarra[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 <= 17) ){
		document.frmbarra[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 ) ;}
	
	
}


function limpaCampo(campo)
{
	if (document.form[campo].value == 0)
		document.form[campo].value = ""

}


function troca_simples(nome,char_at,char_novo)
{
  var var_aux; //Cria variável auxiliar
  tam = nome.length; //Tamanho do texto
  var_aux = "";
	for(i = 0; i<tam;i++)
	{
		if (nome.substring(i,i+1) == char_at)
		{  
			var_aux = var_aux + char_novo;  //se existir substitui por acento
		}
		else
		{
			var_aux = var_aux + nome.substring(i,i+1)
		}
		
	}
  return(var_aux) 
}

function tira_char(nome,caracter)
{
  var var_aux; //Cria variável auxiliar
  tam = nome.length; //Tamanho do texto
  var_aux = "";
	for(i = 0; i<tam;i++)
	{
		if (nome.substring(i,i+1) == caracter)
		{  
			var_aux = var_aux;  //se existir substitui por acento
		}
		else
		{
			var_aux = var_aux + nome.substring(i,i+1)
		}
		
	}
  return(var_aux) 
}


function FormataDado(campo,tammax,pos,teclapres){
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( "-", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length ;

	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

	if (tecla == 8 ){ tam = tam - 1 ; }
			
	if ( tecla == 8 || tecla == 88 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		if ( tam <= 2 ){
	 		document.form[campo].value = vr ;}
		if ( tam > pos && tam <= tammax ){
			document.form[campo].value = vr.substr( 0, tam - pos ) + '-' + vr.substr( tam - pos, tam );}
	}
}

function FormataValor_estatico(campo) {
	vr = document.form[campo].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	tam = vr.length;

	 tam = vr.length + 1 ; 

	
		if (tam  == 0) document.form[campo].value = '';
		if (tam  == 1) document.form[campo].value = '0,0' + vr;
		if ( tam <= 2 ){ 
	 		document.form[campo].value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		document.form[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		document.form[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) ){
	 		document.form[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 <= 17) ){
	 		document.form[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 ) ;}

	
}

function FormataValor(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	if (whichCode == 13) return true;  // Enter
	
	key = String.fromCharCode(whichCode);  // Get key value from key code
	
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
			aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
			aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

function SaltaCampo (campo,prox,tammax,teclapres){
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108 ){
		document.form[campo].value = vr.substr( 0, vr.length - 1 ); }
	else{
	 	vr = vr.replace( "-", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( ",", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	tam = vr.length;	


		
	 	if (tecla != 0 && tecla != 9 && tecla != 16 )
			if ( tam == tammax )	
				document.form[prox].focus() ;	}
}

function FormataData(campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			document.form[campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document.form[campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); }
}


function FormataCpf(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;

	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 <= 2 ){ 
	 		document.form[campo].value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		document.form[campo].value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		document.form[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		document.form[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) ){
	 		document.form[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 <= 17) ){
	 		document.form[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 ) ;}
	}		
}

function FormataCgc(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;

	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 <= 2 ){ 
	 		document.form[campo].value = vr ; }
	 	if ( (tam > 2) && (tam <= 6) ){
	 		document.form[campo].value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 7) && (tam <= 9) ){
	 		document.form[campo].value = vr.substr( 0, tam - 6 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 10) && (tam <= 12) ){
	 		document.form[campo].value = vr.substr( 0, tam - 9 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 13) && (tam <= 14) ){
	 		document.form[campo].value = vr.substr( 0, tam - 12 ) + '.' + vr.substr( tam - 12, 3 ) + '.' + vr.substr( tam - 9, 3 ) + '/' + vr.substr( tam - 6, 4 ) + '-' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		document.form[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 ) ;}
	}		
}

function vazio(str)
{
	if (str == "")
		return true;
	else
		return false;
}
//verifica se alguma opçao diferente do selecionar foi selecionado
function naoselecionado(str)
{
	if (str == "none")
		return true;
	else
		return false;
}

//função para validar CNPJ-CGC ou CPF
function Valida_cnpj_cpf(msCPF_CGC){
	//alert(msCPF_CGC)
	if (!(checa(msCPF_CGC))){
		return false;
	}
	else{ 
		return true; 
	}
}

function checa(msCPF_CGC){
	if ((msCPF_CGC.length != 14) && (msCPF_CGC.length !=11)){
		return false;
	}
			  
	if ((!(modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 2)).toString()+modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 1)).toString() == msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))) && (modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 2)) + "" + modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 1)) != msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))){
		return false;
	}
	return true;
}

function modulo(msCPF_CGC){
	soma=0;
	ind=2;
				
	for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
		soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
		ind++;
			      
		if(msCPF_CGC.length>11){ 
			if(ind>9) ind=2; 
        }
	}
				
	resto = soma - (Math.floor(soma / 11) * 11);
					 
	if(resto < 2){ 
		return 0; 
	}
	else{ 
		return (11 - resto); 
	}
}

function modulo_cic(msCPF_CGC){
   	soma=0;
	ind=2;

	for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
		 soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
		 ind++;
					 
		 if(msCPF_CGC.length>11){	
		    if(ind>9) ind=2; 
		 }
	}				
				
	resto = soma - (Math.floor(soma / 11) * 11);
					  
	if(resto < 2){	
		return 0; 
	}
	else{ 
		return 11 - resto;
	}
}
