/* Minification failed. Returning unminified contents.
(5,31): run-time error CSS1031: Expected selector, found '='
(5,31): run-time error CSS1025: Expected comma or open brace, found '='
(30,2): run-time error CSS1019: Unexpected token, found ')'
(33,1): run-time error CSS1019: Unexpected token, found '$'
(33,3): run-time error CSS1030: Expected identifier, found 'extend('
(33,3): run-time error CSS1019: Unexpected token, found 'extend('
(34,12): run-time error CSS1036: Expected expression, found '{'
(133,2): run-time error CSS1019: Unexpected token, found ')'
(135,18): run-time error CSS1031: Expected selector, found '='
(135,18): run-time error CSS1025: Expected comma or open brace, found '='
 */
// Extension to the kendo binder to allow binding of the raw value of a masked textbox, rather than the formatted value
// On the input element, add the following attribute to use this raw binder: 
    // data_bind = "raw: [model field name goes here]"

kendo.data.binders.widget.raw = kendo.data.Binder.extend({
    init: function (widget, bindings, options) {
        //call the base constructor
        kendo.data.Binder.fn.init.call(this, widget.element[0], bindings, options);
        this.widget = widget;
        this._change = $.proxy(this.change, this);
        this.widget.first("change", this._change);
        this._initChange = false;
    },
    change: function () {
        var that = this;
        var value = that.widget.raw();

        that._initChange = true;

        that.bindings.raw.set(value);

        that._initChange = false;
    },
    refresh: function () {
        if (!this._initChange) {
            var value = this.bindings.raw.get();
            this.widget.value(value);
        }
    }
});
// Add functionality to the default kendo validator 

$.extend(true, kendo.ui.validator, {
    rules: {
        validmask: function (input) {
            if (input.is("[data-validmask-msg]") && input.val() !== "") {
                var maskedtextbox = input.data("kendoMaskedTextBox");
                return maskedtextbox.value().indexOf(maskedtextbox.options.promptChar) === -1;
            }
            return true;
        },
        feb29: function (input) {
            if (input.is("[data-annivday]") && input.val() == 29) {
                var month = $(input.context).find("[data-annivmonth]");
                if (month != null) {
                    return (month.val() != 2);
                }
                return true;
            }
            return true;
        },
        daysInMonth: function (input) {
            var day = input.val();
            if (input.is("[data-annivday]") && day != "" && day >= 29) {
                var month = $(input.context).find("[data-annivmonth]");
                if (month != null) {
                    // 30 days hath september...
                    switch (month.val()) {
                        case "9":
                        case "4":
                        case "6":
                        case "11":
                            return day <= 30;
                            break;
                        case "2":
                            return day <= 29; // We'll let the other rule deal with leap years
                            break;
                        default:
                            return day <= 31;
                    }
                }
                return true;
            }
            return true;
        },
        available: function (input) { // http://www.telerik.com/blogs/extending-the-kendo-ui-validator-with-custom-rules
            var validate = input.data('available');
            if (typeof validate !== 'undefined' && validate !== false) {
                var id = input.attr('id');
                var originalValue = input.attr('data-available-original');
                var cache = availability.cache[id] = availability.cache[id] || {};
                cache.checking = true;
                var settings = {
                    url: input.data('availableUrl') || '',
                    message: kendo.template(input.data('availableMsg')) || ''
                };

                // The user hasn't modified the starting value
                if (input.val() === originalValue) {
                    return true;
                }

                // The value is available
                if (cache.value === input.val() && cache.valid) {
                    return true;
                }

                // If the value in the cache and the input value are the same 
                // and the cached state is not valid, the value is not available
                if (cache.value === input.val() && !cache.valid) {
                    cache.checking = false;
                    return false;
                }

                // Otherwise, go to the ajax check
                availability.check(input, settings, this);
                // return false which goes into 'checking...' mode
                return false;
            }
            // this rule does not apply to the input
            return true;
        },
    },
    messages: {
        feb29: function (input) {
            return "February 29th cannot be used as an employer anniversary.";
        },
        daysInMonth: function (input) {
            return "Days entered is too large for the corresponding month entered.";
        },
        availability: function (input) {
            var id = input.attr('id');
            var msg = kendo.template(input.data('availableMsg') || '');
            var cache = availability.cache[id];
            if (cache.checking) {
                return "Checking..."
            }
            else {
                return msg(input.val());
            }
        }
    }
})

var availability = {

    cache: {},

    check: function (element, settings, validatorInstance) {
        var id = element.attr('id');
        var cache = this.cache[id] = this.cache[id] || {};
        $.ajax({
            url: settings.url,
            dataType: 'json',
            data: { value: element.val() }, // Requires that the controller method being posted to has a parameter named 'value'

            success: function (data) {
                // set the value on the cache object so that it can be referenced in the next validation run
                cache.valid = data;
                // trigger validation again
                validatorInstance.validateInput(element);
            },
            failure: function () {
                // the ajax call failed so just set the field as valid since we don't know for sure that it's not
                cache.valid = true;
                // trigger validation
                validatorInstance.validateInput(element);
            },
            complete: function () {
                // cache the inputs value
                cache.value = element.val();
            }
        });
    }
};
