(function($) {
	$(document).ready(function(){
		
		$('.ajaxform').submit(function() {
			
			function disableForm(form)
			{
				$(':input', form).each(function() {
					this.disabled = true;
				})				
			}
			function enableForm(form)
			{
				$(':input', form).each(function() {
					this.disabled = false;
				})				
			}
			
			var waiting = $("<div class=\"waiting\">").text("čekejte prosím...");
			var formik = $(this);
			
			formik.before(waiting);
			disableForm(formik);
			
			var infor = $("<div class=\"hlaseni\">");


			var inputs = [];
			$(':input', this).each(function() {
				inputs.push(this.name + '=' + encodeURI(this.value));
				
			})
			
			inputs.push('ajaxform=1');
		
			if ($(this).attr('action') && $(this).attr('action') != '')
			{
				var action = $(this).attr('action');
			}
			else
			{
				var action = document.location.pathname;
			}
			
			//console.log(action);
		
			$.ajax({
				data: inputs.join('&'),
				type: "POST",
				url: action,
				timeout: 15000,
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					enableForm(formik);
					waiting.hide();
					$(".hlaseni").remove();
					formik.prepend(infor);
					infor.text(textStatus);
				},
				success: function(r) { 
					enableForm(formik);
					waiting.hide();
					$(".hlaseni").remove();
					formik.prepend(infor);
					infor.text(r);
					

				}
				
			}) 

			return false;
		})
		
	});  
})(jQuery);
