﻿$(function () {
    $.validator.addMethod("requiredif", function (value, element, param) {

        if ($('input[name=' + param.propertyname + ']').filter(':checked').val().toUpperCase() == param.propertyvalue.toUpperCase()) {
            if (!this.depend(param, element))
                return "dependency-mismatch";
            switch (element.nodeName.toLowerCase()) {
                case 'select':
                    // could be an array for select-multiple or a string, both are fine this way
                    var val = $(element).val();
                    return val && val.length > 0;
                case 'input':
                    if (this.checkable(element))
                        return this.getLength(value, element) > 0;
                default:
                    return $.trim(value).length > 0;
            }
        }

        return true;
    });


    $.validator.unobtrusive.adapters.add("requiredif", ["targetpropertyname", "targetpropertyvalue"], function (options) {
        if (options.element.tagName.toUpperCase() !== "INPUT" || options.element.type.toUpperCase() !== "CHECKBOX") {
            options.rules["requiredif"] = {
                propertyname: options.params.targetpropertyname,
                propertyvalue: options.params.targetpropertyvalue
            };
            options.messages["requiredif"] = options.message;
        }
    });
} (jQuery));
