﻿// Revium jquery extensions
// Copyright (c) 2009 Revium pty ltd http://revium.com.au
// Licensed under the GPL licence:
// http://www.gnu.org/licenses/gpl.html

(function($) {
    // To capitalise text input when user types in (to make just the 1st symbol uppercase)
    // Developed by Evgeny Petrov, Revium
    // Ver 1.0, 24/11/2009
    $.fn.capitalise = function() {
        $(this).keyup(function() {
            var el = $(this);
            var value = "";
            if (el.length > 0) {
                value = el.val().toString().substr(0, 1).toUpperCase() + el.val().toString().substr(1);
            }
            el.val(value);
        });
    }

    // To cancel clipboard paste event
    // Developed by Evgeny Petrov, Revium
    // Ver 1.0, 24/11/2009
    $.fn.nopaste = function() {
        $(this).bind('paste', function(e) {
            e.preventDefault();
        });
    }

    // To click default button when Enter key is hit. 
    // ustom selectors are used insead of predefined (aka "deafult" class) to give it extra flexibility.
    // Developed by Evgeny Petrov, Revium
    // Ver 1.0, 13/01/2010
    $.fn.clickbutton = function(button_selector) {
        $(this).keypress(function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $(button_selector).click();
                return false;
            } else {
                return true;
            }
        });
    }

    // maxlength for textarea
    // Copyright (c) 2008 Viral Patel 
    // http://viralpatel.net/blogs 
    // Dual licensed under the MIT and GPL licenses: 
    // http://www.opensource.org/licenses/mit-license.php 
    // http://www.gnu.org/licenses/gpl.html 
    $.fn.maxlength = function() {
        $("textarea[maxlength]").keypress(function(event) {
            var key = event.which;
            if (key >= 33 || key == 13) {
                var maxLength = $(this).attr("maxlength");
                var length = this.value.length;
                if (length >= maxLength) {
                    event.preventDefault();
                }
            }
        });
    }

    // To attach an error label next to an input, emulates validation error
    // Developed by Evgeny Petrov, Revium
    // Ver 1.0, 21/06/2010
    $.fn.error = function(msg) {
        if ($(this).next().filter("label.error").lenght) {
            $(this).next().text(msg);
        } else {
            $(this).after("<label class='error' for='" + $(this).attr("id") + "'>" + msg + "<label/>");
        }
    }

    // To remove all error labels (validation errors) within an element
    // Developed by Evgeny Petrov, Revium
    // Ver 1.0, 21/06/2010
    $.fn.clearErrors = function() {
        $(this).find("label.error").remove();
    }

    // ASP.NET Id selector, e.g. $('textarea:id(MsgTextBox)').val();
    // Developed by Evgeny Petrov, Revium
    // Ver 1.0, 01/03/2010
    $.expr[':'].id = function(el, i, match) {
        var x = $(el).filter("[id$=_" + match[3] + "]");
        if (x.length == 0) {
            return null;
        } else {
            return x;
        }
    };

    // ASP.NET Name selector, e.g. $('input:name(OptionsRadioButtonList):checked').val();
    // Developed by Evgeny Petrov, Revium
    // Ver 1.0, 01/03/2010
    $.expr[':'].name = function(el, i, match) {
        var x = $(el).filter("[name$=$" + match[3] + "]");
        if (x.length == 0) {
            return null;
        } else {
            return x;
        }
    };


})(jQuery);

