function filtraTexto(texto, permissao, escopo) {
        try {
                var novoTexto = '';

                if (permissao == 'PodeTerApenas') {
                        for (i = 0; i < texto.length; i++) {
                                if (escopo.indexOf(texto.charAt(i)) != -1) {
                                        novoTexto += texto.charAt(i);
                                }
                        }
                } else {
                        for (i = 0; i < texto.length; i++) {
                                if (escopo.indexOf(texto.charAt(i)) == -1) {
                                        novoTexto += texto.charAt(i);
                                }
                        }
                }

                return novoTexto;
        } catch (e) {
                alert('Erro: '+e.toString());
                return false;
        }
}

function Numero(val) {
        v = val.value;
        v=v.replace();
}

function textoMaiusculo(texto) {
        v = texto.value;
        texto.value = v.toUpperCase();
}

function mascaraMoeda(valor) {
        v = valor.value;
        v=v.replace(/\D/g,""); //Remove tudo o que não é dígito
        if (v.length == 1) {
                v = "0,0" + v;
        } else {
                if (v.length == 2) {
                        v = "0," + v;
                } else {
                        v=v.replace(/(\d{2})$/,",$1"); //Coloca a virgula
                        v=v.replace(/(\d+)(\d{3},\d{2})$/g,"$1.$2"); //Coloca o primeiro ponto
                        var qtdLoop = (v.length-3)/3;
                        var count = 0;
                        while (qtdLoop > count) {
                                count++;
                                v=v.replace(/(\d+)(\d{3}.*)/,"$1.$2"); //Coloca o resto dos pontos
                        }
                        v=v.replace(/^(0)(\d)/g,"$2"); //Coloca hífen entre o quarto e o quinto dígitos
                }
        }
        valor.value = v;
}

function mascaraData(valor) {
        v = valor.value;
        v=v.replace(/\D/g,"")
        v=v.replace(/^(\d\d)(\d)/g,"$1/$2")
        v=v.replace(/(\d{2})(\d)/,"$1/$2")
        valor.value = v;
}

function validaData(idCampo)
{
        var data = document.getElementById(idCampo).value;
        if (data > '') {

                var bissexto = 0;
                var tam = data.length;
                if (tam == 10) {
                        var dia = data.substr(0,2)
                        var mes = data.substr(3,2)
                        var ano = data.substr(6,4)
                        if ((ano > 1900)||(ano < 2100)) {
                                switch (mes) {
                                        case '01':
                                        case '03':
                                        case '05':
                                        case '07':
                                        case '08':
                                        case '10':
                                        case '12':
                                                if  (dia <= 31) {
                                                        return true;
                                                }
                                                break

                                        case '04':
                                        case '06':
                                        case '09':
                                        case '11':
                                                if  (dia <= 30) {
                                                        return true;
                                                }
                                                break
                                        case '02':
                                                /* Validando ano Bissexto / fevereiro / dia */
                                                if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) {
                                                        bissexto = 1;
                                                }
                                                if ((bissexto == 1) && (dia <= 29)) {
                                                        return true;
                                                }
                                                if ((bissexto != 1) && (dia <= 28)) {
                                                        return true;
                                                }
                                                break
                                }
                        }
                }
                campo = document.getElementById(idCampo);
                alert("A data informada é inválida!");
                setTimeout("campo.focus()", 100);
                return false;

        } else {
                return false;
        }
}

function formataTel(objeto) {
        try {
                vr = filtraTexto(objeto.value, "PodeTerApenas", "0123456789");

                if (vr.length <= 4) {
                        objeto.value = vr;
                }

                if (vr.length >= 5 && vr.length <= 8) {
                        objeto.value = vr.substr(0, vr.length - 4) + '-' + vr.substr(-4);
                }

                if (vr.length == 9) {
                        objeto.value = vr.substr(-9, 1) + ') ' + vr.substr(-8, 4) + '-' + vr.substr(-4);
                }

                if (vr.length >= 10) {
                        objeto.value = '(' + vr.substr(-10, 2) + ') ' + vr.substr(-8, 4) + '-' + vr.substr(-4);
                }
        } catch(e) {
                alert(e.toString());
        }
}

