function forum_validate (form)
{
	if (form.author   .value == '') { alert("Необходимо указать ваше имя!"); return false; }
	if (form.topic    .value == '') { alert("Необходимо указать тему!"); return false; }
	if (form.message.value == '') { alert("Необходимо ввести текст сообщения!!"); return false; }
	if (form.gp_answer.value == '') { alert("Необходимо ввести проверочный код!"); return false; }
	return true;
}

function forum_show_form (id)
{
	var rrow = document.getElementById('forum_reply_'+id);
	if (rrow) rrow.style.display = 'none';

	var mrow = document.getElementById('forum_message_'+id);
	if (!mrow) return true;

	var frow = document.getElementById('forum_reply_dynamic');
	if (!frow) return true;

	var form = document.getElementById('forum_reply_form');
	if (!form) return true;

	var topic = document.getElementById('forum_topic_'+id);
	if (!topic) topic = ''; else topic = topic.innerHTML;

	if (mrow.nextSibling && (mrow.nextSibling.id == frow.id) && (frow.style.display != 'none'))
	{
		frow.style.display = 'none';
//			frow.cells[1].style.paddingLeft = '0px';
//			form.action = ROOT + 'forum/post';
//			form.topic.value = '';
	} else
	{
		frow.style.display = 'none';
			mrow.parentNode.insertBefore(frow, mrow.nextSibling);
			frow.cells[1].style.paddingLeft = mrow.cells[1].style.paddingLeft;
			form.action = ROOT + 'forum/post/' + id;
			form.topic.value = forum_generate_topic(topic);
		frow.style.display = '';
	}

	return false;
}

function forum_generate_topic (topic)
{
	var replies = 0;

	var pos = 0;
	do
	{
		pos = topic.indexOf('RE: ', pos);
		if (pos != -1)
		{
			replies++;
			topic = topic.substr(0, pos) + topic.substr(pos+4);
		}
	} while (pos != -1);

	var pos1 = 0, pos2 = 0;
	do
	{
		pos1 = topic.indexOf('RE[', pos1);
		pos2 = topic.indexOf(']: ', pos1+1);
		if ((pos1 != -1) && (pos2 != -1))
		{
			var n = parseInt(topic.substr(pos1+3, pos2-pos1+1-3));
			replies += (n > 0) ? n : 0;
			topic = topic.substr(0, pos1) + topic.substr(pos2+3);
		}
	} while ((pos1 != -1) && (pos2 != -1));

	return 'RE[' + (replies+1) + ']: ' + topic;
}

