(function($){
	
	$.fn.sendForm = function (options) {
	
		var $this = $(this);
		var elementSetToBlock = [];
		
		if($this.size() == 0 || $this.get(0).tagName !== 'FORM') {
			
			if(console && console.log)
				console.log('ERROR: sendForm expects a FORM element');
			else
				alert('ERROR: sendForm expects a FORM element');
		}
		
		options = $.extend({}, {}, options);
		
		var form = {};
		
		
		
//	alert($this.html())

		
		// Add event
		$this.bind('submit', function (e) {
			



			e.preventDefault();
			$.sendForm._send($this, options);
			
			return false;
		});
		
		// Onsubmit, send form
		$.sendForm = {
			
			_send: function (form, options) {
				
				$this = form;
				options = options;
				
				form = {
					method: $this.attr('method'),
					url: $this.attr('action')
				};
				
				var check = $.sendForm._check();
				
				if(check.length > 0) {
					// Error found
					return false;
				}
				
				if(options.beforeSend) {
					
					if(!options.beforeSend($this, options))
						return false;
				}
				
				$.ajax({
					type: form.method.toUpperCase(),
					url: form.url,
					dataType: 'json',
					data: $this.serializeArray(),
					success: function(t){ if(options.success) options.success(t); $.sendForm._empty(); },
					// error(), Nasty solution that will use a html/xml string instead of json object
					error: function (t, textStatus, errorThrown){ if(options.success) options.success(t.responseText); $.sendForm._empty(); }
				});
			},
			
			_check: function () {
				
				$(elementSetToBlock).each(function(){
					
					this.css('display', 'none');
				});
				
				elementSetToBlock = [];
				
				$this.find('.validate:input').each(function(){
					
					var is_wrong = false;
					
					if($(this).hasClass('email')) {
						
						var pattern = /^[a-zA-Z0-9-_\.]+@[a-zA-Z0-9-_\.]+\.[a-zA-Z]{2,4}$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('date')) {
						
						var pattern = /^[0-9]{2}[-| ]{1}[0-9]{2}[-| ]{1}[0-9]{2,4}$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('password-check')) {
						
						var password = $this.find('.validate.password').val();
						
						if(password !== $(this).val())
							is_wrong = true;

						//var pattern = /^[0-9]{2}[-| ]{1}[0-9]{2}[-| ]{1}[0-9]{2,4}$/;
						//is_wrong = $(this).val().match(pattern) > -1;
						
					} else if ($(this).hasClass('username')) {

						var pattern = /^[a-zA-Z-_0-9]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('firstname')) {

						var pattern = /^[a-zA-Z\s]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('lastname')) {

						var pattern = /^[a-zA-Z\s]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('phone')) {

						var pattern = /^[0-9\s\+-]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('zipcode')) {

						var pattern = /^[a-zA-Z0-9\s]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('adress')) {

						var pattern = /^[a-zA-Z0-9\s]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					} else if ($(this).hasClass('city')) {

						var pattern = /^[a-zA-Z0-9\s]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					}else if ($(this).hasClass('text')) {

						var pattern = /^[a-zA-Z-_0-9!@#$%^&*()=|?,.:;\/\s\+\\]+$/;
						is_wrong = $(this).val().match(pattern) == null;
					}
					
					if($(this).hasClass('empty') == true && $(this).val() == '')
						return true;
					
					// Final check (per element)
					if ($(this).val() == '' || is_wrong) {
						
						var found = false;
						
						var c = $(this).attr('class'),
							el = $this.find('.validation.'+c.replace('validate', ''));
						if(el.size() > 0) {
							
							el.show();
							elementSetToBlock.push(el);
							found = true;
						}
						
						if(!found) {
							
							$(this).parent().find('*').each(function(){

								if($(this).css('display') == 'none') {

									$(this).show();
									elementSetToBlock.push($(this));
									found = true;
									return;
								}
							});
						}
					}
				});
				
				return elementSetToBlock;
			},
			
			_empty: function () {
				
				return;
				
				var pattern = new RegExp(/(checkbox|submit)/);
				
				$this.find('input').each(function(){
					// Todo reset checkbox (and other input elements?)
					if(!pattern.test($(this).attr('type')))
						$(this).val('');
				});
			}
		}
	};
})(jQuery);