function AddText(NewCode) 
{
	if (document.getElementById("ggbox").createTextRange && document.getElementById("ggbox").caretPos) 
	{
		var caretPos = document.getElementById("ggbox").caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? NewCode + ' ' : NewCode;
	} 
	else 
	{
		document.getElementById("ggbox").value+=NewCode
	}
	setfocus();
}
function setfocus()
{
  document.getElementById("ggbox").focus();
}

//开始自定

function bold(){
    if(document.selection && document.selection.type == "Text"){
		var range = document.selection.createRange();
		range.text = "[b]" + range.text + "[/b]";
	}else{
		txt=prompt("文字将变粗体","");     
		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{  
		txt=prompt("文字将变斜体","");     
		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{  
		txt=prompt("文字将变下划线","");     
		if(txt!=null){           
			AddTxt="[u]"+txt;
			AddText(AddTxt);
			AddText("[/u]");
		}       
	}
}

function left(){
    if(document.selection && document.selection.type == "Text"){
		var range = document.selection.createRange();
		range.text = "[align=left]" + range.text + "[/align]";
	}else{  
		txt=prompt("文字左对齐","");     
		if(txt!=null){           
			AddTxt="[align=left]"+txt;
			AddText(AddTxt);
			AddText("[/align]");
		}       
	}
}

function center(){
    if(document.selection && document.selection.type == "Text"){
		var range = document.selection.createRange();
		range.text = "[align=center]" + range.text + "[/align]";
	}else{  
		txt=prompt("文字居中","");     
		if(txt!=null){           
			AddTxt="[align=center]"+txt;
			AddText(AddTxt);
			AddText("[/align]");
		}       
	}
}

function right(){
    if(document.selection && document.selection.type == "Text"){
		var range = document.selection.createRange();
		range.text = "[align=right]" + range.text + "[/align]";
	}else{  
		txt=prompt("文字右对齐","");     
		if(txt!=null){           
			AddTxt="[align=right]"+txt;
			AddText(AddTxt);
			AddText("[/align]");
		}       
	}
}

function url() {
 	if (document.selection && document.selection.type == "Text"){
		var range = document.selection.createRange();
		range.text = "[url]" + range.text + "[/url]";
	}else{
		txt2=prompt("链接显示名","");
		if (txt2!=null) {
			txt=prompt('URL:',"http://");
			if (txt2!=null) {
				if (txt2==""){
					AddTxt="[url]"+txt;
					AddText(AddTxt);
					AddTxt="[/url]";
					AddText(AddTxt);
				}else{
					if(txt==""){
						AddTxt="[url]"+txt2;
						AddText(AddTxt);
						AddTxt="[/url]";
						AddText(AddTxt);
					}else{
						AddTxt="[url="+txt+"]"+txt2;
						AddText(AddTxt);
						AddTxt="[/url]";
						AddText(AddTxt);
					}
				}
			}
		}
	}
}

function image(){
    if(document.selection && document.selection.type == "Text"){
		var range = document.selection.createRange();
		range.text = "[img]" + range.text + "[/img]";
	}else{  
		txt=prompt("图片地址","http://");     
		if(txt!=null){           
			AddTxt="[img]"+txt;
			AddText(AddTxt);
			AddText("[/img]");
		}       
	}
}

function media(type) {
 	if (document.selection && document.selection.type == "Text"){
		var range = document.selection.createRange();
		range.text = "[media=" + type + ",400,300]" + range.text + "[/media]";
	} else {
		txt2=prompt("请输入" + type + "的尺寸, 宽,高","480,360");
		if (txt2!=null) {
			txt=prompt('URL:',"http://");
			if (txt!=null) {
				if (txt2=="") {
					AddTxt="[media=" + type + ",480,360]" + txt;
					AddText(AddTxt);
					AddTxt="[/media]";
					AddText(AddTxt);
				} else {
					AddTxt="[media=" + type + "," + txt2 +"]" + txt;
					AddText(AddTxt);
					AddTxt="[/media]";
					AddText(AddTxt);
				}
			}
		}
	}
}

defmode = "normalmode";		// default mode (normalmode, advmode, helpmode)

if (defmode == "advmode") {
        helpmode = false;
        normalmode = false;
        advmode = true;
} else if (defmode == "helpmode") {
        helpmode = true;
        normalmode = false;
        advmode = false;
} else {
        helpmode = false;
        normalmode = true;
        advmode = false;
}

function chmode(swtch){
        if (swtch == 1){
                advmode = false;
                normalmode = false;
                helpmode = true;
                alert(help_mode);
        } else if (swtch == 0) {
                helpmode = false;
                normalmode = false;
                advmode = true;
                alert(adv_mode);
        } else if (swtch == 2) {
                helpmode = false;
                advmode = false;
                normalmode = true;
                alert(normal_mode);
        }
}

var text_input = "输入信息";
var fontsize_help = "设置字号\n\n将标签所包围的文字设置成指定字号。\n例如：[size=3]文字大小为 3[/size]";
var fontsize_normal = "请输入要设置为指定字号的文字。";
function chsize(size) {
        if (helpmode) {
                alert(fontsize_help);
	} else 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]");
                }        
        }
}

var color_help = "定义文本颜色\n\n将标签所包围的文本变为制定颜色。\n例如：[color=red]红颜色[/color]";
var color_normal = "请输入要设置成指定颜色的文字。";
function chcolor(color) {
        if (helpmode) {
                alert(color_help);
		} else 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 icon(pic){          
			AddTxt="[img="+pic;
			AddText(AddTxt);
			AddText("]");
}