var IframeID;
var IsCode
IsCode="No"
function HtmlEdit(){
document.writeln("<div><select name=\"selectFont1\" onchange=\"Insert(this[this.selectedIndex].value);this.selectedIndex=0;\"><option selected>特殊贴↓</option><option value=\"[COIN=1000]内容[/COIN]\">金钱可见</option><option value=\"[GRADE=1]内容[/GRADE]\">等级可见</option><option value=\"[REPLY]内容[/REPLY]\">回复可见</option><option value=\"[USERNAME=昵称]内容[/USERNAME]\">指定读者</option><option value=\"[MARK=3]内容[/MARK]\">Y 币可见</option><option value=\"[BUYPOST=100]内容[/BUYPOST]\">付费可见</option><option value=\"[DATE=2006-10-1]内容[/DATE]\">日期可见</option><option value=\"[SEX=1]内容[/SEX]\">性别可见</option><option value=\"[LOGIN]内容[/LOGIN]\">登陆可见</option></select>");
document.writeln("<select name=\"selectFont\" onChange=\"FormatText('fontname',this[this.selectedIndex].value);\"><option selected>字体<option value=\"removeFormat\">默认字体<option value=\"宋体\">宋体<option value=\"黑体\">黑体<option value=\"隶书\">隶书<option value=\"幼圆\">幼圆<option value=\"楷体_GB2312\">楷体<option value=\"仿宋_GB2312\">仿宋<option value=\"新宋体\">新宋体<option value=\"华文彩云\">华文彩云<option value=\"华文仿宋\">华文仿宋<option value=\"华文新魏\">华文新魏<option value=\"Arial\">Arial<option value=\"Arial Black\">Arial Black<option value=\"Arial Narrow\">Arial Narrow<option value=\"Century\">Century<option value=\"Courier New\">Courier New<option value=\"Georgia\">Georgia<option value=\"Impact\">Impact<option value=\"Lucida Console\">Lucida Console<option value=\"MS Sans Serif\">MS Sans Serif<option value=\"System\">System<option value=\"Symbol\">Symbol<option value=\"Tahoma\">Tahoma<option value=\"Times New Roman\">Times New Roman&nbsp; &nbsp;<option value=\"Verdana\">Verdana<option value=\"Webdings\">Webdings<option value=\"Wingdings\">Wingdings</option></select> <select onChange=\"FormatText('fontsize',this[this.selectedIndex].value);\" name=\"D2\"><option class=\"heading\" selected>字号<option value=1>一号<option value=2>二号<option value=3>三号<option value=4>四号<option value=5>五号<option value=6>六号<option value=7>七号</option></select>");

document.writeln("<img id=\"backcolor\" src=images\/edit\/fbcolor.gif alt=\"突出颜色\" title=\"突出颜色\" align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\" onClick=\"color(\'backcolor\')\">");
document.writeln("<img id=\"forecolor\"  src=images\/edit\/fgcolor.gif alt=\"字体颜色\" title=\"字体颜色\" align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\"  onClick=\"color(\'forecolor\')\">");
document.writeln("<img align=absmiddle src=images\/edit\/separator.gif>");
document.writeln("<img alt=\"插入FLASH文件\" src=images\/edit\/swf.gif align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\" onclick=flash()>");
document.writeln("<img alt=\"插入Media Player文件\" src=images\/edit\/mp.gif align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\" onclick=MediaPlayer()>");
document.writeln("<img alt=\"插入RealPlayer文件\" src=images\/edit\/rm.gif align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\" onclick=RealPlay()><br>");

document.writeln("<img src=images\/edit\/code.gif alt=\"代码\" title=\"代码\" align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\" onClick=Insert(\"[code]内容[/code]\")>");
document.writeln("<img src=images\/edit\/replace.gif alt=\"替换\" title=\"替换\" align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\" onClick=replace()>");
document.writeln("<img align=absmiddle src=images\/edit\/table.gif alt=\"插入表格\" title=\"插入表格\" style=\"cursor:pointer;width:16px;height:16px;\" onClick=inserttable()>");
document.writeln("<img src=images\/edit\/img.gif alt=\"插入图片\" title=\"插入图片\" align=absmiddle style=\"cursor:pointer;width:16px;height:16px;\" onClick=img()>");
FormatTextlist="-|粗体 bold|倾斜 italic|下划线 underline|删除线 strikethrough|上标 superscript|下标 subscript|删除文字格式 RemoveFormat|-|左对齐 Justifyleft|居中 JustifyCenter|右对齐 JustifyRight|-|编号 insertorderedlist|项目符号 InsertUnorderedList|减少缩进量 Outdent|增加缩进量 indent|-|插入超链接 createLink|去掉超链接 Unlink|普通水平线 InsertHorizontalRule"
list= FormatTextlist.split ('|'); 
for(i=0;i<list.length;i++){
if (list[i]=="-"){document.write("<img align=\"absmiddle\" src=\"images\/edit\/separator.gif\" />");
}else{
var TextName= list[i].split (' '); 
if ((i==2 || i==3 ||i==4)&&(ns6)){
//document.write(" <img align=absmiddle src=images\/edit\/"+TextName[1]+"1.gif alt="+TextName[0]+" style=\"width:16px;height:16px;\" >");
}
else{
document.write(" <img align=\"absmiddle\" src=\"images\/edit\/"+TextName[1]+".gif\" alt=\""+TextName[0]+"\" title=\""+TextName[0]+"\" style=\"cursor:pointer;width:16px;height:16px;\" onClick=\"FormatText('"+TextName[1]+"')\" \/> ");
}
}
}



document.writeln("<div><iframe ID=\"edit\" MARGINHEIGHT=\"5\" MARGINWIDTH=\"5\" width=\"99%\" height=\"250\"><\/iframe><\/div>");
document.writeln("<div style=\"float:right;\"><span id=\"CNo\" style=\"height:21\" ><img id=\"CNo\" width=\"52\" height=\"21\" src=\"images\/edit\/codeA.gif\" onClick=\"setCode(\'No\')\" ></span><span style=\"height:21\" id=\"CYes\"><img src=\"images\/edit\/codeB.gif\" width=\"52\" height=\"21\" onClick=setCode(\'Yes\')></span> <img src=\"Images\/plus.gif\" style=\"cursor:pointer\" onclick=\"textarea_Size(250,document.getElementById(\'edit\'))\" alt=\"增加编辑框的高度\" \/> <img src=\"Images\/minus.gif\" style=\"cursor:pointer\" onclick=\"textarea_Size(-250,document.getElementById(\'edit\'))\" alt=\"减小输入框的高度\" \/><\/div>");
document.writeln("<div id=\"MaoDiv\" style=\"background:#FFFFFF;position: absolute; width:570px; height:160px; display:none;\"><iframe id=\"MView\" name=\"MView\" src=\"about:blank\" frameborder=\"0\" scrolling=\"no\" valign=\"top\" width=\"100%\" height=\"100%\"><\/iframe><\/div>");
if (ns6){
	IframeID=document.getElementById("edit").contentWindow;
	document.writeln("<div id=\"colorpalette\" style=\"position: absolute;visibility: hidden;width:201px;height:151px;left:400px;top:350px\" ><iframe src=\"images/edit/colors.htm\" frameborder=\"0\" scrolling=\"no\"  width=\"100%\" height=\"100%\" ><\/iframe><\/div>");
	document.addEventListener("mousedown", dismisscolorpalette, true);
	IframeID.document.addEventListener("mousedown", dismisscolorpalette, true);
	document.addEventListener("keypress", dismisscolorpalette, true);
	IframeID.document.addEventListener("keypress", dismisscolorpalette, true);
}else{
	IframeID=frames["edit"];
	}
	document.getElementById("CYes").style.border="1px outset"
	document.getElementById("CNo").style.border="1px inset"
	document.getElementById("CYes").style.cursor="pointer"
if (navigator.appVersion.indexOf("MSIE 6.0",0)==-1){IframeID.document.designMode="On"}
	IframeID.document.open();
	IframeID.document.write ('<html><head><style type=text/css>.quote{margin:5px 20px;padding:5px; background:#EEEEEF;border:1px solid #CCC ;}\nbody{boder:0px}.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;}</style><script language=\"JavaScript\">i=0;function ctlent(event){if((event.ctrlKey && event.keyCode == 13) || (event.altKey && event.keyCode == 83))if(i==0){i=1;parent.checkform()}}<\/script></head><body onkeydown=\"ctlent(event)\"></body></html>');
	IframeID.document.close();
	IframeID.document.body.style.fontSize="14px";
	IframeID.document.body.style.background="#FFFFFF"
	IframeID.document.body.contentEditable = "True";
	IframeID.document.body.innerHTML=document.getElementById("content").value;
}
function dismisscolorpalette(){document.getElementById("colorpalette").style.visibility="hidden";}

