function ltrim ( s )
{
	return s.replace( /^\s*/, "" )
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}

function isbetween ( s, min, max )
{
	if ( isNaN ( s ) )
		return islenbetween ( s, min, max );
	else
		return isnumbetween ( s, min, max );
}

function isnumbetween ( s, min, max )
{
	return ( ( s >= ( min != null ? min : Number.MIN_VALUE ) )
		&&  ( s <= ( max != null ? max : Number.MAX_VALUE ) ) );
}

function islenbetween ( s, min, max )
{
	return ( ( s.length >= ( min != null ? min : 0 ) )
		&&  ( s.length <= ( max != null ? max : Number.MAX_VALUE ) ) );
}

function notyet ()
{
	window.alert ( "Sorry, this function has not been implemented yet!" );
	return false;
}

function helpWindow ( url )
{
	return window.open ( url, "myhelp", "dependent=yes,height=320,resizable=yes,scrollbars=yes,width=440" );
}

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (dec == null) dec ="";

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;
if (key == 44){
	if (window.event)
   		window.event.keyCode = 46;
	else if (e)
   		e.which =  46;
	return true
}
// numbers
else if ((("0123456789"+dec).indexOf(keychar) > -1))
   return true;

// decimal point jump
else
   return false;
}

function submitForm(e)
{
var key;
var keychar;


if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if (key==13)
   return true;
else
   return false;
}

/* 
 *
 *
 * */

function soNumero(myfield,e)
{
    return numbersonly(myfield, e);
}

function limitarCampo(myfield,qte)
{
   ret = (myfield.value.length < qte)? true: false;
   
   if(ret==false)
       alert("Você excedeu o limite máximo de " + qte + " caracteres deste campo.");
   
   return ret;
}

function habilita(campo,opcao)
{
  campo.disabled = opcao;
}

function charFilter(e, filtro)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;
if (key == 44){
	if (window.event)
   		window.event.keyCode = 46;
	else if (e)
   		e.which =  46;
	return true
}
// numbers
else if ((filtro.indexOf(keychar) == -1))
   return true;

// decimal point jump
else
   return false;
}

function taNoArray(arr,cod)
{
	for (i=0; i<arr.length; i++)
   if (arr[i] == cod)
     return true;

 return false;
}


		/*
		
		tipos:
			d = data  'yyyymmdd'    retorno: dd/mm/yyyy
			n = nire  11122233334   retorno: '111.222.3333-4'   
			c = cnpj  17436239000101   retorno:   '17.436.239/0001-01'
			f = cpf   ''
			e = cep   '00000000'   retorno: 00000-000
			t = telefone   '00000000'   retorno: 0000-0000
			a = atividade  '22.22'       --- mudou para  2222-2/22
			s = sub-ativ   '33.22.11'
			u = suframa  '33.22.11'
			l = entidade sindical '111.111.11112-2'
			m = dinheiro
		    b = agencia Banco do Brasil 1234-1
		*/				