function formataCPF(objeto) {
        try {
                vr = filtraTexto(objeto.value, "PodeTerApenas", "0123456789");

                if (vr.length <= 2) {
                        objeto.value = vr;
                }

                if (vr.length >= 3 && vr.length <= 5) {
                        objeto.value = vr.substr(0, vr.length - 2) + '-' + vr.substr(-2);
                }

                if (vr.length >= 6 && vr.length <= 8) {
                        objeto.value = vr.substr(0, vr.length - 5) + '.' + vr.substr(-5, 3) + '-' + vr.substr(-2);
                }

                if (vr.length >= 9) {
                        objeto.value = vr.substr(0, vr.length - 8) + '.' + vr.substr(-8, 3) + '.' + vr.substr(-5, 3) + '-' + vr.substr(-2);
                }
        } catch(e) {
                alert(e.toString());
        }
}

// Valida o CPF
function validaCPF(campo, id) {
        CPF = campo.value;
        var POSICAO, I, N, SOMA, DV, DV_INFORMADO;
        var DIGITO = new Array;
        DV_INFORMADO = CPF.substr(12, 2); // Retira os dois últimos dígitos do número informado

        // Desemembra o número do CPF na array DIGITO
        N = 0;
        for (I = 0; I <= 10; I++) {
                if (!(I == 3 || I == 7)) {
                        DIGITO[N] = CPF.substr(I, 1);
                        N++;
                }

        }

        // Calcula o valor do 10º dígito da verificação
        POSICAO = 10;
        SOMA = 0;
        for (I = 0; I <= 8; I++) {
                SOMA = SOMA + DIGITO[I] * POSICAO;
                POSICAO--;
        }
        DIGITO[9] = SOMA % 11;
        if (DIGITO[9] < 2) {
                DIGITO[9] = 0;
        } else {
                DIGITO[9] = 11 - DIGITO[9];
        }

        // Calcula o valor do 11º dígito da verificação
        POSICAO = 11;
        SOMA = 0;
        for (I = 0; I <= 9; I++) {
                SOMA = SOMA + DIGITO[I] * POSICAO;
                POSICAO--;
        }
        DIGITO[10] = SOMA % 11;
        if (DIGITO[10] < 2) {
                DIGITO[10] = 0;
        } else {
                DIGITO[10] = 11 - DIGITO[10];
        }

        // Verifica se os valores dos dígitos verificadores conferem
        DV = DIGITO[9] * 10 + DIGITO[10];
        if (DV != DV_INFORMADO || DV == '00000000000') {                
                
                return false;
        }
}


function formataCEP(objeto) {
        try {
                vr = filtraTexto(objeto.value, "PodeTerApenas", "0123456789");

                if (vr.length <= 3) {
                        objeto.value = vr;
                }

                if (vr.length >= 4) {
                        objeto.value = vr.substr(0, vr.length - 3) + '-' + vr.substr(-3);
                }
        } catch(e) {
                alert(e.toString());
        }
}

function inteiro(valor) {
        v = valor.value;
        v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
        valor.value = v;
}

/*Insere os campos de experiência profissional*/
function insereExperiencia() {

        ul = document.getElementById('esperiencia');
        indice = parseInt(document.getElementById('qtdEsperiencia').value) + 1;

        if ( indice <= 5 ) {
                h4 = document.createElement('h4');
                texto = 'Outra Esperiência';
                text = document.createTextNode(texto);
                h4.appendChild(text);
                ul.appendChild(h4);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Empresa:');
                label.appendChild(texto);
                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('name', 'empresa[' + indice + ']');
                input.setAttribute('class', 'formGrande');
                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Data de Entrada:');
                label.appendChild(texto);
                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('name', 'dataEntrada[' + indice + ']');
                input.setAttribute('id', 'dataEntrada[' + indice + ']');
                input.setAttribute('class', 'formMedio');
                input.setAttribute('maxlength', '10');
                input.setAttribute('onkeyup', 'mascaraData(this)');
                input.setAttribute('onchange', 'validaData(this.id)');
                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Data de Saída:');
                label.appendChild(texto);
                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('name', 'dataSaida[' + indice + ']');
                input.setAttribute('class', 'formMedio');
                input.setAttribute('maxlength', '10');
                input.setAttribute('onkeyup', 'mascaraData(this)');
                input.setAttribute('onchange', 'validaData(this.id)');
                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Último Cargo:');
                label.appendChild(texto);
                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('name', 'ultimoCargo[' + indice + ']');
                input.setAttribute('class', 'formGrande');
                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                li = document.createElement('li');
                li.setAttribute('class', 'textar')
                label = document.createElement('label');
                label.setAttribute('class', 'labeltextar')
                texto = document.createTextNode('Empresa:');
                label.appendChild(texto);
                textarea = document.createElement('textarea');
                textarea.setAttribute('name', 'funcoesEmpresa[' + indice + ']');
                textarea.setAttribute('cols', '25');
                textarea.setAttribute('rows', '5');
                li.appendChild(label);
                li.appendChild(textarea);
                ul.appendChild(li);
                br = document.createElement('br');
                ul.appendChild(br);

                document.getElementById('qtdEsperiencia').value++;
        }
}

