/*jslint unparam: true */
/*global document, $, jQuery, id, request */

function urlencode(str) {
	"use strict";
	str = String(str);
	str = str.replace(',', '%2C');
	str = str.replace(' ', '+');
	str = str.replace('&', '%26');
	str = str.replace('~', '%7E');
	str = str.replace('?', '%3F');
	str = str.replace('=', '%3D');
	str = str.replace(';', '%3B');
	str = str.replace('\'', '%27');
	str = str.replace('"', '%22');
	return str;
}

function fieldValidationOnBlur(element) {
	"use strict";
	var fieldValue = $(element).val(),
			fid = $(element).attr('id'),
			formId = fid.substr(0, fid.lastIndexOf('-')),
			form = $('form.' + fid.substr(0, fid.lastIndexOf('-'))),
			formClass = form.attr('data-class'),
			recordClass = form.attr('data-record-class'),
			recordId = form.attr('data-record-id'),
			name = fid.substr(fid.lastIndexOf('-') + 1),
			errorsId = formId + '-' + name + '-errors';
	if ('checkbox' === $(element).attr('type') && !$(element).is(':checked')) {
		fieldValue = '0';
	}
	$.post(request.baseUrl + '/?module=cms&controller=form&action=validate', {
		field: name, 
		class: formClass,
		recordClass: recordClass,
		recordId: recordId,
		value: urlencode(fieldValue)
	},
	function (result) {
		$('#' + errorsId).parent().removeClass('error');
		if (result) {
			$('#' + errorsId).parent().addClass('error');
		}
		$('#' + errorsId).html(result);
	});
}

$(document).ready(function () {
	"use strict";

	//podłączenie walidacji na blurze
	$('.validate').on('blur', function () {
		fieldValidationOnBlur($(this));
	});

	//checkbox na change robi blur (walidacja)
	$('input[type="checkbox"].validate').on('change', function () {
		$(this).trigger('blur');
	});

	//pola do przeciwdziałania robotom bez JS
	$('div.antirobot > input').val('js-' + $('div.antirobot > input').val() + '-js');

});
@@ -1204,24 +1204,41 @@
  (result) %7B%0A
+%09%09if (result) %7B%0A%09
 %09%09$('#' + er
@@ -1258,14 +1258,44 @@
 t().
-remove
+addClass('error');%0A%09%09%09$(element).add
 Clas
@@ -1308,27 +1308,22 @@
 or');%0A%09%09
-if (result)
+%7D else
  %7B%0A%09%09%09$(
@@ -1339,35 +1339,74 @@
 orsId).parent().
-add
+removeClass('error');%0A%09%09%09$(element).remove
 Class('error');%0A
@@ -1603,16 +1603,13 @@
 lur(
-$(
 this)
-)
 ;%0A%09%7D

Fix wyświetlania errors w formie