brkL="[";brkR="]";selted='';
d=document;
function insPic(s1,s2,s3)
{
if(!d.all&&selted==''&&s3==3)
  {s1=s2;s2=brkL+'/'+s2+brkR;s3='';}
if(s3==2||s3==3){s1+=s2;s2='';}
s1=brkL+s1+(s2==brkR?'=':brkR);
var isPic=s2==null;
var s=insField.value;var insPosL,insPosR;

if(insField.selectionEnd==null){var ch=0;
    if(d.selection&&d.selection.createRange){
      var tR=d.selection.createRange();var ch='character'
      var tR1=d.body.createTextRange();}
  if(!ch||tR.parentElement&&tR.parentElement()!=insField)
    {insPosL=insPosR=s.length;}
  else{
    insPosL=tR.text.length;
    if(insField.type=='textarea'){
      tR1.moveToElementText(insField);
      tR.setEndPoint('StartToStart',tR1);
      insPosR=tR.text.length;
    }else{tR.moveStart('textedit',-1);
    insPosR=tR.text.length;}
    insPosL=insPosR-insPosL;
}}else{
  insPosL=insField.selectionStart;
  insPosR=insField.selectionEnd;
  if(insBeg&&self.opera&&!insPosL&&!insPosR)
    {insPosL=insPosR=s.length;insBeg=0;}

}var insText=s.substring(insPosL,insPosR);
if((isInSel=selted==insText)&&s3==3)
  {isInSel=insText.length;insText='';}
if(d.all)insField.defaultValue=s;else defa=s;
if(isPic&&!(s3==2&&insText!='')){s2=s1;s1='';} //picture
insField.value=s.substring(0,insPosL)+s1+insText+s2
  +s.substring(insPosR,s.length);
if(isInSel&&s3==3)insPosR-=isInSel;
var insCursor=insPosR+s1.length
  +(isPic||insPosL!=insPosR?s2.length:0);

if(d.body.createTextRange){setTimeout(
"var t=insField.createTextRange();t.collapse();t.moveEnd('"
  +ch+"',"+insCursor+");t.moveStart('"+ch+"',"+insCursor//L--------------
  +");t.select();",1);
}else{  if(d.all)insField.focus();
  if(insField.selectionEnd!=null){
    insField.selectionStart=insCursorL;
    insField.selectionEnd=insCursor
      +(d.all?1:0);
    setTimeout("insField.focus();",111);
  if(d.all){var tR=d.selection.createRange();
    if(insCursorL==insCursor)tR.collapse();
    else tR.moveEnd('character',-1);tR.select();}
}}}

function insTag(s,c)
{

insPic(s,(c?c:'')+brkL+'/'+s+brkR
  +(c?', ':''),c?2:null);
}
function insListTag(s,c)
{

document.getElementById('message').focus();
//window.alert(s);
if (s!='')
insPic(s,(c?c:'')+brkL+'/'+s+brkR +(c?', ':''),c?2:null);
}



function insTagArg(s){insPic(s,brkR);}  //'b',']'
function insBack(){with(insField){var s=d.all?value:defa;
  value=d.all?defaultValue:defa;
  if(d.all)defaultValue=s;else defa=s;}}
d.onmouseup=function(){
  if(self.opera)str=d.getSelection();}
function insCapt(s){insPic(s+brkR
  +(selted=(d.getSelection?(self.str?str
    :(d.all?(d.getSelection()
    ?d.getSelection():d.selection.createRange().text)
    :getSelection()))
    :(d.selection?d.selection.createRange().text:'')))
  +brkL+'/',s,3);}

onload=function()
{insField=d.getElementById('message');insBeg=1}


function winop()
{ windop = window.open("inc/smileform","","resizable=0, status=0, scrollbars=1, height=300,width=400,left=400,top=100");}

function winimage()
{ windop = window.open("inc/imageform","","resizable=0, status=0, scrollbars=1, height=400,width=300,left=10,top=10");}

function popimage()
{ windop = window.open("inc/popup_imageform","","resizable=0, status=0, scrollbars=1, height=400,width=300,left=250,top=10");}


function tag_url()
{   var FoundErrors = ''
    var enterURL   = prompt('Введите ссылку', "http://")
    var enterTITLE = prompt('Введите название ссылки', "Ссылка")
    if (!enterURL) {
        FoundErrors += " " + error_no_url
    }
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title
    }
    if (FoundErrors) {
        alert("Error!"+FoundErrors)
        return
    }
document.getElementById('message').focus();
document.getElementById('message').value+= '[URL='+enterURL+']'+enterTITLE+'[/URL]';
}


function mail_url()
{   var FoundErrors = ''
    var enterTITLE = prompt('Введите Е-Майл', "mail@mail.ru")
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title
    }
    if (FoundErrors) {
        alert("Error!"+FoundErrors)
        return
    }
document.getElementById('message').focus();
document.getElementById('message').value+= '[MAIL=mailto:'+enterTITLE+']'+enterTITLE+'[/MAIL]';
}




