function rewind_pic(id) {
	$(id ? id : 'rnd_picnum').src = '/includes/rnd_image.php?n=' + Math.round(Math.random() * 100);
}

function showErrorForm() {
	if(!$defined($('overlay'))) {
		new Element('div', {
			'id': 'overlay'
		}).setStyle('height', document.getScrollSize().y).set('opacity', 0.5).inject(document.body, 'top').addEvent('click', function() {
			hideErrorForm();
			return false;
		});
	}

	$('formText').setStyle('display', 'block');
	$('formResult').setStyle('display', 'none');
	$('errorForm').setStyle('display', 'block');
}

function hideErrorForm() {
	$('errorForm').setStyle('display', 'none');
	$('overlay').destroy();
	$('errorForm').reset();
	$('errorFormStatus').set('html', '');
	rewind_pic();
}

function errorFormSend() {
	options = {
		url: '/ajax/ajax.php?action=send_error',
		data: $('errorForm'),
		onRequest: function() {
   			$('errorFormSubmit').set('disabled', true);
			$('errorFormStatus').set('html', 'загрузка...');
		},
		onSuccess: function(result) {
			if(result['result']) {
				$('formText').setStyle('display', 'none');
				$('formResult').setStyle('display', 'block');
				$('errorFormStatus').set('html', '');
				$('errorForm').reset();
			}
			else if(result['errors']) {
				$('errorFormStatus').set('html', 'Ошибка! ' + result['errors']);
			}
   			$('errorFormSubmit').set('disabled', false);
		}
	};
	new Request.JSON(options).send();
}

function comments_add() {
	var form = document.getElement('.b-comments-form');
	options = {
		url: '/ajax/ajax.php?action=comments_add',
		data: form,
		onRequest: function() {
   			form.getElement('.b-comment-submit').set('disabled', true);
		},
		onSuccess: function(result) {
			if(result['result']) {
				alert('Комментарий отправлен.');
				form.reset();
			}
			else if(result['errors']) {
				alert(result['errors']);
			}
   			form.getElement('.b-comment-submit').set('disabled', false);
		}
	};
	new Request.JSON(options).send();
}
