//-------------------------------------------- mascara CNPJ
function mascaraCNPJ(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 < 20)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,2);
   }
}
//-------------------------------------------- mascara CPF
function mascaraCPF(Campo, teclapres){
   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 > 3 && tam < 6)
         Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);
      if(tam >= 7 && tam < 8)
         Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
      if(tam >= 10 && tam < 16)
		 Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,2);
   }
}
//-------------------------------------------- mascara CEP
function mascaraCEP(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)
         Campo.value = vr.substr(0, 5) + '-' + vr.substr(5, 3);
   }
}
//-------------------------------------------- mascara Data
function mascaraDATA(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr = vr.replace("/", "");
   vr = vr.replace("/", "");
   tam = vr.length + 1 ;
   if(tecla != 9 && tecla != 8){
      if(tam > 2 && tam < 4)
         Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
      if(tam >= 5 && tam < 10)
         Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
   }
}
//-------------------------------------------- mascara Hora
function mascaraHORA(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr = vr.replace("/", "");
   vr = vr.replace("/", "");
   tam = vr.length + 1 ;
   if(tecla != 9 && tecla != 8){
      if(tam > 2 && tam < 4)
         Campo.value = vr.substr(0, 2) + 'h' + vr.substr(2, tam);
   }
}
//-------------------------------------------- Valida CPF
function validaCPF(valor){
   valor = valor.replace(".", "");
   valor = valor.replace(".", "");
   valor = valor.replace("-", "");
   cpf = valor;
   erro = new String;
   if(cpf.length < 11){
      alert("Sao necessarios 14 digitos para verificação do CPF");
	  return false;
   }
   if(cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
      alert("CPF inválido");
	  return false;
   }
   var a = [];
   var b = new Number;
   var c = 11;
   for (i=0; i<11; i++){
      a[i] = cpf.charAt(i);
      if(i < 9)
	     b += (a[i] * --c);
   }
   if((x = b % 11) < 2)
      a[9] = 0;
   else
      a[9] = 11-x;
   b = 0;
   c = 11;
   for(y=0; y<10; y++)
      b += (a[y] * c--); 
   if((x = b % 11) < 2)
      a[10] = 0;
   else
      a[10] = 11-x;
   if((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]))
      erro +="CPF inválido";
   if(erro.length > 0){
      alert(erro);
      return false;
   }
   return true;
}
//-------------------------------------------- Valida CNPJ
function validaCNPJ(valor){
   CNPJ = valor;
   erro = new String;
   if(CNPJ.length < 18){
      alert("Sao necessarios 18 digitos para verificação do CNPJ");
	  return false;
   }
   if((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
      if(erro.length == 0){
         alert("CNPJ inválido");
	     return false;
      }
   }
   if(document.layers && parseInt(navigator.appVersion) == 4){
      x = CNPJ.substring(0,2);
      x += CNPJ.substring(3,6);
      x += CNPJ.substring(7,10);
      x += CNPJ.substring(11,15);
      x += CNPJ.substring(16,18);
      CNPJ = x; 
   }
   else{
      CNPJ = CNPJ.replace(".","");
      CNPJ = CNPJ.replace(".","");
      CNPJ = CNPJ.replace("-","");
      CNPJ = CNPJ.replace("/","");
   }
   var a = [];
   var b = new Number;
   var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
   for (i=0; i<12; i++){
      a[i] = CNPJ.charAt(i);
      b += a[i] * c[i+1];
   }
   if((x = b % 11) < 2)
      a[12] = 0;
   else
      a[12] = 11-x;
   b = 0;
   for (y=0; y<13; y++){
      b += (a[y] * c[y]); 
   }
   if((x = b % 11) < 2)
      a[13] = 0;
   else
      a[13] = 11-x;
   if((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13]))
      erro +="CNPJ inválido";
   if(erro.length > 0){
      alert(erro);
      return false;
   }
   else
      return true;
   return true;
}
function SomenteNumeros(input){
   if((event.keyCode<48)||(event.keyCode>57))
      event.returnValue = false;
}
function Show(obj){
   obj.style.visibility = 'visible';
   obj.style.display = 'inline';
}
function Hide(obj){
   obj.style.visibility = 'hidden';
   obj.style.display = 'none';
}
function abre(nomelink, w, h){
   window.open(nomelink,'entrar','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,fullscreen=no,width='+w+',height='+h+'')
}
function formataReais(valor){
   sign = (valor == (valor = Math.abs(valor)));
   valor = Math.floor(valor*100+0.50000000001);
   cents = valor%100;
   valor = Math.floor(valor/100).toString();
   if(cents<10)
      cents = "0" + cents;
   for(var i = 0; i < Math.floor((valor.length-(1+i))/3); i++)
      valor = valor.substring(0,valor.length-(4*i+3))+'.'+
   valor.substring(valor.length-(4*i+3));
   valor = (((sign)?'':'-') + valor + ',' + cents);
   return valor;
}
function FormataValor(campo,tammax,teclapres){
   var tecla = teclapres.keyCode;
   var vr = 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 < 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 )
         campo.value = vr;
      tam = tam - 1;
      if((tam > 2) && (tam <= 5))
         campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam );
      if((tam >= 6) && (tam <= 8))
         campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, 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 - 2, 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 - 2, 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 );
   }
}
//-------------------------------------------- Contar caracteres
function textCounter(field, countfield, maxlimit){
   if(field.value.length > maxlimit)
      field.value = field.value.substring(0, maxlimit);
   else 
      countfield.value = maxlimit - field.value.length;
}
//--------------------------------------- span link
function span_muda(element){
   document.all[element].style.color = "#0072BC";
}
function span_retorna(element){
   document.all[element].style.color = "#000099";
}
function span_muda2(element){
   document.all[element].style.color = "#0072BC";
}
function span_retorna2(element){
   document.all[element].style.color = "#000099";
}
function span_muda3(element){
   document.all[element].style.color = "#0072BC";
}
function span_retorna3(element){
   document.all[element].style.color = "#000000";
}
