function limpaParaMascara(sujeira,filtro,tipo){
	numeros = "0123456789";
	valores = "0123456789,";
	letras  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁÉÍÓÚÀÈÌÒÙÄËÏÖÜÂÊÎÔÛÃÕáéíóúàèìòùäëïöüâêîôûãõçÇ&ªº'\"\|@_<>!#$%&*()={[}]?:+-.,;/\\0123456789 ";
	letra_numero  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
	retorno2 = '';
	if (tipo == 1) {
		if (sujeira.substring(0,1) == "-") ind = 1;
		else ind = 0;
	}
	else ind = 0;
	switch (filtro){
		case 'numeros': {
			for ( i=ind; i < sujeira.length; i++ ) {
				if( numeros.indexOf(sujeira.charAt(i))>-1 ) { 
					retorno2 += sujeira.charAt(i);
				}
			}
		break;	}
		case 'valores': {
			for ( i=ind; i < sujeira.length; i++ ) {
				if( valores.indexOf(sujeira.charAt(i))>-1 ) { 
					retorno2 += sujeira.charAt(i);
				}
			}
			if (sujeira.charAt(0)=='-') {
				retorno2 = "-"+retorno2;
			}
		break;	}
		case 'letras': {
			for ( i=0; i < sujeira.length; i++ ) {
				if( letras.indexOf(sujeira.charAt(i))>-1 ) { 
					retorno2 += sujeira.charAt(i);
				}
			}
		break;	}
		case 'letra_numero': {
			for ( i=ind; i < sujeira.length; i++ ) {
				if( letra_numero.indexOf(sujeira.charAt(i))>-1 ) { 
					retorno2 += sujeira.charAt(i);
				}
			}
		break;	}
	}
	if (tipo == 1) {
		if (sujeira.substring(0,1) == "-") retorno2 = "-" + retorno2;
	}
	return retorno2;
}

function validaCPF (CPF) {
    CPF = limpaParaMascara(CPF,'numeros');    
    if (CPF.length != 11) { for(countZeros=0 ; countZeros < ((11-CPF.length)+2) ; countZeros++){ CPF = "0"+CPF; } };
	if(CPF == '00000000000'){ return false; }
    soma = 0;
    for(i=0 ; i<9 ; i++) {
        soma = soma + eval(CPF.charAt(i) * (10 - i));
    }
    Resto = 11 - ( soma - (parseInt(soma / 11) * 11) );
    if ( (Resto == 10) || (Resto == 11) ) { Resto = 0; }
    if ( Resto != eval( (CPF.charAt(9) ) ) ) { return false; }
	soma = 0;
    for (i = 0;i<10;i++) {
        soma = soma + eval(CPF.charAt(i) * (11 - i));
	} 
    Resto = 11 - ( soma - (parseInt(soma / 11) * 11) );
    if ( (Resto == 10) || (Resto == 11)) {
		Resto = 0;
	}
    if ( Resto != eval( (CPF.charAt(10)) )) {
		return false;
	}
	return true;
}
