jQuery.noConflict();
jQuery(document).ready(function($) {
										  
	var form = $('#Body form');
	var formid = form.attr('id');
	var next = $('.ButtonWrapper', form);
	var nextInput = $('input', next);
	var nextSpan = $('span', next);
										  
	$.ajaxSetup({
		url: 'FormValidate.php',
		dataType: 'json',
		type: 'POST'
	});
	
	
	function doAjax(action, field) {
		$.ajax({
			data: form.serialize() + '&action=' + action + '_' + formid + '&field=' + field,
			success: function (j) {				
				for (var i in j[0]) {
					$('#' + i + 'ErrMsg', form).text(j[0][i]);
				}
				
				if (j[1]) {
					nextInput.show();
					nextSpan.hide();
				}
				else {
					nextInput.hide();
					nextSpan.show();					
				}
				
				if (formid == 'Prijavnica') {
					if (j[2]) {
						$('#ClanstvoFields').show();
					}
					else {
						$('#ClanstvoFields').hide();						
					}
				}
			}
		});			
	}
	
	$('input.CheckBox').click(function () {
		doAjax('validate', $(this).attr('name'));
	});													
	
	$('textarea, input:not(.Button)', form).keyup(function () {
		var name = $(this).attr('name');
		var value = this.value;
		if (this.value != this.lastValue) {
			if (this.timer) { clearTimeout (this.timer) };			
			this.timer = setTimeout(function () { doAjax('validate', name) }, 300);			
			this.lastValue = this.value;
		}		
	});
	
	nextSpan.click(function () {
		doAjax('validate', 'all');
	});	
	
});