(function ($) {
    $.validator.unobtrusive.adapters.add("requiredcheckbox", function (options) {
        //b-required for checkboxes
        if (options.element.tagName.toUpperCase() === "INPUT" && options.element.type.toUpperCase() === "CHECKBOX") {
            //setValidationValues(options, "required", true);
            options.rules["required"] = true;
            if (options.message) {
                options.messages["required"] = options.message;
            }
        }
    });

})(window.jQuery);;
(function ($) {
    $.validator.unobtrusive.adapters.add("requireddependent", ["propertytocompareon"], function (options) {
        options.rules["requireddependent"] = "#" + options.params.propertytocompareon;
        options.messages["requireddependent"] = options.message;
    });
})(window.jQuery);

// Validation attribute the tells if the property is required depending of another filled in property.
// The validated property is only required when the one to compare on contains a value.
(function ($) {
    $.validator.addMethod("requireddependent", function (value, element, params) {
        //Remove error class
        if ($(element).parent("div").hasClass("divError")) $(element).parent("div").removeClass("divError");

        //When the target property to compare is an "input type=hidden" and its value is 'false', it means that the 'element' property is not required.
        var targetAttribute = 'data-val-requireddependent-propertytocompareon';
        var propertyToCompareOn = $(element).attr(targetAttribute);

        if ($('#' + propertyToCompareOn).is(':hidden') && $(params).val().match(/^(false)$/i)) {
            return true;
        }

        //Adding error class when the dependency is set on a checkbox.
        if ($(element).is(':checkbox') && !$(element).is(':checked') && $(params).val() != null && $(params).val() != "") {
            $(element).parent("div").addClass("divError");
            return false;
        }

        // if the compare value is set but not the tested value => not valid
        if ($(params).val() != null && $(params).val() != "" && (value == null || value == "")) {
            return false;
        }

        //Add class error
        $(element).parent("div").removeClass("divError");

        return true;
    });

})(window.jQuery);;
