/* Input Validation Object
 * For the Digital Order Logistics Software
 * 10/15/2010 Randy Little
 * Billy Reid, INC
 * Using libraries: jQuery
 */
 
 (function(jQuery){  
	 jQuery.fn.validate = function(options)
	 {
		var defaults = {
			numerical: false,
			alphabetical: false,
			empty: false,
			spaces: false,
			email: false,
			domain: false,
			noforwardslash: false,
			nobackslash: false,
			max: 0
		}
		var options = jQuery.extend(defaults, options);
		
		return this.each(function() {

			obj = jQuery(this);
			
			if ( options.max > 0 )
			{
				jQuery(obj).attr("maxlength",options.max);
			}
		
			// Realtime validation.
			jQuery(obj).keydown(function(e){
				k=String.fromCharCode(e.keyCode);
				if ( options.numerical )
				{
					if
					(
						k.match(/[0-9]/) 
					)
					{
						e.preventDefault();
					}
				}
				if ( options.alphabetical )
				{
                                        if ( event.keyCode != 46 && event.keyCode != 8 ) {
                                                if
                                                (
                                                         (e.keyCode < 48 || e.keyCode > 57) && (e.keyCode < 96 || e.keyCode > 105)
                                                )
                                                {
                                                        /* Temporary Adaptation to make this passive. */
                                                        e.preventDefault();
                                                }
                                        }
				}
				if ( options.spaces )
				{
					if
					(
						k.match(/ /)
					)
					{
						e.preventDefault();
					}
				}
				if ( options.noforwardslash )
				{
					if
					(
						e.keyCode == 191
					)
					{
						e.preventDefault();
					}
				}
				if ( options.nobackslash )
				{
					if
					(
						e.keyCode == 220
					)
					{
						e.preventDefault();
					}
				}
			});
			
			// Post-data-entry validation.
			jQuery(obj).blur(function(){
				var err = {
					flag: false,
					type: ""
				}
				if ( options.max > 0 )
				{
					if
					(
						jQuery(this).val().length > options.max
					)
					{
						err.flag = true;
						err.type = "too long";
					}
				}
				if ( options.numerical )
				{
					if
					(
						jQuery(this).val().match(/[0-9]/g)
					)
					{
						err.flag = true;
						err.type = "cannot contain numerical characters";
					}
				}
				if ( options.alphabetical )
				{
					if
					(
						jQuery(this).val().match(/[abcdefghijklmnopqrstuvwxyz]/gi)
					)
					{
						err.flag = true;
						err.type = "cannot contain alphabetical characters";
					}
				}
				if ( options.noforwardslash )
				{
					if
					(
						jQuery(this).val().match(/\//gi)
					)
					{
						err.flag = true;
						err.type = "cannot contain forwardslashes";
					}
				}
				if ( options.nobackslashslash )
				{
					if
					(
						jQuery(this).val().match(/\\/gi)
					)
					{
						err.flag = true;
						err.type = "cannot contain backslashes";
					}
				}
				if ( options.spaces )
				{
					if
					(
						jQuery(this).val().match(/ /g)
					)
					{
						err.flag = true;
						err.type = "cannot contain spaces";
					}
				}
				if ( options.empty )
				{
					if
					(
						jQuery(this).val() == ""
					)
					{
						err.flag = true;
						err.type = "cannot be empty";
					}
				}
				if ( options.email )
				{
					if
					(
						!jQuery(this).val().match(/@/g) ||
						!jQuery(this).val().match(/\./g)
					)
					{
						err.flag = true;
						err.type = "is not a valid email address";
					}
				}
				if ( options.domain )
				{
					if
					(
						!jQuery(this).val().match(/^http:\/\//i) ||
						!jQuery(this).val().match(/\./g)
					)
					{
						err.flag = true;
						err.type = "is not a valid domain name";
					}
				}
				
				if ( err.flag ) { jQuery(this).addClass('jq-validate-error'); } else { if ( jQuery(this).hasClass ('jq-validate-error') ) { jQuery(this).removeClass('jq-validate-error'); } }
			});

		});
	 };
 })(jQuery); 

