// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function habilitaLink(){
    element = document.getElementById('menu_link');
    element2 = document.getElementById('paginas');
    if (element.value != 'paginas?pagina='){
        element.disabled = false ;
        element2.disabled = true ;
    }
}


function validaCampoVazio() {
    campo = document.getElementById('nome_arquivo');
    if(campo.value == "") {
        alert("Nome da página não pode ser vazio!");
        campo.focus();
        return false;
    }
}

function validaCamposYouTube() {
    title = document.getElementById('video_title');
    if(title.value == "") {
        alert("Título não pode ser vazio!");
        title.focus();
        return false;
    }
    content = document.getElementById('video_content');
    if(content.value == "") {
        alert("Descrição não pode ser vazio!");
        content.focus();
        return false;
    }
    keywords = document.getElementById('video_keywords');
    if(keywords.value == "") {
        alert("Palavras-chave não pode ser vazio!");
        keywords.focus();
        return false;
    }
}

function validaEmail(e){
    var returnval=emailfilter.test(e.value)
    if (returnval==false){
        alert("Por favor digite um E-mail válido!")
        e.select()
        return false;
    }
    alert("Enviado com Sucesso. Obrigado !")
    return returnval
}

function validaTexto(campoTexto){
    var texto = campoTexto.value;
    if (texto == ""){
        alert("Preencha o campo de texto!")
    }
    return texto
}

function mascaraData(campoData){
    
    var data = campoData.value;
    if (data.length == 2){
        data = data + '/';
        campoData.value = data;
        return true;
    }
    if (data.length == 5){
        data = data + '/';
        campoData.value = data;
        return true;
    }
}

function validaBanner(){

    if(document.getElementById('banner_banner').value ==""){
        alert('Por favor cadastre um banner');
        return false;
        
    }
}

function validaForm(){
    login = document.getElementById('user_login').value;
    user_password = document.getElementById('user_password').value;
    user_password_confirmation = document.getElementById('user_password_confirmation').value;
    
    if(login == "") {
        alert("Campo login não preenchido na aba Dados do Usuário");
        return false;
    }
    else if(user_password == "") {
        alert("Campo senha não preenchido na aba Dados do Usuário");
        return false;
    }
    else  if(user_password_confirmation == "") {
        alert("Campo confirmar senha não preenchido na aba Dados do Usuário");
        return false;
    }
    else  if (user_password != user_password_confirmation) {
        alert("As Senhas não conferem na aba Dados do Usuário.");
        return false;
    }
    else{
        return true;
    }
}
function validaFormEditar(){
    login = document.getElementById('user_login').value;
    //    user_password = document.getElementById('user_password').value;
    //    user_password_confirmation = document.getElementById('user_password_confirmation').value;

    if(login == "") {
        alert("Campo login não preenchido na aba Dados do Usuário");
        return false;
    }
    else{
        return true;
    }
}
function validaFormOuvidoria(){
    categoria = document.getElementById('categoria').value;
    nome = document.getElementById('nome').value;
    email = document.getElementById('email').value;
    telefone = document.getElementById('telefone').value;
    assunto = document.getElementById('assunto').value;
    mensagem = document.getElementById('mensagem').value;

    if(categoria == "") {
        alert("Campo categoria não selecionado");
        document.getElementById('categoria').focus();
        return false;
    }
    if(nome == "") {
        alert("Campo nome não preenchido");
        document.getElementById('nome').focus();
        return false;
    }
    if(email == "") {
        alert("Campo email não preenchido");
        document.getElementById('email').focus();
        return false;
    }else{
        //validar email(verificao de endereco eletronico)
        parte1 = email.indexOf("@");
        parte3 = email.length;
        if (!(parte1 >= 3 && parte3 >= 9)) {
            alert("O campo email deve ser conter um endereço eletrônico!");
            document.getElementById('email').focus();
            return false;
        }
    //        if(emailfilter.test(email.value) == false){
    //            alert("Por favor digite um E-mail válido!");
    //            document.getElementById('email').focus();
    //            return false;
    //        }
    }
    if (telefone == "") {
        alert("Campo telefone não preenchido");
        document.getElementById('telefone').focus();
        return false;
    }
    if (assunto == "") {
        alert("Campo assunto não preenchido");
        document.getElementById('assunto').focus();
        return false;
    }
    if (mensagem == "") {
        alert("Campo mensagem não preenchido");
        document.getElementById('mensagem').focus();
        return false;
    }
    alert("Enviado com Sucesso. Obrigado !")
    return true;
    
}

