function testinput(re, str){
	if (str.search(re) != -1)
		return true;
	else
		return false;
}
function findObj(n, d) { //v4.01
  	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  	if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function showHideLayers() { //v6.0
 		var i,p,v,obj,args=showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
   	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
   	obj.visibility=v; }
}

function testa_data(dta, formname, campo) { // fun??o complementar para testar a validade da data
	if(!isdate(dta)) {
		alert('Digite uma data v?lida');
		document[formname][campo].value = '';
		document[formname][campo].focus();
		return false;
	}
}

function FormataData(campo,formname,teclapres) { // M?scara para os campos de data
	var tecla = teclapres.keyCode;
	vr = document[formname][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[formname][campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document[formname][campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 
	}
}

function FormataCNPJ(Campo, teclapres){

	var tecla = teclapres.keyCode;

	var vr = new String(Campo.value);
	vr = vr.replace(".", "");
	vr = vr.replace(".", "");
	vr = vr.replace("/", "");
	vr = vr.replace("-", "");

	tam = vr.length + 1 ;

	
	if (tecla != 9 && tecla != 8){
		if (tam > 2 && tam < 6)
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2, tam);
		if (tam >= 6 && tam < 9)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
		if (tam >= 9 && tam < 13)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
		if (tam >= 13 && tam < 15)
			Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
	}
}


function FormataCNPJDSF(objeto, evento){
	evita_letra2(evento);
	
	cnpj = objeto.value.replace(/[^0-9]/gi, '');
	tamanho = cnpj.length;
	
	if(tamanho >= 12)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3)+"."+cnpj.substr(5, 3)+"/"+cnpj.substr(8, 4)+"-"+cnpj.substr(12, 2);
	else if(tamanho >= 8)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3)+"."+cnpj.substr(5, 3)+"/"+cnpj.substr(8, 4);
	else if(tamanho >= 5)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3)+"."+cnpj.substr(5, 3);
	else if(tamanho >= 2)
		objeto.value = cnpj.substr(0, 2)+"."+cnpj.substr(2, 3);
	else
		objeto.value = cnpj;

}


function FormataIE(Campo, teclapres){
  if(Campo.value.length < 12) {	
	var tecla = teclapres.keyCode;
	var vr = new String(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, 2) + '.' + vr.substr(2, tam);
		}
		
		if (tam > 5 && tam < 7) {
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2,4) + '.' + vr.substr(5,tam-4);
		}
		
		if (tam >= 9) {
			Campo.value = vr.substr(0, 2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,6) + '-' + vr.substr(8,tam-4);
		}
	}
  }
}

function FormataCep(Campo, teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace("-", "");

	tam = vr.length + 1;
	
	if (tecla != 9 && tecla != 8){
		if (tam > 5 && tam < 10)
			Campo.value = vr.substr(0, 5) + '-' + vr.substr(5, tam);
	}
}


function FormataCPFDSF(objeto, evento){
	evita_letra2(evento);
	cpf = objeto.value.replace(/[^0-9]/gi, '');
	tamanho = cpf.length;
	
	if(tamanho >= 9)
		cpf = cpf.substr(0, 3)+"."+cpf.substr(3, 3)+"."+cpf.substr(6, 3)+"-"+cpf.substr(9, 3);
	else if(tamanho >= 6)
		cpf = cpf.substr(0, 3)+"."+cpf.substr(3, 3)+"."+cpf.substr(6, 3);
	else if(tamanho >= 3)
		cpf = cpf.substr(0, 3)+"."+cpf.substr(3, 3);
	else 
		cpf = cpf;
		
	objeto.value = cpf;
}



