/******************************************************************************
  Crossday Discuz! Board - BB Code Insert
  Modified by: Crossday Studio (http://crossday.com), Weiming Bianzhou
  Based upon:  XMB CodeInsert (http://www.xmbforum.com), matt
*******************************************************************************/
var text_input = "在此输入文字";
var help_mode = "Discuz! 代码 - 帮助信息\n\n点击相应的代码按钮即可获得相应的说明和提示";
var adv_mode = "Discuz! 代码 - 直接插入\n\n点击代码按钮后不出现提示即直接插入相应代码";
var normal_mode = "Discuz! 代码 - 提示插入\n\n点击代码按钮后出现向导窗口帮助您完成代码插入";
var email_help = "插入邮件地址\n\n插入邮件地址连接。\n例如：\n[email]support@crossday.com[/email]\n[email=support@crossday.com]Dai Zhikang[/email]";
var email_normal = "请输入链接显示的文字，如果留空则直接显示邮件地址。";
var email_normal_input = "请输入邮件地址。";
var fontsize_help = "设置字号\n\n将标签所包围的文字设置成指定字号。\n例如：[size=3]文字大小为 3[/size]";
var fontsize_normal = "请输入要设置为指定字号的文字。";
var font_help = "设定字体\n\n将标签所包围的文字设置成指定字体。\n例如：[font=仿宋]字体为仿宋[/font]";
var font_normal = "请输入要设置成指定字体的文字。";
var bold_help = "插入粗体文本\n\n将标签所包围的文本变成粗体。\n例如：[b]Crossday 工作室[/b]";
var bold_normal = "请输入要设置成粗体的文字。";
var italicize_help = "插入斜体文本\n\n将标签所包围的文本变成斜体。\n例如：[i]Crossday 工作室[/i]";
var italicize_normal = "请输入要设置成斜体的文字。";
var quote_help = "插入引用\n\n将标签所包围的文本作为引用特殊显示。\n例如：[quote]Discuz! 版权所有 - Crossday Studio[/quote]";
var quote_normal = "请输入要作为引用显示的文字。";
var color_help = "定义文本颜色\n\n将标签所包围的文本变为制定颜色。\n例如：[color=red]红颜色[/color]";
var color_normal = "请输入要设置成指定颜色的文字。";
var center_help = "居中对齐\n\n将标签所包围的文本居中对齐显示。\n例如：[align=center]内容居中[/align]";
var center_normal = "请输入要居中对齐的文字。";
var link_help = "插入超级链接\n\n插入一个超级连接。\n例如：\n[url]http://www.crossday.com[/url]\n[url=http://www.crossday.com]Crossday 工作室[/url]";
var link_normal = "请输入链接显示的文字，如果留空则直接显示链接。";
var link_normal_input = "请输入 URL。";
var image_help = "插入图像\n\n在文本中插入一幅图像。\n例如：[img]http://www.crossday.com/cdb/images/logo.gif[/img]";
var image_normal = "请输入图像的 URL。";
var flash_help = "插入 flash\n\n在文本中插入 flash 动画。\n例如：[swf]http://www.crossday.com/cdb/images/banner.swf[/swf]";
var flash_normal = "请输入 flash 动画的 URL。";
var code_help = "插入代码\n\n插入程序或脚本原始代码。\n例如：[code]echo\"这里是我们的论坛\";[/code]";
var code_normal = "请输入要插入的代码。";
var list_help = "插入列表\n\n插入可由浏览器显示来的规则列表项。\n例如：\n[list]\n[*]；列表项 #1\n[*]；列表项 #2\n[*]；列表项 #3\n[/list]";
var list_normal = "请选择列表格式：字母式列表输入 \"A\"；数字式列表输入 \"1\"。此处也可留空。";
var list_normal_error = "错误：列表格式只能选择输入 \"A\" 或 \"1\"。";
var list_normal_input = "请输入列表项目内容，如果留空表示项目结束。";
var underline_help = "插入下划线\n\n给标签所包围的文本加上下划线。\n例如：[u]Crossday 工作室[/u]";
var underline_normal = "请输入要加下划线的文字。";

