function comment_show(curpage,cat,id) {
	var action = 'comshow';
	var comdiv = document.getElementById('comwrap');
	document.getElementById('comments_pager_top').innerHTML = "<div style=\"text-align: center;\">Загрузка...</div>";
	document.getElementById('comments_pager_bottom').innerHTML = "<div style=\"text-align: center;\">Загрузка...</div>";
	var req = new Subsys_JsHttpRequest_Js();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			comdiv.innerHTML = req.responseText;
		}
	}
	req.caching = false;
	req.open('POST', '/ajax/comments.php', true);
	req.send({ act: action, curpage: curpage, cat: cat, id: id });
} 
function comment_add() {
	var action = 'comadd';
	document.forms.comform.subbut.disabled = 'disabled';
	var txt = document.forms.comform.text.value;
	var id = document.forms.comform.id.value;
	var div = document.forms.comform.div.value;
	var keystring = document.forms.comform.keystring.value;
	var req = new Subsys_JsHttpRequest_Js();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			var comdiv = document.getElementById('comwrap');
			comdiv.innerHTML = req.responseText;
			document.forms.comform.subbut.disabled = '';
			if (req.responseJS.answer == "fuu" ) {
				document.getElementById('commentform').innerHTML = "<div id=\"commentform\" style=\"color: red;\">Нука фу!. <a href=\"javascript:;\" onclick=\"appendform('"+div+"','"+id+"');\">Новое сообщение</a></div>";
			}
			else {
				document.getElementById('commentform').innerHTML = "<div id=\"commentform\" style=\"color: green;\">Добавлено. <a href=\"javascript:;\" onclick=\"appendform('"+div+"','"+id+"');\">Новое сообщение</a></div>" ;
			}
		}
	}

	req.caching = false;
	req.open('POST', '/ajax/comments.php', true);
	req.send({ act: action, txt: txt,id: id, div: div, keystring: keystring });
} 
function appendform(cat,id) {
	var action = 'appendform';
	var req = new Subsys_JsHttpRequest_Js();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			document.getElementById('commentform').innerHTML = req.responseText;
		}
	}
	req.caching = false;
	req.open('POST', '/ajax/comments.php', true);
	req.send({ act: action, cat: cat, id: id});
}
function offsetPosition(element) {
    var offsetTop = 0;
    do {
        offsetTop  += element.offsetTop;
    } while (element = element.offsetParent);
    return [offsetTop];
}
function comment_preview(){
	var action = 'compre';
	document.forms.comform.subbut.disabled = 'disabled';
	document.getElementById('prevlink').onclick = '';
	var txt = document.forms.comform.text.value;
	var id = document.forms.comform.id.value;
	var div = document.forms.comform.div.value;
	var keystring = document.forms.comform.keystring.value;
	var req = new Subsys_JsHttpRequest_Js();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			var comdiv = document.getElementById('compre');
			comdiv.style.left = '50%';
			comdiv.style.marginLeft = '-340px';
			comdiv.style.position = 'absolute';
			comdiv.style.display = 'block';
			document.getElementById('pretext').innerHTML = req.responseText;
			comdiv.style.top = offsetPosition(document.getElementById('prevlink')) - 65 - comdiv.clientHeight + 'px';
		}
	}

	req.caching = false;
	req.open('POST', '/ajax/comments.php', true);
	req.send({ act: action, txt: txt,id: id, div: div, keystring: keystring });
}
function comment_preview_close() {
	document.getElementById('prevlink').onclick = comment_preview;
	document.forms.comform.subbut.disabled = '';
	document.getElementById('compre').style.display = 'none';
}
function comment_preview_send() {
	var txt = document.forms.comform.text.value;
	if (txt.length == 0) {
		alert('Нельзя отправлять пустые сообщения!');
	}
	else if (txt.length < 6000) {
		var action = 'precheck';
		
		document.forms.comform.subbut.disabled = 'disabled';
		var keystring = document.forms.comform.keystring.value;
		var req = new Subsys_JsHttpRequest_Js();
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.responseText == "ok") {
					comment_add();
				}
				else {
					document.forms.comform.subbut.disabled = '';
					document.getElementById('compre').style.display = 'none';
					document.getElementById('prevlink').onclick = comment_preview;
					alert('Ошибка ввода проверочного кода.');
				}
			}
		}
	
		req.caching = false;
		req.open('POST', '/ajax/comments.php', true);
		req.send({ act: action, keystring: keystring });
	}
	else {
		alert('Сообщение больше 6000 символов.');
	}
}
function bbhelp_open(){
	document.getElementById('bbhelp').style.display = 'block';
	document.getElementById('bblink').onclick = bbhelp_close;
}
function bbhelp_close(){
	document.getElementById('bbhelp').style.display = 'none';
	document.getElementById('bblink').onclick = bbhelp_open;
}
function getsel() {
	if (document.getSelection) {
		text = document.getSelection();
	} else if (document.selection && document.selection.createRange) {
		text = document.selection.createRange().text;
	}
	if (text != "") {
		document.forms.comform.text.innerHTML += "[br][q]"+text+"[/q][br]";
	}
	else {
		alert('До нажатия на эту педаль - нужно выделить цитируемый текст!');
	}
}
