//<![CDATA[


var _main        = {
    init          : function() {
        this.before();
    },
    before        : function() {
        _loader.core();
    },
    after         : function() {
        validatecontact.init();
    }
};


var validatecontact   = {

    init    : function() {
        if (this.check())  this.core();
    },

    check   : function() {
        return ( _exists('#contact-us') &&
                typeof jQuery.fn.validate == 'function'  &&
                typeof jQuery.fn.ajaxSubmit == 'function' ) ? true : false;        
    },

    core    : function() {

        $.validator.addMethod("alpha", function(value, element) {
			return this.optional(element) || /^([a-zà-úA-ZÀ-Ú ]+)$/i.test(value);
		});

        $('#contact-us').validate({
            rules   :{
                'data[duvidas][nome]' :{
                    required    : true,
                    alpha       : true,
                    minlength   : 2,
                    maxlength   : 80
                },
                'data[duvidas][email]' :{
                    required    : true,
                    email       : true,
                    maxlength   : 80
                    //remote      : '/check-email/'
                },
                'data[duvidas][assunto]' :{
                    required    : true
                },
                'data[duvidas][mensagem]' :{
                    required    : true,
                    maxlength   : 500
                }
            },
            messages:{
                'data[duvidas][nome]' :{
                    required    : 'Campo de preenchimento obrigatório.',
                    minlength   : 'O campo nome deve conter no mínimo 2 caracteres.',
                    maxlength   : 'O campo nome deve conter no máximo 80 caracteres.',
                    alpha       : 'Apenas caracteres alpha'
                },
                'data[duvidas][email]' :{
                    required    : 'Campo de preenchimento obrigatório.',
                    email       : 'E-mail inválido.',
                    maxlength   : 'O campo e-mail deve conter no máximo 80 caracteres.'
                    //remote      : jQuery.format('O e-mail {0} já está em uso, utilize outro.')
                },
                'data[duvidas][assunto]' :{
                    required    : 'Selecione o tipo de assunto'
                },
                'data[duvidas][mensagem]' :{
                    required    : 'Campo de preenchimento obrigatório.',
                    maxlength   : 'O campo nome deve conter no máximo 500 caracteres.'
                }
            },
            
            onkeyup             : false,
            submitHandler       : function(form) {
                $(form).submit();
            }
        });
    },
    
    result  : function(data, teste) {

        var status  = data.claro.status;

        var html    = '<div id="error"><ol>';

        if (status == 0) {
            $.each(data.claro.error, function(i, item) {
                html    += '<li>' + item + '</li>\n';
           });

           html += '</ol></div>';

           $('ol.submit').after(html);

        } else {

            var html    = '';

            $.get('/fale-conosco/sucesso.shtm', function(data) {
              html = data;
            });

            $('form#contact-us').fadeOut( function() {
                $(this).html(html).fadeIn('slow');
            });
        }

    }    
};


_main.init();


//]]>