//document.onmouseover=showPopupText;
var postSubmited = false;

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);
        }
}

function AddText(NewCode) {
//	alert(document.input.message.value);
        if(document.all){
        	insertAtCaret(document.postform.pagetext, NewCode);
        	setfocus();
        } else{
        	document.postform.pagetext.value += NewCode;
        	setfocus();
        }
}

function storeCaret (textEl){
        if(textEl.createTextRange){
                textEl.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 email() {
        if (helpmode) {
                alert(email_help);
	} else 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 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]");
                }
        }
}

function chfont(font) {
        if (helpmode){
                alert(font_help);
	} else if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[font=" + font + "]" + range.text + "[/font]";
        } else if (advmode) {
                AddTxt="[font="+font+"] [/font]";
                AddText(AddTxt);
        } else {
                txt=prompt(font_normal,text_input);
                if (txt!=null) {
                        AddTxt="[font="+font+"]"+txt;
                        AddText(AddTxt);
                        AddText("[/font]");
                }
        }
}


function bold() {
        if (helpmode) {
                alert(bold_help);
	} else 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 (helpmode) {
                alert(italicize_help);
	} else 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 quote() {
        if (helpmode){
                alert(quote_help);
	} else if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[quote]" + range.text + "[/quote]";
        } else if (advmode) {
                AddTxt="\r[quote]\r[/quote]";
                AddText(AddTxt);
        } else {
                txt=prompt(quote_normal,text_input);
                if(txt!=null) {
                        AddTxt="\r[quote]\r"+txt;
                        AddText(AddTxt);
                        AddText("\r[/quote]");
                }
        }
}

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 center() {
        if (helpmode) {
                alert(center_help);
	} else if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[center]" + range.text + "[/center]";
        } else if (advmode) {
                AddTxt="[align=center] [/align]";
                AddText(AddTxt);
        } else {
                txt=prompt(center_normal,text_input);
                if (txt!=null) {
                        AddTxt="\r[align=center]"+txt;
                        AddText(AddTxt);
                        AddText("[/align]");
                }
        }
}

