function createBigForm (t, type) {
	if (document.getElementById('unauthorized') && (auth == 0 || name == 0)) {
		currentForm = t;
		currentFormType = type;
		insertAuth(t);
		return false;
	}
	t = t.parentNode.parentNode;
	if (t.parentNode.className == 'c') {
		t.parentNode.className = 'c createdForm';
		bForm = crEl('form');
		bForm.setAttribute('method', 'post');
		bForm.setAttribute('action', '/php_includes/forum_v2.php');
		bTextarea = crEl('textarea');
		bTextarea.setAttribute('name', 'text')
		bSubmit = crEl('input');
		bSubmit.setAttribute('type', 'submit');
		bSubmit.className = 'submit';

		bHidden = crEl('input');
		bHidden.setAttribute('type', 'hidden');
		bHidden.setAttribute('name', 'iaction');

		bForumId = crEl('input');
		bForumId.setAttribute('type', 'hidden');
		bForumId.setAttribute('name', 'forum_id');
		bForumId.setAttribute('value', forum_id);

		if (type == 'comment') {
			bForm.setAttribute('action', '/php_includes/forum_v2.php');
			bForm.appendChild(bTextarea);
			bForm.appendChild(bForumId);
			bTopicId = crEl('input');
			bTopicId.setAttribute('type', 'hidden');
			bTopicId.setAttribute('name', 'topic_id');
			bTopicId.setAttribute('value', topic_id);
			bForm.appendChild(bTopicId);
			
			bSubmit.setAttribute('value', langVars[9]);
			bHidden.setAttribute('value', 'comment_add');
			bForm.appendChild(bHidden);
			focusEl = bTextarea;
			bForm.appendChild(bSubmit);
			bForm.setAttribute('onkeypress', 'submitBigShort(event, this, \'comment\')');
			if (typeof(bForm.onkeypress) != 'function') { bForm.setAttribute('onkeypress', function() {submitBigShort(event, this, 'comment')}) }
		} else if (type == 'topic') {
			bForm.setAttribute('action', '/php_includes/forum_v2.php');
			bForm.appendChild(bForumId);

			label1 = crEl('label');
			label2 = crEl('label');
			label1.appendChild(crTxt(langVars[10]));
			label2.appendChild(crTxt(langVars[11]));
			bForm.appendChild(label1);
			bInput = crEl('input');
			bInput.setAttribute('type', 'text');
			bInput.setAttribute('name', 'title');
			bForm.appendChild(bInput);
			bHidden.setAttribute('value', 'topic_add');
			bForm.appendChild(bHidden);
			bForm.appendChild(label2);
			bForm.appendChild(bTextarea);
			bSubmit.setAttribute('value', langVars[12]);
			focusEl = bInput;
			bForm.appendChild(bSubmit);
			bForm.setAttribute('onkeypress', 'submitBigShort(event, this, \'topic\')');
			if (typeof(bForm.onkeypress) != 'function') { bForm.setAttribute('onkeypress', function() {submitBigShort(event, this, 'topic')}) }
		}

		bForm.setAttribute('onsubmit', 'if (checkBigForm(this, \''+type+'\') != true) {return false;}');
		if (typeof(bForm.onsubmit) != 'function') { bForm.setAttribute('onsubmit', function() {if (checkBigForm(this, type) !=true) {return false}}) }

		t.appendChild(bForm);
		if (focus == 1) {
			focusEl.focus();
		}
	} else if (t.parentNode.className == 'c createdForm') { t.parentNode.className = 'c collapsedForm' }
	  else if (t.parentNode.className == 'c collapsedForm') {
		t.parentNode.className = 'c createdForm';
		if (type == 'comment') { focusEl = t.getElementsByTagName('textarea')[0]; }
		else if (type == 'topic') { focusEl = t.getElementsByTagName('input')[0]; }
		focusEl.focus();
	}
	return false;
}

