(function($) {
	$.extend($.fn, {
		enableSubmission: function() {
			this.validSubmit(function() {
				var form = this;
				var buttons =  $(this).find(":submit").attr("disabled","disabled");
				$(form).remove(".form_legend");
				$.ajax({
					async: false,
					dataType: "json",
					data: $(this).serialize(),
					success: function(response) {
						response.error === undefined || (function(error) {
							error.badField === undefined || (function(field) {
								error.clear && field.val('');
								field.focus().resetValidation().addClass('invalid').message().html(error.msg===undefined?'':error.msg).addClass('invalid');
							})($(form).find(error.badField));
							typeof Recaptcha == "undefined" || Recaptcha.reload();
							error.msg === undefined || $(form).find(".form_legend").append("<p class='invalid'>error.msg</p>");
						})(response.error);
						response.alert === undefined || alert(response.alert);
						response.redirect === undefined || (function(redirect){
							(redirect.parent ? fnparentredirect : fnredirect)(redirect.path,redirect.module);
						})(response.redirect);
						response.msg === undefined || $(form).find(".form_legend").append("<label class='valid'>"+response.msg+"</label>");  
						
					},
					error: function(obj,status) {
						alert("Response error: " + status);
					},
					timeout: 30000,
					type: $(this).attr('method'),
					url: $(this).attr('action')
				});
				buttons.removeAttr("disabled");
				return false;
			});
			return this;
		},
		failedSubmit: function(fn) {
			if(fn === undefined)
				return this.trigger('failedSubmit');
			this.bind('failedSubmit',fn);
			return this;
		}
	});
})(jQuery);
