/** 
 * Retorna TRUE se o VPF for válido 
 */
function CCPF_isCPFValido(cpf) {
    cpf = C_EliminaMascaraCPFCNPJ(cpf);
    if (cpf == "") {
        return true;
    } else if (String(cpf).length != 11) {
        return false;
    }
    rcpf1 = cpf.substr(0,9);
    rcpf2 = cpf.substr(9,2);
    d1 = 0;
    for (i=0; i<9; i++) {
        d1 += rcpf1.charAt(i)*(10-i);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) {
        d1 = 0;
    }
    if (rcpf2.toString().charAt(0) != d1.toString().charAt(0)) {
        return false;
    }
    d1 *= 2;
    for (i=0; i<9; i++) {
        d1 += rcpf1.charAt(i)*(11-i);
    }
    d1 = 11 - (d1 % 11);
    if (d1 > 9) {
        d1 = 0;
    }
    if (rcpf2.toString().charAt(1) != d1.toString().charAt(0)) {
        return false;
    }
    charOld = cpf.charAt(0);
    todosNumsIguais = false;
    for (i=1;i<String(cpf).length;i++) {
        todosNumsIguais = (charOld == cpf.charAt(i));
        if (!todosNumsIguais) {
            return true; 
        } else {
            charOld = cpf.charAt(i);
        }
    }
    return false;
}

/**
 * Verifica se o valor do campo é válido
 */
function CCPF_verificaValor(ctrl) {
    //se não for válido, limpa o campo e mostra uma mensagem
    if (!CCPF_isCPFValido(ctrl.value)) {
        //alert (msgKey("label.js.cpfInvalido",ctrl.value));
        //ctrl.focus();
        if(ctrl.className.indexOf('erro')==-1){
        	ctrl.className=ctrl.className+' erro';
        }
        return false;
    }
    if(ctrl.className.indexOf('erro')!=-1){
      	ctrl.className=ctrl.className.substring(0,ctrl.className.indexOf('erro'));
    }
    return true;
}    

/** 
 * Tratamento de digitação no componente.
 */
function CCPF_KPS(ctrl, event) {
    ctrl.setAttribute("formato","000.000.000-00");
    CM_KPS(ctrl, event);
}

/**
 * Trata a digitação no campo.
 */
function CCPF_KDN(ctrl, event) {
    ctrl.setAttribute("formato","000.000.000-00");
    CM_KDN(ctrl, event);
}

/** Trata a saída do campo para não permitir que o campo fique com valores inválidos **/
function CCPF_BLR(ctrl) {
    CCPF_verificaValor(ctrl);
}

function CCPF_MOV(ctrl, e){
	if(ctrl.className.indexOf('erro')!= -1){
		C_mostraHint(e, msgKey('label.js.cpfInvalido',ctrl.value));
	}
}

function CCPF_MMOV(ctrl, event){
	if(ctrl.className.indexOf('erro')!= -1){
		C_moveHint(event);
	}
}

function CCPF_MOUT(ctrl, event){
	if(ctrl.className.indexOf('erro')!= -1){
		C_escondeHint();
	}
}


