/**
 * Adds jQuery.validate() to an HTML form.
 *
 * @author Martin Enderleit
 * @param {type} $form
 */
function addValidatorToForm($form) {
    $form.validate();
    $form.find('label.required').each(function () {
        var $this = $(this);
        var $html = $this.html();
        if (!endsWith($html, '*')) {
            $this.append('*');
        }
        try {
           var  $element = $this.closest('.form-group').find('.form-control:last');
            if ($element.hasClass('glms-selected')) {
                $.validator.addMethod('notEmpty', function (value, element, args) {
                    return $(element).find('option').length > 0;
                }, '* must not be empty');
                $element.rules('add', {
                    notEmpty: true
                });
            } else {
                $element.rules('add', 'required');
            }
        } catch (err) {
            $.notify(err, 'danger');
        }
    });
    $form.validate({
        showErrors: function (errorMap, errorList) {
            $.each(this.successList, function (index, value) {
                return $(value).popover("hide");
            });
            return $.each(errorList, function (index, value) {
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "left",
                    html: true,
                    content: value.message
                });
                _popover.data("bs.popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        }
    });
}
