// Copyright 2008 by Magnus Health Technology
// Author: x.lu@magnushealth.com

jQuery.fn.xiaoTab = function() {
	
	var goPrev = function(elem){
		var prev = elem.prev(":text");
		if (prev.size()) {
			prev[0].focus();
		}
	};
	
	var pattern = new RegExp('[^0-9]+', 'g');
	
	var goNext = function(from, charCode){
		var next = from.next(":text");
		if (next.size() && $.inArray(charCode, ignoreKeys) == -1) {
			if (!$.browser.msie) {
				next.val(String.fromCharCode(charCode));
			}
			next[0].focus();
		}
	};
	
	var ignoreKeys = [8, 9, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];
	
	return this.each(function(){
		if ( jQuery(this).is(":text") ) {
		
			jQuery(this).keydown(function() {
				this.value = this.value.replace(pattern, "");
			});
			
			jQuery(this).keypress(function(e){
				var el = jQuery(this);
				if ( el.val().length >= el.attr("maxlength"))  {
					goNext(el, e.which);					
				} /*else if ( el.val().length == 0 && e.which == 8 ) {
					goPrev(el)
				}
				*/
			});
		}
	});
};