/*
Author: mg12
Update: 2008/09/23
Author URI: http://www.fighton.cn/
*/

function reply(authorId, commentId, commentBox, admin) {
	var author = document.getElementById(authorId).innerHTML;
	var comment = document.getElementById(commentId).innerHTML;
	var divClass = admin ? 'at-admin' : 'at-regular';

	var insertStr = '<a href="#' + commentId + '" class="' + divClass + '">@' + author.replace(/\t|\n/g, "") + '</a> \n';

	appendReply(insertStr, commentBox);
}

function quote(authorId, commentId, commentBodyId, commentBox, admin) {
	var author = document.getElementById(authorId).innerHTML;
	var comment = document.getElementById(commentBodyId).innerHTML;
	var divClass = admin ? 'adminquote' : 'regularquote';

	var insertStr = '<blockquote cite="#' + commentBodyId + '" class="' + divClass + '">';
	insertStr += '\n<strong><a href="#' + commentId + '">' + author.replace(/\t|\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') {
		myField = document.getElementById(commentBox);

	} else {
		return false;
	}

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

	if (myField.value == '') {
		myField.value += insertStr;
	} else {
		myField.value += '\n\n' + insertStr;
	}
	myField.focus();
}

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

	} else {
		return false;
	}

	if(document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = insertStr;
		myField.focus();

	} else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = startPos;
		myField.value = myField.value.substring(0, startPos)
					  + insertStr
					  + myField.value.substring(endPos, myField.value.length);
		cursorPos += insertStr.length;
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;

	} else {
		myField.value += insertStr;
		myField.focus();
	}
}
