/*
 * 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(format1, format2, bInteger) {
		return this.each(function() {
			$(this).keydown(function(event){
				if( (( (!bInteger && decimalSeparator=='.') || thousandsSeparator=='.') && event.keyCode==190) ||
				    (( (!bInteger && decimalSeparator==',') || thousandsSeparator==',') && event.keyCode==188) ||
				    (thousandsSeparator==' ' && event.keyCode==32) )
				  return true;
				else			
				  return KEYS_ALLOWED[event.keyCode] ? true : false;
			});
			$(this).focus(function(){
                                $(this).css("text-align", "left");
                                var num = $.parseNumber($(this).attr('numberdata'), {format: (bInteger ? '############' : '############.00'), locale: 'us'}) 
                                if( num==0 ) {
                                    $(this).val('');
                                } else {
                                    $(this).val($.formatNumber(num, {format: format1, locale: us_LANG, decimalSeparatorAlwaysShown: !bInteger}));
                                }
			});
			
                        $(this).blur(function(){
                                $(this).css("text-align", "right");
                                $(this).attr('numberdata', $.parseNumber( $(this).val(), {format: format1, locale: us_LANG} ) );
				$(this).val( $.formatNumber( $(this).attr('numberdata'), {format: format2, locale: us_LANG, decimalSeparatorAlwaysShown: !bInteger}) );
			});

                        $(this).css("text-align", "right")
                        $(this).attr('numberdata', $(this).val());
                        $(this).formatNumber({format: format2, locale: us_LANG, decimalSeparatorAlwaysShown: !bInteger});
		});
	};
	var KEYS_ALLOWED = {
		   8 : 'BACKSPACE'
		,  9 : 'TAB'
		, 13 : 'ENTER'
		, 35 : 'END'
		, 36 : 'HOME'
		, 37 : 'LEFT_ARROW'
		, 39 : 'RIGHT_ARROW'
		, 46 : 'DELETE'
		, 48 : 'ZERO'
		, 49 : 'ONE'
		, 50 : 'TWO'
		, 51 : 'THREE'
		, 52 : 'FOUR'
		, 53 : 'FIVE'
		, 54 : 'SIX'
		, 55 : 'SEVEN'
		, 56 : 'EIGHT'
		, 57 : 'NINE'
		, 96 : 'numpad 0'
		, 97 : 'numpad 1'
		, 98 : 'numpad 2'
		, 99 : 'numpad 3'
		, 100 : 'numpad 4'
		, 101 : 'numpad 5'
		, 102 : 'numpad 6'
		, 103 : 'numpad 7'
		, 104 : 'numpad 8'
		, 105 : 'numpad 9'
		, 109 : '-'
	};
})(jQuery);
