(function($) {
    
    $.fn.focusNextInputField = function() {
        return this.each(function() {
            var fields = $(this).parents('form:eq(0),body').find('input:visible').not('[type=hidden]'),
                index = fields.index( this );
            if ( index > -1 && ( index + 1 ) < fields.length ) {
                fields.eq( index + 1 ).focus();
            }
            return false;
        });
    };

    $.fn.isFocused = function() {
        return (this.get(0) == window.focusedInput);
    }
    
    function onFocus() {
        window.focusedInput = this;
    }
    
    function onBlur() {
        window.focusedInput = null;
    }
    
    $(function () {
        $('input, textarea, select').focus(onFocus).blur(onBlur);
        $('.focused').focus().select();
    });
    
})(jQuery);