function cleanHtml(){
	if(IsCode=="Yes")setCode("No");
	sYxBBs=IframeID.document.body.innerHTML
	sYxBBs = sYxBBs.replace(/<a([^>]*) href\s*=\s*([^\s|>]*)([^>]*)/gi, "<a href=$2 target=\"_blank\" ");
	sYxBBs = sYxBBs.replace(/<a([^>]*)><\/a>/gi, "")
	sYxBBs = sYxBBs.replace(/<p>&nbsp;<\/p>/gi,"")
	sYxBBs = sYxBBs.replace(/<p><\/p>/gi,"<p>")
	sYxBBs = sYxBBs.replace(/<div><\/\1>/gi,"")
	sYxBBs = sYxBBs.replace(/(\r\n){10,}/gi,"$1")
	sYxBBs = sYxBBs.replace(/(<br>\s*){10,}/gi,"<br>")
	sYxBBs = sYxBBs.replace(/(<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>)/gi, "");
	sYxBBs = sYxBBs.replace(/<\\?\?xml[^>]*>/gi, "") ;
	sYxBBs = sYxBBs.replace(/&nbsp;/gi, " ");
	sYxBBs = sYxBBs.replace(/.width>/gi, ".width@YxBBs@");
	sYxBBs = sYxBBs.replace(/.height>/gi, ".height@YxBBs@");
	sYxBBs = sYxBBs.replace(/<img([^>]*) (src\s*=([^\s|>])*)([^>]*)>/gi,"<img $2>");
	sYxBBs = sYxBBs.replace(/.width@YxBBs@/gi, ".width>");
	sYxBBs = sYxBBs.replace(/.height@YxBBs@/gi, ".height>");
	sYxBBs = sYxBBs.replace(/<([^>]*) id\s*=\s*(content|caption)([^>]*)>/gi,"<$1$3>");
	sYxBBs = sYxBBs.replace(/(<(div|span|table) style\s*=\s*[^>]*)(position\s*:\s*absolute)([^>]*>)/gi, "$1$4");
return sYxBBs;
}
function setHTML(html) {
	switch (IsCode){
	case "Yes":
		if(ns6){
		var Yx=document.createTextNode(html);
		IframeID.document.body.innerHTML = "";
		IframeID.document.body.appendChild(Yx);
		}
	else{
		IframeID.document.body.innerText=html;
	}
    	document.getElementById("CNo").style.border="1px outset"
		document.getElementById("CNo").style.cursor="pointer"
 		document.getElementById("CYes").style.border="1px inset"
		document.getElementById("CYes").style.cursor=""
		document.getElementById("EditorB").style.display = 'none';
   break;
	case "No":
	if(ns6){
		var Yx= IframeID.document.body.ownerDocument.createRange();
		Yx.selectNodeContents(IframeID.document.body);
		IframeID.document.body.innerHTML = Yx.toString();
		}else{
		IframeID.document.body.innerHTML = html;
		}
		document.getElementById("CYes").style.cursor="pointer"
		document.getElementById("CYes").style.border="1px outset"
		document.getElementById("CNo").style.border="1px inset"
        document.getElementById("CNo").style.cursor=""
		document.getElementById("EditorB").style.display = 'block';
	break;
	}
}
function setCode(ast){
	if (ast!=IsCode){
		switch(IsCode){
		case "Yes":
			Yx=IframeID.document.body.innerText;
			 break;
		case "No":
		     Yx = IframeID.document.body.innerHTML;
		      break;
		}
		IsCode = ast;
		setHTML(Yx)
	}
}

