//FUNÇÃO QUE SÓ ACEITA NÚMEROS
function criticaNumero(keypress){
	verificaNumero = true;
	caracteres = '0123456789';

	if(keypress == 36 || keypress == 40 || keypress == 41 || keypress == 42 || keypress == 43 || keypress == 46 || keypress == 63 || keypress == 91 || keypress == 92 || keypress == 94 || keypress == 124){
		event.returnValue = false;
		verificaNumero = false;
//	}else if ((caracteres.search(String.fromCharCode (keypress))==-1)){
	}else if ( !((keypress >= 48 && keypress <= 57) || (keypress >= 96 && keypress <= 105)) ){
		event.returnValue = false;
		verificaNumero = false;
	}
	
	return verificaNumero;
}


function retiraAlpha(valor){

	valor = replaceAll(valor," ", "" );
	valor = replaceAll(valor,"a", "" );
	valor = replaceAll(valor,"A", "" );
	valor = replaceAll(valor,"ã", "" );
	valor = replaceAll(valor,"Ã", "" );
	valor = replaceAll(valor,"â", "" );
	valor = replaceAll(valor,"Â", "" );
	valor = replaceAll(valor,"á", "" );
	valor = replaceAll(valor,"Á", "" );
	valor = replaceAll(valor,"b", "" );
	valor = replaceAll(valor,"B", "" );
	valor = replaceAll(valor,"c", "" );
	valor = replaceAll(valor,"C", "" );
	valor = replaceAll(valor,"ç", "" );
	valor = replaceAll(valor,"Ç", "" );
	valor = replaceAll(valor,"d", "" );
	valor = replaceAll(valor,"D", "" );
	valor = replaceAll(valor,"e", "" );
	valor = replaceAll(valor,"é", "" );
	valor = replaceAll(valor,"É", "" );
	valor = replaceAll(valor,"E", "" );
	valor = replaceAll(valor,"ê", "" );
	valor = replaceAll(valor,"Ê", "" );
	valor = replaceAll(valor,"f", "" );
	valor = replaceAll(valor,"F", "" );
	valor = replaceAll(valor,"g", "" );
	valor = replaceAll(valor,"G", "" );
	valor = replaceAll(valor,"h", "" );
	valor = replaceAll(valor,"H", "" );
	valor = replaceAll(valor,"i", "" );
	valor = replaceAll(valor,"I", "" );
	valor = replaceAll(valor,"j", "" );
	valor = replaceAll(valor,"J", "" );
	valor = replaceAll(valor,"k", "" );
	valor = replaceAll(valor,"K", "" );
	valor = replaceAll(valor,"l", "" );
	valor = replaceAll(valor,"L", "" );
	valor = replaceAll(valor,"m", "" );
	valor = replaceAll(valor,"M", "" );
	valor = replaceAll(valor,"n", "" );
	valor = replaceAll(valor,"N", "" );
	valor = replaceAll(valor,"o", "" );
	valor = replaceAll(valor,"O", "" );
	valor = replaceAll(valor,"õ", "" );
	valor = replaceAll(valor,"Õ", "" );
	valor = replaceAll(valor,"ó", "" );
	valor = replaceAll(valor,"Ó", "" );
	valor = replaceAll(valor,"ô", "" );
	valor = replaceAll(valor,"Ô", "" );
	valor = replaceAll(valor,"p", "" );
	valor = replaceAll(valor,"P", "" );
	valor = replaceAll(valor,"q", "" );
	valor = replaceAll(valor,"Q", "" );
	valor = replaceAll(valor,"r", "" );
	valor = replaceAll(valor,"R", "" );
	valor = replaceAll(valor,"s", "" );
	valor = replaceAll(valor,"S", "" );
	valor = replaceAll(valor,"t", "" );
	valor = replaceAll(valor,"T", "" );
	valor = replaceAll(valor,"u", "" );
	valor = replaceAll(valor,"U", "" );
	valor = replaceAll(valor,"v", "" );
	valor = replaceAll(valor,"V", "" );
	valor = replaceAll(valor,"x", "" );
	valor = replaceAll(valor,"X", "" );
	valor = replaceAll(valor,"y", "" );
	valor = replaceAll(valor,"Y", "" );
	valor = replaceAll(valor,"x", "" );
	valor = replaceAll(valor,"X", "" );
	valor = replaceAll(valor,"w", "" );
	valor = replaceAll(valor,"W", "" );

	return valor;
}

