
function preview_comment (id)
	{
	document.getElementById("preview"+id).style.display = 'block';
	var params = "comment=" + encodeURI( document.getElementById("commenttext"+id).value );
	do_post ("http://bdg.by/comments/preview.html", "preview"+id, params);
	document.getElementById("previewtext"+id).style.display = 'block';
	}
	
function add_comment(id)
{
	var islogged = document.getElementById('islogged').value;
	if (islogged == "true")
	{
		document.getElementById("preview"+id).style.display = 'none';
		document.getElementById("previewtext"+id).style.display = 'none';
	
		var params = "comment=" + encodeURI( document.getElementById("commenttext"+id).value ) + "&" +
		"item=" + encodeURI( document.getElementById("item"+id).value ) + "&" +
		"type=" + encodeURI( document.getElementById("type"+id).value ) + "&" +
		"level=" + encodeURI( document.getElementById("level"+id).value ) + "&" +
		"parent=" + encodeURI( document.getElementById("parent"+id).value );
	
		var time = new Date().getTime();
		var div1 = document.createElement("div");
		div1.setAttribute("id", "comment_new_"+time);
		div1.className="commentitem";
		var div2 = document.getElementById("commentendof"+id);
		var parentDiv = div2.parentNode;
		parentDiv.insertBefore(div1, div2);
		do_post ("http://bdg.by/comments/post.html", "comment_new_"+time, params);
	
		var matches = document.getElementById("comment_new_"+time).innerHTML.match(/comment([0-9]*)/);
		var div3 = document.createElement("div");
		div3.setAttribute("id", "commentendof"+matches[1]);
		div3.className="hidden";
		parentDiv.insertBefore(div3, div1.nextSibling);
	
		close_form(id);
	
		ScrollToElement(document.getElementById("comment_new_"+time));
		
		document.getElementById('commenttext'+id).value = "";
	}
	else 
	{
	formFade("regformcomment");
	}
}

function show_form(id)
	{
	activeForm = document.getElementById('activeForm').value
	if (activeForm != "")
	{
		close_form(activeForm);
	}
	document.getElementById('commentform'+id).style.display = 'block';
	if (id != 0) {document.getElementById('commentreply'+id).style.display = 'none';}
	document.getElementById('activeForm').value = id;
	document.getElementById('commenttext'+id).focus();
	}
	
function close_form(id)
	{
	document.getElementById('commentform'+id).style.display = 'none';
	if (id != 0) {document.getElementById('commentreply'+id).style.display = 'block';}
	}

function ScrollToElement(theElement)
{
	var selectedPosX = 0;
	var selectedPosY = 0;

	while(theElement != null)
	{
		selectedPosX += theElement.offsetLeft;
		selectedPosY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}

	window.scrollTo(selectedPosX,selectedPosY);
}
	
