var counterEmotion = 0;
function emoticon(text) {
	if(counterEmotion++ > 20) return;
	var txtarea = document.getElementById('reply_text');
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

function ins(name){
  var input=document.getElementById('reply_text');
  input.value=input.value+"[b]"+name+"[/b]"+" \n";
}

function Insert(text){
  if (text!="") paste("[q]"+text+"[/q]\n", 0);
}

function paste(text, flag){
  if ((document.selection)&&(flag)) {
	  document.getElementById('reply_text').focus();
	  document.selection.createRange().text = text;
  }
  else document.getElementById('reply_text').value += text;
}

function get_selection() {
  if (document.getSelection){
	selection = document.getSelection();
	selection = selection.replace(/\r\n\r\n/gi, "_doublecaret_");
	selection = selection.replace(/\r\n/gi, " ");
  while (selection.indexOf("  ") !=-1) selection = selection.replace(/  /gi, "");
	selection = selection.replace(/_doublecaret_/gi, "\r\n\r\n");
  }
  else selection = document.selection.createRange().text;
}

function tag_url()
{
    var FoundErrors = 0;
    var enterURL   = prompt("Ââåäèòå ññûëêó", "http://");
    var enterTITLE = prompt("Ââåäèòå çàãîëîâîê", "");

    if (!enterURL) {
        FoundErrors += 1;
    }
    if (!enterTITLE) {
        FoundErrors += 1;
    }

    if (FoundErrors > 0) {
        alert("Îøèáêà");
        return;
    }

	Insert_link(enterURL, enterTITLE);
}

function Insert_link(url,title){
  if ((url!="")&&(title!="")) paste("[URL="+url+"]"+title+"[/URL]\n", 0);
}
