var sCurrMode
StyleEditorHeader = "<style>BLOCKQUOTE {  BORDER-LEFT: #cccccc 1px dotted;  BORDER-BOTTOM: #cccccc 1px dotted;BORDER-RIGHT: #cccccc 1px dotted; BORDER-TOP: #cccccc 1px dotted; PADDING-LEFT: 4px;  PADDING-RIGHT: 4px; PADDING-BOTTOM: 4px;PADDING-TOP: 4px;}</style><script language='JavaScript'>document.onkeypress = function(e) {e = e ? e : event;return ctlent(e);};i=0;function ctlent(eventobject){if(event.ctrlKey && (event.keyCode==13 || event.keyCode==10) && i==0){i=1;parent.document.form.EditSubmit.click();}}<\/script><body onkeypress=ctlent()>" ;
// 根据浏览器类型选择编辑器
if (navigator.appName == "Microsoft Internet Explorer"){IsHtmlEditor=1}else{IsHtmlEditor=0}
if (IsHtmlEditor==1){
// IE浏览器
	YBBFont="<option value='宋体'>宋体</option> <option value='黑体'>黑体</option> <option value='隶书'>隶书</option> <option value='幼圆'>幼圆</option> <option value='新宋体'>新宋体</option><option value='Arial'>Arial</option><option value='Courier New'>Courier New</option><option value='Garamond'>Garamond</option><option value='Georgia'>Georgia</option><option value='Tahoma'>Tahoma</option><option value='Times New Roman'>Times</option><option value='Verdana'>Verdana</option>"
	document.write("<style>TD.StatusBarBtnOff {padding:1px 5px;border:1px outset;cursor:pointer;}TD.StatusBarBtnOn {padding:1px 5px;border:1px inset;background-color: #EEEEEE;}</style>");
	document.write("<table width=100% height=100% cellpadding=2 cellspacing=0><tr id=EditButton style=DISPLAY:block><td align=left colspan=6>");
	document.write("<select onChange=\"FormatText('FormatBlock',this[this.selectedIndex].value);this.selectedIndex=0;\"><option>段落</option><option value=\"&lt;P&gt;\">正文</option><option value=\"&lt;H1&gt;\">标题一</option><option value=\"&lt;H2&gt;\">标题二</option><option value=\"&lt;H3&gt;\">标题三</option><option value=\"&lt;H4&gt;\">标题四</option><option value=\"&lt;H5&gt;\">标题五</option><option value=\"&lt;H6&gt;\">标题六</option><option value=\"&lt;PRE&gt;\">预设格式</option></select> ");
	document.write("<select onChange=\"FormatText('fontname',this[this.selectedIndex].value);this.selectedIndex=0;\"><option>字体</option>"+YBBFont+"</select> ");
	document.write("<select onChange=\"FormatText('fontsize',this[this.selectedIndex].value);this.selectedIndex=0;\"><option>大小</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option><option value=6>6</option><option value=7>7</option></select> ");
	document.write("<img src=Editor/Button/fgcolor.gif title=\"字体颜色\" align=absmiddle style=cursor:hand onClick=foreColor()>");
	document.write("<img src=Editor/Button/fbcolor.gif title=\"突出显示\" align=absmiddle style=cursor:hand onClick=BackColor()>");
	document.write("<img src=Editor/Button/InsertImage.gif title=\"插入图片\" align=absmiddle style=cursor:hand onClick=InsertImage()>");
	document.write("<img title=\"插入 FLASH、MediaPlayer、RealPlayer 文件\" src=Editor/Button/mp.gif align=absmiddle style=cursor:hand onclick=MediaPlayer()>");
	document.write("<img src=Editor/Button/replace.gif title=\"替换\" align=absmiddle style=cursor:hand onClick=replace()>");
	document.write("<img title=\"插入表情\" src=Editor/Button/em.gif align=absmiddle style=cursor:hand onclick=em()>");
	document.write("<img src=Editor/Button/specialchar.gif title=\"插入特殊字符\" align=absmiddle style=cursor:hand onClick=specialchar()>");
	document.write('<img src="Editor/Button/insertdate.gif" title=\"插入今天的日期\" align=absmiddle border="0" style=cursor:hand onClick=insertdate()>');
	document.write('<img src="Editor/Button/inserttime.gif" title=\"插入当前的时间\" align=absmiddle border="0" style=cursor:hand onClick=inserttime()>');
	document.write('<img src="Editor/Button/Break.gif" title=\"换行符\" align=absmiddle border="0" style=cursor:hand onClick=Break()>');

	var FormatTextlist="插入输入框 InsertInputText|插入文字输入区 InsertTextArea|插入单选钮 InsertInputRadio|插入复选框 InsertInputCheckbox|插入下拉框 InsertSelectDropdown|插入按钮 InsertButton|<br>|粗体 bold|倾斜 italic|下划线 underline|上标 superscript|下标 subscript|删除线 strikethrough|删除文字格式 RemoveFormat|左对齐 Justifyleft|居中 JustifyCenter|右对齐 JustifyRight|两端对齐 justifyfull|编号 insertorderedlist|项目符号 InsertUnorderedList|减少缩进量 Outdent|增加缩进量 indent|普通水平线 InsertHorizontalRule|段落 InsertParagraph|全选 selectAll|取消选择 unselect|删除当前选中区 Delete|剪切 cut|复制 copy|粘贴 paste|撤消 undo|恢复 redo|插入超链接 createLink|去掉超链接 Unlink|打印 Print"
	var list= FormatTextlist.split ('|'); 
	for(i=0;i<list.length;i++) {
	if (list[i]=="<br>"){document.write("<br>");
	}else{
	var TextName= list[i].split (' '); 
	document.write("<img align=absmiddle width=20 height=20 src=Editor/Button/"+TextName[1]+".gif title="+TextName[0]+" style=cursor:hand onClick=FormatText('"+TextName[1]+"')>");
	}
	}
	document.write("<img title=\"清洁代码\" src=Editor/Button/Cleanup.gif align=absmiddle style=cursor:hand onclick=CleanCode()>");


	document.write("</td></tr><tr><td height=100% colspan=6><iframe ID=HtmlEditor MARGINHEIGHT=5 MARGINWIDTH=5 width=100% height=100%></iframe></td></tr>");
	document.write("<tr><td></div><table border=0 cellpadding=0 cellspacing=0 height=20>");
	document.write("<tr>");
	document.write("<td width=5>&nbsp;</td>");
	document.write("<td class=StatusBarBtnOn id=HtmlEditor_EDIT onclick=setMode('EDIT')><img border=0 src=Editor/Button/modeedit.gif align=absmiddle></td>");
	document.write("<td width=5>&nbsp;</td>");
	document.write("<td class=StatusBarBtnOff id=HtmlEditor_CODE onclick=setMode('CODE')><img border=0 src=Editor/Button/modecode.gif align=absmiddle></td>");
	document.write("<td width=5>&nbsp;</td>");
	document.write("<td class=StatusBarBtnOff id=HtmlEditor_VIEW onclick=setMode('VIEW')><img border=0 src=Editor/Button/modepreview.gif width=50 height=15 align=absmiddle></td>");
	document.write("<td width=5>&nbsp;</td>");
	document.write("<td align=right width=100%><img border=0 src=Editor/Button/+.gif align=absmiddle onclick=sizeChange(100) style=cursor:pointer title='增大编辑区'><img border=0 src=Editor/Button/-.gif align=absmiddle onclick=sizeChange(-100) style=cursor:pointer title='减小编辑区'></td>");
	document.write("</tr></table>");
	document.write("</td></tr></table>");

	var IframeID=document.getElementById("HtmlEditor").contentWindow;
	if (navigator.appVersion.indexOf("MSIE 6.0",0)==-1){IframeID.document.designMode="On"} //可编辑与StyleEditorHeader生效
	IframeID.document.open();
	IframeID.document.write(StyleEditorHeader+document.form.Body.value+"</body></html>");
	IframeID.document.close();
	IframeID.document.body.contentEditable = "True";
	IframeID.document.body.style.fontSize="10pt";

}else{
	// 其他浏览器
	document.write ('<textarea id=TextBody style="width:100%;height:100%"></textarea>')
	html = document.form.Body.value;
	html = html.replace(/<br>/gi,"\n");
	document.form.TextBody.value=html; 
}
// 取编辑器的内容
function getHTML() {
	var html;
	if (IsHtmlEditor==0){
		html = document.form.TextBody.value;
		html = html.replace(/\n/gi,"<br>");
	}else
	if(sCurrMode=="CODE"){
		html = IframeID.document.body.innerText;
	}else{
		html = IframeID.document.body.innerHTML;
	}
	return html;
}

