function Resto(S)

{

	var invertido = Inverte(Limpa(S));

	var soma = 0;

	for (var i=0; i<invertido.length; i++) {

		soma = soma + (i+2) * eval(invertido.charAt(i));

	}

	soma *= 10;

	return ((soma % 11) % 10);

}



function Limpa(S)

{

	var Digitos = "0123456789";

	var temp = "";

	var digito = "";

	for (var i=0; i<S.length; i++){

		digito = S.charAt(i);

		if (Digitos.indexOf(digito)>=0) {

			temp = temp + digito;

		}

	}

	return temp;

}



function Inverte(S)

{

	var temp = "";

	for (var i=0; i<S.length; i++) {

		temp = S.charAt(i) + temp;

	}

    return temp;

}







function Mascarar_CPF(objeto,tammax,teclapres)

{

	var keypress = teclapres.keyCode;

	

	campo = eval(objeto);



	if (keypress != 8) {

		

	 	if (campo.value.length == 3 ) { 

			campo.value = campo.value;

			campo.value = campo.value + '.';

		}

		if (campo.value.length == 7 ) { 

			campo.value = campo.value;

			campo.value = campo.value + '.';

		}

		if (campo.value.length == 11 ) { 

			campo.value = campo.value;

			campo.value = campo.value + '-';

		}

	}

}



function Validar_CPF(cpf)

{

	if ((cpf == "000.000.000-00") || (cpf == "111.111.111-11") || (cpf == "222.222.222-22") || (cpf == "333.333.333-33") || (cpf == "444.444.444-44") || (cpf == "555.555.555-55") || (cpf == "666.666.666-66") || (cpf == "777.777.777-77") || (cpf == "888.888.888-88") || (cpf == "cpf")) {

		return (false);

	}

	var result = "";

	var OK = false;

	var temp = Limpa(cpf);



	if (temp.length>10) {

		var work = temp.substring(0,(temp.length)-2);

		var resto = Resto(work);

		OK = (resto == eval(temp.charAt((temp.length)-2)));

		if (OK){

			work = work + resto;

			resto = Resto(work);

			OK = (resto == eval(temp.charAt((temp.length)-1)));

		}

	}





if (OK){

		return (true);

	}

	else 

	{

		return (false);

	}

}