function insereInformacao() {
        ul = document.getElementById('formacao');
        indice = parseInt(document.getElementById('qtdFormacao').value) + 1 ;

        if(indice <= 5){
                h4 = document.createElement('h4');
                texto = document.createTextNode('Outra Formação');
                h4.appendChild(texto);
                ul.appendChild(h4);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Grau de Formação:');
                label.appendChild(texto);
                select = document.createElement('select');
                select.setAttribute('name', 'grauFormacao[' + indice + ']');
                select.setAttribute('class', 'formGrande');

                option = document.createElement('option');
                option.setAttribute('value', '');
                texto = document.createTextNode('Selecione o grau de sua formação');
                option.appendChild(texto);
                select.appendChild(option);

                option = document.createElement('option');
                option.setAttribute('value', 'Ensino Médio');
                texto = document.createTextNode('Ensino Médio');
                option.appendChild(texto);
                select.appendChild(option);
                select.appendChild(option);

                option = document.createElement('option');
                option.setAttribute('value', 'Técnico');
                texto = document.createTextNode('Técnico');
                option.appendChild(texto);
                select.appendChild(option);
                select.appendChild(option);

                option = document.createElement('option');
                option.setAttribute('value', 'Graduação');
                texto = document.createTextNode('Graduação');
                option.appendChild(texto);
                select.appendChild(option);
                select.appendChild(option);

                option = document.createElement('option');
                option.setAttribute('value', 'Pós-Graduação');
                texto = document.createTextNode('Pós-Graduação');
                option.appendChild(texto);
                select.appendChild(option);
                select.appendChild(option);

                option = document.createElement('option');
                option.setAttribute('value', 'Mestrado');
                texto = document.createTextNode('Mestrado');
                option.appendChild(texto);
                select.appendChild(option);
                select.appendChild(option);

                option = document.createElement('option');
                option.setAttribute('value', 'Doutorado');
                texto = document.createTextNode('Doutorado');
                option.appendChild(texto);
                select.appendChild(option);
                select.appendChild(option);

                li.appendChild(label);
                li.appendChild(select);
                ul.appendChild(li);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Nome da Instituição:');
                label.appendChild(texto);

                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('class', 'formGrande');
                input.setAttribute('name', 'instituicao[' + indice + ']');

                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Nome do Curso:');
                label.appendChild(texto);

                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('class', 'formGrande');
                input.setAttribute('name', 'curso[' + indice + ']');

                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Data de Início');
                label.appendChild(texto);

                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('class', 'formMedio');
                input.setAttribute('name', 'dataInicio[' + indice + ']');
                input.setAttribute('id', 'dataInicio[' + indice + ']');
                input.setAttribute('maxlength', '10');
                input.setAttribute('onkeyup', 'mascaraData(this)');
                input.setAttribute('onchange', 'validaData(this.id)');

                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                li = document.createElement('li');
                label = document.createElement('label');
                texto = document.createTextNode('Status: ');
                label.appendChild(texto);
                li.appendChild(label);
                select = document.createElement('select');
                select.setAttribute('name', 'status['+indice+']');
                select.setAttribute('onchange', 'mostraDataConclusao(this.value, '+indice+')');
                option = document.createElement('option');
                texto = document.createTextNode('Selecione');
                option.appendChild(texto);
                select.appendChild(option);
                option = document.createElement('option');
                option.setAttribute('value', 'Cursando');
                texto = document.createTextNode('Cursando');
                option.appendChild(texto);
                select.appendChild(option);
                option = document.createElement('option');
                option.setAttribute('value', 'Concluído');
                texto = document.createTextNode('Concluído');
                option.appendChild(texto);
                select.appendChild(option);
                li.appendChild(select);
                ul.appendChild(li);

                li = document.createElement('li');
                li.setAttribute('id', 'conclusao['+indice+']');
                li.setAttribute('style', 'display:none');
                label = document.createElement('label');
                texto = document.createTextNode('Data de Conclusão:');
                label.appendChild(texto);

                input = document.createElement('input');
                input.setAttribute('type', 'text');
                input.setAttribute('class', 'formMedio');
                input.setAttribute('name', 'dataConclusao[' + indice + ']');
                input.setAttribute('id', 'dataConclusao[' + indice + ']');
                input.setAttribute('maxlength', '10');
                input.setAttribute('onkeyup', 'mascaraData(this)');
                input.setAttribute('onchange', 'validaData(this.id)');

                li.appendChild(label);
                li.appendChild(input);
                ul.appendChild(li);

                br = document.createElement('br');
                ul.appendChild(br);
                
                document.getElementById('qtdFormacao').value++;
        }

}

