/**
 */
jQuery.fn.inputValue = function(options) {
	var settings = jQuery.extend( {
		check : function(val) {
			return true;
		},
		success : function(val) {
		},
		failed : function(val) {
		},
		nullable : true
	}, options);
	var old_value = null;
	return jQuery(this)
			.attr("input_queue", "global")
			.data("id", new Date().getDate() + Math.random())
			.keyup(function(e) {
				if (!e.charCode)
					var code = String.fromCharCode(e.which);
				else
					var code = String.fromCharCode(e.charCode);

				// if (code && (typeof (e.keyCode) == 'undefined' || (e.keyCode
					// != 8 && e.keyCode != 46))) { keyCode != (bkspc , del)
					if (code) {
						var s = jQuery(this).val();
						jQuery(this).data("value", s);
						if (s !== old_value) {
							old_value = s;
							if ((settings.nullable && settings.nullable == true && s === "")
									|| settings.check(s)) {
								jQuery(this).data("success", true);
								settings.success(s);
								e.preventDefault();
							} else {
								jQuery(this).data("success", false);
								settings.failed(s);
								e.preventDefault();
							}
						}
					}
					if (e.ctrlKey && code == 'v')
						e.preventDefault();
					jQuery(this).bind('contextmenu', function() {
						return false
					});
				})
			.blur(function() {
				var s = jQuery(this).val();
				jQuery(this).data("value", s);
				if (s !== old_value) {
					old_value = s;
					if ((settings.nullable && settings.nullable == true && s === "")
							|| settings.check(s)) {
						jQuery(this).data("success", true);
						settings.success(s);
					} else {
						jQuery(this).data("success", false);
						settings.failed(s);
					}
				}
			})
			.keyup();

};
/**
 * input fields will accept valid email address
 */
jQuery.fn.inputEmail = function(options) {
	return this
			.each(function() {
				var prevchk = options.check;
				var opts = jQuery
						.extend(
								options,
								{
									check : function(val) {
										return val
												.match(/^[a-z0-9_\-]+(\.[_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/)
												&& (prevchk ? prevchk(val)
														: true);
									}
								});
				jQuery(this).inputValue(opts);
			});
};
/**
 * input fields will accept digits only
 */
jQuery.fn.inputInteger = function(options) {
	return this.each(function() {
		var prevchk = options.check;
		var opts = jQuery.extend(options, {
			check : function(val) {
				return val.match(/^\d+$/) && (prevchk ? prevchk(val) : true);
			}
		});
		jQuery(this).inputValue(opts);
	});
};
/**
 * input fields will accept digits and dots.
 */
jQuery.fn.inputFloat = function(options) {
	return this.each(function() {
		var prevchk = options.check;
		var opts = jQuery.extend(options, {
			check : function(val) {
				return val.match(/^\d+\.{0,1}\d*$/)
						&& (prevchk ? prevchk(val) : true);
			}
		});
		jQuery(this).inputValue(opts);
	});
};

