function ChecaCPF(strCPF) 
{
	var Soma;
	var Resto;
	Soma = 0;   
	strCPF  = RetiraCaracteresInvalidos(strCPF,11);
	if (strCPF == "00000000000")
		return false;
	if (strCPF == "11111111111")
		return false;
	if (strCPF == "22222222222")
		return false;
	if (strCPF == "33333333333")
		return false;
	if (strCPF == "44444444444")
		return false;
	if (strCPF == "55555555555")
		return false;
	if (strCPF == "66666666666")
		return false;
	if (strCPF == "77777777777")
		return false;
	if (strCPF == "88888888888")
		return false;
	if (strCPF == "99999999999")
		return false;
   for (i=1; i<=9; i++)
		Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (11 - i); 
   Resto = (Soma * 10) % 11;
	if ((Resto == 10) || (Resto == 11)) 
		Resto = 0;
   if (Resto != parseInt(strCPF.substring(9, 10)) )
      return false;
	Soma = 0;
   for (i = 1; i <= 10; i++)
        Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (12 - i);
   Resto = (Soma * 10) % 11;
   if ((Resto == 10) || (Resto == 11)) 
		Resto = 0;
	if (Resto != parseInt(strCPF.substring(10, 11) ) )
        return false;
   return true;
}
function ChecaCNPJ(cgc) 
{

    var CGC1, CGC2; 
    var Soma, Digito;
    var i , j; 
    var ContIni , ContFim;
    var controle;
	 cgc  = RetiraCaracteresInvalidos(cgc,14);
	 if (cgc == "00000000000000")
		return false;
	 if (cgc == "11111111111111")
		return false;
	 if (cgc == "22222222222222")
		return false;
	 if (cgc == "33333333333333")
		return false;
	 if (cgc == "44444444444444")
		return false;
	 if (cgc == "55555555555555")
		return false;
	 if (cgc == "66666666666666")
		return false;
	 if (cgc == "77777777777777")
		return false;
	 if (cgc == "88888888888888")
		return false;
	 if (cgc == "99999999999999")
		return false;
    CGC1 = cgc.substring(0,12);
    CGC2 = cgc.substring(12,14);
    controle = "";
    ContIni = 1;
    ContFim = 12;
	 K = 0;
    for (j = 1; j <= 2; j++)
		{
         Soma = 0;
         for (i = ContIni; i <=ContFim; i++)
			{
			  Mult = (ContFim + 1 + j - i);
			  if (Mult > 9)
				  Mult = Mult - 8;
           Soma = Soma + (parseInt(CGC1.substring(i - j, i-K)) * Mult);
         }
         if (j == 2)
			 Soma = Soma + (2 * Digito);
	      Digito = (Soma * 10) % 11;
         if (Digito == 10)
				 Digito = 0;
         controle = controle + Digito;
         ContIni = 2;
			K=1;
         ContFim = 13;
       } 
       if (controle != CGC2)
          return false;
		return true;
}

function RetiraCaracteresInvalidos(strCampo,tam) {
	nTamanho = strCampo.length;
	szCampo = "";
	j=0;
	for (i = nTamanho-1;i>=0;i--) 
	{
		if (isDigit(strCampo.charAt(i)))	{
			szCampo = strCampo.charAt(i) + szCampo;
			j++;
			if (j == tam) {
				break;
			}
		}
	}
	if (szCampo.length < tam) {
		for (i = szCampo.length;i<tam;i++) 
		{
			szCampo = "0" + szCampo;
		}
	}
   return szCampo;
}
function isDigit (c){   
   return ((c >= "0") && (c <= "9"))
}  
function isEmpty(s){
    return ((s.value == null) || (s.value.length == 0))
}

function ChecaCNPJCPF(campo) {
	if (campo.value.length<11) {
		return false;
	}	
				
	if (isEmpty(campo)) {
    	return (false);
	}
	
	if (campo.value.length>11) {
		if (!ChecaCNPJ(campo.value)) {
	    	return (false);
		}
	} else {
    	if (!ChecaCPF(campo.value)) {
        	return (false);
		}
   }
   return (true);
}