﻿function PasteTag(t1,t2)
{
    var textBox = VE;
    if (true) {
	    if (document.selection) 
	    {
		    textBox.focus();

		    var txt = textBox.value;
		    var str = document.selection.createRange();
			

		    if (str.text == "") {
			    str.text = t1 + t2;
		    } else if (txt.indexOf(str.text)>=0) {
			    str.text = t1 + str.text + t2;
		    } else {
			    textBox.value = txt + t1 + t2;
		    }
	    }
    } else 
    {
	    textBox.value = textBox.value + t1 + t2;
    }
}

// добавление смайла
function sm(text)
{ 
    if (text!="") paste(':' + text + ':', '')
}

function WriteTag(text)
{ 
    if (text!="") paste(text, '')
}

function Insert(nickname, text)
{
    if (text!="")
	    paste("[quote "+nickname+"]"+text+"[/quote]\n", 0);
    VE.focus();
}

function paste(text, flag)
{
    if ((document.selection)&&(flag))
    {
	    VE.focus();
	    document.forms[0].document.selection.createRange().text = text;
    }
    else 
	    VE.value += text;
}

function catchSelection()
{
    if (window.getSelection)
    {
	    selection = window.getSelection().toString();
    }
    else if (document.getSelection)
    {
	    selection = document.getSelection();
    }
    else if (document.selection)
    {
	    selection = document.selection.createRange().text;
    }
}

function InsertNick(nickname)
{
    if(document.selection)
    {
	    VE.focus();
	    document.selection.createRange().text += '[b]' + nickname + '[/b], ';
    }
    else
    {
	    VE.focus();
	    VE.value+= '[b]' + nickname + '[/b], ';
    }
}  