function FormataCPF(pForm,pCampo,pTamMax,pPos1,pPos2,pPosTraco,pTeclaPres,Pos){
	 var wTecla, wVr, wTam;	 
	 
	 var total = pForm[pCampo].length;
	 wTecla = pTeclaPres.keyCode;
	 
	 if(total == undefined) {
	 	wVr = pForm[pCampo].value;
	 }else{
	 	wVr = pForm[pCampo][Pos].value;
	 }
	 
	 wVr = wVr.toString().replace( "-", "" );
	 wVr = wVr.toString().replace( ".", "" );
	 wVr = wVr.toString().replace( ".", "" );
	 wVr = wVr.toString().replace( "/", "" );
	 wTam = wVr.length ;

	 if (wTam < pTamMax && wTecla != 8) { 
	    wTam = wVr.length + 1 ; 
	 }

	 if (wTecla == 8 ) { 
	    wTam = wTam - 1 ; 
	 }
   
	 if ( wTecla == 8 || wTecla == 88 || wTecla >= 48 && wTecla <= 57 || wTecla >= 96 && wTecla <= 105 ){
		  if ( wTam <= 2 ){
		  	if(total == undefined) {
			 	pForm[pCampo].value = wVr;
			 }else{
			 	pForm[pCampo][Pos].value = wVr;
			 }
		  }
		  
		  if (wTam > pPosTraco && wTam <= pTamMax) {
		        wVr = wVr.substr(0, wTam - pPosTraco) + '-' + wVr.substr(wTam - pPosTraco, wTam);
		  }
		  
		  if ( wTam == pTamMax){
		        wVr = wVr.substr( 0, wTam - pPos1 ) + '.' + wVr.substr(wTam - pPos1, 3) + '.' + wVr.substr(wTam - pPos2, wTam);
		  }
	  	  if(total == undefined) {
		 	pForm[pCampo].value = wVr;
		 }else{
		 	pForm[pCampo][Pos].value = wVr;
		 }
	 }
}

function isdate2(obj){
var mes, ano;
var retval = false;
ano=obj.substring(3,7);
//alert(obj);
if (obj != ""){
   if (obj.length != 7 || obj.substring(2,3)!="/") {         
   		retval = false;
   }
   else {     
      mes = obj.substring(0,2);
      if (nochars(mes) && parseInt(mes,10) > 0 && parseInt(mes,10) <= 12){
         mes = parseInt(mes,10);
         if (nochars(ano) && parseInt(ano,10) >= 0000) {
            if ((parseInt(ano,10) % 4) == 0) {
               bissexto = 1;
            }
            else {
               bissexto = 0;
            }
			retval = true;
         } else {
		 	retval = false;
		 }
      } else {
	  		retval = false;
	  }
   }
}
else {
     retval = true;
}
//alert(retval);
return retval;
}

//Verifica se ? uma data v?lida
function isdate(obj){
var mes, dia, ano;
var retval = false;
ano=obj.substring(6,10);
//alert(obj);
if (obj != ""){
   if (obj.length != 10 || obj.substring(2,3)!="/" || obj.substring(5,6)!="/") {         
   }
   else {     
      mes = obj.substring(3,5);
      if (nochars(mes) && parseInt(mes,10) > 0 && parseInt(mes,10) <= 12){
         mes = parseInt(mes,10);
         if (nochars(ano) && parseInt(ano,10) >= 0000) {
            if ((parseInt(ano,10) % 4) == 0) {
               bissexto = 1;
            }
            else {
               bissexto = 0;
            }
            dia = obj.substring(0,2);
            if (nochars(dia)){
               dia = parseInt(dia,10);
               if (((mes==1 || mes==3 || mes==5 || mes==7 || mes==8 || mes==10 || mes==12) && (dia >= 1 && dia <= 31)) ||
                  ((mes==4 || mes==6 || mes==9 || mes==11) && (dia >= 1 && dia <= 30)) ||
                  (mes==2 && dia >= 1 && dia <= (28 + bissexto))) {
                  retval=true;
               }
            }
         }
      }
   }
}
else {
     retval = true;
}
//alert(retval);
return retval;
}

// Verifica se existem letras na variavel
function nochars(strval){
  var retval = true;
  for (var i = 0; i < strval.length; i++){
     if (strval.substring(i,i+1) < "0" || strval.substring(i,i+1) > "9"){
        retval=false;
        break;
     }
  }
  return retval;
}

//Utiliza??o: onkeypress="evita_letra(event)" onKeydown="FormataHora(this,event)" maxlength="5"
function FormataHora(campo,formname,teclapres){
	var tecla = teclapres.keyCode;
	vr = document[formname][campo].value;
	vr = vr.replace( ":", "" );
	tam = vr.length + 1;
	
	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 ){
			document[formname][campo].value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );
		}
	}
}