//--->Função para verificar se o valor digitado é número...<---
function digitos(event){
    if (window.event) {
        // IE
        key = event.keyCode;
    } else if ( event.which ) {
        // netscape
        key = event.which;
    }
    if ( key != 8 || key != 13 || key < 48 || key > 57 )
        return ( ( ( key > 47 ) && ( key < 58 ) ) || ( key == 8 ) || ( key == 13 ) );
    return true;
}

function Mascara(tipo, campo, teclaPress) {
    if (window.event)
    {
        var tecla = teclaPress.keyCode;
    } else {
        tecla = teclaPress.which;
    }

    var s = new String(campo.value);
    // Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
    s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

    tam = s.length + 1;

    if ( tecla != 9 && tecla != 8 ) {
        switch (tipo)
        {
            case 'CPF' :
                if (tam > 3 && tam < 7)
                    campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
                if (tam >= 7 && tam < 10)
                    campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
                if (tam >= 10 && tam < 12)
                    campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
                break;

            case 'CNPJ' :

                if (tam > 2 && tam < 6)
                    campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
                if (tam >= 6 && tam < 9)
                    campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
                if (tam >= 9 && tam < 13)
                    campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
                if (tam >= 13 && tam < 15)
                    campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
                break;

            case 'TEL' :
                if (tam > 2 && tam < 4)
                    campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
                if (tam >= 7 && tam < 11)
                    campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
                break;

            case 'DATA' :
                if (tam == 3 )
                    campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
                if (tam > 4 && tam < 11)
                    campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
                break;

            case 'DATA_TIME_LINE' :
                if (tam == 3 )
                    campo.value = s.substr(0,2) + '/' + s.substr(4, tam);
                if (tam > 4 && tam < 8)
                    campo.value = s.substr(0,2) + '/' + s.substr(2,4);
                break;

            case 'CEP' :
                if (tam > 2 && tam < 4)
                    campo.value = s.substr(0,2) + ' ' + s.substr(2, tam);
                if (tam > 5 && tam < 7)
                    campo.value = s.substr(0,5) + '-' + s.substr(5, tam);
                break;

            case 'HORA' :
                if (tam > 2 && tam < 4)
                    campo.value = s.substr(0,2) + ':' + s.substr(2, tam);
                if (tam > 4 && tam < 11)
                    campo.value = s.substr(0,2) + ':' + s.substr(2,2) + ':' + s.substr(4,tam-4);
                break;

        }
    }

}
function validaSenha(){


    campo = document.forms[0];

    if(campo.login.value == "") {
        alert("Campo login não preenchido");
        campo.login.focus();
        return false;
    }
    else if(campo.password.value == "") {
        alert("Campo senha não preenchido");
        campo.password.focus();
        return false;
    }
    else  if(campo.password_confirmation.value == "") {
        alert("Campo confirmar senha não preenchido");
        campo.password_confirmation.focus();
        return false;
    }
    else  if (campo.password.value != campo.password_confirmation.value) {
        alert("As Senhas não conferem.");
        campo.password.focus();
        return false;
    }
    else
        return true;
}

function mascaraHora(campoHora){
    var hora = campoHora.value;
    if (hora.length == 2){
        hora = hora + ':';
        campoHora.value = hora;
        return true;
    }
}

