function bbcode(open, end){ 
        var tArea = document.getElementById("txt");
        var isIE = (document.all)? true : false; 
        var open = (open)? open : ""; 
        var end = (end)? end : ""; 
if(isIE){ 
        tArea.focus(); 
        var curSelect = document.selection.createRange(); 
if(arguments[2]){ 
        curSelect.text = open + arguments[2] + "]" + curSelect.text + end; 
}else{ 
        curSelect.text = open + curSelect.text + end; 
 } 
}else if(!isIE && typeof tArea.selectionStart != "undefined"){ 
        var selStart = tArea.value.substr(0, tArea.selectionStart); 
        var selEnd = tArea.value.substr(tArea.selectionEnd, tArea.value.length); 
        var curSelection = tArea.value.replace(selStart, '').replace(selEnd, '');
if(arguments[2]){ 
        tArea.value = selStart + open + arguments[2] + "]" + curSelection + end + selEnd;
}else{ 
        tArea.value = selStart + open + curSelection + end + selEnd; 
 } 
}else{ 
        tArea.value += (arguments[2])? open + arguments[2] + "]" + end : open + end; 
 }
	tArea.focus();
}

function bbCodeURL()
{

	var tourl=prompt("Please enter your URL","http://");
	var totext=prompt("Please enter text (leave empty for url)","");
	if(totext!="")
		bbcode('[url='+tourl+']'+totext+'[/url]','');
	else
		bbcode('[url='+tourl+']'+tourl+'[/url]','');
}

function bbCodeImage()
{

        var tourl=prompt("Please enter URL of the image","http://");
        bbcode('[img]'+tourl+'[/img]','');
}

function bbCodeMail()
{

        var tourl=prompt("Please enter mail","");
        bbcode('[mail]'+tourl+'[/mail]','');
}