// N?o permite digitar letras em um input type=text
function evita_letra(tecla) {
	if (tecla.keyCode < 45 || tecla.keyCode > 57) 
		tecla.returnValue = false;
}

// N?o permite digitar letras em um input type=text
function evita_letra2(tecla) {
	if (tecla.keyCode < 48 || tecla.keyCode > 57) 
		tecla.returnValue = false;
}

// s? permite digitar letras em um input type=text
function testa_alfa(tecla) {
	if (tecla.keyCode < 65 || tecla.keyCode > 122 || (tecla.keyCode > 90 && tecla.keyCode < 97)) 
		tecla.returnValue = false;
}

function FormataValor(tammax,teclapres, tf) { // formata os campos com a m?scara valor
	var tecla = teclapres.keyCode;
	vr = tf.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 < 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 ){ 
	 		tf.value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		tf.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		tf.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		tf.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) ){
	 		tf.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) ){
	 		tf.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 isANumber(number) {
  answer = 1;

  if (!parseFloat(number)) {
    //the first digit wasn't numeric
    answer = 0;
  } else {
    //the first digit was numeric, so check the rest
    for (var i=0; i<number.length; i++) {
      if ((number.charAt(i) != "0")
        && (!parseFloat(number.charAt(i)))) {
          answer = 0;
          break;
      }
    }
  }
  if (answer == 1) {
    orderPlaced = true;
  }
  if (number == 0) answer = 1;
  return answer;
}


function ToggleAll(tf,checked) {
    //alert(document.forms["cadastro_regiao"].action);
	len = document.forms[tf].elements.length;
	alert('teste');
    var i = 0;
    for(i = 0; i < len; i++) {
        document.forms[tf].elements[i].checked = checked;
    }
}

function fixWin() {
			 var x = 400;
			 var y = 150;
			 window.moveTo(parseInt((screen.availWidth - x) / 2),parseInt((screen.availHeight - y) / 2));
}

// valida o CNPJ
function val_cnpj(numero) {
	//numero = numero1.value;
	dig_1 = 0;
	dig_2 = 0;
	controle_1 = 5;
	controle_2 = 6;

if ( (numero.length != 18)  || (numero.substring(2, 3) != ".") || (numero.substring(6, 7) != ".") || (numero.substring(10, 11) != "/") || (numero.substring(15, 16) != "-") ) {
     return false;
}else{ 
  	   numero = (numero.substring(0, 2)) + "" + (numero.substring(3, 6)) + "" + (numero.substring(7, 10))+ "" + (numero.substring(11, 15)) + "" + (numero.substring(16, 18)) 
	   for ( i=0 ; i < 12 ; i++) {
	        dig_1 = dig_1 + parseFloat(numero.substring(i, i+1) * controle_1);
	          controle_1 = controle_1 - 1;
	        if (i == 3) {
	           controle_1 = 9;
	        }
	   }
	   
	   resto = dig_1 % 11;
	   dig_1 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_1 = 0;
	   }
	   
	   for ( i=0 ; i < 12 ; i++) {
	        dig_2 = dig_2 + parseInt(numero.substring(i, i+1) * controle_2);
	          controle_2 = controle_2 - 1;
	        if (i == 4) {
	           controle_2 = 9;
	        }
	   }
	   
	   dig_2 = dig_2 + (2 * dig_1);
	   resto = dig_2 %11;
	   dig_2 = 11 - resto;
	   
	   if ((resto == 0) || (resto == 1)){
	        dig_2 = 0;
	   }
	   
	   dig_ver = (dig_1 * 10) + dig_2;
	   
	   if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
	          return false;
	   }
 }
 return true;
}
		
