﻿//JQuery force numeric
$.fn.numeric = function() {
    return this.each(function() {
        $(this).keydown(function(event) {
            var code = event.keyCode;
            if ((code >= 48 && code <= 57) || (code >= 96 && code <= 105) || code == 8 || code == 110 || code == 46) {
                //Key Accepted
            } else {
                event.preventDefault();
            }
        });

        $(this).focus(function() {
            this.select();
        });
    });
}

var settings = {
    save: function(key, value) {
        $.get("/Preference/Save", { key: key, value: value });
    }
}

var delay = (function() {
    var timer = 0;
    return function(callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();

var noduplicates = (function() {
    return function() {
        $(".no-duplicates").change(function() {
            $(".no-duplicates option").removeAttr("disabled");
            jQuery.each($(".no-duplicates"), function(ai, aitem) {
                if ($(aitem).val() != "") {
                    jQuery.each($(".no-duplicates"), function(bi, bitem) {
                        if ($(aitem).attr("id") != $(bitem).attr("id")) {
                            var i = "#" + $(bitem).attr("id") + " option[value='" + $(aitem).val() + "']";
                            $(i).attr("disabled", "disabled");
                        }
                    });
                }
            });
        });
    };
})();

var enterSubmit = (function() {
    return function() {
        $(".enterSubmit").keypress(function(e) {
            var key = (e.keyCode ? e.keyCode : e.which);
            switch (key) {
                case 13:
                    var form = $(this).parents('form:first');
                    e.preventDefault();
                    form.submit();
                    break;
            }
        });
    };
})();


var confirmDelete = (function() {
    return function() {
        $(".confirmDelete").click(function(e) {
            return confirm('Are you sure you wish to DELETE this item?');
        });
    };
})();

var SelectImage = {
    init: function(id, url) {
        $('.selectImage .browse').click(function() {
            window.open(url + "?field=" + id, "ImageBrowser", "menubar=0,resizable=0,width=586,height=660");
            return false;
        });
    }
};

$.fn.textAreaLength = function() {
    return this.each(function() {
    if ($(this).attr("maxlength")) {
            var func = function() {
                var len = parseInt($(this).attr("maxlength"), 10);
                if ($(this).val().length > len) {
                    this.value = this.value.substr(0, len);
                    return false;
                }
            }

            $(this).keyup(func);
            $(this).blur(func);
        }
    });
};

// jQuery getQueryParam Plugin 1.0.0 (20100429)
// By John Terenzio | http://plugins.jquery.com/project/getqueryparam | MIT License
(function($) { $.getQueryParam = function(param) { var pairs = location.search.substring(1).split('&'); for (var i = 0; i < pairs.length; i++) { var params = pairs[i].split('='); if (params[0] == param) { return params[1] || ''; } } return undefined; }; })(jQuery);
