function insertTagPrompt(mode){
	var obj = document.getElementById("body"), sel;
	obj.focus();
	if(mode == "url"){
		url = prompt("ใส่ URL ที่ต้องการ","http://");
		if(url != null && url != ""){
			txt = prompt("ใส่ชื่อ Link (ถ้าไม่ต้องการให้เว้นว่างไว้)","");
			if(txt != null && txt != ""){
				obj.value += "[url="+url+"]"+txt+"[/url] ";
			}else{
				obj.value +=  "[url]"+url+"[/url] ";
			}
		}
	}else	if(mode == "img"){
		img1 = prompt("ใส่ URL ของรูปที่ต้องการ\nตัวอย่าง : http://www.example.com/pic.jpg","http://");
		if(img1 != null && img1 != ""){
			img2 = prompt("ใส่ URL ของรูปขนาดเล็กเพื่อทำ Thumbnail (หากไม่ต้องการให้เว้นว่างไว้)\nตัวอย่าง : http://www.example.com/pic_small.jpg","");
			if(img2 != null && img2 != ""){
				obj.value += "[url="+img1+"][img]"+img2+"[/img][/url] ";
			}else{
				obj.value += "[img]"+img1+"[/img] ";
			}
		}
	}else if(mode == "flash"){
		flash = prompt("ใส่ URL ของไฟล์ .swf\nตัวอย่าง : http://www.example.com/flash.swf","http://");
		if(flash != null && flash != ""){
			obj.value += "[flash]"+flash+"[/flash] ";
		}
	}else if(mode == "youtube"){
		youtube = prompt("ใส่ URL ของ Youtube","");
		if(youtube != null && youtube != ""){
			obj.value +="[youtube]"+youtube+"[/youtube] ";
		}
	}else if(mode == "slide"){
		slide = prompt("ใส่เลข channel ของ slide.com","");
		if(slide != null && slide != ""){
			obj.value +="[slide]"+slide+"[/slide] ";
		}
	}
	obj.focus();
}

function insertImage(){

}


function insertTag(Tag, fTag){  
	ajtBBCode(Tag, fTag, "body");
}

function insertTxt(txt){
	ajtTexte(txt, "body");
}

function ajtTexte(txt,id){
	var obj = document.getElementById(id), sel;
	obj.focus();
	if(document.selection && document.selection.createRange){
		sel = document.selection.createRange();
		if(sel.parentElement()==obj)//si sel est dans obj
			sel.text = sel.text+txt;
	}else if(String(typeof obj.selectionStart)!="undefined"){
		sel = obj.selectionStart;
		obj.value = (obj.value).substring(0,sel) + txt + (obj.value).substring(sel,obj.value.length);
	}else{
		obj.value+=txt;
	}
	 obj.focus();
}

function ajtBBCode(Tag, fTag, id){
	var obj = document.getElementById(id), sel;
	obj.focus();
	if(document.selection && document.selection.createRange){//if ie
		sel = document.selection.createRange();
		if (sel.parentElement()==obj)//si sel est dans obj
			sel.text = Tag+sel.text+fTag;
	}else if(String(typeof obj.selectionStart)!="undefined"){ 
		var longueur= parseInt(obj.textLength);
		var selStart = obj.selectionStart;
		var selEnd = obj.selectionEnd;
		if (selEnd == 2 || selEnd == 1)selEnd = longueur;
		obj.value = (obj.value).substring(0,selStart) + Tag + (obj.value).substring(selStart,selEnd) + fTag + (obj.value).substring(selEnd,longueur);
	}else{
		obj.value+=Tag+fTag;
	}
	obj.focus();
}
