(function($) {

$.fn.extend({
	validateForm: function() {
		this.each(function() {
			$(this).find('input:button').each(function() { this.disabled = true; });	
			$(this).find('input:submit').each(function() { this.disabled = true; });	
			$(this).find('input:text, textarea')
				.data('isValid', true)
				.mouseover(function() {
					var validatedForm = $(this).parents('form')[0];
					$(validatedForm).executeValidation();
				})
				.keyup(function() {
					var validatedForm = $(this).parents('form')[0];
					$(validatedForm).executeValidation();
				})				
				.focus(function() { $(this).css('color', '#000000'); })
				.change(function() {	
					var validatedForm = $(this).parents('form')[0];
					$(validatedForm).executeValidation();									
					if ($(this).data('isValid') == true)	$(this).css('color', '#000000'); 
				})
				.blur(function() { if ($(this).data('isValid') != true) $(this).css('color', '#FF0000'); });
			$(this).find('ul.radio-validation').data('isValid', true);
			$(this).find('input:radio')
				.click(function() {
					var validatedForm = $(this).parents('form')[0];
					$(validatedForm).executeValidation();
				});
			$(this).executeValidation();
		});
	},
	executeValidation: function() {
		var formIsValid = 
			this.find('input:text.text-validation, textarea.text-validation').validateText() & 
			this.find('input:text.email-validation').validateEmail() &
			this.find('input:text.date-validation').validateDate() &
			this.find('input:text.number-validation').validateNumber() &
			this.find('ul.radio-validation').validateRadioButtons();
		if (formIsValid) {
			this.find('div.notice').css('display','none');
			this.find(':button').each(function() { this.disabled = false; $(this).removeClass('button-inactive'); $(this).addClass('button-active'); });
			this.find(':submit').each(function() { this.disabled = false; $(this).removeClass('button-inactive'); $(this).addClass('button-active'); });

		}
		else {
			this.find('div.notice').css('display','block');
			this.find(':submit').each(function() { this.disabled = true; $(this).addClass('button-inactive'); $(this).removeClass('button-active'); });
			this.find(':button').each(function() { this.disabled = true; $(this).addClass('button-inactive'); $(this).removeClass('button-active'); });

		}

	},
	validateText: function() {
		var valid = true;
		this.each(function() { 		
			var text = $.trim(this.value);
			$(this).data('isValid', (text.length > 0));
			valid = valid && $(this).data('isValid'); 
		});
		return valid;
	},
	validateEmail: function() {
		var valid = true;
		var rx = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/);
		this.each(function() { 
			var matches = rx.exec(this.value);
			$(this).data('isValid',  (matches != null && this.value == matches[0]));			
			valid = valid && $(this).data('isValid'); 
		});
		return valid;
	},
	validateDate: function() {
		var valid = true;
		var rx = new RegExp(/^\d{2}\.\d{2}\.\d{4}$/);
		this.each(function() { 
			var matches = rx.exec(this.value);
			$(this).data('isValid',  (matches != null && this.value == matches[0]));			
			valid = valid && $(this).data('isValid'); 
		});
		return valid;
	},	
	validateNumber: function() {
		var valid = true;
		this.each(function() { 
			$(this).data('isValid', !isNaN(Number(this.value))); 
			valid = valid && $(this).data('isValid');
		});
		return valid;
	},	
	validateRadioButtons: function() {
		var valid = true;
		this.each(function() {
			$(this).data('isValid', ($(this).find('input:radio:checked').length > 0));
			valid = valid && $(this).data('isValid');
		});
		return valid;
	}
});

})(jQuery);

jQuery(function($) {
	$('form.needs-validation').validateForm();
});
