///////////////////////////////////////////////////////////
// Retorna TRUE quando digitados apenas números ///////////
///////////////////////////////////////////////////////////
function numbersOnly(e){
    if(document.all){//Internet Explorer
        var tecla = event.keyCode;
    }else if (document.layers){//Mozilla
        var tecla = e.which;
    }//if
    
    //numeros de 0 a 9
    if (tecla > 47 && tecla < 58){ 
	    return true;
    }else{//backspace
	    if (tecla != 8){
    	    event.keyCode = 0;
	    }else{
		    return true;
        }
    }
}
///////////////////////////////////////////////////////////
// Valida CPF /////////////////////////////////////////////
///////////////////////////////////////////////////////////

function validaCpf(param_cpf) {
  
   var cpf = param_cpf;
   cpf = cpf.replace( ".", "" );
   cpf = cpf.replace( ".", "" );
   cpf = cpf.replace( ".", "" );
   cpf = cpf.replace( ".", "" );
   cpf = cpf.replace( ".", "" );
   cpf = cpf.replace( "-", "" );
   cpf = cpf.replace( "-", "" );
   cpf = cpf.replace( "-", "" );

	if(cpf == "00000000000") return false;
	if(cpf == "12345678901") return false;
	if(cpf == "11111111111") return false;
	if(cpf == "22222222222") return false;
	if(cpf == "33333333333") return false;
	if(cpf == "44444444444") return false;
	if(cpf == "55555555555") return false;
	if(cpf == "66666666666") return false;
	if(cpf == "77777777777") return false;
	if(cpf == "88888888888") return false;
	if(cpf == "99999999999") return false;
	if(cpf == '') return true;

	x = 0;
	soma = 0;
	dig1 = 0;
	dig2 = 0;
	texto = "";
	cpf1="";
	len = cpf.length; x = len -1;
	
	if(len != 11){ return false; }
	
	for (var i=0; i <= len - 3; i++){
	    y = cpf.substring(i,i+1);
	    soma = soma + ( y * x);
	    x = x - 1;
	    texto = texto + y;
	}
	
	dig1 = 11 - (soma % 11);
	if(dig1 == 10){ dig1=0; }
	if(dig1 == 11){ dig1=0; }
	cpf1 = cpf.substring(0,len - 2) + dig1;
	x = 11; soma=0;
	
	for (var i=0; i <= len - 2; i++) {
        soma = soma + (cpf1.substring(i,i+1) * x);
    	x = x - 1;
	}
	
	dig2= 11 - (soma % 11);
	if(dig2 == 10){ dig2=0; }
	if(dig2 == 11){ dig2=0; }
		
	if ((dig1 + "" + dig2) == cpf.substring(len,len-2)){ return true; }
	return false;
}

// mascaras

function mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}


function formataCep(v){
    v=v.replace(/D/g,"")                //Remove tudo o que não é dígito
    v=v.replace(/^(\d{5})(\d)/,"$1-$2") //Esse é tão fácil que não merece explicações
    return v
}

function formataUsername(v){
	//v=v.replace(/D/g,"")                //Remove tudo o que não é dígito
	//v=v.replace(/^\s+|\s+$/g,"")
	v=v.replace("!","")
	v=v.replace("@","")
	v=v.replace("#","")
	v=v.replace("$","")
	v=v.replace("%","")
	v=v.replace("¨","")
	v=v.replace("&","")
	v=v.replace("*","")
	v=v.replace("(","")
	v=v.replace(")","")
	v=v.replace("+","")
	v=v.replace("á","a")
	v=v.replace("à","a")
	v=v.replace("ã","a")
	v=v.replace("ç","c")
	v=v.replace("é","e")
	v=v.replace("ê","e")
	v=v.replace("í","i")
	v=v.replace("ú","u")
	v=v.replace("ó","o")
	
	//v=v.replace(/^[A-Z]/g,"")
	return v
}