function hyperlink() {
        if (helpmode) {
                alert(link_help);
        } else 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 image() {
        if (helpmode){
                alert(image_help);
        } else 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 flash() {
        if (helpmode){
                alert(flash_help);
        } else if (advmode) {
                AddTxt="[swf] [/swf]";
                AddText(AddTxt);
        } else {
                txt=prompt(flash_normal,"http://");
                if(txt!=null) {
                        AddTxt="\r[swf]"+txt;
                        AddText(AddTxt);
                        AddText("[/swf]");
                }
        }
}

function code() {
        if (helpmode) {
                alert(code_help);
	} else if (document.selection && document.selection.type == "Text") {
		var range = document.selection.createRange();
		range.text = "[code]" + range.text + "[/code]";
        } else if (advmode) {
                AddTxt="\r[code]\r[/code]";
                AddText(AddTxt);
        } else {
                txt=prompt(code_normal,"");
                if (txt!=null) {
                        AddTxt="\r[code]"+txt;
                        AddText(AddTxt);
                        AddText("[/code]");
                }
        }
}

function list() {
        if (helpmode) {
                alert(list_help);
        } else if (advmode) {
                AddTxt="\r[list]\r[*]\r[*]\r[*]\r[/list]";
                AddText(AddTxt);
        } else {
                txt=prompt(list_normal,"");
                while ((txt!="") && (txt!="A") && (txt!="a") && (txt!="1") && (txt!=null)) {
                        txt=prompt(list_normal_error,"");
                }
                if (txt!=null) {
                        if (txt=="") {
                                AddTxt="\r[list]\r\n";
                        } else {
                                AddTxt="\r[list="+txt+"]\r";
                        }
                        txt="1";
                        while ((txt!="") && (txt!=null)) {
                                txt=prompt(list_normal_input,"");
                                if (txt!="") {
                                        AddTxt+="[*]"+txt+"\r";
                                }
                        }
                        AddTxt+="[/list]\r\n";
                        AddText(AddTxt);
                }
        }
}

function underline() {
        if (helpmode) {
                alert(underline_help);
	} else 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 setfocus() {
        document.postform.pagetext.focus();
}
function ctlent(obj) {
	if(postSubmited == false && (event.ctrlKey && window.event.keyCode == 13) || (event.altKey && window.event.keyCode == 83)) {
		if(this.document.input.pmsubmit) {
			postSubmited = true;
			this.document.input.pmsubmit.disabled = true;
			this.document.input.submit();
		} else if(validate(this.document.input)) {
			postSubmited = true;
			if(this.document.input.topicsubmit) this.document.input.topicsubmit.disabled = true;
			if(this.document.input.replysubmit) this.document.input.replysubmit.disabled = true;
			if(this.document.input.editsubmit) this.document.input.editsubmit.disabled = true;
			this.document.input.submit();
		}
	}
}
/////////////////////////////////////////////
function validatePost(Form, minLen, maxLen,chk_title){
	if(chk_title){
		str=(Form.title.value).replace(/(^ +)|( +$)/gi, '');
		if(str.length==0){
			alert("请填写标题。");
			return false;
		}
	}
	var mess =Form.pagetext.value;
	if (mess.length < minLen){
		alert("内容不能少于"+minLen+"字节。");
		return false;
	}
	if (mess.length > maxLen){
		alert("内容不能多于"+maxLen+"字节。");
		return false;
	}
	else{
		if(Form.authcode.value==""){
			alert("请填写验证码。");
			return false;
		}
	}
//alert("val");
	return true;
}
function setpoll(num){
	if(num!=0)	{
		str="";
		for(i=0;i<num;i++)
			str+="投票选项 "+(i+1)+": <input type=text size=54 maxlength=40 name=polloption"+i+"><br>";
	}
	else
		str="&nbsp;有效投票项为非空选项数";
	pollcell.innerHTML=str;
}
function cleanup(text){
//var is_regexp = (window.RegExp) ? true : false;
	try{
	text=text.replace(/&/gi,"&amp;");
	text=text.replace(/\"/gi,"&quot;");
	text=text.replace(/</gi,"&lt;");
	text=text.replace(/>/gi,"&gt;");
	text=text.replace(/  /gi,"&nbsp;&nbsp;");

	text=text.replace(/\[CODE\]((.|\n)*?)\[\/CODE\]/gi,"<fieldset align=center style=\"width: '90%';border:1px ridge #f0f0f0;\"><legend>代码:</legend><table width='100%' align=center border=0><tr><td>$1<br></td></tr></table></fieldset><br>");
	text=text.replace(/\[QUOTE\]\n*\[legend\]((.|\n)*?)\[\/legend\]((.|\n)*?)\[\/QUOTE\]/gi,"<fieldset align=center style=\"width: '90%';border:1px ridge #f0f0f0;\"><legend>$1</legend><table width='100%' align=center border=0><tr><td>$3<br></td></tr></table></fieldset><br>");
	text=text.replace(/\[QUOTE\]((.|\n)*?)\[\/QUOTE\]/gi,"<fieldset align=center style=\"width: '90%';border:1px ridge #f0f0f0;\"><legend>引用:</legend><table width='100%' align=center border=0><tr><td>$1<br></td></tr></table></fieldset><br>");

	text=text.replace(/\[b\]((.|\n)*?)\[\/b\]/gi,"<b>$1</b>");
	text=text.replace(/\[i\]((.|\n)*?)\[\/i\]/gi,"<i>$1</i>");
	text=text.replace(/\[u\]((.|\n)*?)\[\/u\]/gi,"<u>$1</u>");

	text=text.replace(/\[align=(.*?)\]((.|\n)*?)\[\/align\]/gi,"<div align=$1>$2</div>");

	text=text.replace(/\[img\]((.|\n)*?)\[\/img\]/gi,"<img src='$1'>");
	text=text.replace(/\[swf\]((.|\n)*?)\[\/swf\]/gi,"<OBJECT classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 width=778 height=100 align='absmiddle'><PARAM NAME='movie' VALUE='$1'><PARAM NAME='quality' VALUE='high'><embed src='$1' width='778' height='100' align='absmiddle' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed></OBJECT>");

	text=text.replace(/\[font=((.|\n)*?)\]((.|\n)*?)\[\/font\]/gi,"<font face=$1>$3</font>");
	text=text.replace(/\[color=((.|\n)*?)\]((.|\n)*?)\[\/color\]/gi,"<font color=$1>$3</font>");

	text=text.replace(/\[size=(\d*?)\]((.|\n)*?)\[\/size\]/gi,"<font size=$1>$2</font>");
	text=text.replace(/\[size=normal\]((.|\n)*?)\[\/size\]/gi, "<span style='font-size:14px'>$1</span>");
	text=text.replace(/\[size=small\]((.|\n)*?)\[\/size\]/gi, "<span style='font-size:9pt'>$1</span>");

	text=text.replace(/\[url=((.|\n)*?)\]((.|\n)*?)\[\/url\]/gi, "<a href='$1' target=_blank>$3</a>");
	text=text.replace(/\[url\]((.|\n)*?)\[\/url\]/gi,"<a href='$1' target=_blank>$1</a>");
	text=text.replace(/\[email=((.|\n)*?)\]((.|\n)*?)\[\/email\]/gi,"<a href='mailto:$1'>$3</a>");
	text=text.replace(/\[email\]((.|\n)*?)\[\/email\]/gi,"<a href='mailto:$1'>$1</a>");

	text=text.replace(/\[em(.*?)\]/gi,"<img src='icons/em$1.gif'>");

	text=text.replace(/\[list=1\]((.|\n)*?)\[\/list\]/gi,"<ol>$1</ol>");
	text=text.replace(/\[list=A\]((.|\n)*?)\[\/list\]/gi,"<ul>$1</ul>");
	text=text.replace(/\n?\[\*\]((.|n)*?)\n/gi,"<li>$1</li>");

	text=text.replace(/\n/gi,"<br>");
	text=text.replace(/\t/gi,"&nbsp; &nbsp; &nbsp; &nbsp; ");

	return text;
	}
	catch(e){
		alert(e.description);
	}
}
function preview(){
	form=document.postform;
	if(typeof(form.username)!="undefined"){
		if(form.username.value!="")
			namecell.innerHTML="&nbsp;"+form.username.value;
		else
			namecell.innerHTML="&nbsp;"+form.sessname.value;
	}
	else
		namecell.innerHTML="&nbsp;"+form.sessname.value;
	text=form.pagetext.value;
	text=cleanup(text)+"<br><br>";

	if(typeof(form.userfile)!="undefined"){
		filepath=form.userfile.value;
		if(filepath.lastIndexOf(".")!=-1){
			ext=filepath.substr(filepath.lastIndexOf(".")+1);
			if(form.imgtype.value.indexOf(ext)!=-1)
				text+="<img src='"+filepath+"' alt='"+form.attachdescription.value+"'>";
			else{
				if(ext=="mid"||ext=="midi")
					text+="<embed border=0 width=300 height=40 src='"+filepath+"' autostart=true>";
				else{
					if(ext=="swf"){
						text+="<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000><PARAM NAME=movie VALUE='"+filepath+"'><PARAM NAME=wmode VALUE=opaque><PARAM NAME=quality VALUE=high></OBJECT>";
//						text+="<PARAM NAME=movie VALUE='"+filepath+"'><PARAM NAME=wmode VALUE=opaque><PARAM NAME=quality VALUE=high></OBJECT>";
					}
				}
			}
			text+="<br>["+filepath+"]";
		}
	}
	textcell.innerHTML=text;
}
function prev(idname){
	id=reverse(idname);
	if(id.style.display=="block")
		preview();
}
function refresh(idname){
	id=document.getElementById(idname);
	imgid=document.getElementById("img_"+idname);
	id.style.display="block";
	imgid.src="icons/showblock.gif";
	preview();
}