//Valida o cpf 
function val_cpf(numero) {
  numero = numero.replace(/\./g,"");
  numero = numero.replace(/\-/g,"");
  dig_1 = 0;
  dig_2 = 0;
  controle_1 = 10;
  controle_2 = 11;
  lsucesso = 1;
  if ((numero.length != 11) && (numero.length != 0)) {
     return false;
  }
  else {
     for ( i=0 ; i < 9 ; i++) {
        dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);
          controle_1 = controle_1 - 1;
     }
    
     resto = dig_1 % 11;
     dig_1 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
          dig_1 = 0;
     }
     for ( i=0 ; i < 9 ; i++) {
          dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);
        controle_2 = controle_2 - 1;
     }
     dig_2 = dig_2 + 2 * dig_1;
     resto = dig_2 % 11;
     dig_2 = 11 - resto;
     if ((resto == 0) || (resto == 1)) {
        dig_2 = 0;
     }
     dig_ver = (dig_1 * 10) + dig_2;
     if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {
          return false;
        }
  }

return true;
}


//Checa o campo E-Mail
function verifica_mail(email){
var campo = email;
   if (campo != "") {
   		if (campo.indexOf("@") == -1) return false;
        else {
            if (campo.indexOf(".") == -1) return false;
        }
   }
	return true;
}

function isANumber(number) {
  answer = 1;

  if (!parseFloat(number)) {
    //the first digit wasn't numeric
    answer = 0;
  } else {
    //the first digit was numeric, so check the rest
    for (var i=0; i<number.length; i++) {
      if ((number.charAt(i) != "0")
        && (!parseFloat(number.charAt(i)))) {
          answer = 0;
          break;
      }
    }
  }
  if (answer == 1) {
    orderPlaced = true;
  }
  if (number == 0) answer = 1;
  return answer;
}

function ver_img (foto, w, h) {
		spl = window.open ("","foto","height="+h+",width="+w+",resizable=no");
		spl.document.write ("<html><head><title>IMAGEM</title></head><body leftmargin=0 topmargin=0 marginheight=0 marginwith=0><img src='"+foto+"' border=0></body></html>")
}


//MASCARAS DOS DOCUMENTOS
function formatarDocumento(doc, masc, evento)
{
	evita_letra2(evento);

  	var i = doc.value.length;
  	var saida = masc.substring(0,1);
  	var texto = masc.substring(i)
  	if (texto.substring(0,1) != saida) 
  	{
		doc.value += texto.substring(0,1);
  	}
}

function completarZero(doc, tam)
{
	var i = doc.value.length;
	var ret = "";
	if (i < tam) {
		var dif = (tam - i);
		for(var k=1 ; k<=dif ; k++)
			ret += "0"; 
	}
	doc.value = ret + doc.value;
}

function formataMoeda(obj){

<!-- mesmo que o campo contenha espa?os em branco, ? atribu?do aspas juntas para -->
<!--     que no pr?ximo foco ele n?o tenha mais este espa?o -->
	if (Trim(obj.value) == ""){
        obj.value= "";
	    return true;
    }


	valor = obj.value.replace(/[^0-9.,]/gi, '').replace(/[,]/gi,'.');
	pri = valor.indexOf('.');
	ult = valor.lastIndexOf('.'); 
	
	if (valor == "" || valor == "." || (pri > 0 && (pri != ult) )) { 
		alert('Digite um valor v?lido.');
		obj.focus();
		return (false);
	}
	else{
		obj.value = valor;
	}
}

function soNumero(obj){
	qtd = obj.value.length;
	valor = obj.value.replace(/[^0-9$]/gi, '');
	if(qtd > 0 && valor == ""){
		alert('Digite um valor valido.');
		obj.value = '';
		obj.focus();
		return (false);
	}
	else{
		obj.value = valor;
	}
}

function soMoeda(obj){
	qtd = obj.value.length;
	valor = obj.value.replace(/[^0-9.,$]/gi, '');
	if(qtd > 0 && valor == ""){
		obj.value = '';
		obj.focus();
		return (false);
	}
	else{
		obj.value = valor;
	}
}

function float2moeda(num) {
   x = 0;

   if(num<0) {
      num = Math.abs(num);
      x = 1;
   }   
   
   if(isNaN(num)) 
      num = "0";
   
   cents = Math.floor((num*100+0.5)%100);
   num = Math.floor((num*100+0.5)/100).toString();

   if(cents < 10) 
   	  cents = "0" + cents;
      
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3)) + '.' + num.substring(num.length-(4*i+3));   
   
   ret = num + ',' + cents;   
               
   if (x == 1) 
      ret = ' - ' + ret;
      
   return ret;

}

