﻿/*
wire up a custom mandatory adapter to use existing 'required' rule

see http://stackoverflow.com/questions/4934032/mvc3-make-checkbox-required-via-jquery-validate
for why we're doing this
*/

(function ($) {
    $.validator.unobtrusive.adapters.addBool("mandatory", "required");
}(jQuery));


/*
This conditional required adapter is used on TributeCreateCharityPicker.ascx on the 'add custom charity' form to make donation url
mandatory if charitynumber is missing
*/
(function ($) {
    $.validator.unobtrusive.adapters.add('requiredif', ['dependentname', 'dependentvalue'], function (options) {
        options.rules["requiredif"] = {
            dependentname: options.params.dependentname,
            dependentvalue: options.params.dependentvalue
        };
        options.messages["requiredif"] = options.message;
    });
}(jQuery));

(function ($) {
    $.validator.addMethod('requiredif', function (value, element, params) {
        var $dependentfield = $('[name="' + params.dependentname +'"]');
        if ($dependentfield.length == 0 || $dependentfield.val() != params.dependentvalue)
            return true;
        if (value == null || value.trim().length <= 0) {
            return false;
        }
        return true;
    });
}(jQuery));




(function ($) {
    $.validator.unobtrusive.adapters.add('requiredifchecked', ['dependentname'], function (options) {
        options.rules["requiredifchecked"] = {
            dependentname: options.params.dependentname
        };
        options.messages["requiredifchecked"] = options.message;
    });
}(jQuery));

(function ($) {
    $.validator.addMethod('requiredifchecked', function (value, element, params) {
        var $dependentfield = $('[name="' + params.dependentname + '"]');

        if ($dependentfield.is(':checked')) {
             if (value == null || value.length <= 0) {
                return false;
            }
        }

        return true;
    });
}(jQuery));



/*
this adapter will use ensure field contains a valid timespan.
this isn't absolutely foolproof, but the serverside version is.
*/
(function ($) {
    $.validator.addMethod('istimespan', function (value, element, params) {
        var val = $(element).val().toLowerCase();

        if (val === '' && params.isnotrequired) {
            return true;
        }

        if (val.match(/^\d{1,2}:\d{1,2}\s*(?:am|pm)?$/g) == null) {
            return false;
        }

        var parts = val.split(":");

        if (parts.length !== 2) {
            return false;
        }

        var hours = parseInt(parts[0]);
        var mins = parseInt(parts[1]);

        if (isNaN(hours) || isNaN(mins)) {
            return false;
        }

        var isPm = parts[1].indexOf("pm") > -1;
        var isAm = parts[1].indexOf("am") > -1;

        if ((isPm | isAm) && hours > 12) {
            return false;
        }

        if (hours > 23 | mins > 59) {
            return false;
        }

        return true;
    });
}(jQuery));


(function($) {
    $.validator.unobtrusive.adapters.add('istimespan', ['prefixelement','isnotrequired'], function (options) {
        options.rules['istimespan'] = options.params;
        if (options.message != null) {
            options.messages['istimespan'] = options.message;
        }
    });

}(jQuery));



(function ($) {
    $.validator.addMethod('datewithformat',
        function (value, element, params) {


            var format = params.format.toUpperCase();
          

            if (this.optional(element)) {
                return true;
            }

            return  moment(value, format,true).isValid();
           

        });
}(jQuery));



(function ($) {
    $.validator.unobtrusive.adapters.add('datewithformat', ['format'], function (options) {
        options.rules['datewithformat'] = options.params;
        if (options.message != null) {
            options.messages['datewithformat'] = options.message;
        }
    });

}(jQuery));



$(function ($) {

    // Replace the builtin US date validation with code that uses moment.js and handles a date format parameter
    $.validator.unobtrusive.adapters.add('date', ['format'], function (options) {
        options.rules['date'] = options.params;
        if (options.message != null) {
            options.messages['date'] = options.message;
        }
    });


    $.validator.addMethod(
        "date",
        function (value, element, params) {

            var format = (params.format || 'dd/mm/yyyy').toUpperCase();

            if (this.optional(element)) {
                return true;
            }

            return moment(value, format, true).isValid();
        },
        "Please enter a valid date "
    );
}(jQuery));



