/*
 * Restrict a text input to a certian type
 * Allowed types: integer, float
 * Length: restrict the textbox to a set length, -1 is ignore size
 */

(function($)
{

	$.fn.restriction = function(options)
	{
		var defaults = {
			type: '',
			length: -1
		};
		options = $.extend(defaults, options);
		var event = null;
		var charCode = null;

		function getCharCode()
		{
			return event.keyCode ? event.keyCode : event.which;
		}

		function getSourceElement()
		{
			return event.currentTarget ? event.currentTarget : event.srcElement;
		}

		function checkLength()
		{
			if( options.length > -1 )
			{
				if( $(getSourceElement()).val().length >= options.length )
				{
					return false;
				}
			}

			return true;
		}

		/*
		 * Check to see if the passed in character is in the input box.
		 * Return false if character count is greater than the limit.
		 * @param character
		 * @param limit
		 * @return bool
		 */
		function limitChar(character, limit)
		{
			var returnBool = true;

			if( charCode == character.charCodeAt(0) )
			{
				var targetString = $(getSourceElement()).val();

				if( targetString.split(character).length > limit)
				{
					returnBool = false;
				}
			}

			return returnBool;
		}

		function limitEnter(limit)
		{
			var returnBool = true;

			if( charCode == 13 )
			{
				var targetString = $(getSourceElement()).val();

				if( targetString.split("\n").length > limit)
				{
					returnBool = false;
				}
			}

			return returnBool;
		}

		return this.each(function()
		{
			var obj = $(this);

			obj.keypress( function(localEvent)
			{
				event = localEvent;

				charCode = getCharCode();

				if( charCode == 0 || charCode == 8 || charCode == 9 || charCode == 37 || charCode == 39 )
				{
					return true;
				}
				
				if( event.ctrlKey )
				{
					if( charCode == 120 || charCode == 99 || charCode == 118 )
					{
						return true;
					}
				}


				if( !checkLength(event) )
				{
					return false;
				}

				switch (options.type)
				{
					case 'float':
					case 'integer':
					case 'phone':
					case 'creditcard':
						// Allow numbers 0 - 9
						if( (charCode >= 48) && (charCode <= 57) )
						{
							return true;
						}

						switch (options.type)
						{
							case 'float':
								return limitChar('.', 1);
								break;
							case 'phone':
								switch (charCode)
								{
									case 32: // space
									case 35: // #
									case 40: // (
									case 41: // )
									case 42: // *
									case 43: // +
									case 45: // -
									case 58: // :
									case 91: // [
									case 93: // ]
									case 123: // {
									case 125: // }

										return true;
										break;
									default:
								}
								break;
							case 'creditcard':
								switch (charCode)
								{
									case 32: // space
									case 45: // -
										return true;
										break;
									default:
								}
								break;
						}
						return false;
						break;
					case 'email':
						// Allow 1 case of @
						if( !limitChar('@', 1) )
						{
							return false;
						}

						switch (charCode)
						{
							case 32: // "
							case 40: // (
							case 41: // )
							case 44: // ,
							case 58: // :
							case 59: // ;
							case 60: // <
							case 62: // >
							case 91: // [
							case 92: // \
							case 93: // ]
								return false;
								break;
							default:
						}

						// Allow ascii from ! to ~
						if( (charCode >= 33) && (charCode <= 126) )
						{
							return true;
						}

						return false;
						break;
					case 'address':
						// Allow 1 case of @
						if( !limitEnter(1) )
						{
							return false;
						}

						if( $(getSourceElement()).val().length >= 80 )
						{
							return false;
						}

						return true;
						break;
					default:
						break;
				}
			});
		});
	}
})(jQuery);