$(document).ready(function() {
    function pesquisaCep(url, cep) {
        if (cep.length == 9) {
            $.ajax({
                type: 'POST',
                url: url + '/cep/' + new Date().getTime(),
                data: 'cep='+cep,
                success: function(resp){
                    var obj = eval('(' + resp + ')');
                    if( obj.resultado == '1' ) {
                        $('input[name=endereco]').val(obj.logradouro);
                        $('input[name=bairro]').val(obj.bairro);
                        $('input[name=cidade]').val(obj.cidade);
                        $('input[name=estado]').val(obj.uf);
                    } 
                    else if( obj.resultado == '2' ) {
                        $('input[name=cidade]').val(obj.cidade);
                        $('input[name=estado]').val(obj.uf);
                    } 
                }
            });
        }
    }

    function verificaExistente(url, data, element, message) {
        $.ajax({
            type: 'POST',
            url: url + '/' + new Date().getTime(),
            data: data,
            success: function(resp) {
                if (resp == '1') {
                    $(element).find('label').css('display', '').html(message);
                }
                else {
                    $(element).find('label').css('display', 'none');
                }
            }
        });
    }

    $('input[name=fax]').mask('(99) 9999-9999');
    $('input[name=cep]').mask('99999-999');
    $('input[name=cnpj]').mask('99.999.999/9999-99');
    $('input[name=fone]').mask('(99) 9999-9999');
    $('input[name=fone1]').mask('(99) 9999-9999');
    $('input[name=fone2]').mask('(99) 9999-9999');
    $('input[name=celular]').mask('(99) 9999-9999');
    $('input[name=cpf]').mask('999.999.999-99');
    $('input[name=cpf]').blur(function() {
        verificaExistente(rel_url + 'candidato/cpf_existe', 'documento=' + this.value, '#td_cpf', 'Documento já existente');
    });
    $('input[name=email]').blur(function() {
        verificaExistente(rel_url + 'candidato/email_existe', 'email=' + this.value, '#td_email', 'E-mail já existente');
    });
    $('input[name=cnpj]').blur(function() {
        verificaExistente(rel_url + 'candidato/cnpj_existe', 'email=' + this.value, '#td_cnpj', 'CNPJ já existente');
    });
});