//		teste:
//<form>
//		<input type="hidden" name="teste_2" value="1234567890123">
//		<input type="text" name="teste" 
//		  onblur="grava_campo('c',this.value,teste_2);" 
//		  onkeypress="mantem_formato('c',this.value,this,window.event); return soNumero(this,window.event);">
//		  
//		<input type="button" name="teste_2button" value="enviar" onClick="mostra(teste_2,teste);">	
//		<script>
//			formata_campo('c',document.forms[0].teste_2.value,document.forms[0].teste);
//		</script>
//</form>	
		
		function mantem_formato(tipo,valor,campo,evento)
		{
			var retorno='';

			key = evento.keyCode;
			if ((key==null) || (key==0) || (key==8) ||	(key==9) || (key==13) || (key==27) )
			{	
				if (key==13)
					return false;
				else	
					return true;  
			}

			if (tipo=='t')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==4)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			
			if (tipo=='b')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==4)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			
			if ((tipo=='L') || (tipo=='l'))
			{
				
					if (valor.length==3 || valor.length==7)
					{
						retorno=valor + ".";
						campo.value=retorno;
					}
					if (valor.length==13)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
			}
			if (tipo=='d')
			{
				if (valor.length==2 || valor.length==5)
				{
					retorno=valor + "/";
					campo.value=retorno;
				}
			}
			if (tipo=='D')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==2 || valor.length==5)
					{
						retorno=valor + "/";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			if (tipo=='n')
			{
				if (valor.length==3 || valor.length==7)
				{
					retorno=valor + ".";
					campo.value=retorno;
				}
				if (valor.length==12)
				{
					retorno=valor + "-";
					campo.value=retorno;
				}
			}
			if (tipo=='N')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==3 || valor.length==7)
					{
						retorno=valor + ".";
						campo.value=retorno;
					}
					if (valor.length==12)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			if (tipo=='f')
			{
				if (valor.length==3 || valor.length==7)
				{
					retorno=valor + ".";
					campo.value=retorno;
				}
				if (valor.length==11)
				{
					retorno=valor + "-";
					campo.value=retorno;
				}
			}
			if (tipo=='F')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==3 || valor.length==7)
					{
						retorno=valor + ".";
						campo.value=retorno;
					}
					if (valor.length==11)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			if (tipo=='c')
			{
				if (valor.length==2 || valor.length==6)
				{
					retorno=valor + ".";
					campo.value=retorno;
				}
				if (valor.length==10)
				{
					retorno=valor + "/";
					campo.value=retorno;
				}
				if (valor.length==15)
				{
					retorno=valor + "-";
					campo.value=retorno;
				}
			}
			if (tipo=='C')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==2 || valor.length==6)
					{
						retorno=valor + ".";
						campo.value=retorno;
					}
					if (valor.length==10)
					{
						retorno=valor + "/";
						campo.value=retorno;
					}
					if (valor.length==15)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			if (tipo=='e')
			{
				if (valor.length==5)
				{
					retorno=valor + "-";
					campo.value=retorno;
				}
			}
			if (tipo=='E')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==5)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			if (tipo=='a') {
				if (valor.length==2) {
					retorno=valor + ".";
					campo.value=retorno;
				}
			}
			if (tipo=='A') {
				2222-2/22
				if (numbersonly(campo, evento)==true) {
					if (valor.length==4) {
						retorno=valor + "-";
						campo.value=retorno;
					}
					else if (valor.length==6) {
						retorno=valor + "/";
						campo.value=retorno;
					}
					return true;
				}	
				else{
					return false;
				}	
			}
			if (tipo=='s')
			{
				if (valor.length==2 || valor.length==5)
				{
					retorno=valor + ".";
					campo.value=retorno;
				}
			}
			if (tipo=='S')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==2 || valor.length==5)
					{
						retorno=valor + ".";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			if (tipo=='u')
			{
				if (valor.length==8)
				{
					retorno=valor + "-";
					campo.value=retorno;
				}
			}
			if (tipo=='U')
			{
				if (numbersonly(campo, evento)==true)
				{
					if (valor.length==8)
					{
						retorno=valor + "-";
						campo.value=retorno;
					}
					return true;
				}	
				else
				{
					return false;
				}	
			}
			if (tipo=='m')
			{
				if (currencyFormat(campo, '.', ',', evento)==true)
				{
					return true;
				} else
					return false;
			}
		}
		
		function formata_campo(tipo,valor,campo)
		{
			var retorno='';
						
			if (valor!='')
			{		
			    if (tipo=='t') {
					retorno=valor.substring(0,4) + "-" + valor.substring(4,8);
				}
			    if (tipo=='b') {
					retorno=valor.substring(0,4) + "-" + valor.substring(4,5);
				}
				if (tipo=='l') {
					retorno=valor.substring(0,3) + "." + valor.substring(3,6) + "." + valor.substring(6,11)  + "-" + valor.substring(11,12);
				}
				if (tipo=='d') {
					retorno=valor.substring(6,8) + "/" + valor.substring(4,6) + "/" + valor.substring(0,4);
				}	
				if (tipo=='n') {
					retorno=valor.substring(0,3) + "." + valor.substring(3,6) + "." + valor.substring(6,10) + "-" + valor.substring(10,11);
				}	
				if (tipo=='f') {
					retorno=valor.substring(0,3) + "." + valor.substring(3,6) + "." + valor.substring(6,9) + "-" + valor.substring(9,11) ;
				}
				if (tipo=='c') {
					retorno=valor.substring(0,2) + "." + valor.substring(2,5) + "." + valor.substring(5,8) + "/" + valor.substring(8,12) + "-" + valor.substring(12,14);
				}
				if (tipo=='e') {
					retorno=valor.substring(0,5) + "-" + valor.substring(5,8);
				}	
				if (tipo=='a') {
					retorno=valor.substring(0,2) + "." + valor.substring(2,4);
				}	
				if (tipo=='A') {
					retorno=valor.substring(0,4) + "/" + valor.substring(5,6) + "-" + valor.substring(7,9);
				}	
				if (tipo=='s') {
					retorno=valor.substring(0,2) + "." + valor.substring(2,4) + "." + valor.substring(4,6);
				}	
				if (tipo=='u') {
					retorno=valor.substring(0,8) + "-" + valor.substring(8,9);
				}	
				if (tipo=='m')
				{
				    if(valor.indexOf('.')==-1)
					{
					   valor=valor+'.00';
					}   
				
					retorno=valor.replace('.',',');
					if (retorno.indexOf(',')!=-1)
					{
						var temp = retorno.substring(0, retorno.indexOf(','));
						var temp2 = '';
						var mod = temp.length%3;
						i = 0;
						while(i<temp.length && temp.length > 3)
						{
						   if (mod>0)
							temp2 = temp2 + temp.substring(i, i+mod) + '.';
							
							i = i+mod;
							mod = 3;
						}
						if (temp.length <= 3)
							temp2=temp;
						if (temp2.charAt(temp2.length-1)=='.')
							temp2=temp2.substring(0, (temp2.length-1));
					
						var temp3 = retorno.substring(retorno.indexOf(','), retorno.length);
						if (temp3.length == 2) temp3 = temp3+'0';
						
						retorno = temp2 + temp3;
					}	
				}	
			}		
			campo.value=retorno;
		}


		function grava_campo(tipo,valor,campo)
		{
			var retorno='';
				
			if (tipo=='t')
			{
				retorno=valor.substring(0,4) + valor.substring(5,9);
			}					
			if (tipo=='b')
			{
				retorno=valor.substring(0,4) + valor.substring(5,6);
			}					
            if (tipo=='l')
			{
				retorno=valor.substring(0,3) + valor.substring(4,7) + valor.substring(8,13) + valor.substring(14,15);
			}
			if (tipo=='d')
			{
				retorno=valor.substring(6,10) + valor.substring(3,5) + valor.substring(0,2);
			}
			if (tipo=='n')
			{
				retorno=valor.substring(0,3) + valor.substring(4,7) + valor.substring(8,12)+ valor.substring(13,14);
			}
			if (tipo=='f')
			{
				retorno=valor.substring(0,3) + valor.substring(4,7) + valor.substring(8,11) + valor.substring(12,14) ;
			}
			if (tipo=='c')
			{
				retorno=valor.substring(0,2) + valor.substring(3,6) + valor.substring(7,10) + valor.substring(11,15) + valor.substring(16,18);
			}
			if (tipo=='e')
			{
				retorno=valor.substring(0,5) + valor.substring(6,9);
			}	
			if (tipo=='a')
			{
				retorno=valor.substring(0,4) + valor.substring(5,6) + valor.substring(7,9);
			}	
			if (tipo=='s')
			{
				retorno=valor.substring(0,2) + valor.substring(3,5) + valor.substring(6,8);
			}	
			if (tipo=='u')
			{
				retorno=valor.replace('-','');
			}	
			if (tipo=='m')
			{
				retorno=valor;
				while(retorno.indexOf('.')!= -1) retorno=retorno.replace('.','');
				while(retorno.indexOf(',')!= -1) retorno=retorno.replace(',','.');
			}	

			campo.value=retorno;
		}

		