function FormataParaMoeda(campo,tammax,teclapres,decimal) {
	var tecla = teclapres.keyCode;
	vr = LimparMoeda(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 ) ;
		}
	} 
}


function LimparMoeda(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;
}

function encode64(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function mudaFiltro(objeto){
	if(objeto.value == "data"){
		document.getElementById("divData").style.visibility = "visible";
		document.getElementById("divCodigo").style.visibility = "hidden";
		document.getElementById("divCaixa").style.height = "85px";
	}
	else if(objeto.value == "codigo"){
		document.getElementById("divData").style.visibility = "hidden";
		document.getElementById("divCodigo").style.visibility = "visible";
		document.getElementById("divCaixa").style.height = "85px";
	}
	else{
		document.getElementById("divData").style.visibility = "hidden";
		document.getElementById("divCodigo").style.visibility = "hidden";
		document.getElementById("divCaixa").style.height = "52px";
	}
}

<!-- Funcoes Alexandre Vargas -->


function zerosAEsquerda(num, tamanho){

    if (num == "") {
        for (i=0; i<tamanho-1;i++)
            num += "0";
        return num;
    }
    num= String(num);
    var intTamStr = num.length;
    var intTam = tamanho - intTamStr;

    for (i=0; i<intTam; i++)
        num = "0" + num;
    return num;

}

function validaHora(caixa){
    var sep=0;
    var h_m= caixa.value; 
    for (var i=0; i<h_m.length; i++){
        <!-- tira todos os espa?os dos lados e dentro da string-->
        if (h_m.slice(i, i+1) == " "){
            h_m= h_m.slice(0, i) + h_m.slice(i+1, h_m.length);    
        }

        if (! ((h_m.slice(i, i+1) >=0 && h_m.slice(i, i+1) <=9) || !(h_m.slice(i, i+1) != ":")) ){
<!-- erro -->
            alert ("Digite um hor?rio v?lido!");        
            caixa.focus();
  	        caixa.select();
            return false;
        }else if (h_m.slice(i, i+1) == ":"){
            pos_sep=i; 
            sep++;        
        }
    }	

    if (sep >1){
<!-- erro -->
        alert ("Digite um hor?rio v?lido!");        
	    caixa.select();
        caixa.focus();
        return false;
    }else if (sep == 1){
        var hora= h_m.slice(0, pos_sep);
        var min= h_m.slice(pos_sep+1, h_m.length);              

        if (hora < 0 || hora > 23){
<!-- erro -->
            alert ("Digite um hor?rio v?lido!");        
            caixa.focus();
		    caixa.select();
            return false;
        }else{
        	hora= Number(hora);
            hora= zerosAEsquerda(hora, 2);
        }


        if (min < 0 || min > 59){
<!-- erro -->
            alert ("Digite um hor?rio v?lido!");        
            caixa.focus();
		    caixa.select();
            return false;
        }else{
        	min= Number(min);
            min= zerosAEsquerda(min, 2);
        }	

        h_m= hora+":"+min;
        caixa.value= h_m; 
    }else if (sep == 0){
        if (h_m.length == 4){
            hora= h_m.slice(0,2);
            min= h_m.slice(2,4);
            if (hora <0 || hora >23 || min <0 || min>59){
                alert ("Digite um hor?rio v?lido!");        
                caixa.focus();
			    caixa.select();
                return false;
            }else{
                h_m= hora+":"+min;
                caixa.value= h_m; 
            }
        }else if (h_m < 0 || h_m > 23){
<!-- erro -->
            alert ("Digite um hor?rio v?lido!");        
            caixa.focus();
		    caixa.select();
            return false;
        }else{
            hora= Number(h_m);
            hora= zerosAEsquerda(hora, 2);
            h_m= hora+":00";   
            caixa.value= h_m; 
        }
    }

	
}

function Trim(str){
	while (str.charAt(0) == " ")
	str = str.substr(1,str.length -1);
	
	while (str.charAt(str.length-1) == " ")
	str = str.substr(0,str.length-1);
	
	return str;
}


function blHora(){
 	if (window.event.keyCode < 48 || window.event.keyCode > 58)
        return false;	   
}


function blNumero(){
    if (window.event.keyCode < 48 || window.event.keyCode > 57)
        return false;
    return true;
}

function blMoeda(){
    if (!blNumero()){
        if (window.event.keyCode == 44 || window.event.keyCode == 46){
            window.event.keyCode = 46;
		    return true;
        }else{ 
            return false;        
        }
    }else
        return true;
}

function validaMoeda(caixa) {
    var qtd_virgulas=0;
    var valor= caixa.value; 
   
    if (Trim(valor) == ""){
        caixa.value= "0.00";
        return;
    }

    for (var i=0; i<valor.length; i++){
        if (valor.charAt(i) == "."){
           qtd_virgulas++;
           pos_virgula= i;
        }
    }

    if (qtd_virgulas == 0)
        valor += ".00";

    if (qtd_virgulas == 1){
        arr_valor= valor.split(".");

        if (!arr_valor[0])
            arr_valor[0]= "0";           	
        else
            arr_valor[0]= Number(arr_valor[0]);

            
        if (!arr_valor[1])
            arr_valor[1]= "00";           	
        else if (arr_valor[1].length == 1)
            arr_valor[1] += "0";
        else if (arr_valor[1].length >2){
            arr_valor[1]= arr_valor[1].slice(0, 2);
        }
        valor= arr_valor[0]+'.'+arr_valor[1];
    }

    if (qtd_virgulas > 1){
        alert ('Valor inv?lido!');
        caixa.focus();
        caixa.select();
    }else{
        if (valor > 99999999.99){
            alert ('Valor invalido!');
            caixa.focus();
            caixa.select();
        }
        caixa.value= valor;
    }
}


function cpfcnpj(obj, evento){
	var valida;
	soNumero(obj);
	if(obj.value.replace(/[^0-9]/gi, '').length == 11){
		valida = validaCPF(obj.value);
		if(valida){
			alert(valida);
			obj.value='';
		}else{
			FormataCPFDSF(obj, evento);
            return 'OK';
		}
	}
	else {
		if(obj.value.replace(/[^0-9]/gi, '').length == 14){
			valida = validaCNPJ(obj.value);
			if(valida){
				alert(valida);
				obj.value='';
			}else{
				FormataCNPJDSF(obj, evento);
                return 'OK';
			}
		}
		else {
			/*if(obj.value.replace(/[^0-9]/gi, '').length > 0){
				alert("CPF/CNPJ com formato inv?lido.");
			}*/
			obj.value = '';
		}
	}
}


function validaCNPJ(cnpj){
	digito =  (parseInt(cnpj.charAt(0)) * 5);
	digito += (parseInt(cnpj.charAt(1)) * 4);
	digito += (parseInt(cnpj.charAt(2)) * 3);
	digito += (parseInt(cnpj.charAt(3)) * 2);
	digito += (parseInt(cnpj.charAt(4)) * 9);
	digito += (parseInt(cnpj.charAt(5)) * 8);
	digito += (parseInt(cnpj.charAt(6)) * 7);
	digito += (parseInt(cnpj.charAt(7)) * 6);
	digito += (parseInt(cnpj.charAt(8)) * 5);
	digito += (parseInt(cnpj.charAt(9)) * 4);
	digito += (parseInt(cnpj.charAt(10))* 3);
	digito += (parseInt(cnpj.charAt(11))* 2);
		digito = (digito*10) % 11;
	if(digito == 10)
		digito = 0;
		if(digito == cnpj.charAt(12)){
		digito =  (parseInt(cnpj.charAt(0)) * 6);
		digito += (parseInt(cnpj.charAt(1)) * 5);
		digito += (parseInt(cnpj.charAt(2)) * 4);
		digito += (parseInt(cnpj.charAt(3)) * 3);
		digito += (parseInt(cnpj.charAt(4)) * 2);
		digito += (parseInt(cnpj.charAt(5)) * 9);
		digito += (parseInt(cnpj.charAt(6)) * 8);
		digito += (parseInt(cnpj.charAt(7)) * 7);
		digito += (parseInt(cnpj.charAt(8)) * 6);
		digito += (parseInt(cnpj.charAt(9)) * 5);
		digito += (parseInt(cnpj.charAt(10))* 4);
		digito += (parseInt(cnpj.charAt(11))* 3);
		digito += (parseInt(cnpj.charAt(12))* 2);
			digito = (digito*10) % 11;
		if(digito == 10)
			digito = 0;
			if(digito == cnpj.charAt(13)){
			return "";
		}
		else{
			return "CNPJ Inv?lido";
		}
	}
	else{
		return "CNPJ Inv?lido";
	}
}

function validaCPF(cpf){
	digito =  (parseInt(cpf.charAt(0)) * 10);
	digito += (parseInt(cpf.charAt(1)) * 9);
	digito += (parseInt(cpf.charAt(2)) * 8);
	digito += (parseInt(cpf.charAt(3)) * 7);
	digito += (parseInt(cpf.charAt(4)) * 6);
	digito += (parseInt(cpf.charAt(5)) * 5);
	digito += (parseInt(cpf.charAt(6)) * 4);
	digito += (parseInt(cpf.charAt(7)) * 3);
	digito += (parseInt(cpf.charAt(8)) * 2);

	digito = (digito*10) % 11;

	if(digito == 10)
		digito = 0;
		if(digito == cpf.charAt(9)){
		digito =  (parseInt(cpf.charAt(0)) * 11);
		digito += (parseInt(cpf.charAt(1)) * 10);
		digito += (parseInt(cpf.charAt(2)) * 9);
		digito += (parseInt(cpf.charAt(3)) * 8);
		digito += (parseInt(cpf.charAt(4)) * 7);
		digito += (parseInt(cpf.charAt(5)) * 6);
		digito += (parseInt(cpf.charAt(6)) * 5);
		digito += (parseInt(cpf.charAt(7)) * 4);
		digito += (parseInt(cpf.charAt(8)) * 3);
		digito += (parseInt(cpf.charAt(9)) * 2);

		digito = (digito*10) % 11;
		if(digito == 10)
			digito = 0;


		if(digito == cpf.charAt(10)){
			return "";
		}
		else{
			return "CPF Inv?lido";
		}
	}
	else{
		return "CPF Inv?lido";
	}
}


function validaPorcentagem(caixa) {
    var qtd_virgulas=0;
    var valor= caixa.value;

    if (Trim(valor) == ""){
        caixa.value= "0.000000";
        return;
    }

    for (var i=0; i<valor.length; i++){
        if (valor.charAt(i) == "."){
           qtd_virgulas++;
           pos_virgula= i;
        }
    }

    if (qtd_virgulas == 0){
        valor = Number(caixa.value);
        if (valor > 100){
            alert ('Valor invalido!');
            caixa.focus();
            caixa.select();
            return;
        }else{
            valor += ".000000";
        }
    }


    if (qtd_virgulas == 1){
        arr_valor= valor.split(".");

        if (!arr_valor[0]){
            arr_valor[0]= "0";
        }else{
            arr_valor[0]= Number(arr_valor[0]);
            if (arr_valor[0] == 100){
                if (Number(arr_valor[1]) > 0){
                    alert ('Valor invalido!');
                    caixa.focus();
                    caixa.select();
                    return;
                }
            }else if (arr_valor[0] > 100){
                alert ('Valor invalido!');
                caixa.focus();
                caixa.select();
                return;
            }
        }
        if (!arr_valor[1])
            arr_valor[1]= "000000";
        else if (arr_valor[1].length == 1)
            arr_valor[1] += "00000";
        else if (arr_valor[1].length == 2)
            arr_valor[1] += "0000";
        else if (arr_valor[1].length == 3)
            arr_valor[1] += "000";
        else if (arr_valor[1].length == 4)
            arr_valor[1] += "00";
        else if (arr_valor[1].length == 5)
            arr_valor[1] += "0";
        else if (arr_valor[1].length > 5)
            arr_valor[1]= arr_valor[1].slice(0, 6);

        valor= arr_valor[0]+'.'+arr_valor[1];
    }

    if (qtd_virgulas > 1){
        alert ('Valor invalido!');
        caixa.focus();
        caixa.select();
        return;
    }else{
        caixa.value= valor;
    }
}


<!-- Fim das fun??es Alexandre Vargas -->
