﻿/*
* jQuery Number Input plugin 1.0
* Released: July 14, 2008
* 
* Copyright (c) 2008 Chris Winberry
* Email: M8R-tk5fe51@mailinator.com
* 
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* 
* Dual licensed under the MIT and GPL licenses:
*   http://www.opensource.org/licenses/mit-license.php
*   http://www.gnu.org/licenses/gpl.html
*
* @license http://www.opensource.org/licenses/mit-license.php
* @license http://www.gnu.org/licenses/gpl.html
* @project jquery.numberInput
*/
(function($) {
    $.fn.numberInput = function() {
        return this.each(function() {
            var length = parseInt($(this).attr('length')) || 3;
            var isDouble = $(this).attr('double') == 'true' || false;
            $(this).keydown(function(event) {
                if (event.shiftKey && event.keyCode != 16) {
                    return false;
                }
                var keys_allowed = isDouble ? KEYS_ALLOWED_DOUBLE : KEYS_ALLOWED_INT;
                if ($(this).val().toString().length >= length)
                    return CONTROL_KEYS[event.keyCode] ? true : false;
                return (CONTROL_KEYS[event.keyCode] || keys_allowed[event.keyCode]) ? true : false;
            });
        });
    };
    var CONTROL_KEYS = {
        8: 'BACKSPACE'
        , 9: 'TAB'
		, 13: 'ENTER'
		, 37: 'LEFT_ARROW'
		, 39: 'RIGHT_ARROW'
		, 46: 'DELETE'
    };
    var KEYS_ALLOWED_INT = {
        48: 'ZERO'
		, 49: 'ONE'
		, 50: 'TWO'
		, 51: 'THREE'
		, 52: 'FOUR'
		, 53: 'FIVE'
		, 54: 'SIX'
		, 55: 'SEVEN'
		, 56: 'EIGHT'
		, 57: 'NINE'
		, 96: 'ZERO2'
		, 97: 'ONE2'
		, 98: 'TWO2'
		, 99: 'THREE2'
		, 100: 'FOUR2'
		, 101: 'FIVE2'
		, 102: 'SIX2'
		, 103: 'SEVEN2'
		, 104: 'EIGHT2'
		, 105: 'NINE2'
    };
    var KEYS_ALLOWED_DOUBLE = {
        48: 'ZERO'
		, 49: 'ONE'
		, 50: 'TWO'
		, 51: 'THREE'
		, 52: 'FOUR'
		, 53: 'FIVE'
		, 54: 'SIX'
		, 55: 'SEVEN'
		, 56: 'EIGHT'
		, 57: 'NINE'
		, 96: 'ZERO2'
		, 97: 'ONE2'
		, 98: 'TWO2'
		, 99: 'THREE2'
		, 100: 'FOUR2'
		, 101: 'FIVE2'
		, 102: 'SIX2'
		, 103: 'SEVEN2'
		, 104: 'EIGHT2'
		, 105: 'NINE2'
        , 110: 'DECIMAL2'
        , 190: 'DECIMAL'
        , 188 : 'COMMA'
    };
})(jQuery);