function retiraCaracteresEspeciais(valor){
	
	valor = replaceAll(valor,"{", "" );
	valor = replaceAll(valor,"}", "" );
	valor = replaceAll(valor,"(", "" );
	valor = replaceAll(valor,")", "" );
	valor = replaceAll(valor,"<", "" );
	valor = replaceAll(valor,">", "" );
	valor = replaceAll(valor,"[", "" );
	valor = replaceAll(valor,"]", "" );
	valor = replaceAll(valor,"|", "" );
	valor = replaceAll(valor,"\\", "" );
	valor = replaceAll(valor,"/", "" );
	valor = replaceAll(valor,"&", "" );
	valor = replaceAll(valor,"*", "" );
	valor = replaceAll(valor,"$", "" );
	valor = replaceAll(valor,"%", "" );
	valor = replaceAll(valor,"?", "" );
	valor = replaceAll(valor,"!", "" );
	valor = replaceAll(valor,"^", "" );
	valor = replaceAll(valor,"~", "" );
	valor = replaceAll(valor,"`", "" );
	valor = replaceAll(valor,"'", "" );
	valor = replaceAll(valor,'"', '' );
	valor = replaceAll(valor,",", "" );
	valor = replaceAll(valor,";", "" );
	valor = replaceAll(valor,":", "" );
	valor = replaceAll(valor,"@", "" );
	valor = replaceAll(valor,"¨", "" );
	valor = replaceAll(valor,"=", "" );
	valor = replaceAll(valor,"#", "" );
	valor = replaceAll(valor,"+", "" );
	valor = replaceAll(valor,"-", "" );
	valor = replaceAll(valor,".", "" );

	return valor;
}

function replaceAll (s, fromStr, toStr){
	var new_s = s;
	for (i = 0; i < 100 && new_s.indexOf (fromStr) != -1; i++){
		new_s = new_s.replace (fromStr, toStr);
	}
	return new_s;
}

function verificaNumero(objeto){

	vr = objeto.value;
	vr = retiraCaracteresEspeciais(vr);
	vr = retiraAlpha(vr);

	objeto.value = vr;
}

function formataData(campo,teclapres) {
	if(criticaNumero(teclapres.keyCode)){
		var tecla = teclapres.keyCode;
		vr = 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 )
				campo.value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
			if ( tam >= 5 && tam <= 10 )
				campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
		//}
	}
}