function submitBigShort (e, t, type) {
	if (((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)) {
		if (checkBigForm(t, type)) t.submit();
	}
}

function checkBigForm (t, type) {
	tAr = t.getElementsByTagName('textarea')[0];
	iPt = t.getElementsByTagName('input')[1];
	if (type == 'topic') {
		t.getElementsByTagName('input')[3].setAttribute('disabled', 'disabled');
		if (iPt.value == 0)
		{
			iPt.focus();
			return false;
		}
		else
		{
			if (tAr.value == 0)
			{
				tAr.focus();
				return false;
			}
			else
			{
				if (tAr.value != 0 && iPt.value != 0)
				{
					return true;
				}
			}
		}
	} else if (type == 'comment') {
		if (tAr.value == 0) {
			//tAr.focus();
			return false;
		}

		t.getElementsByTagName('input')[3].setAttribute('disabled', 'disabled');

		var text = tAr.value;
		text.replace('?', '%3F');
		text.replace('&', '%26');
		
		$.post (
			'/php_includes/forum_v2.php',
			{
				iaction: 'comment_add',
				forum_id: forum_id,
				topic_id: topic_id,
				parent_id: 0,
				text: htmlentities(text)
			},
			function (data, status)
			{
				if (data=='403')
				{
					alert('Вы заблокированы на форуме и не можете отправлять сообщения!');
					return;
				}
				data = eval("(" + data + ")");
				if (data['id']) {
					addComment(document.getElementById('comments'), 0, data['text'], data['id'], data['nick'], data['name'], data['company'], data['city'], data['date']);
					t.parentNode.parentNode.className = 'c';
					t.parentNode.removeChild(t);
				} else {
					t.getElementsByTagName('textarea')[0].removeAttribute('disabled');
					t.getElementsByTagName('input')[0].removeAttribute('disabled');
					t.className = '';
					loadingEl = t.getElementsByTagName('input')[0];
					loadingEl.value = lAnsware;
				}
			}
		)
		
		// вот отсюда посылаем запрос на сервер. comId - это идентификатор комментария, на который был написан ответ. comText - это текст комментария, который мы отправляем. В ответ получаем всю хуйню.
		// если ответ не пришел:
		//t.getElementsByTagName('input')[0].removeAttribute('disabled');
		//tAr.setAttribute.removeAttribute('disabled');
		//alert('все плохо, ответ не пришел =( попробуйте еще раз');
		//return false
		// если пришел:
		return false;
	}
}

function crEl (el) { return document.createElement(el); }
function crTxt (el) { return document.createTextNode(el); }


function insertAuth(t) {
	unAuth = document.getElementById('unauthorized').cloneNode(document.getElementById('unauthorized'));
	document.getElementById('unauthorized').parentNode.removeChild(document.getElementById('unauthorized'));
	if (forum_locked == 1) {
		unAuth.className = 'unauth';
		unAuth.getElementsByTagName('input')[0].value = 'main_login';
		head = unAuth.getElementsByTagName('div')[0].firstChild;
		head.nodeValue = langVars[6];
		a = unAuth.getElementsByTagName('a')[1];
		a.firstChild.nodeValue = langVars[3];
		a.setAttribute('href', '/agent/agents/register/');
		a.setAttribute('onclick', '');
		p = unAuth.getElementsByTagName('p')[0];
		p.firstChild.nodeValue = langVars[0];
	} else {
		if (auth == 1 && name == 0) {
			unAuth.getElementsByTagName('input')[0].value = 'set_name';
			head = unAuth.getElementsByTagName('div')[0].firstChild;
			head.nodeValue = langVars[8];
			p = unAuth.getElementsByTagName('p')[0];
			p.firstChild.nodeValue = '';
			unAuth.className = 'unname';
		} else {
			unAuth.className = 'unreg';
			unAuth.getElementsByTagName('input')[0].value = 'register';
			head = unAuth.getElementsByTagName('div')[0].firstChild;
			head.nodeValue = langVars[2];
			p = unAuth.getElementsByTagName('p')[0];
			p.firstChild.nodeValue = langVars[7];
			a = unAuth.getElementsByTagName('a')[1];
			a.firstChild.nodeValue = langVars[5];
			/*
			if (unAuth.getElementsByTagName('div')[0].firstChild.nodeValue == langVars[1]) {
				t.getElementsByTagName('input')[0].value = 'register';
				head.nodeValue = head.nodeValue.replace(langVars[6], langVars[3]);
				p.firstChild.nodeValue = langVars[7];
				a.firstChild.nodeValue = a.firstChild.nodeValue.replace(langVars[4], langVars[5]);
			}
			*/
		}
	}
	t.parentNode.parentNode.appendChild(unAuth);
	return false;
	if (unAuth.getElementsByTagName('input')[0].value == 0) {
		//unAuth.getElementsByTagName('input')[0].focus();
	} else {
		//unAuth.getElementsByTagName('input')[0].focus();
	}
}

function closeUnauth() {
	document.getElementById('unauthorized').className = '';
}


function turnUnauth() {
	t = document.getElementById('unauthorized');
	head = t.getElementsByTagName('div')[0].firstChild;
	ipt = t.getElementsByTagName('input');
	label = t.getElementsByTagName('label')[1];
	p = t.getElementsByTagName('p')[0];
	a = t.getElementsByTagName('a')[1];
	if (t.className == 'unreg') {
		t.className = 'unauth';
		t.getElementsByTagName('input')[0].value = 'main_login';
		head.nodeValue = head.nodeValue.replace(langVars[3], langVars[6]);
		p.firstChild.nodeValue = langVars[0];
		a.firstChild.nodeValue = a.firstChild.nodeValue.replace(langVars[5], langVars[4]);
	} else {
		t.className = 'unreg';
		t.getElementsByTagName('input')[0].value = 'register';
		head.nodeValue = head.nodeValue.replace(langVars[6], langVars[3]);
		p.firstChild.nodeValue = langVars[7];
		a.firstChild.nodeValue = a.firstChild.nodeValue.replace(langVars[4], langVars[5]);
	}
	/*if (ipt[0].value == 0) ipt[0].focus()
	else { try {ipt[1].focus();} catch (e) { ipt[0].focus(); } }
	*/
	ipt[1].value = '';
}

function setAuth() {
	t = document.getElementById('unauthorized');
	p = t.getElementsByTagName('p')[0];
	var url = '/php_includes/register_v2.php';

	if (t.getElementsByTagName('input')[0].value == 'set_name')
	{
		$.get (
			url,
			{
				iaction: 'set_name',
				geo_area_href: geo_area_href,
				region_id: t.getElementsByTagName('select')[0].value,
				city_id: t.getElementsByTagName('select')[1].value,
				name: t.getElementsByTagName('input')[5].value
			},
			function (data, status)
			{
				result = eval("(" + data + ")");
				if (result['finish'] == 1)
				{
					name = 1;
					closeUnauth();
					if (currentFormType) {
						createBigForm (currentForm, currentFormType);
					} else {
						createForm (currentForm);
					}
				}
				else
				{
					if (result['errors'].length > 0)
					{
						tmp = '';
						for (i = 0; i < result['errors'].length; i++)
						{
							tmp += result['errors'][i] + "\r\n";
						}
						p.firstChild.nodeValue = tmp;
					}
				}
			}
		);
	}
	if (t.getElementsByTagName('input')[0].value == 'main_login')
	{
		$.get (
			url,
			{
				iaction: 'main_login',
				geo_area_href: geo_area_href,
				email: t.getElementsByTagName('input')[3].value,
				password: t.getElementsByTagName('input')[4].value
			},
			function (data, status)
			{
				result = eval("(" + data + ")");
				if (result['finish'] == 1) {
					auth = 1;
					if (result['name'] == 1) {
						name = 1;
						closeUnauth();
						if (forum_locked == 1) {
							document.location.reload();
						} else {
							if (currentFormType) {
								createBigForm (currentForm, currentFormType);
							} else {
								createForm (currentForm);
							}
						}
					} else {
						name = 0;
						setAuth();
					}
				} else {
					if (result['errors'].length > 0)
					{
						tmp = '';
						for (i = 0; i < result['errors'].length; i++)
						{
							tmp += result['errors'][i] + "\r\n";
						}
						p.firstChild.nodeValue = tmp;
					}
				}
			}
		)
		
		if (auth == 1 && name == 0) {
			insertAuth(t);
		}
	}
	if (t.getElementsByTagName('input')[0].value == 'register') {
		$.get (
			url,
			{
				iaction: 'register',
				geo_area_href: geo_area_href,
				region_id: t.getElementsByTagName('select')[0].value,
				city_id: t.getElementsByTagName('select')[1].value,
				name: t.getElementsByTagName('input')[5].value,
				email: t.getElementsByTagName('input')[3].value
			},
			function (data, status)
			{
				result = eval("(" + data + ")");
				if (result['finish'] == 1) {
					auth = 1;
					name = 1;
					closeUnauth();
					if (currentFormType) {
						createBigForm (currentForm, currentFormType);
					} else {
						createForm (currentForm);
					}
				} else {
					if (result['errors'].length > 0)
					{
						tmp = '';
						for (i = 0; i < result['errors'].length; i++)
						{
							tmp += result['errors'][i] + "\r\n";
						}
						p.firstChild.nodeValue = tmp;
					}
				}
			}
		)
	}
	
	// t.getElementsByClassName('submit')[0].disabled = false;
	return false;
}