function validaVagas(){

    if (document.getElementById('vaga_departamento').value==""){

        alert('Preencha o departamento !');
        return false;
    }

    if (document.getElementById('vaga_nivel_experiencia').value==""){

        alert('Preencha o nivel de experiência !');
        return false;
    }

    if (document.getElementById('vaga_titulo_cargo').value==""){

        alert('Preencha o título do cargo !');
        return false;
    }

    if (document.getElementById('vaga_periodo').value==""){

        alert('Preencha o período !');
        return false;
    }

    if (document.getElementById('vaga_duracao').value==""){

        alert('Preencha a duração ! !');
        return false;
    }

    if (document.getElementById('vaga_remuneracao_id').value==""){

        alert('Preencha a remuneração ! !');
        return false;
    }

    if (document.getElementById('vaga_beneficios').value==""){

        alert('Preencha os benefícios ! !');
        return false;
    }

    if (document.getElementById('vaga_numero_vagas').value==""){

        alert('Preencha os numero de vagas  !');
        return false;
    }

    if (document.getElementById('vaga_sexo').value==""){
    
        alert('Preencha o sexo !');
        return false;
    }

    if (document.getElementById('vaga_descricao_oferta').value==""){

        alert('Preencha a descrição da oferta  !');
        return false;
    }

    if (document.getElementById('vaga_perfil_candidato').value==""){

        alert('Preencha o perfil do candidato  !');
        return false;
    }

    return true;
    
}
function before_preview_pagina(){
    document.getElementById("preview_content").value=document.getElementById("pagina[html]").value;
    document.getElementById("preview_layout").value=document.getElementById("template").value;
}

function moedaMask(formatar, correcao){
    var valorFormatar = new String(formatar);
    pontos = valorFormatar.length / 3 + 1;
    for(a=0; a < pontos; a++){
        valorFormatar = valorFormatar.replace(",","");
        valorFormatar = valorFormatar.replace(".","");
    }
    if( valorFormatar.length < 15 ){
        var valorFormatado ="";
        if ( isNaN(correcao) ){
            correcao = 0
        };
        if ( !isNaN(valorFormatar) ){
            var virgula = valorFormatar.length - 2 - correcao;
            for (j =0; j < valorFormatar.length; j++){
                if(j == virgula){
                    valorFormatado += "," + valorFormatar.charAt(j);
                }else{
                    if(j == virgula-4 || j == virgula-7 || j == virgula-10 || j == virgula-13){
                        valorFormatado +=  valorFormatar.charAt(j) + ".";
                    }else{
                        valorFormatado += valorFormatar.charAt(j);
                    }//else
                }//else
            }//for
            if ( valorFormatado.charAt(0) == ","){
                valorFormatado = "0" + valorFormatado;
            }
            if ( valorFormatado.charAt(0) == "0"){                
                if ( valorFormatado.charAt(1) != "," && valorFormatado.charAt(1) != ""){
                    valorFormatado = valorFormatado.substr(1);
                }
            }
            return(valorFormatado);
        }
    }//if tamanho do número for menor que 13 algarismos}//moedaMask(valo, casas_decimais);
}//formatar o valor como moeda

function formataMoeda(campo, decimal){
    var valor = campo.value;
    //eval("valor = new String(document.form1." + campo + ".value)");

    var aux2 = moedaMask(valor, decimal);
    if ( aux2 != null ){
        //eval( "document.form1." + campo + ".value = aux2" );

        campo.value = aux2;
    }
    return aux2;
}//formatar um campo

function ativaVivenciaInternacional(){
    
    document.getElementById('vivencia_internacional').style.display ='';
}

function ocultaVivenciaInternacional(){

    document.getElementById('vivencia_internacional').style.display ='none';
}

function mudaCorSpanCandidatar(table_id,vagas_acao_id){
//    alert(document.getElementById(span_class).style.color);
//    document.getElementById(tr_vaga_id).style.color ='#AA0000';
//    document.getElementById(span_class).style.color ='vaga_listagem vaga_listagem_exists';
    document.getElementById(table_id).className='vaga_listagem vaga_listagem_exists' ;
    $(vagas_acao_id).setStyle({
        backgroundColor: '#AA0000'
    });
    //    document.getElementById(table_id). = '1px solid #12315D';
}