function formataValor(form, campo,tammax,teclapres) {

	if(criticaNumero(teclapres.keyCode) == true){

		var tecla = teclapres.keyCode;
		vr = document.forms[form][campo].value;
		vr = retiraCaracteresEspeciais(vr);
		vr = retiraAlpha(vr);
		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 == 1){
				document.forms[form][campo].value = "0,0" + vr;
			}
		 	if ( (tam >= 2) && (tam <= 5) ){
		 		document.forms[form][campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
		 	}
		 	if ( (tam >= 6) && (tam <= 8) ){
		 		document.forms[form][campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
		 	if ( (tam >= 9) && (tam <= 11) ){
		 		document.forms[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.forms[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.forms[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 ) ;}
		}
		if(document.forms[form][campo].value.length == 4){
			var valorFinal = "";
			if(document.forms[form][campo].value.substr(0,1) == "0"){
				valorFinal += document.forms[form][campo].value.substr(1,document.forms[form][campo].value.length);
				document.forms[form][campo].value = valorFinal;
			}
			

		}
	
		for (var ct = 0; ct < document.forms[form].elements.length; ct++) {
			if (document.forms[form].elements[ct].name == document.forms[form].elements[campo].name) {
				if ( !teclapres.shiftKey && tecla == 9 && document.forms[form].elements[ct+1] && document.forms[form].elements[ct+1].name == "senhaConta" && document.applets['tclJava'] ){
					document.applets['tclJava'].setFocus();
				}	
			}
		}
	}
}

function formataValor(objeto,tammax,teclapres) {

	if(criticaNumero(teclapres.keyCode) == true){

		var tecla = teclapres.keyCode;
		vr = objeto.value;
		vr = retiraCaracteresEspeciais(vr);
		vr = retiraAlpha(vr);
		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 == 1){
				objeto.value = "0,0" + vr;
			}
		 	if ( (tam >= 2) && (tam <= 5) ){
		 		objeto.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
		 	}
		 	if ( (tam >= 6) && (tam <= 8) ){
		 		objeto.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
		 	if ( (tam >= 9) && (tam <= 11) ){
		 		objeto.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) ){
		 		objeto.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) ){
		 		objeto.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 ) ;}
		}
		if(objeto.value.length == 4){
			var valorFinal = "";
			if(objeto.value.substr(0,1) == "0"){
				valorFinal += objeto.value.substr(1, objeto.value.length);
				objeto.value = valorFinal;
			}
			

		}
	}
}

function verificaFormatacao(objeto) {

	vr = objeto.value;
	vr = retiraCaracteresEspeciais(vr);
	vr = retiraAlpha(vr);
	tam = vr.length;

	if ( tam == 1){
 		objeto.value = "0,0" + vr ; }
	if ( tam == 2 ){
 		objeto.value = "0," + vr ; }
 	if ( (tam > 2) && (tam <= 5) ){
 		objeto.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 6) && (tam <= 8) ){
 		objeto.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 9) && (tam <= 11) ){
 		objeto.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) ){
 		objeto.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) ){
 		objeto.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 formataHora
*************************************************/

function formataHora(Campo,teclapress){

	var tecla = teclapress.keyCode;

	if(criticaNumero(tecla) == true){	
    	
	    vr = Campo.value;
	    vr = vr.replace( ":", "" );
	    vr = vr.replace( ";", "" );
	    tam = vr.length + 1;
	
	    if (tecla != 9 && tecla != 8 ){
	        if ( tam > 2 && tam < 5 )
	        	Campo.value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );
	 	}
	}
}


function formataGrupo(Campo,teclapress){

	var tecla = teclapress.keyCode;

	if(criticaNumero(tecla) == true){	
    	
	    vr = Campo.value;
	    vr = vr.replace( ".", "" );
	    tam = vr.length + 1;
	
	    if (tecla != 9 && tecla != 8 ){
	        if ( tam > 2 && tam < 5 )
	        	Campo.value = vr.substr( 0, tam - 2  ) + '.' + vr.substr( tam - 3, tam );
	 	}
	}
}

/*************************************************
* function abreJanela
*************************************************/
function abreJanela(url){
	window.open(url, "CEFAK", "scrollbars=yes,resizable=yes,width=600,height=500");
}


/*************************************************
* function mascaranum
*************************************************/
function mascaranum(evento,obj){
	var inteiro,dec,index;
		
	var value = (parseFloat((obj.value).replace(",",""))).toString();
			
	if((evento.keyCode<96 || evento.keyCode>105) && (evento.keyCode<48 || evento.keyCode>57) && evento.keyCode!=9 && evento.keyCode!=8 && evento.keyCode!=46 && (evento.keyCode<37 || evento.keyCode>40)){
		evento.returnValue=false;			
	}

        if (!isNaN(value)){
	
	if(value.length == 1){
		value = "0,0" + value;
	}else if(value.length == 2){
		value = "0," + value;
	}else if(value.length > 2){
		if ( !isNaN(value)){
			inteiro = value.substring(0,value.length - 2);
			dec = value.substring(value.length - 2);
			value =  inteiro + "," + dec;
		}else{
			value =  "0,00";
		}
	}
         }
         else { value = "0,00"; }
	//alert("int: " + inteiro);
	//alert("dec: " + dec);
	obj.value = value;
	
}


