
jQuery.fn.validate = function() {
	
	var methods = {
		text : function($this) {
			return ($this.val() && $this.val() != '') ? true : false;
		},
		
		selectone : function($this) {
			return ($("option:selected", $this).val()) ? true : false;
		},
		
		radio : function($this) {
			e	  = false;
			name  = $this.attr('name');
			
			$('form').find("input[@name='"+name+"']").each(function(){
				if (this.checked) { e = true; };
			});
			
			return e;
		},
		
		checkbox : function($this) {
			return ($this.attr('checked')) ? true : false;
		},
		
		email : function($this) {
			e = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($this.val());
			return e;
		},
		
		date : function($this) {
			var i            = 0;
			var date         = new Array();
            var RegExPattern = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
			$($this).parent().children('select').each(function(){
				if (val = this.value)
				{
					date[i] = val;	
					i++;
				}
			});

			value  = (i == 3) ? date.join('/') : false;
			result = value.match(RegExPattern);

			if (value && result) {
				return true;
			}

			return false;
		}
	};

	return this.each(function(){
		$this = jQuery(this);

		$this.bind('submit', reset);
		$this.bind('submit', validate);
	});	
	
	function validate() {
		var $this  = jQuery(this);
		var fields = $this.find('div.required>:input:not(.notValidate)');
		var e	   = true;

		fields.each(function() {
			e = check(this);

			if ($(this).parent().is('.date') || $(this).parent().is('.email')) {
				e = check_type(this);
			}

			if (!e) { $(this).validateSetError() }
			
			return e;
		});

		return e;
	};
	
	function check(el)
	{
		$this 	   = $(el);
		rule_name  = (el.type) ? el.type : 'text';
		rule       = methods[rule_name.replace(/-/, '')];
		e          = rule($this);
		
		return e;
	};
	
	function check_type(el)
	{
		$this    = $(el);
		specials = new Array('date', 'email');
		e        = true;
		
		for (i in specials) {
			name = specials[i];

			if ($this.parent().is('.'+name)) {
				rule = methods[name];
				e    = rule($this);
			}
		}
		return e;
	};
	
	function reset() {
		$this = jQuery(this);

		$this.find('div.required').removeClass('error');
		$this.find('div.required>p').hide();
	};

};

/* estendo l'oggetto jQuery */
jQuery.fn.validateSetError = function() {
	this.parent().addClass('error').find('p').fadeIn('slow');
	this.focus().scroll();
		
	return this;
};
