var text_input = "Text";
var adv_mode = "UBB Code - Insert Immediately\n\nClick on the button will insert the UBB Code immediately";
var normal_mode = "UBB Code - Prompt before Insert\n\nClick on the button will display the propmt window which will guide you to insert UBB Code";
var email_normal = "请输入要添加邮箱链接的内容，留空表示直接使用邮箱地址";
var email_normal_input = "请输入要添加的邮箱地址";
var fontsize_normal = "请输入要添加大小效果的内容";
var bold_normal = "请输入要添加加粗效果的内容";
var italicize_normal = "请输入要添加斜体效果的内容";
var underline_normal = "请输入要添加下划线效果的内容";
var color_normal = "请输入要添加颜色效果的内容";
var link_normal = "请输入要添加链接效果的内容，留空表示直接使用链接地址";
var link_normal_input = "请输入要添加的链接地址";
var image_normal = "请输入要添加的图片地址";
var media_type = "请输入要添加的媒体类型，wmp=Windows Media Player，rm=Realone Player，swf=Flash";
var media_url = "请输入要添加的媒体地址";
var defmode = "normalmode";
if (defmode == "advmode") {
		normalmode = false;
		advmode = true;
} else {
		normalmode = true;
		advmode = false;
}
function setfocus() {
		document.form1.content.focus();
}
function chmode(swtch){
		if (swtch == 0) {
			normalmode = false;
			advmode = true;
			alert(adv_mode);
		} else if (swtch == 2) {
			advmode = false;
			normalmode = true;
			alert(normal_mode);
		}
}
function AddText(NewCode) {
	if(document.all){
		insertAtCaret(document.form1.content, NewCode);
		setfocus();
	} else{
		document.form1.content.value += NewCode;
		setfocus();
	}
}
function storeCaret(cursorPosition) {
	if (cursorPosition.createTextRange) cursorPosition.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text){
	if (textEl.createTextRange && textEl.caretPos){
		var caretPos = textEl.caretPos;
		caretPos.text += caretPos.text.charAt(caretPos.text.length - 2) == ' ' ? text + ' ' : text;
	} else if(textEl) {
		textEl.value += text;
	} else {
		textEl.value = text;
	}
}
function chsize(size) {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[size=" + size + "]" + range.text + "[/size]";
		} else if (advmode) {
			AddTxt="[size="+size+"] [/size]";
			AddText(AddTxt);
		} else {                       
			txt=prompt(fontsize_normal,text_input); 
			if (txt!=null) {             
			AddTxt="[size="+size+"]"+txt;
			AddText(AddTxt);
			AddText("[/size]");
			}        
		}
}
function bold() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[b]" + range.text + "[/b]";
		} else if (advmode) {
			AddTxt="[b] [/b]";
			AddText(AddTxt);
		} else {  
			txt=prompt(bold_normal,text_input);     
			if (txt!=null) {           
			AddTxt="[b]"+txt;
			AddText(AddTxt);
			AddText("[/b]");
			}       
		}
}
function italicize() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[i]" + range.text + "[/i]";
		} else if (advmode) {
			AddTxt="[i] [/i]";
			AddText(AddTxt);
		} else {   
			txt=prompt(italicize_normal,text_input);     
			if (txt!=null) {           
			AddTxt="[i]"+txt;
			AddText(AddTxt);
			AddText("[/i]");
			}               
		}
}
function underline() {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[u]" + range.text + "[/u]";
		} else if (advmode) {
			AddTxt="[u] [/u]";
			AddText(AddTxt);
		} else {  
			txt=prompt(underline_normal,text_input);
			if (txt!=null) {           
			AddTxt="[u]"+txt;
			AddText(AddTxt);
			AddText("[/u]");
			}               
		}
}
function chcolor(color) {
		if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[color=" + color + "]" + range.text + "[/color]";
		} else if (advmode) {
			AddTxt="[color="+color+"] [/color]";
			AddText(AddTxt);
		} else {  
		txt=prompt(color_normal,text_input);
			if(txt!=null) {
			AddTxt="[color="+color+"]"+txt;
			AddText(AddTxt);
			AddText("[/color]");
			}
		}
}
function hyperlink() {
		if (advmode) {
			AddTxt="[url] [/url]";
			AddText(AddTxt);
		} else { 
			txt2=prompt(link_normal,""); 
			if (txt2!=null) {
			txt=prompt(link_normal_input,"http://");      
			if (txt!=null) {
				if (txt2=="") {
						AddTxt="[url]"+txt;
						AddText(AddTxt);
						AddText("[/url]");
				} else {
						AddTxt="[url="+txt+"]"+txt2;
						AddText(AddTxt);
						AddText("[/url]");
				}         
			} 
			}
		}
}
function email() {
	if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[email]" + range.text + "[/email]";
	} else if (advmode) {
		AddTxt="[email] [/email]";
			AddText(AddTxt);
		} else { 
			txt2=prompt(email_normal,""); 
			if (txt2!=null) {
			txt=prompt(email_normal_input,"name@domain.com");      
			if (txt!=null) {
				if (txt2=="") {
						AddTxt="[email]"+txt+"[/email]";
			
				} else {
						AddTxt="[email="+txt+"]"+txt2+"[/email]";
				} 
				AddText(AddTxt);                
			}
			}
		}
}
function image() {
		if (advmode) {
			AddTxt="[img] [/img]";
			AddText(AddTxt);
		} else {  
			txt=prompt(image_normal,"http://");    
			if(txt!=null) {            
			AddTxt="\r[img]"+txt;
			AddText(AddTxt);
			AddText("[/img]");
			}       
		}
}
function media() {
	txt=prompt(media_type,"wmp");
	while ("wmp,rm,swf".indexOf(txt)<0||txt=="") {
		txt=prompt(media_type,"wmp");               
	}
	txt1=prompt(media_url,"http://");
	if(txt!=null&&txt1!=null) {  
			AddTxt="\r["+txt+"]"+txt1;
			AddText(AddTxt);
			AddText("[/"+txt+"]");
	}       
}
function insertIcon(iconCode) {
	var txtarea = document.form1.content;
	iconCode = '' + iconCode + '';
	if (txtarea.createTextRange && txtarea.caretPos) {
	var caretPos = txtarea.caretPos;
	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? iconCode + ' ' : iconCode;
	txtarea.focus();
	} else {
	txtarea.value  += iconCode;
	txtarea.focus();
	}
}