function replace(strIn, strFind, strRpc) {
	outV = strFind; // replace this
	add = strRpc; // with this
	temp = "" + strIn // temporary holder

	while (temp.indexOf(outV)>-1) {
	pos= temp.indexOf(outV);
	temp = "" + (temp.substring(0, pos) + add + 
	temp.substring((pos + outV.length), temp.length));
	}
	return temp;
}

String.prototype.trim = function(){
   return this.replace(/(^\s*)|(\s*$)/g, "");
}

function validaCPFCNPJ(num) {
	retorno = true;		
	
	vr = replace(num,'-','');
	vr = replace(vr,'.','');
	vr = replace(vr,'/','');
	vr = vr.trim();

	if (vr.length == 11) {

			for (i = 0; i < vr.length - 1; i++) {
				if (vr.charAt(i) != vr.charAt(i + 1)) {
					digitos_iguais = 0;
					break;
				}
			}	
		
		if (!digitos_iguais) {
			
			numeros = vr.substring(0,9);
			digitos = vr.substring(9);
			soma = 0;

			for (i = 10; i > 1; i--) {
				soma += numeros.charAt(10 - i) * i;
			}

			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

			if (resultado != digitos.charAt(0)) {
				retorno = false;
			}

			numeros = vr.substring(0,10);
			soma = 0;

			for (i = 11; i > 1; i--) {
				soma += numeros.charAt(11 - i) * i;
			}

			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;

			if (resultado != digitos.charAt(1)) {
				retorno = false;
			}
		
		} else {			
			retorno = false;
		}
		
	} else if (vr.length >= 14) {

		for (i = 0; i < vr.length - 1; i++) {
			if (vr.charAt(i) != vr.charAt(i + 1)) {
				digitos_iguais = 0;
				break;
			}
		}
			  
		if (!digitos_iguais) {
			
			tamanho = vr.length - 2
			numeros = vr.substring(0,tamanho);
			digitos = vr.substring(tamanho);
			soma = 0;
			pos = tamanho - 7;
			
			for (i = tamanho; i >= 1; i--) {
				soma += numeros.charAt(tamanho - i) * pos--;
				if (pos < 2) {
					pos = 9;
				}
			}
			
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
					
			if (resultado != digitos.charAt(0)) {
				retorno = false;
			}

			tamanho = tamanho + 1;				
			numeros = vr.substring(0,tamanho);
			soma = 0;
			pos = tamanho - 7;

			for (i = tamanho; i >= 1; i--) {
				soma += numeros.charAt(tamanho - i) * pos--;
				if (pos < 2) {
					pos = 9;
				}
			}
			
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
					
				if (resultado != digitos.charAt(1)) {

					  retorno = false;

				}

		} else {

			retorno = false;
		}

	}  else {
		
		retorno = false;
		
	}
	
	return retorno;

}