// Мой ответ -----------

function postReply(authorId, commentarea){

	var author = document.getElementById(authorId).innerHTML;
	var comment = document.getElementById(commentarea);
			
	author = '<font color="#600000">' + author.replace(/\t|\n|\r\n/g, "") + ' </font>' + '\n';

	if (document.selection) {
		comment.focus();
		sel = document.selection.createRange();
		sel.text = author;
		comment.focus();
	}
	// MOZILLA/NETSCAPE support
	else if (comment.selectionStart || comment.selectionStart == '0') {
		var startPos = comment.selectionStart;
		var endPos = comment.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = comment.scrollTop;
		if (startPos != endPos) {
			comment.value = comment.value.substring(0, startPos)
			              + author
			              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + author.length
		}
		else {
			comment.value = comment.value.substring(0, startPos) 
				              + author
				              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + author.length;
		}
		comment.focus();
		comment.selectionStart = cursorPos;
		comment.selectionEnd = cursorPos;
		comment.scrollTop = scrollTop;
	}
	else {
		comment.value += author;
	}
}

function postReply2(link, commentarea){

	var comment = document.getElementById(commentarea);
		
	if (document.selection) {
		comment.focus();
		sel = document.selection.createRange();
		sel.text = link;
		comment.focus();
	}
	// MOZILLA/NETSCAPE support
	else if (comment.selectionStart || comment.selectionStart == '0') {
		var startPos = comment.selectionStart;
		var endPos = comment.selectionEnd;
		var cursorPos = endPos;
		var scrollTop = comment.scrollTop;
		if (startPos != endPos) {
			comment.value = comment.value.substring(0, startPos)
			              + link
			              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + link.length
		}
		else {
			comment.value = comment.value.substring(0, startPos) 
				              + link
				              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + link.length;
		}
		comment.focus();
		comment.selectionStart = cursorPos;
		comment.selectionEnd = cursorPos;
		comment.scrollTop = scrollTop;
	}
	else {
		comment.value += link;
	}
}

// ---------------


/*
Author: mg12
Update: 2008/08/13
Author URI: http://www.neoease.com/
*/
(function() {

function reply(authorId, commentId, commentBox) {
	var author = document.getElementById(authorId).innerHTML;
	var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + ' </a> \n';
	
	appendReply(insertStr, commentBox);
}

function quote(authorId, commentId, commentBodyId, commentBox) {
	var author = document.getElementById(authorId).innerHTML;
	var comment = document.getElementById(commentBodyId).innerHTML;

	var insertStr = '<blockquote cite="#' + commentBodyId + '">';
	insertStr += '\n<strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :</strong>';
	insertStr += comment.replace(/\t/g, "");
	insertStr += '</blockquote>\n';

	insertQuote(insertStr, commentBox);
}

function appendReply(insertStr, commentBox) {
	if(document.getElementById(commentBox) && document.getElementById(commentBox).type == 'textarea') {
		field = document.getElementById(commentBox);

	} else {
		alert("The comment box does not exist!");
		return false;
	}

	if (field.value.indexOf(insertStr) > -1) {
		alert("You've already appended this reply!");
		return false;
	}

	if (field.value.replace(/\s|\t|\n/g, "") == '') {
		field.value = insertStr;
	} else {
		field.value = field.value.replace(/[\n]*$/g, "") + '\n\n' + insertStr;
	}
	field.focus();
}

function insertQuote(insertStr, commentBox) {
	if(document.getElementById(commentBox) && document.getElementById(commentBox).type == 'textarea') {
		field = document.getElementById(commentBox);

	} else {
		alert("The comment box does not exist!");
		return false;
	}

	if(document.selection) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = insertStr;
		field.focus();

	} else if (field.selectionStart || field.selectionStart == '0') {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var cursorPos = startPos;
		field.value = field.value.substring(0, startPos)
					  + insertStr
					  + field.value.substring(endPos, field.value.length);
		cursorPos += insertStr.length;
		field.focus();
		field.selectionStart = cursorPos;
		field.selectionEnd = cursorPos;

	} else {
		field.value += insertStr;
		field.focus();
	}
}

function loadCommentShortcut(frm, submitbnt, desc) {
	document.getElementById(frm).onkeydown = function (moz_ev) {
		var ev = null;
		ev = window.event ? window.event : moz_ev;
		if (ev != null && ev.ctrlKey && ev.keyCode == 13) {
			document.getElementById(submitbnt).click();
		}
	};
	document.getElementById(submitbnt).value += desc;
}

window['CMT'] = {};
window['CMT']['reply'] = reply;
window['CMT']['quote'] = quote;
window['CMT']['loadCommentShortcut'] = loadCommentShortcut;

})();
