function storeCaret(text) { 
	if (text.createTextRange) {
		text.caretPos = document.selection.createRange().duplicate();
	}
}
function replaceT(text){
    if (document.post.body.createTextRange && document.post.body.caretPos) {
        var caretPos = document.post.body.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
        text + ' ' : text;
    }
    else document.post.body.value += text;
    document.post.body.focus(caretPos)
}

function quote_paste(id){
	var quote=document.getElementById("post_"+id).innerHTML;
	var body=document.getElementById("body2").value;
	quote=quote.replace(/<[^>]*>/g,"");
	document.getElementById("body2").value="[quote]"+quote+"[/quote]"+body;
}

function addURL(){
	var p=prompt('Введите ссылку','http://');	
	if(p!='http://' && p!=null){
		var a=prompt('Введите текст ссылки','');
		var body=document.getElementById("body2").value;
		if(a==false){
			a=p;
		}
		document.getElementById("body2").value=body+"[url="+p+"]"+a+"[/url]"
	}
}
function addMail(){
	var p=prompt('Введите e-mail адрес','');
	var body=document.getElementById("body2").value;
	if(p!=false && p!=null){
		document.getElementById("body2").value=body+"[email="+p+"]"+p+"[/email]"
	}
}

function addImage(){
	var p=prompt('Введите URL изображения','http://');
	var body=document.getElementById("body2").value;
	if(p!='http://' && p!=null){
		document.getElementById("body2").value=body+"[image]"+p+"[/image]"
	}
}

function surroundText(open, close)
{
    msgfield = (document.all) ? document.all.body2 : document.forms['post']?document.forms['post']['body2']:document.forms['post']['body2'];
    var bSelStart = msgfield.selectionStart, text;
    var st = msgfield. scrollTop, sh = msgfield. scrollHeight;

    // IE support
    if (document.selection && document.selection.createRange && !bSelStart && msgfield.caretPos){
        text = open;
        if (close != "") text += document.selection.createRange().text;
        text += close;
        msgfield. caretPos. text = text;
    }else if (bSelStart || msgfield.selectionStart == '0') {
        var startPos = msgfield.selectionStart;
        var endPos = msgfield.selectionEnd;
        text = msgfield.value.substring(0, startPos) + open;
        if (close != "") text += msgfield.value.substring(startPos, endPos);
        text += close + msgfield.value.substring(endPos, msgfield.value.length);
        msgfield.value = text;
		endPos = close. length? endPos: startPos;
		msgfield.selectionStart = endPos + open.length + close.length;
		msgfield.selectionEnd = endPos + open.length + close.length;
    }else{
        msgfield.value += open + close;
    }
    msgfield. scrollTop = st + msgfield. scrollHeight - sh;
    return;
}