function apenasNumeros(str){
// Deixa so' os digitos no numero
var digitos = "0123456789";
var temp = "";
var digito = "";
	for (var i=0; i<str.length; i++){
      digito = str.charAt(i);
      if (digitos.indexOf(digito)>=0)
      {
      	temp=temp+digito;
      }
	}
	return temp;
}

function inverteString(str)
{
	// Inverte o string S
	var temp="";
    for (var i=0; i<str.length; i++){
        temp=str.charAt(i)+temp
    }
    return temp
}

function Resto(S)
{
	// Retorna o digito verificador (entrar com S "limpo")
	var invertido = inverteString(apenasNumeros(S));
	var soma = 0;
   for (var i=0; i<invertido.length; i++){
        soma=soma+(i+2)*parseInt(invertido.charAt(i))
   }
   soma*=10;
   return ((soma % 11) % 10)
}

// Confere se o CPF dado esta' OK
function isCPF( cpf)
{
	var result = "";
	var OK = false;
	var temp = apenasNumeros(cpf);
	if( apenasNumerosRepetidos( temp))
		return false;

	if (temp.length>10)
	{
		var work=temp.substring(0,(temp.length)-2)
		var resto = Resto(work);
		OK = (resto==parseInt(temp.charAt((temp.length)-2)));
		if (OK)
		{
			work=work+resto;
			resto= Resto(work);
			OK = (resto==parseInt(temp.charAt((temp.length)-1)));
		}
	}
	return (OK)
}

function apenasNumerosRepetidos(str)
{
	var prim= str.charAt(0);
	var numbers= new String("0123456789");
	if (numbers.indexOf(prim) == -1)
		return false;
	for( var i=1; i< str.length; i++)
	{
		if( str.charAt(i) != prim)
			return false;
	}
	return true;
}

function ApagaDepois(Source,Chars)
{
	var temp =  Source;
	var i = Source.indexOf(Chars);
   if (i>=0)
   	temp = Source.substring(0,i)
   return temp
}