function CheckForm(form){
	if(sCurrMode=="CODE"){alert("编辑器处于代码状态下不能发送内容！");return false;}

	document.form.Body.value = getHTML();

	Subject=document.form.Subject
	Body=document.form.Body
	VerifyCode=document.form.VerifyCode

 	if(typeof(Subject) != "undefined" ){
		if(Subject.value.length<2){alert("标题不能小于2个字符！");return false;}
  	}

 	if(typeof(Body) != "undefined" ){
		if(Body.value.length<2){alert("内容不能小于2个字符！");return false;}
		if(Body.value.length>60000){alert("内容不能超过60000个字符！");return false;}
  	}

 	if(typeof(VerifyCode) != "undefined" ){
		if(VerifyCode.value.length<4){alert("请输入验证码！");return false;}
  	}

	document.form.EditSubmit.click()
}


// 预览
function Preview()
{
	document.form.Body.value = getHTML();

	Subject=document.form.Subject
	Body=document.form.Body

 	if(typeof(Subject) != "undefined" ){
		if(Subject.value.length<2){alert("标题不能小于2个字符！");return false;}
  	}

 	if(typeof(Body) != "undefined" ){
		if(Body.value.length<2){alert("内容不能小于2个字符！");return false;}
		if(Body.value.length>60000){alert("内容不能超过60000个字符！");return false;}
  	}

	document.getElementById('Preview').style.display = '';
	Ajax_CallBack('form','Preview','Loading.asp?menu=Preview');
	window.location="#Preview";
}

