﻿$(document).ready(function() {
    // Button apply
    $('a.button').append("<span></span");
});


jQuery(function($) {

    // ignore these keys
    var ignore = [8, 9, 13, 33, 34, 35, 36, 37, 38, 39, 40, 46];

    // use keypress instead of keydown as that's the only
    // place keystrokes could be canceled in Opera
    var eventName = 'keypress';

    // handle textareas with maxlength attribute
    $('textarea[maxlength]')

    // this is where the magic happens
    .live(eventName, function(event) {
        var self = $(this),
          maxlength = self.attr('maxlength'),
          code = $.data(this, 'keycode');

        // check if maxlength has a value.
        // The value must be greater than 0
        if (maxlength && maxlength > 0) {

            // continue with this keystroke if maxlength
            // not reached or one of the ignored keys were pressed.
            return (self.val().length < maxlength
                 || $.inArray(code, ignore) !== -1);

        }
    })

    // store keyCode from keydown event for later use
    .live('keydown', function(event) {
        $.data(this, 'keycode', event.keyCode || event.which);
    });

});

jQuery.fn.getCheckboxVal = function() {
    var vals = [];
    var i = 0;
    this.each(function() {
        vals[i++] = jQuery(this).val();
    });
    return vals;
}


$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};