/***********************************************************************

	Copyright (C) 2008  PunBB

***********************************************************************/

function insert_text(open, close)
{

	msgfield = (document.all) ? document.all.message : ((document.getElementById('afocus') != null) ? (document.getElementById('afocus').message) : (document.getElementsByName('message')[0]));

	// IE support
	if (document.selection && document.selection.createRange)
	{
		msgfield.focus();
		sel = document.selection.createRange();
		sel.text = open + sel.text + close;
		msgfield.focus();
	}

	// Moz support
	else if (msgfield.selectionStart || msgfield.selectionStart == '0')
	{
		var startPos = msgfield.selectionStart;
		var endPos = msgfield.selectionEnd;

		msgfield.value = msgfield.value.substring(0, startPos) + open + msgfield.value.substring(startPos, endPos) + close + msgfield.value.substring(endPos, msgfield.value.length);
		msgfield.selectionStart = msgfield.selectionEnd = endPos + open.length + close.length;
		msgfield.focus();
	}

	// Fallback support for other browsers
	else
	{
		msgfield.value += open + close;
		msgfield.focus();
	}

	return;
}

function postQuote(postid,commentarea,alertmsg) {
	var posttext = '';
	if (window.getSelection){
		posttext = window.getSelection();
	}
	else if (document.getSelection){
		posttext = document.getSelection();
	}
	else if (document.selection){
		posttext = document.selection.createRange().text;
	}
	else {
		return true;
	}
	if (posttext==''){
		alert(alertmsg);
		return true;
	} else {
		var quote='[quote]'+posttext+'[/quote]\n';
		var comment=document.getElementById(commentarea);
		addQuote(comment,quote);
	}
	return false;
}

function addQuote(comment,quote){
	// Derived from Alex King's JS Quicktags code (http://www.alexking.org/)
	// Released under LGPL license
	// IE support
	if (document.selection) {
		comment.focus();
		sel = document.selection.createRange();
		sel.text = quote;
		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)
			              + quote
			              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + quote.length
		}
		else {
			comment.value = comment.value.substring(0, startPos)
				              + quote
				              + comment.value.substring(endPos, comment.value.length);
			cursorPos = startPos + quote.length;
		}
		comment.focus();
		comment.selectionStart = cursorPos;
		comment.selectionEnd = cursorPos;
		comment.scrollTop = scrollTop;
	}
	else {
		comment.value += quote;
	}
}

// str_replace("что заменяем", "чем заменяем", "исходная строка")
function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}

function commentQuote(commentid,username,commentarea) {
	var id = 'comment-'+commentid;
	var commenttext = document.getElementById(id).innerHTML;
	commenttext = str_replace('<BR>','\n',commenttext);
	commenttext = str_replace('<br>','\n',commenttext);
	commenttext = str_replace('<br/>','\n',commenttext); commenttext = str_replace('<br />','\n',commenttext);
	commenttext = str_replace('<BR/>','\n',commenttext); commenttext = str_replace('<BR />','\n',commenttext);
	var quote = '[quote='+username+']'+commenttext+'[/quote]\n';
	var comment = document.getElementById(commentarea);
	addQuote(comment,quote);
	return false;
}