function CheckLength(){
	document.form.Body.value = getHTML();
	MessageLength=document.form.Body.value.length;
	alert("最大字符为 "+60000+ " 字节\n您的内容已有 "+MessageLength+" 字节");
}


// 调整编辑器的大小
function sizeChange(size){
	var obj=document.getElementById("HtmlEditor");
	obj.height = (parseInt(obj.offsetHeight) + size);
	if (parseInt(obj.offsetHeight)< 200 ){obj.height ="100%"}
}

// 设置编辑器的内容
function setHTML(html) {
	IframeID.document.body.innerHTML = html;
	switch (sCurrMode){
	case "EDIT":
		HtmlEditor.document.open();
		HtmlEditor.document.write(StyleEditorHeader+html);
		HtmlEditor.document.close();
		HtmlEditor.document.body.contentEditable="true";
		EditButton.style.display = '';
                break;
	case "CODE":
		HtmlEditor.document.open();
		HtmlEditor.document.write(html);
		HtmlEditor.document.close();
		HtmlEditor.document.body.innerText=html;
		HtmlEditor.document.body.contentEditable="true";
		EditButton.style.display = 'none';
                break;
	case "VIEW":
		HtmlEditor.document.open();
		HtmlEditor.document.write(StyleEditorHeader+html);
		HtmlEditor.document.close();
		HtmlEditor.document.body.contentEditable="false";
		EditButton.style.display = 'none';
		break;
	}
}

// 改变模式：代码、编辑、预览
function setMode(NewMode){
	if (NewMode!=sCurrMode){
		switch(sCurrMode){
		case "EDIT":
			sBody = HtmlEditor.document.body.innerHTML;
			break;
		case "CODE":
			sBody = HtmlEditor.document.body.innerText;
			break;
		case "VIEW":
			sBody = HtmlEditor.document.body.innerHTML;
			break;
		default:
			sBody = IframeID.document.body.innerHTML;
			break;
		}
		// 换图片
		try{
			document.all["HtmlEditor_CODE"].className = "StatusBarBtnOff";
			document.all["HtmlEditor_EDIT"].className = "StatusBarBtnOff";
			document.all["HtmlEditor_VIEW"].className = "StatusBarBtnOff";
			document.all["HtmlEditor_"+NewMode].className = "StatusBarBtnOn";
			}
		catch(e){
			}
		sCurrMode = NewMode;
		setHTML(sBody);
		
	}
}


