/*
	Template code jQuery plugin wrapper.
	(c) 2010 Rob Watkins
	
	This code is free to use for any purpose, provided the copyright notice and credit to original author is maintained.
	The code is provided AS IS. No guarantee is made on the fitness of this code for any purpose. Use at your own risk.
	
	Usage:
	
	
	$('selector').validated({type:'float'}) 						// Text field that will only accept floating point numbers
	
*/
/*   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 
 *   DEPENDENCIES
 *
 *   jQuery (based on 1.4)
 *   tiny_template_base.js
 *   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~  */

(function($) 
{ 
	// DEFAULT PROPERTIES
	var defaults =
	{
		type: 'none'
	};
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
	// Main template call
  $.fn.validated = function(props /* See defaults above */) 
	{			
		// "Globally" accessible to all functions within hTemplate		
		var $this = this;		
		var $props = props ? $.extend(false, $.fn.validated.defaults, props) : $.fn.validated.defaults;
		//
			
		// UTILITY METHODS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// These must appear before the each() iterator or they are not defined
		this.validatedElementKeyPressed = function(e)
		{				
			var props = this.__validationProps;
						
			if(props.type != 'none')
			{
				var rectrl = /[\x00\x03\x08\x0A\x0D]/;				
				var keyval = String.fromCharCode(e.keyCode);
				
				if(!(e.ctrlKey || e.keyCode == 0 || rectrl.test(keyval)))
				{
					var val = this.value + keyval;
					var fail = false;
					
					// Partial value matches - remember that the user has not finished entering data at this point, so we may have to allow the field contents 
					// to be invalid to enable character validation. See indiviual expressions for details.
					// Upshot is you ALWAYS need to validate the final result as well.
					if(props.type == 'float')
						// Will allow entry of a single '-', '+' if allowing negatives, or '.' in all cases
						fail = !$this.validateRegEx(val, props.allowNegatives != false ? /^[-+]?\d*\.?\d*$/ : /^\d*\.?\d*$/); 
					else if(props.type == 'int')
						// Will allow entry of a single '-', '+' if allowing negatives
						fail = !$this.validateRegEx(val, props.allowNegatives != false ? /^[-+]?\d*$/ : /^\d*$/); 
					else if(props.type == 'time')
					{
						// Will allow entry of one / two digit number, no final minute and invalid times. 
						fail = !$this.validateRegEx(val, /^([012]|[012]\d|[012]\d\:|[012]\d\:[0-5]|[012]\d\:[0-5]\d)$/); 
					}
					//
					
					if(fail)
					{
						e.stopPropagation();
						e.preventDefault();
					}
				}
			}
		}
		
		this.validateRegEx = function(value, re)
		{
			return re.test(value);
		}
		
		// END OF UTILITY METHODS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 		
		// For each selected element, if it is a text field, attach keypress handler
		$this.each(function(index) 
		{				
			if(this.nodeName == 'INPUT' && this.type == 'text')
			{
				this.__validationProps = $props;
				$(this).bind('keypress', $this.validatedElementKeyPressed);
			}
		});

		// Return jQuery object for chaining
		return $this; 
	};
		
 	$.fn.validated.defaults = defaults;
	
})(jQuery);
 