care_form.js 4.38 KB
define(
    ['jquery'], function ($) {
        $(document).ready(
            function () {
                // Check if browser supports HTML5 input placeholder
                function supports_input_placeholder() {
                    var i = document.createElement('input');
                    return 'placeholder' in i;
                }

                // Change input text on focus
                if (!supports_input_placeholder()) {
                    jQuery('.wpcf7-text').each(
                        function () {
                            var self = jQuery(this);
                            var value = jQuery.trim(self.val());
                            if (value == '') self.val(self.attr('placeholder'));
                        }
                    );
                    jQuery('.wpcf7-text').focus(
                        function () {
                            var self = jQuery(this);
                            if (self.val() == self.attr('placeholder')) self.val('');
                        }
                    ).blur(
                        function () {
                            var self = jQuery(this);
                            var value = jQuery.trim(self.val());
                            if (value == '') self.val(self.attr('placeholder'));
                        }
                    );
                }

                //functionality for add-file link
                $('a.add_file').on(
                    'click', function (e) {
                        //show by click the first one from hidden inputs
                        $('p.hide:not(:visible):first').show('slow');
                        e.preventDefault();
                    }
                );

                //functionality for del-file link
                $('a.del_file').on(
                    'click', function (e) {
                        //var init
                        var input_parent = $(this).parent();
                        var input_wrap = input_parent.find('span');
                        //reset field value
                        input_wrap.html(input_wrap.html());
                        //hide by click
                        input_parent.hide('slow');
                        e.preventDefault();
                    }
                );

                $('a.first-file').on(
                    'click', function (e) {
                        //reset field value
                        $('.wpcf7-file').eq(0).replaceWith($('.wpcf7-file').eq(0).val('').clone(true));
                        $(this).hide();
                        e.preventDefault();
                    }
                );

                $('.wpcf7-file').eq(0).change(
                    function () {
                        var fileName = $(this).val();
                        if (fileName != '') {
                            $('a.first-file').show();
                        } else {
                            $('a.first-file').hide();
                        }
                    }
                );

                var eventFields = $('input[name="event-date"], input[name="event-location"], textarea[name="purpose-event"], input[name="role"], textarea[name="recognize-event"]');
                eventFields.removeClass('wpcf7-validates-as-required');
                eventFields.parents('.form-control').hide();

                $('input[name="donation"]').click(
                    function () {
                        var value = $(this).attr('value');
                        var isDonation = value.indexOf('donation') > 0;
                        var requiredClassName = 'wpcf7-validates-as-required';
                        var requiredAttr = "aria-required";
                        var eventFields = $('input[name="event-date"], input[name="event-location"], textarea[name="purpose-event"], input[name="role"], textarea[name="recognize-event"]');

                        // Check if it is donation or event
                        if (!isDonation) {
                            eventFields.addClass(requiredClassName).attr(requiredAttr, true);
                            eventFields.parents('.form-control').slideDown();
                        } else {
                            eventFields.removeClass(requiredClassName).attr(requiredAttr, false);
                            eventFields.parents('.form-control').slideUp();

                        }
                    }
                );
            }
        );
    }
);