/* 

    Document   : funcoes.js
    Created on : 09/03/2009, 16:07:15
    Author     : Vagner e Stephan - Ação Direta Web Studio
    Description: Funções - Content do Portal de Negócios Berreta Investimentos
	
*/

$(document).ready(function(){

    // Link Externo - não use target="_blank" (Y)
    $("a[@rel=externo]").attr('target','_blank');
	
    // Mascára para input
    $('#comprar_telefone').mask("(99) 9999 - 9999");
    $('#fale_telefone').mask("(99) 9999 - 9999");
    $('#fale_celular').mask("(99) 9999 - 9999");
    $('#vender_telefone').mask("(99) 9999 - 9999");
    $('#vender_celular').mask("(99) 9999 - 9999");
    $('#comprar_celular').mask("(99) 9999 - 9999");
	
    // Máscara Money
    $("#vender_valor_parcela").maskMoney({
        symbol:"R$",
        decimal:",",
        thousands:"."
    });
    $("#vender_valor_pretendido").maskMoney({
        symbol:"R$",
        decimal:",",
        thousands:"."
    });
    $("#vender_credito").maskMoney({
        symbol:"R$",
        decimal:",",
        thousands:"."
    });
    $("#fale_valor").maskMoney({
        symbol:"R$",
        decimal:",",
        thousands:"."
    });
	
	
    // Input File Style
    $("input.anx").filestyle({
        image: "../public/imagens/btn-procurar-curriculo.jpg",
        imageheight : 42,
        imagewidth : 90,
        width : 210
		
    });

});

// Ajax, Resposta Cadastro de Newsletter
function enviaNewsletter(url) {
    if(!$('#newsletterForm #email').val()) {
        alert('Informe o email...');
    } else if(!$('#newsletterForm #nome').val()) {
        alert('Informe o nome...');
    } else {
        if($('#newsletterForm #enviado').val()==0) {
            $('#newsletterForm #enviado').val(1);
            $.ajax({
                method: 'post',
                url: url + 'cadastra-newsletter.php',
                data: $('#newsletterForm').formToArray(),
                type: 'POST',
                async: false,
                timeout: 1000,
                success: function(html){
                    Shadowbox.open({
                        player: 'html',
                        content: html,
                        width: 400,
                        height: 200
                    });
                    $('#newsletterForm').clearForm();
                }
            });
        }
    }
}

// Validação Form Contato

function validaForm(form) {
    msg = new Array();

    formElements = $(form).map(function(){
        return $.makeArray(this.elements);
    });
    formElements.each(function(i){
        a = $(this);
        nomeObj = a.attr('name');
        idObj 	= a.attr('id');
        relObj 	= a.attr('rel');
        if ((relObj=="required_email" )) {
            b=a.prev('input:first')
            if(!validaEmail(a.val())){
                a.val();
                a.addClass('campo_requer');
                b.addClass('campo_requer');
                msg.push('»   ' + a.attr('title'));
            } else {
                a.removeClass('campo_requer');
                b.removeClass('campo_requer');
            }
        }else if(relObj=="required") {
            b=a.prev('input:first')
            if((a.val() == null) || (a.val() == "")){
                a.addClass('campo_requer');
                b.addClass('campo_requer');
                msg.push('»   ' + a.attr('title'));
            } else {
                a.removeClass('campo_requer');
                b.removeClass('campo_requer');
            }
        }
    });

    if(msg.length==0) {
        return true;
    }

    alert('Verifique os erros informados:\n\n' + msg.join('\n'));
    return false;
}

// Valida Campo E-mail

function validaEmail(value) {
    expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
    if(!expressao.test(value)) {
        return false;
    }
    return true;
}
