// JavaScript Document
function pad(number,length,caracter) {
    var str = '' + number;
    while (str.length < length)
        str = caracter + str;
    return str;
}

function ValidaCPFCNPJ(campo) { 
	var valor = campo.value;
	if (valor=="")
    	return true;
	if(ValidaCPF(valor)) {
		campo.value = pad(valor,11,"0");
		return true;
	}else if(ValidaCNPJ(valor)) {
		campo.value = pad(valor,14,"0");
	 	 return true;
	} else {
		alert('CPF/CNPJ inválido');
		campo.value='';
		campo.focus();
	}
}

function ValidaCPF(CampoNumero) {

  var RecebeCPF=pad(CampoNumero,11,"0");

//Retirar todos os caracteres que nao sejam 0-9

  if (RecebeCPF.length !=11) {
		//return false;
  }
  else
    if (RecebeCPF=="00000000000") {
	  return false;
    }
    if (RecebeCPF=="11111111111") {
	  return false;
    }
    if (RecebeCPF=="22222222222") {
	  return false;
    }
    if (RecebeCPF=="33333333333") {
	  return false;
    }
    if (RecebeCPF=="44444444444") {
	  return false;
    }
    if (RecebeCPF=="55555555555") {
	  return false;
    }
    if (RecebeCPF=="66666666666") {
	  return false;
    }
    if (RecebeCPF=="77777777777") {
	  return false;
    }
    if (RecebeCPF=="88888888888") {
	  return false;
    }
    if (RecebeCPF=="99999999999") {
	  return false;
    }
    else {
      var Numero = new Array();
		for(i=0;i<11;i++) {
	    Numero[i+1] = parseInt(RecebeCPF.substr(i,1));
	  }
	  soma=10*Numero[1]+9*Numero[2]+8*Numero[3]+7*Numero[4]+6*Numero[5]+5*Numero[6]+4*Numero[7]+3*Numero[8]+2*Numero[9];
	  
	  soma=soma-(11*(parseInt(soma/11)));

      if (soma==0 || soma==1) {
        resultado1=0;
      }
      else {
        resultado1=11-soma;
      } 

      if (resultado1==Numero[10]) {
        soma=Numero[1]*11+Numero[2]*10+Numero[3]*9+Numero[4]*8+Numero[5]*7+Numero[6]*6+Numero[7]*5+Numero[8]*4+Numero[9]*3+Numero[10]*2;
        soma=soma-(11*(parseInt(soma/11)));

        if (soma==0 || soma==1) {
          resultado2=0;
        }
        else {
          resultado2=11-soma;
        } 

        if (resultado2==Numero[11]) {
				return true;
        }
        else {
				return false;
        } 
      }
      else {
			return false;
      } 
    } 
} 

function ValidaCNPJ(CampoNumero) {
   RecebeCNPJ=pad(CampoNumero,14,"0");
   if (RecebeCNPJ.length!=14) {
     //return false;
   }
   else
    if (RecebeCNPJ=="00000000000000") {
     return false;
   }
   else {
    var Numero = new Array();
		for(i=0;i<14;i++) {
	    Numero[i+1] = parseInt(RecebeCNPJ.substr(i,1));
	  }

    soma=Numero[1]*5+Numero[2]*4+Numero[3]*3+Numero[4]*2+Numero[5]*9+Numero[6]*8+Numero[7]*7+Numero[8]*6+Numero[9]*5+Numero[10]*4+Numero[11]*3+Numero[12]*2;

    soma=soma-(11*(parseInt(soma/11)));

   if (soma==0 || soma==1) {
     resultado1=0;
   }
   else {
    resultado1=11-soma;
   }
   if (resultado1==Numero[13]) {
    soma=Numero[1]*6+Numero[2]*5+Numero[3]*4+Numero[4]*3+Numero[5]*2+Numero[6]*9+Numero[7]*8+Numero[8]*7+Numero[9]*6+Numero[10]*5+Numero[11]*4+Numero[12]*3+Numero[13]*2;
    soma=soma-(11*(parseInt(soma/11)));
    if (soma==0 || soma==1) {
     resultado2=0;
    }
   else {
   resultado2=11-soma;
   }
   if (resultado2==Numero[14]) {
    return true;
   }
   else {
     return false;
   }
  }
  else {
   return false;
  }
 }
}

