/***************************/
/* VALIDAÇÃO DE CFP E CNPJ */

function ClearStr(str, char)
{
  while((cx=str.indexOf(char))!=-1)
  {		
    str = str.substring(0,cx)+str.substring(cx+1);
  }
  return(str);
}

function ParseNumb(c)
{
  c=ClearStr(c,"-");
  c=ClearStr(c,"/");
  c=ClearStr(c,",");
  c=ClearStr(c,".");
  c=ClearStr(c,"(");
  c=ClearStr(c,")");
  c=ClearStr(c," ");
  if((parseFloat(c) / c != 1))
  {
    if(parseFloat(c) * c == 0)
    {
      return(c);
    }
    else
    {
      return(0);
    }
  }
  else
  {
    return(c);
  }
  return(c);
}


function Verify(CNUMB,CTYPE)
{
  if(CTYPE=='CPF'){
    if(CNUMB=='12345678909') return false;
    if(CNUMB=='11111111111') return false;
    if(CNUMB=='22222222222') return false;
    if(CNUMB=='33333333333') return false;
    if(CNUMB=='44444444444') return false;
    if(CNUMB=='55555555555') return false;
    if(CNUMB=='66666666666') return false;
    if(CNUMB=='77777777777') return false;
    if(CNUMB=='88888888888') return false;
    if(CNUMB=='99999999999') return false;
    if(CNUMB=='00000000000') return false;
  }

  CNUMB=ParseNumb(CNUMB)
  if(CNUMB == 0)
  {
    return(false);
  }
  else
  {
    g=CNUMB.length-2;
    if(TestDigit(CNUMB,CTYPE,g))
    {
      g=CNUMB.length-1;
      if(TestDigit(CNUMB,CTYPE,g))
      {	
        return(true);
      }
      else
      {
        return(false);
      }
    }
    else
    {
      return(false);
    }
  }
}

function TestDigit(CNUMB,CTYPE,g)
{
  var dig=0;
  var ind=2;
  for(f=g;f>0;f--)
  {
    dig+=parseInt(CNUMB.charAt(f-1))*ind;
    if (CTYPE=='CNPJ')
    { if(ind>8) {ind=2} else {ind++} }
    else
    { ind++ }
  }
  dig%=11;
  if(dig<2)
  {
    dig=0;
  }
  else
  {
    dig=11-dig;
  }
  if(dig!=parseInt(CNUMB.charAt(g)))
  {
    return(false);
  }
  else
  {
    return(true);
  }
}



/***************************/
/* VALIDAÇÃO DE NIRE */

function fc_validaNire(VMNire){
    //if(comecacom359) return true;
    var VMVetFator = new Array(2,10,9,8,7,6,5,4,3,2);
    var VMVetNire = new Array(0,0,0,0,0,0,0,0,0,0);
    
    for(i=0;i<10;i++){
        VMVetNire[i] = parseInt(VMNire.charAt(i));
    }
 
    VMTotal = 0;
    
    for(i=0;i<10;i++){
        VMTotal = VMTotal + (VMVetFator[i] * VMVetNire[i]);
    }

    VMResto = VMTotal % 11;

    if(VMResto==0) VMDv = '1';
    else if(VMResto==1) VMDv = '0';
    else VMDv = ''+(11-VMResto);
    return (VMNire.substr(0,10)+VMDv == VMNire);
}

/***************************/
/* VALIDAÇÃO DE NIRE FILIAL*/

function fc_validaNireFilial(VMNire){

  var msg1 = 'Atenção! \n\nO NIRE informado corresponde a uma filial, e as informações contidas na Ficha de Breve Relato Simples (FBR) serão da matriz. Se a matriz não possuir registro na Junta Comercial do Estado de São Paulo, esta FBR não conterá informações referentes ao(s) sócio(s), ao(s) diretor(es) ou ao(s) administrador(es).\nObservação: a Junta Comercial do Estado de São Paulo não se comunica com a Junta Comercial de outros Estados.\n\nDeseja confirmar seu pedido?';
  var msg2 = 'Atenção! \n\nO NIRE informado corresponde a uma empresa localizada em outro Estado. Lembramos que, caso a(s) empresa(s) consultada(s) tenha(m) matriz(es) localizada(s) fora do Estado de São Paulo e não possua(m) filial(is) na Jucesp, a Ficha de Breve Relato Simples não poderá ser emitida. Caso a(s) empresa(s) tenha(m) filial(is), não haverá informações cadastrais dos sócios/diretoria cadastradas na(s) FBR(s). \n\nDeseja confirmar seu pedido?';
  if (parseInt(VMNire.charAt(0)) == 3)
  {
    if (parseInt(VMNire.charAt(1)) == 5)
    {
      if (parseInt(VMNire.charAt(2)) == 9)
      {
        return confirm(msg1);
        /*if (!confirm())
          return false;
        else
          return true;*/
      }
      else if(!VMNire.charAt(2).match('[12345]'))
      {
        return confirm(msg2);
        /*if (!confirm())
          return false;
        else
          return true;*/
      }
      else
      {
         return true;
      }
    }
    else
    {
      return confirm(msg2);
      return true;
    }
  }
  else
    return confirm(msg2);
    return true;
}