function formataCampo(object,evento,mascara){
    var tecla = evento.keyCode;
    var valor, temp, temp2;
    valor = object.value;
    temp = "";
    temp2 = "";
  if ( tecla ==8 || tecla == 88 || tecla>=48 && tecla<=57 || tecla>=96 && tecla<=105){
    if (tecla==8)  valor=valor.substring(0,valor.length);

    for (i=0;i<valor.length;i++){
      if (valor.substring(i,i+1) != "." && valor.substring(i,i+1) != "," && valor.substring(i,i+1) != "-" && valor.substring(i,i+1) != "/"
&& valor.substring(i,i+1) != "(" && valor.substring(i,i+1) != ")" && valor.substring(i,i+1) != ":")
        temp = temp + valor.substring(i,i+1);
    }

    temp2 = "";
    j = 0;
    for (i=0; i<temp.length;i++){
      if (mascara.substring(j,j+1)=="#"){
        temp2 = temp2 + temp.substring(i,i+1);
        j = j + 1;
      }
      else{
        temp2 = temp2 + mascara.substring(j,j+1) + temp.substring(i,i+1);
        j = j + 2;
      }
    }
    object.value = temp2;
  }
 }



 
var isNN = (navigator.appName.indexOf("Netscape") != -1);

/*************************************************
* function autoTab
*************************************************/
function autoTab(input, len, e){
  var keyCode = (isNN) ? e.which : e.keyCode;
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  var cont=1;
  var valido=false;
  if ((input.value.length >= len && !containsElement(filter, keyCode)) || keyCode == 13){
      if (keyCode != 13) input.value = input.value.slice(0, len);
      while(!valido){
         //alert('cont: ' + input.form[(getIndex(input)+cont) % input.form.length].name + '\n' +
         //      'disabled: ' + input.form[(getIndex(input)+cont) % input.form.length].disabled + '\n' +
         //      'type: ' + input.form[(getIndex(input)+cont) % input.form.length].type);
         if(!input.form[(getIndex(input)+cont) % input.form.length].disabled && input.form[(getIndex(input)+cont) % input.form.length].style.display!='none' && input.form[(getIndex(input)+cont) % input.form.length].type!='hidden'){
            input.form[(getIndex(input)+cont) % input.form.length].focus();
            valido=true;
         }else{
            cont++;
         }
      }

}

/*************************************************
* function containsElement
*************************************************/
function containsElement(arr, ele){
  var found = false, index = 0;
  while (!found && index < arr.length)
    if (arr[index] == ele) found = true;
    else index++;
  return found;
}

/*************************************************
* function getIndex
*************************************************/
function getIndex(input){
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
      if (input.form[i] == input) index = i;
      else i++;
    return index;
  }
 return true;
}


/*************************************************
* function autoEnter
*************************************************/
function autoEnter(input, e){
  var keyCode = (isNN) ? e.which : e.keyCode;
  var filter = (isNN) ? [0,13] : [0,13];
  if (containsElement(filter, keyCode)){
      input.form[(getIndex(input)+1) % input.form.length].focus();
}


/*************************************************
* function containsElement
*************************************************/
function containsElement(arr, ele){
  var found = false, index = 0;
  while (!found && index < arr.length)
    if (arr[index] == ele) found = true;
    else index++;
  return found;
}


/*************************************************
* function getIndex
*************************************************/
function getIndex(input){
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
      if (input.form[i] == input) index = i;
      else i++;
    return index;
  }
 return true;
}


/*************************************************
* function setFocus
*************************************************/
function setFocus(){
    if (document.forms[0]){
      for (i=0; i<document.forms[0].length; i++){
        if (document.forms[0].elements[i].type == "text" || document.forms[0].elements[i].type == "select-one"){
            document.forms[0].elements[i].focus();
            return null;
        }
      }
    }
}