function FormatText(command,option){IframeID.focus();IframeID.document.execCommand(command,true,option);}


function CleanCode(){
	var body = IframeID.document.body;
	var html = IframeID.document.body.innerHTML;
	html = html.replace(/\<p>/gi,"[$p]");
	html = html.replace(/\<\/p>/gi,"[$\/p]");
	html = html.replace(/\<br>/gi,"[$br]");
	html = html.replace(/\<[^>]*>/g,"");
	html = html.replace(/\[\$p\]/gi,"<p>");
	html = html.replace(/\[\$\/p\]/gi,"<\/p>");
	html = html.replace(/\[\$br\]/gi,"<br>");
	IframeID.document.body.innerHTML = html;
}



function Break(){
	IframeID.focus();
	sel=IframeID.document.selection.createRange();
	sel.pasteHTML("<br>");
}
function insertdate(){
	IframeID.focus();
	sel=IframeID.document.selection.createRange();
	sel.pasteHTML(new Date().toLocaleDateString());
}

function inserttime(){
	IframeID.focus();
	sel=IframeID.document.selection.createRange();
	sel.pasteHTML(new Date().toLocaleTimeString());
}


//////替换内容
function replace()
{
  var arr = showModalDialog("Editor/Replace.htm", "", "dialogWidth:22em;dialogHeight:10em;status:0;help:0");
	if (arr != null){
		var ss;
		ss = arr.split("*")
		a = ss[0];
		b = ss[1];
		i = ss[2];
		con = IframeID.document.body.innerHTML;
		if (i == 1)
		{
			con = bbsxp_rCode(con,a,b,true);
		}else{
			con = bbsxp_rCode(con,a,b);
		}
		IframeID.document.body.innerHTML = con;
	}
	else IframeID.focus();
}
function bbsxp_rCode(s,a,b,i){
	a = a.replace("?","\\?");
	if (i==null)
	{
		var r = new RegExp(a,"gi");
	}else if (i) {
		var r = new RegExp(a,"g");
	}
	else{
		var r = new RegExp(a,"gi");
	}
	return s.replace(r,b); 
}
//////替换内容结束


function em(){
	var arr = showModalDialog("Editor/Emotion.htm", "", "dialogWidth:20em; dialogHeight:10em; status:0;help:0");
	if (arr != null){
		IframeID.focus()
		sel=IframeID.document.selection.createRange();
		sel.pasteHTML(arr);
	}
}

function InsertImage(){
	var url = showModalDialog("Editor/Image.htm", "", "dialogWidth:22em; dialogHeight:9em; status:0;help:0");
	if (url != null){
		IframeID.focus()
		sel=IframeID.document.selection.createRange();
		sel.pasteHTML(url);
	}
}

function MediaPlayer(){
	var arr = showModalDialog("Editor/MediaPlayer.htm", "", "dialogWidth:21em; dialogHeight:11em; status:0;help:0");
	if (arr != null){
		IframeID.focus()
		sel=IframeID.document.selection.createRange();
		sel.pasteHTML(arr);
	}
}

function specialchar(){
	var arr = showModalDialog("Editor/specialchar.htm", "", "dialogWidth:28em; dialogHeight:16em; status:0;help:0");
	IframeID.focus();
	sel=IframeID.document.selection.createRange();
	if (arr != null)sel.pasteHTML(arr);
}

function BackColor(){
  var arr = showModalDialog("Editor/selcolor.htm", "", "dialogWidth:18em; dialogHeight:18em; status:0;help:0");
  if (arr != null) FormatText('BackColor', arr);
  else IframeID.focus();
}

function foreColor(){
  var arr = showModalDialog("Editor/selcolor.htm", "", "dialogWidth:18em; dialogHeight:18em; status:0;help:0");
  if (arr != null) FormatText('forecolor', arr);
  else IframeID.focus();
}
function ComposeKeyPress(e) {
	if ((e.keyCode == 10 || e.keyCode == 13) && e.ctrlKey) {
		CheckForm("form");
	}
	return true;
}
document.onkeypress = function(e) {e = e ? e : event;return ComposeKeyPress(e);};