/*
  Funcoes genericas de javascript
*/

function autochangeCampo(value, maxSize, nextCampo,e){
 var key_ok;
 
if (window.event)
   key = window.event.keyCode;
else if (e)
  key = e.which;

  if (
      (key!=0)
   && (key!=null)
   && (key!=8) //TAB
   && (key!=9) //Backspace
   && (key!=27) //ESC
   && (key!=20) //CapsLock
   && (key!=16) //Shift
   && (key!=35) //END
   && (key!=36) //HOME
   && (key!=37) //ARROW
   && (key!=38) //ARROW
   && (key!=39) //ARROW
   && (key!=40) //ARROW
   && (key!=45) //INSERT
   && (key!=46) //DELETE
   && (key!=93) //SELECT KEY ??
  )
    key_ok = true
  else
    key_ok = false
    
  if (key == 13){
    document.getElementById(nextCampo).focus();
  }
  else
    if ((value.length == maxSize) && (key_ok == true))
      document.getElementById(nextCampo).focus()
}

function key_val(e,tipo, nextCampo){
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) //TAB
   || (key==9) //Backspace
   || (key==27) //ESC
   || (key==20) //CapsLock
   || (key==16) //Shift
   || (key==35) //END
   || (key==36) //HOME
   || (key==37) //ARROW
   || (key==38) //ARROW
   || (key==39) //ARROW
   || (key==40) //ARROW
   || (key==45) //INSERT
   || (key==46) //DELETE
   || (key==93) //SELECT KEY ??
   )
   return true;

// ENTER
if (key==13){
  return false;
  document.getElementById(nextCampo).focus();
}else
if ((("0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZ/z").indexOf(keychar) > -1) && (tipo == 3))
   return true;

if ((("0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz").indexOf(keychar) > -1) && (tipo == 2))
   return true;
else
  if ((("0123456789").indexOf(keychar) > -1) && (tipo == 1))
    return true;
  else
    return false;
}

/*  --------------------------------------   */

  function validaCPFCNPJ(dado){
    if (isCpf(dado)){
     window.document.getElementById('img_cpfcnpj').src = 'public/imagens/okcpf50x20.png';
    }
    else{
      if (isCnpj(dado)){
       window.document.getElementById('img_cpfcnpj').src = 'public/imagens/okcnpj50x20.png';
      }
      else{
       window.document.getElementById('img_cpfcnpj').src = 'public/imagens/erro50x20.png';
      }
    }
     window.document.getElementById('img_cpfcnpj').style.visibility = 'visible';
  };


/* ------------------------------------------ */
  function mostra_formCMC7(){
    velho = window.document.getElementById('old')
    novo  = window.document.getElementById('new')
    
    cpf = window.document.getElementById('cpf_cnpj')

    if (novo.style.display != 'block'){
      velho.style.display = 'none'
      novo.style.display = 'block'

      cpf.onKeyPress = function() {return key_val(event,1,'cmc7-1') }
      cpf.onKeyDown  = function() {autochangeCampo(this.value,16,'cmc7-1',event) }
      
      /* FF */
      cpf.setAttribute('onKeyPress', 'return key_val(event,1,\'cmc7-1\');' );
      cpf.setAttribute('onKeyDown', 'autochangeCampo(this.value,16,\'cmc7-1\',event)' );

   }else{
      velho.style.display = 'block'
      novo.style.display = 'none'
      /* FF */

      cpf.onKeyPress = function() {return key_val(event,1,'banco') }
      cpf.onKeyDown  = function() {autochangeCampo(this.value,16,'banco',event) }
      
      cpf.setAttribute('onKeyPress', 'return key_val(event,1,\'banco\');' );
      cpf.setAttribute('onKeyDown', 'autochangeCampo(this.value,16,\'banco\',event)' );
   }

   
   cpf.focus()
  };
  
/* ------------------------------------------------ */
function calculaC3(numero){
  var x;
  var resto;
  var tam;
  var digito;
  var d;
  var d_aux;

  digito = 0;

  d_aux = numero;
  tam = d_aux.length;
  d = numero

  for (x = 1; x < d_aux.length; x++){
    digito = digito + ((d[x])*((d.length)+2-x));
  }

  digito = digito * 10;
  resto  = digito % 11;
  digito = resto;
  resto  = digito % 10;
  return resto + ' di='+ digito +' re=' + resto;
}