function Insert(theSmilie){
IframeID.focus();
if(ns6){
IframeID.document.body.innerHTML+=theSmilie
}else{
sel=IframeID.document.selection.createRange();
sel.pasteHTML(theSmilie);
}
}
	

function inserttable(){
if(ns6){
 e = document.getElementById("edit");
    rowstext = prompt("创建表格，请输入表格的行数：",2);
    colstext = prompt("创建表格，请输入表格的列数：",2);
    rows = parseInt(rowstext);
	cols = parseInt(colstext);
    if ((rows > 0) && (cols > 0)) {
      table = e.contentWindow.document.createElement("table");
      table.setAttribute("border", "1");
      table.setAttribute("cellpadding", "2");
      table.setAttribute("cellspacing", "2");
      tbody = e.contentWindow.document.createElement("tbody");
      for (var i=0; i < rows; i++) {
        tr =e.contentWindow.document.createElement("tr");
        for (var j=0; j < cols; j++) {
          td =e.contentWindow.document.createElement("td");
          br =e.contentWindow.document.createElement("br");
          td.appendChild(br);
          tr.appendChild(td);
        }
        tbody.appendChild(tr);
      }
      table.appendChild(tbody);      
      insertNodeAtSelection(e.contentWindow, table);
    }
	IframeID.focus();
}else{
	var arr = showModalDialog("images/edit/table.html", window, "dialogWidth:22em; dialogHeight:22em; status:0; help:0;scroll:no;");
	if (arr)
	{
		IframeID.document.body.innerHTML+=arr;
	}
	IframeID.focus();
}
}
  
