﻿var contact = new Class({
    initialize: function() {
        $('send').addEvent('click', function() { this._clearResponse(); this._checkFields(); } .bind(this));
    },
    _checkFields: function() {
        var notEmpty = true;
        $('recherche').getElements('input.toCheck').each(function(inputElt) {
            notEmpty = notEmpty && inputElt.get('value') != '';
        });
        
        if (notEmpty) {
            this._sendReq();
        }
        else {
            $("response").setStyle('background-image', 'none');
            $("response").set('html', "<span class='error'>Merci de remplir tout les champs obligatoires.</span>");
        }
    },
    _sendReq: function() {
        var req = new Request.HTML({
            url: "/ajax/sendContact.aspx",
            update: $("response")
        }).addEvent('success', function() { $("response").setStyle('background-image', 'none'); });

        req.post({
            nom: $('nom').get('value'),
            email: $('mail').get('value'),
            comments: $('msg').get('value')
        });
    },
    _clearResponse: function() {
        $("response").empty();
        $("response").setStyle('background-image', 'url(/assets/ajax-loader-black.gif)');
    }
});
