$(document).ready(function () {
    menuToggle();
    manageFormContact();
    manageFormSearch();
});

function menuToggle() {
    var box = $('.menu-content');
    var button = $('.toggle');
    button.on('click', function () {
        box.toggle('slow');
    });
}

function manageFormSearch(){
    $(".dropdown-search").on("change", "input[type='checkbox']", function() {
        $(this).closest("li").toggleClass("active", this.checked);
    });

    $(document).on('click', '.allow-focus', function (e) {
        e.stopPropagation();
    });

    $('.pagination a').click(function (e) {
        console.log(this.href.split('page=')[1]);
        $('.hidden-page').val(this.href.split('page=')[1]);
        $('.search-button').click();
        return false;
    });
}

function manageFormContact() {
    $('#contact_lccap').val('blop');
    $('#contact-form-btn').on('click', function () {
        if (checkFormValidity('#contact-form')) {
            $('#contact-form').fadeOut(200);
            $form = $('#contact-form');
            $.ajax({
                url: $form.prop('action'),
                method: $form.prop('method'),
                data: $form.serialize(),
                dataType: "json",
                success: function (response) {
                    $('#contact-form').html('<p class="success-post">Merci pour votre message, nous vous r&eacute;pondrons dans les plus brefs d&eacute;lais.</p>');
                    $('#contact-form').fadeIn(500);
                }
            });
        }
    });
}

function checkFormValidity(formId) {
    if (!document.getElementById(formId.substr(1)).checkValidity()) {
        document.getElementById(formId.substr(1)).reportValidity()
        return false;
    } else {
        return true;
    }
}