function insertNodeAtSelection(win, insertNode)
  {
      var sel = win.getSelection();
      var range = sel.getRangeAt(0);
      sel.removeAllRanges();
      range.deleteContents();
      var container = range.startContainer;
      var pos = range.startOffset;
      range=document.createRange();

      if (container.nodeType==3 && insertNode.nodeType==3) {
        container.insertData(pos, insertNode.nodeValue);
        range.setEnd(container, pos+insertNode.length);
        range.setStart(container, pos+insertNode.length);

      } else {

        var afterNode;
        if (container.nodeType==3) {
          var textNode = container;
          container = textNode.parentNode;
          var text = textNode.nodeValue;
          var textBefore = text.substr(0,pos);
          var textAfter = text.substr(pos);

          var beforeNode = document.createTextNode(textBefore);
          afterNode = document.createTextNode(textAfter);
          container.insertBefore(afterNode, textNode);
          container.insertBefore(insertNode, afterNode);
          container.insertBefore(beforeNode, insertNode);
          container.removeChild(textNode);

        } else {
          afterNode = container.childNodes[pos];
          container.insertBefore(insertNode, afterNode);
        }
        range.setEnd(afterNode, 0);
        range.setStart(afterNode, 0);
      }

      sel.addRange(range);
  };

function color(str){
	if(ns6){
	parent.command = str;
	if(str=="backcolor"){parent.command = "hilitecolor";};
    buttonElement = document.getElementById(str);
    document.getElementById("colorpalette").style.left = getOffsetLeft(buttonElement)+"px";
    document.getElementById("colorpalette").style.top = getOffsetTop(buttonElement) + buttonElement.offsetHeight+"px";
    document.getElementById("colorpalette").style.visibility="visible";
	}
else{
	var arr = showModalDialog("images/edit/selcolor.htm", "", "dialogWidth:18em; dialogHeight:19em; status:0;help:0;scroll:no;");
  if (arr != null) FormatText(str, arr);
  else IframeID.focus();
	}
	
	}

function FormatText(command,option){
	if(command == "createLink"){if(ns6)option = prompt("请输入网址 URL:", "http://");}
	IframeID.focus();
	IframeID.document.execCommand(command,false,option);
	}


function DoTitle(addTitle){
document.getElementById("caption").value=addTitle+document.getElementById("caption").value;
document.getElementById("caption").focus();
document.getElementById("caption").value+="";
}

function checkform(maxnum){
contentLength=IframeID.document.body.innerHTML.length;
if(contentLength<1){alert("您还没有填写内容!");IframeID.focus();return false;}
if(document.getElementById("caption").value.length<1){alert("呵呵，您忘了填写标题啦！");document.getElementById("caption").focus();return false;};
document.getElementById("sayb").disabled=true;
document.getElementById("sayb").value="正在发送"
document.getElementById("content").value=IframeID.document.body.innerHTML; 
document.getElementById("content").value=cleanHtml(); 
CopyBody();
document.say.submit()
}

function Goreset(){IframeID.document.body.innerHTML=document.say.content.value;}

function Gopreview()
{
document.preview.content.value=cleanHtml()
document.preview.caption.value=document.say.caption.value;
//openwin('preview',750,450);
document.preview.submit()
}

