﻿/*
 * Allows only valid characters to be entered into input boxes.
 *
 * @name     numeric
 * @example  $(".numeric").numeric();
 *
 */
jQuery.fn.numeric = function()
{
	this.keypress(
		function(e)
		{
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			// allow enter/return key (only when in an input box)
			if(key == 13 && this.nodeName.toLowerCase() == "input")
			{
				return true;
			}
			else if(key == 13)
			{
				return false;
			}
			
			// allow tab
			if (key == 9)
			{
				return true;
			}
			
			var allow = false;
			// allow Ctrl+A
			if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
			// allow Ctrl+X (cut)
			if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
			// allow Ctrl+C (copy)
			if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
			// allow Ctrl+Z (undo)
			if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
			// allow or deny Ctrl+V (paste), Shift+Ins
			if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */ || (e.shiftKey && key == 45)) return true;
			// backspace & delete
			if ( key == 8  || key == 46 ) return true;
			// left & right
			if ( key == 37  || key == 39 ) return true;

			if ( e.ctrlKey || e.altKey || e.metaKey ) return true;

			// if a number was not pressed
			if(key < 48 || key > 57)
			{
				allow = false;
			}
			else
			{
				allow = true;
			}
			return allow;
		}
	);
	
	this.keyup(function(e){
		if (this.nodeName.toLowerCase() == "input")
		{
			// make sure that no characters were pasted
			var val = jQuery(this).attr("value");
			
			var i = 0;
			while (true)
			{
				if ((val.charCodeAt(i) < 48) || (val.charCodeAt(i) > 57))
				{
					val = val.substr(0, i - 1) + val.substr(i + 1);
				}
				else
				{
					i++;
				}
				
				if (i >= val.length)
				{
					break;
				}
			}
			
			
			jQuery(this).attr("value", val);
		}
	});
	
	this.blur(function(){
		var val      = jQuery(this).attr("value");
		
		if (parseInt(val, 10) < 0)
		{
			val = "0";
		}
			
		jQuery(this).attr("value", val);
	});
	
	return this;
	
}

jQuery.fn.numericDecimal = function (c, d) {
	c = c || ".";
	d = typeof d == "function" ? d: function () {};
	this.keypress(function (e) {
		var a = e.charCode ? e.charCode: e.keyCode ? e.keyCode: 0;
		if (a == 13 && this.nodeName.toLowerCase() == "input") {
			return true
		} else if (a == 13) {
			return false
		}
		var b = false;
		if ((e.ctrlKey && a == 97) || (e.ctrlKey && a == 65)) return true;
		if ((e.ctrlKey && a == 120) || (e.ctrlKey && a == 88)) return true;
		if ((e.ctrlKey && a == 99) || (e.ctrlKey && a == 67)) return true;
		if ((e.ctrlKey && a == 122) || (e.ctrlKey && a == 90)) return true;
		if ((e.ctrlKey && a == 118) || (e.ctrlKey && a == 86) || (e.shiftKey && a == 45)) return true;
		if (a < 48 || a > 57) {
			if (a == 45 && this.value.length == 0) return true;
			if (a == c.charCodeAt(0) && this.value.indexOf(c) != -1) {
				b = false
			}
			if (a != 8 && a != 9 && a != 13 && a != 35 && a != 36 && a != 37 && a != 39 && a != 46) {
				b = false
			} else {
				if (typeof e.charCode != "undefined") {
					if (e.keyCode == e.which && e.which != 0) {
						b = true
					} else if (e.keyCode != 0 && e.charCode == 0 && e.which == 0) {
						b = true
					}
				}
			}
			if (a == c.charCodeAt(0) && this.value.indexOf(c) == -1) {
				b = true
			}
		} else {
			b = true
		}
		return b
	}).blur(function () {
		var a = jQuery(this).val();
		if (a != "") {
			var b = new RegExp("^\\d+$|\\d*" + c + "\\d+");
			if (!b.exec(a)) {
				d.apply(this)
			}
		}
	});
	return this
}