/*
Author: mg12
Update: 2008/02/23
Description: Quote comment.
*/

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

	var insertStr = '<blockquote cite="#' + commentId + '" class="' + divClass + '">';
	insertStr += '\n<strong>' + author.replace(/\t|\n/g, "") + ':</strong>';
	insertStr += comment.replace(/\t/g, "");
	insertStr += '</blockquote>\n';

	quoteToInsert(insertStr, commentBox);
}

function quoteToInsert(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();
	}
}