//---替换内容
function replace()
{
if(ns6)return false;
  var arr = showModalDialog("images/edit/replace.html", "", "dialogWidth:22em;dialogHeight:13em;status:0;help:0;scroll:no;");
	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 = Yx_rCode(con,a,b,true);
		}else{
			con = Yx_rCode(con,a,b);
		}
		IframeID.document.body.innerHTML = con;
	}
	else IframeID.focus();
}
function Yx_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 img(){
url=prompt("请输入图片文件地址:","http://");
if(!url || url=="http://") return;
FormatText('InsertImage',url)
}

function RealPlay(){
if(ns6){
var enter = prompt("请输入网上RealPlay播放文件的地址","http://");
if (!enter || enter=="http://")return;
Insert("[RM]"+enter+"[/RM]")
}else{	
var arr = showModalDialog("images/edit/RealPlay.htm", "", "dialogWidth:22em; dialogHeight:11em;status:0;help:0;scroll:no;");
if (arr != null){
IframeID.focus()
sel=IframeID.document.selection.createRange();
sel.pasteHTML(arr);
}
}
}
function MediaPlayer(){
if(ns6){
var enter = prompt("请输入网上MediaPlayer文件的地址","http://");
if (!enter || enter=="http://")return;
Insert("[MP]"+enter+"[/MP]")
}else{
var arr = showModalDialog("images/edit/MediaPlayer.htm", "", "dialogWidth:22em; dialogHeight:11em; status:0;help:0;scroll:no;");
if (arr != null){
IframeID.focus()
sel=IframeID.document.selection.createRange();
sel.pasteHTML(arr);
}
}
}

function flash(){
if(ns6){
var enter = prompt("请输入网上 Flash 文件的地址","http://");
if (!enter || enter=="http://")return;
Insert("[FLASH]"+enter+"[/FLASH]")
}else{
var arr = showModalDialog("images/edit/flash.htm", "", "dialogWidth:22em; dialogHeight:11em; status:0;help:0;scroll:no;");
if (arr != null){
IframeID.focus()
sel=IframeID.document.selection.createRange();
sel.pasteHTML(arr);
}
}
}

//投票...>>>
function SetNum(obj){
	str = '';
	if(!obj.value){
		obj.value = 1;
	}
	for(i=1;i<=obj.value;i++){
		str += '<div>选项'+i+'：<input type="text" name="votes'+i+'" style="width:70%" \/><\/div>';
	}
	document.getElementById("optionid").innerHTML=str+'<input type="hidden" name="autovalue" value="'+obj.value+'" \/>';
}
//..<<

//控制文本框
function textarea_Size(num,obj)
{
	if (parseInt(obj.height)+num>=250) {
		obj.height = parseInt(obj.height) + num;	
	}
	if (num>0)
	{
		obj.width="99%";	
	}
}
function CheckLength(maxnum){alert("最大字符为 "+maxnum+ " 字节\n您的内容已有 "+IframeID.document.body.innerHTML.length+" 字节");}


function Especial(A,B){
fontbegin=A
fontend=B
fontchuli();
}
function fontchuli(){
if(ns6){
	sel=IframeID.getSelection()
	var html = IframeID.document.body.innerHTML;
	if (sel!=""){
		html = html.replace(sel,fontbegin + sel + fontend);
		IframeID.document.body.innerHTML=html}
	else
		{IframeID.document.body.innerHTML+=(fontbegin +"" +  fontend)}
	IframeID.focus()
}else{
	IframeID.focus()
	sel=IframeID.document.selection.createRange();
	if (sel.text!=""){sel.pasteHTML(fontbegin + sel.text + fontend);}
	else{sel.pasteHTML(fontbegin + "" + fontend);}
}
}
function openwin()
{document.preview.caption.value=document.yimxu.caption.value;
document.preview.content.value=document.yimxu.content.value;
var popupWin = window.open('See.Asp?Action=preview', 'preview', 'left=0,width=600,height=400,resizable=1,scrollbars=yes,menubar=no,status=yes');
document.preview.submit()
}

var y=0;function presskey(eventobject){if(event.ctrlKey && window.event.keyCode==13){y++;if (y>1) {alert('帖子正在发出，请耐心等待！');return false;}this.document.yimxu.submit();this.document.yimxu.BtnPost.disabled=true;}}

function textarea_Size1(num,obj)
{
 if (parseInt(obj.rows)+num>=5) {
  obj.rows = parseInt(obj.rows) + num; 
 }
 if (num>0)
 {
  obj.width="90%";
 }
}
function CopyBody()
{
IframeID.focus();
IframeID.document.execCommand('selectAll');
IframeID.document.execCommand('copy');
}

