
function isEmail(strValue)
{
  var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;

  return (strValue != '' && objRE.test(strValue));
}

function validaCPF(strValor)
{
    var blnRetorno;
    var intCasa = 0;
    var intSoma = 0;
    var intDigito1;
    var intDigito2;
    var intAux;
    var strTexto;
    strValor = numval(strValor);
    blnRetorno = !(strValor == "00000000000" || strValor == "11111111111" || strValor == "22222222222" || strValor == "33333333333" || strValor == "44444444444" || strValor == "55555555555" || strValor == "66666666666" || strValor == "77777777777" || strValor == "88888888888" || strValor == "99999999999")
    if(blnRetorno) {
      intAux = strValor.length-1;
      for (var intI=0;intI<=strValor.length-3;intI++) {
        intCasa = strValor.substring(intI,intI+1);
        intSoma = intSoma + (intCasa * intAux);
        intAux = intAux - 1;
      }
      intDigito1 = 11 - (intSoma % 11);
      if(intDigito1 == 10) intDigito1=0 ;
      if(intDigito1 == 11) intDigito1=0 ;
      strTexto = strValor.substring(0,strValor.length - 2) + intDigito1;
      intAux = 11; intSoma=0;
      for(var intI=0;intI<=strValor.length - 2;intI++) {
        intSoma = intSoma + (strTexto.substring(intI,intI+1) * intAux);
        intAux = intAux - 1;
      }
      intDigito2 = 11 - (intSoma % 11);
      if(intDigito2 == 10) intDigito2=0;
      if(intDigito2 == 11) intDigito2=0;
      blnRetorno = ((intDigito1 + "" + intDigito2) == strValor.substring(strValor.length,strValor.length-2))
    }
    return(blnRetorno);
}


function validaCNPJ(valorStr)
{
    var localCGC;
    var localResult;
    var digit1;
    var digit2;
    var ii;
    var nComp;
    var soma;
    localCGC = numval(valorStr);

    if(localCGC.length < 14) {
        var diff = 14-localCGC.length;
        for (var d = 0; d <  diff; d++) {
            localCGC = "0"+localCGC;
        }
    }else if(localCGC.length == 15) {
        localCGC.substring(1);
    }

    if((localCGC.length < 14) || (localCGC == "00000000000000")) {
        localResult = false ;
    }else{
        localResult = true ;
    }

    if(localResult == true) {
        soma = 0 ;
        for (ii = 1; ii <= 12; ii++) {
            if (ii < 5) {
                soma = soma + (localCGC.substring(ii-1,ii) * (6-ii));
            } else {
                soma = soma + (localCGC.substring(ii-1,ii) * (14-ii));
            }
        }
        digit1 = 11 - (soma % 11);
        if (digit1 > 9) digit1 = 0;
        soma = 0 ;
        for(ii = 1; ii <= 13; ii++) {
            if(ii < 6) {
                soma += (localCGC.substring(ii-1,ii) * (7-ii));
            }else{
                soma += (localCGC.substring(ii-1,ii) * (15-ii));
            }
        }
        digit2 = 11 - (soma % 11);
        if(digit2 > 9) digit2 = 0;

        if((digit1 == localCGC.substr(12,1)) && (digit2 == localCGC.substr(13,1))) {
            localResult = true
        } else  {
            localResult = false
        }

    }
    return localResult;
}

function numval(strValor) {
  if(!strValor) return '';
  strValor.toString();
  var out = '';
  for(var x = 0; x < strValor.length; x++) {
      var ch = strValor.charAt(x);
      switch(ch) {
          case '1' : case '2' : case '3' : case '4' : case '5' :
          case '6' : case '7' : case '8' : case '9' : case '0' :
          out += ch; break;
      }
      /* if(ord($ch)>=48 && ord($ch)<=57) $out .= $ch; */
  }
  return out;
}

function validaCEP(valor)
{

    valor = numval(valor);

    if(valor.substring(0,1) == '0') valor = valor.substring(1); // o parseInt não entende número começando com zero

    valorInt = parseInt(valor);

    if(isNaN(valorInt)) {

        return false;

    }else if(valorInt < 1000000 || valorInt > 99999999) {

        return false;

    }else{

        return true;

    }
}