function fncRetornaGrupo(frm) {
	var retorno='';

	retorno=frm.ramcod.value.substring(0,3);
	frm.ram_cnae_guia.value = retorno;
//	campo.value=retorno;
}



		function mostra(hidden,valor)
		{
			alert("hidden: " + hidden.value);
			alert("campo: " + valor.value);

		}		

function limpaSUFRAMA(f)
{
	while(f.value.indexOf('-')!= -1){f.value=f.value.replace('-','');}
	while(f.value.indexOf(' ')!= -1){f.value=f.value.replace(' ','');}
	while(f.value.indexOf('.')!= -1){f.value=f.value.replace('.','');}
}

function validaSUFRAMA(campo)
{

	f = new Object();
	f.value = campo;
	limpaSUFRAMA(f);
	campo = f.value;
	
	if(campo.length != 9)
	{
		return false;
	}
	else
	{
		rcpf1 = campo.substring(0,8);
		rcpf2 = campo.substring(8);
		d1 = 0;
		for (i=0;i<8;i++) 
		{
			d1 += parseInt(rcpf1.substring(i,i+1)) *(i+2);
		}
		d1 = (d1 % 11);
		
		if (d1>9) d1 = 0;
  
		if (rcpf2!= d1) return false;
	}
		return true;
}


function currencyFormat(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;
}

/* confirma ou não a exclusão */
function confirmaExclusao(texto)
{
  var name = confirm(texto)
  if (name == true)
    return true;
  else 
    return false;
}

/*NAO USAR ESTE! confirma ou não a exclusão e caso confirme dá um submit no form passado como parametro. */
function confirmaExclusao(texto, formulario)
{
  var name = confirm(texto)
  if (name == true)
    formulario.submit();
  else 
    return false;
}

/* confirma ou não a exclusão e caso confirme dá um submit no form passado como parametro, para o evento passado como parametro. */
function confirmaExclusao(texto, formulario,evento)
{
  var name = confirm(texto)
  if (name == true)
  {
    formulario.event.value=evento;
    formulario.submit();
  }
  else 
    return false;
}