function adicionarCurso() {
        ul = document.getElementById('cursos');
        indice = parseInt(document.getElementById('qtdCurso').value) + 1 ;

        li = document.createElement('li');
        label = document.createElement('label');
        texto = document.createTextNode('Curso ' + indice + ':');
        label.appendChild(texto);

        input = document.createElement('input');
        input.setAttribute('type', 'text');
        input.setAttribute('class', 'formGrande');
        input.setAttribute('name', 'areaConhecimento[' + indice + ']');

        li.appendChild(label);
        li.appendChild(input);
        ul.appendChild(li);

        document.getElementById('qtdCurso').value++;
}

function mostraDataConclusao(valor, indice) {
        if (valor == "Cursando") {
                document.getElementById('conclusao['+indice+']').style.display = 'none';
        } else {
                document.getElementById('conclusao['+indice+']').style.display = 'block';
        }
}

function validaDados() {
        if (document.curriculo.nome.value.length < 10) {
                alert("Por favor, informe o seu NOME COMPLETO corretamente!");
                document.curriculo.nome.focus();
                return false;
        }

        if (document.curriculo.dataNasc.value.length < 8) {
                alert("Por favor, Informe sua data de Nascimento corretamente!");
                document.curriculo.dataNasc.focus();
                return false;
        }
        if (document.getElementById('sexof').checked == false && document.getElementById('sexom').checked == false) {
                alert("Por favor, Informe seu Sexo!");
                document.getElementById('sexof').focus();
                return false;
        }

        if (document.curriculo.estadoCivil.value.length < 5) {
                alert("Por favor, informe o seu estado civil!");
                document.curriculo.estadoCivil.focus();
                return false;
        }

        if (document.curriculo.logradouro.value.length < 3) {
                alert("Por favor, informe o ENDEREÇO da residência corretamente!");
                document.curriculo.logradouro.focus();
                return false;
        }

        if (document.curriculo.numero.value.length < 1) {
                alert("Por favor, informe o NÚMERO da residência corretamente!");
                document.curriculo.numero.focus();
                return false;
        }

        if (document.curriculo.bairro.value.length < 3) {
                alert("Por favor, informe o BAIRRO da residência corretamente!");
                document.curriculo.bairro.focus();
                return false;
        }

        if (document.curriculo.estado.value.length < 2) {
                alert("Por favor, informe o ESTADO da residência corretamente!");
                document.curriculo.estado.focus();
                return false;
        }
        if (document.curriculo.cidade.value.length < 3) {
                alert("Por favor, informe a CIDADE onde você reside corretamente!");
                document.curriculo.cidade.focus();
                return false;
        }

        if (document.curriculo.telf.value.length < 12 && document.curriculo.telc.value.length < 12) {
                alert("Você deve informar pelo menos um telefone, para que possamos entrar em contato futuramente!");
                document.curriculo.telf.focus();
                return false;
        }

        // os dados com preenchimento obrigatorio estao preenchidos
        if (document.curriculo.email.value.length < 10) {
                alert("Por favor, informe o seu E-MAIL corretamente!");
                document.curriculo.email.focus();
                return false;
        }

        if (document.curriculo.resumo.value.length == "") {
                alert("Por favor, faça um resumo do seu currículo!");
                document.curriculo.resumo.focus();
                return false; 
        }
        
        return true;
}



