//////////////////////////// 以下是初始化代码 ////////////////////////////
// 定义全局变量
var sCurrMode=null;
var bInitialized = false;
var oLinkField = null;

var sHtmlEditor = null;		//HTML编辑框对象
var sHtmlEditorContent = null;	//源代码编辑框对象




// 全局设置对象
var sLinkFieldName = "content"; 

var systemObj = new Object() ;

systemObj.InitMode  = "Design";
systemObj.ToolsMode = "open";	//工具栏状态

// 浏览器版本检测
var IsIE = false;
var IsFirefox = false;
var IsOther = false;

if (window.navigator.userAgent.indexOf("Opera")>=1)
	{ IsOther = true;	}
else
	{
		if (window.navigator.userAgent.indexOf("MSIE")>=1)
			{ IsIE = true;	}
		else
			{
				if (window.navigator.userAgent.indexOf("Firefox")>=1)
					{ IsFirefox = true ;	}
				else
					{ IsOther = true; 	}
			}
	}

//对HTML编辑框进行初始化
function HtmlEditor_Initialized() {



	var i, s, curr;

	oLinkField = document.getElementById(sLinkFieldName);
	
	if (document.getElementById("ContentFlag").value=="0")
	{ 
		document.getElementById("ContentEdit").value = oLinkField.value;
		document.getElementById("ContentLoad").value = oLinkField.value;
		document.getElementById("ModeEdit").value = systemObj.InitMode;
		document.getElementById("ContentFlag").value = "1";
	}

	
	if (IsIE)
	{ sHtmlEditor = window.frames["HtmlEditor"] ; }
	else
	{
		if (IsFirefox || IsOther)
		{ sHtmlEditor = document.getElementById("HtmlEditor").contentWindow ;}
	}



	sHtmlEditorContent = document.getElementById("HtmlEditorContent") ;
	setMode(document.getElementById("ModeEdit").value);


}



//////////////////////////// 以上是初始化代码 ////////////////////////////


//////////////////////////// 以下是公用函数 ////////////////////////////

// 检测当前是否在预览模式
function isModeView(){
	if (sCurrMode=="View"){
		alert("预览时不允许设置编辑区内容。");
		return true;
	}
	return false;
}

// 检测当前是否在设计模式
function isModeDesign(){
	if (sCurrMode =="Design"){
		return true;
	}
	else
	{
		alert("当前模式不支持使用该功能。");
		return false;
	}
}

// 检测有效的浏览器版本
function isAllowBrowser(){
	if (IsIE) return true;

	alert("该功能只能用于IE 6.0 或以后的版本。");
	return false;
}


// 检测当前工具栏状态
function ChkToolsMode(){
	switch (systemObj.ToolsMode){
		case "open":
				return true;
				break;
		case "CloseHTML":
				alert("抱歉，您目前没有使用高级编辑模式（支持图片、文字特效等）的权限，目前只能使用普通文本输入。");
				return false;
				break;
				
		default:
				return false;
	}
}

// 判断是否在编辑状态
function validateMode(){
	if (bEditMode) return true;
	alert("请先点编辑器下方的“编辑”按钮，进入“编辑”状态，然后再使用系统编辑功能!");
	sHtmlEditorContent.focus();
	return false;
}

// 在当前文档位置插入.
function insertHTML(html) {

	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;


	sHtmlEditor.focus();
	if (isModeView()) return false;
	if (sHtmlEditor.document.selection.type.toLowerCase() != "none"){
		sHtmlEditor.document.selection.clear() ;
	}
	if (sCurrMode!="Design"){
		html=HTMLEncode(html);
	}
	sHtmlEditor.document.selection.createRange().pasteHTML(html) ; 
}


// 替换特殊字符
function HTMLEncode(text){

	if ( typeof( text ) != "string" )
		text = text.toString() ;
		
	text = text.replace(/&/g, "&amp;") ;
	text = text.replace(/"/g, "&quot;") ;
	text = text.replace(/</g, "&lt;") ;
	text = text.replace(/>/g, "&gt;") ;
	text = text.replace(/'/g, "&#146;") ;
	text = text.replace(/\ /g,"&nbsp;");
	text = text.replace(/\n/g,"<br>");
	text = text.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
	return text;
}

// 修改按钮风格函数
function setButtonStyle(t,n)
{
	if(n==1) 
	{ t.className ="overcolor"; }
	else
	{
		if(n==2) 
			t.className ="clickcolor";
		else 
			t.className ="outcolor";
	}
}


// 显示HTML工具栏
function setHtmlEditToolStyle(inMode)
{
	if (inMode == "Design")
	{
		document.getElementById("HtmlEditTable_1").className='';	
		document.getElementById("selectFont").disabled = false;
		document.getElementById("selectFont").disabled = false;
		document.getElementById("FontSize").disabled = false;
		document.getElementById("formatSelect").disabled = false;
	}
	else
	{
		document.getElementById("HtmlEditTable_1").className='baw';
		document.getElementById("selectFont").disabled = true;
		document.getElementById("FontSize").disabled = true;
		document.getElementById("formatSelect").disabled = true;		
	}

}


// 显示当前状态的编辑框函数
function setHtmlEditModeStyle(t)
{
	switch(t){
	case "Code":	//源代码编辑状态
		document.getElementById("DesignMode").style.display = "none" ; 
		document.getElementById("CodeMode").style.display = "" ; 
		break;
	case "Design":	//HTML编辑状态
		document.getElementById("DesignMode").style.display = "" ; 
		document.getElementById("CodeMode").style.display = "none" ; 
		break;
	}
}

//////////////////////////// 以上是公用函数 ////////////////////////////


//////////////////////////// 以下是常用函数 ////////////////////////////

//格式化函数，执行命令
function FormatText(command, option){

  if (!ChkToolsMode()) return;
  if (!isModeDesign()) return;

  sHtmlEditor.focus();

  if (option==null) 
  	{ sHtmlEditor.document.execCommand(command);}
  else 
  	{ sHtmlEditor.document.execCommand(command,"",option);}

}

// 前景色（文字颜色）
function ForeColor()
{
  if (!ChkToolsMode()) return;  
  if (!isAllowBrowser()) return;
  if (!isModeDesign()) return;
  
  var arr = showModalDialog("/script/user/editor/editor_selcolor.htm", "", "dialogWidth:300px; dialogHeight:250px; status:0");
  if (arr != null) FormatText('forecolor', arr);
  else sHtmlEditor.focus();
}


// 背景色
function BackColor()
{
  if (!ChkToolsMode()) return;
  if (!isAllowBrowser()) return;
  if (!isModeDesign()) return;

  var arr = showModalDialog("/script/user/editor/editor_selcolor.htm", "", "dialogWidth:300px; dialogHeight:250px; status:0");
  if (arr != null) FormatText('BackColor', arr);
  else sHtmlEditor.focus();
}


// 纯文本粘贴
function PasteText()
{
  if (!isAllowBrowser()) return;
  var sText = HTMLEncode( clipboardData.getData("Text") ) ;
  insertHTML(sText);
}

// 插入滚动文本
function InsertMarquee()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
	var arr = showModalDialog("/script/user/editor/editor_marquee.htm", "", "dialogWidth:430px;dialogHeight:130px;help:0;  scroll:0; status:0");  
	if (arr != null){ range.pasteHTML(arr); }
}


// 插入发光字体
function InsertGlowText()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
		
	var arr = showModalDialog("/script/user/editor/editor_glowtext.htm", "", "dialogWidth:430px;dialogHeight:150px;help:no;scroll:no;status:no");

	if (arr != null){
		range.pasteHTML(arr);
	}
}


// 插入特殊符号
function InsertSpecialChar(){

	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
  
	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
	var arr = showModalDialog("/script/user/editor/editor_specialchar.htm", "", "dialogWidth:430px;dialogHeight:280px;help:no; scroll: no; status:no");  
	if (arr != null){	range.pasteHTML(arr);}
}



// 插入代码
function InsertCode()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	sHtmlEditor.focus();

	var range = sHtmlEditor.document.selection.createRange(); 
	var range_text = range.text ; 
	
	insertHTML('<table width=95% border="0" align="Center" cellpadding="6" cellspacing="0" style="border: 1px Dotted #CCCCCC; TABLE-LAYOUT: fixed; font-size:9pt;"><tr><td bgcolor=#FDFDDF style="WORD-WRAP: break-word"><font style="color: #990000;font-weight:bold">以下是代码片段：</font><br>'+HTMLEncode(range_text)+'</td></tr></table>');
}


// 插入引用
function InsertQuote()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	sHtmlEditor.focus();

	var range = sHtmlEditor.document.selection.createRange(); 
	var range_text = range.text ; 
	
	insertHTML('<table width=95% border="0" align="Center" cellpadding="6" cellspacing="0" style="border: 1px Dotted #CCCCCC; TABLE-LAYOUT: fixed; font-size:9pt;"><tr><td bgcolor=#F3F3F3 style="WORD-WRAP: break-word"><font style="color: #990000;font-weight:bold">以下是引用片段：</font><br>'+HTMLEncode(range_text)+'</td></tr></table>');
}



// 插入栏目框
function InserFieldset()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	sHtmlEditor.focus();

	var range = sHtmlEditor.document.selection.createRange();
	var arr = showModalDialog("/script/user/editor/editor_fieldset.htm", "", "dialogWidth:500px; dialogHeight:230px; status:no;help:no");
  
	if (arr != null){
		range.pasteHTML(arr);
	}
}


// 插入swf
function InsertSWF()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();	
	var arr = showModalDialog("/script/user/editor/editor_flash.htm", "", "dialogWidth:400px; dialogHeight:400px; help: no; scroll: yes; status: no"); 
	if (arr != null){
		range.pasteHTML(arr);
	}
}


// 插入Windows Media
function InsertWMV()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;	

	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
	var arr = showModalDialog("/script/user/editor/editor_media.htm", "", "dialogWidth:400px; dialogHeight:535px; help: no; scroll: yes; status: no");
	if (arr != null){
		range.pasteHTML(arr);
	}
}


// 插入Real Media
function InsertRM()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	
	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
	var arr = showModalDialog("/script/user/editor/editor_rm.htm", "", "dialogWidth:400px; dialogHeight:535px; help: no; scroll: yes; status: no");
	if (arr != null){
		range.pasteHTML(arr);
	}
}


// 插入图片
function InsertPIC(inUserID)
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	
	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
	
	//var arr = showModalDialog("/script/user/editor/editor_pic.asp?userid="+inUserID, "", "dialogWidth:430px;dialogHeight:460px;help:no;scroll:yes;status:no");
	//if (arr != null){
	//	range.pasteHTML(arr);
	//}


	window.open("/script/user/editor/editor_pic.asp?userid="+inUserID,"","height=430,width=460,resizable=no,scrollbars=no,status=no,toolbar=no,menubar=no,location=no");


}



// 插入涂鸦板
function InsertPaint(inUserID)
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	
	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();

	var arr = showModalDialog("/script/user/editor/editor_painter.asp?userid="+inUserID+"&rnd=" + Math.random(), window, "dialogWidth:520px;dialogHeight:440px;help:no;scroll:no;status:no");
	if (arr != null){
		range.pasteHTML(arr);
	}
	
}


// 插入奇虎组图插件
function InsertXKOO(inUserID)
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	
	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();

	//读取奇虎组图参数
	var phid = document.getElementById("phid").value;
	var phid_string = "";
	if ( phid != 0 && phid != ""){
		phid_string = "&phid=" + phid;
	}


	window.open("/script/user/editor/editor_xkoo.asp?userid="+inUserID + phid_string ,"","height=450,width=760,resizable=no,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no");

	
}



// 插入超级链接
function InsertURL()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	
	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
	
	var arr = showModalDialog("/script/user/editor/editor_url.htm", "", "dialogWidth:450px;dialogHeight:300px;help:no;scroll:no;status:no");

	if (arr != null){
		range.pasteHTML(arr);
	}
}


// 插入网页
function InsertWebPage()
{
	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	
	sHtmlEditor.focus();
	var range = sHtmlEditor.document.selection.createRange();
	
	var arr = showModalDialog("/script/user/editor/editor_iframe.htm", "", "dialogWidth:430px;dialogHeight:200px;help:no;scroll:no;status:no");

	if (arr != null){
	var ss;
	ss=arr.split("*")
	a=ss[0];
	b=ss[1];
	c=ss[2];
	d=ss[3];
	e=ss[4];
	var str1;
	if (a!="http://" && a!="")
	{
		str1="<iframe src='"+a+"'"
		str1+=" scrolling="+b+""
		str1+=" frameborder="+c+""
		if(d!='')str1+=" marginheight="+d
		if(e!='')str1+=" marginwidth="+e
		str1=str1+"></iframe>"
		
		range.pasteHTML(str1);
	}
	else sHtmlEditor.focus();
	}
}


// 暂时解决一下通用性问题 查找替换
function FindSTR(){

	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;
	
	sHtmlEditor.focus();
	var arr = showModalDialog("/script/user/editor/editor_find.htm", window, "dialogWidth:320px; dialogHeight:190px; help: no; scroll: no; status: no");
}


//////////////////////////// 以上是常用函数 ////////////////////////////




//////////////////////////// 以下是表格函数 ////////////////////////////
// 插入表格
function TableInsert()
{
  if (!ChkToolsMode()) return;
  if (!isAllowBrowser()) return;
  if (!isModeDesign()) return;

  var arr = showModalDialog("/script/user/editor/editor_table.htm", "", "dialogWidth:400px; dialogHeight:290px; status:no;help:no");
  
  if (arr != null){
	  var ss;
	  ss=arr.split("*")
	  row=ss[0];
	  col=ss[1];
	  width=ss[2];
	  height=ss[3];
	  cellpadding=ss[4];
	  cellspacing=ss[5];
	  align=ss[6];
	  border=ss[7];
	  var string;
	  string="<table border="+border+" width="+width+" height="+height+" cellpadding="+cellpadding+" cellspacing="+cellspacing+" align="+align+">";
	  for(i=1;i<=row;i++){
		string=string+"<tr>";
		for(j=1;j<=col;j++){ string=string+"<td></td>"; }
		string=string+"</tr>";
	  }
	  string=string+"</table>";
	  content=sHtmlEditor.document.body.innerHTML;
	  content=content+string;
	  sHtmlEditor.document.body.innerHTML=content;
  }
  else sHtmlEditor.focus();
}

// 光标是否在表格中
function isCursorInTableCell() {

	var elem = sHtmlEditor.document.selection.createRange().parentElement();
	while (elem.tagName.toUpperCase() != "TD" && elem.tagName.toUpperCase() != "TH"){
		elem = elem.parentElement;
		if (elem == null)	break;
	}
	if (elem) {
		selectedTD = elem;
		selectedTR = selectedTD.parentElement;
		selectedTBODY =  selectedTR.parentElement;
		selectedTable = selectedTBODY.parentElement;
		return true;
	}
}


// 插入行（在下方）
function TableRowInsertBelow() {

	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	if (isCursorInTableCell()){
		var numCols = 0;
		allCells = selectedTR.cells;
		for (var i=0;i<allCells.length;i++) {
			numCols = numCols + allCells[i].getAttribute('colSpan');
		}
		var newTR = selectedTable.insertRow(selectedTR.rowIndex+1);
		for (i = 0; i < numCols; i++) {
			newTD = newTR.insertCell();
			newTD.innerHTML = "&nbsp;";
			if (borderShown == "yes") {
				newTD.runtimeStyle.border = "1px dotted #BFBFBF" ;
			}
		}
	}
}

// 删除行
function TableRowDelete() {

	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	if (isCursorInTableCell()) {
		selectedTable.deleteRow(selectedTR.rowIndex);
	}
}


// 插入列（在右侧）
function TableColInsertRight() {

	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	if (isCursorInTableCell()) {
		moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex);
		allRows = selectedTable.rows;
		for (i=0;i<allRows.length;i++) {
			rowCount = allRows[i].cells.length - 1;
			position = rowCount - moveFromEnd;
			if (position < 0) {	position = 0;}
			newCell = allRows[i].insertCell(position+1);
			newCell.innerHTML = "&nbsp;";
			if (borderShown == "yes") {
				newCell.runtimeStyle.border = "1px dotted #BFBFBF";
			}
		}    
	}
}

// 删除列
function TableColDelete() {

	if (!ChkToolsMode()) return;
	if (!isAllowBrowser()) return;
	if (!isModeDesign()) return;

	if (isCursorInTableCell()) {
		moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex);
		allRows = selectedTable.rows;
		for (var i=0;i<allRows.length;i++) {
			endOfRow = allRows[i].cells.length - 1;
			position = endOfRow - moveFromEnd;
			if (position < 0) {	position = 0}
			allCellsInRow = allRows[i].cells;
			if (allCellsInRow[position].colSpan > 1) {

				allCellsInRow[position].colSpan = allCellsInRow[position].colSpan - 1;
			} 
			else
			{ allRows[i].deleteCell(position);}
		}
	}
}


//////////////////////////// 以上是表格函数 ////////////////////////////



//////////////////////////// 以下是自定义右键函数 ////////////////////////////


// 菜单
var oPopupMenu = null;
if (IsIE){
    oPopupMenu = window.createPopup();
}

// 菜单常量
var sMenuHr="<tr><td align=center valign=middle height=2><TABLE border=0 cellpadding=0 cellspacing=0 width=128 height=2><tr><td height=1><\/td><\/tr><tr><td height=1><\/td><\/tr><\/TABLE><\/td><\/tr>";

var sMenu1="<TABLE border=0 cellpadding=0 cellspacing=0 style='border-top:window 1px solid;border-left:window 1px solid;border-bottom:buttonshadow 1px solid;border-right:buttonshadow 1px solid;background-color:#CCCCCC'><tr><td width=18 valign=bottom align=center style='background-positionY: 35%; background-repeat:no-repeat;'><\/td><td style='background-color:buttonface'><TABLE border=0 cellpadding=0 cellspacing=0>";

var sMenu2="<\/TABLE><\/td><\/tr><\/TABLE>";


// 字体处理
function format(what,opt){
	if (!validateMode()) return;
	if (opt=="removeFormat"){
		what=opt;
		opt=null;
	}
	if (opt==null) sHtmlEditor.document.execCommand(what);
	else sHtmlEditor.document.execCommand(what,"",opt);
	pureText = false;
	sHtmlEditor.focus();
}

//文本源码，复制粘贴的问题。
function format2(what,opt){
	if (opt=="removeFormat"){
		what=opt;
		opt=null;
	}
	if (opt==null) sHtmlEditor.document.execCommand(what);
	else sHtmlEditor.document.execCommand(what,"",opt);

	pureText = false;
	sHtmlEditor.focus();
}

// 取菜单行
function getMenuRow(s_Disabled, s_Event, s_Image, s_Html) {
	var s_MenuRow = "";
	s_MenuRow = "<tr><td align=center valign=middle><TABLE border=0 cellpadding=0 cellspacing=0 width=112 style='font-size: 9pt'><tr "+s_Disabled+"><td valign=middle height=20 class=MouseOut onMouseOver=this.className='MouseOver'; onMouseOut=this.className='MouseOut';";
	if (s_Disabled==""){
		s_MenuRow += " onclick=\"parent."+s_Event+";parent.oPopupMenu.hide();\"";
	}

	s_MenuRow += ">"
	if (s_Image !=""){
		s_MenuRow += "&nbsp;<img border=0 src='../../images/editor/"+s_Image+"' width=16 height=16 align=absmiddle "+s_Disabled+">&nbsp;";
	}else{
		s_MenuRow += "&nbsp;";
	}
	s_MenuRow += s_Html+"<\/td><\/tr><\/TABLE><\/td><\/tr>";
	return s_MenuRow;
}

// 取标准的format菜单行
function getFormatMenuRow(menu, html, image){
	var s_Disabled = "";
	if (!sHtmlEditor.document.queryCommandEnabled(menu)){
		s_Disabled = "disabled";
	}
	var s_Event = "format('"+menu+"')";
	var s_Image = menu+".gif";
	if (image){
		s_Image = image;
	}
	return getMenuRow(s_Disabled, s_Event, s_Image, html)
}

// 不能通用右键的问题
function getFormatMenuRow2(menu, html, image){
	var s_Disabled = "";
	if (!sHtmlEditor.document.queryCommandEnabled(menu)){
		s_Disabled = "disabled";
	}
	var s_Event = "format2('"+menu+"')";
	var s_Image = menu+".gif";
	if (image){
		s_Image = image;
	}
	return getMenuRow(s_Disabled, s_Event, s_Image, html)
}

// 右键菜单
function showContextMenu(event){

	if (!bEditMode) return false;
	var width = 130;
	var height = 0;
	var lefter = event.clientX;
	var topper = event.clientY;

	var oPopDocument = oPopupMenu.document;
	var oPopBody = oPopupMenu.document.body;

	var sMenu="";
        
	sMenu += getFormatMenuRow2("cut", "剪切");
	sMenu += getFormatMenuRow2("copy", "复制");
	sMenu += getFormatMenuRow2("paste", "常规粘贴");
	sMenu += getMenuRow("", "PasteText()", "paste.gif", "纯文本粘贴");	
	sMenu += getFormatMenuRow2("delete", "删除");
 	sMenu += getFormatMenuRow2("selectall", "全选");
	sMenu += getMenuRow("", "FindSTR()", "replace.gif", "查找替换...");

	sMenu = sMenu1 + sMenu + sMenu2;

	oPopDocument.open();
	oPopDocument.write("<head><link href=../../css/user.css type=\"text/css\" rel=\"stylesheet\"></head><body scroll=\"no\"  leftmargin='0' topmargin='0' style='body:margin:0px;border:0px' onConTextMenu=\"event.returnValue=false;\">"+sMenu);
	oPopDocument.close();

	height = 145;
	if(lefter+width > document.body.clientWidth) lefter=lefter-width;
	oPopupMenu.show(lefter, topper, width, height, sHtmlEditor.document.body);
	return false;
}

//////////////////////////// 以上是自定义右键函数 ////////////////////////////


//////////////////////////// 以下是HTML编辑框函数 ////////////////////////////
// 改变模式：代码、编辑、文本、预览
function setMode(NewMode){

	if (NewMode!=sCurrMode){

		var sBody = "";
		switch(sCurrMode){
		case "Code":	//源代码编辑状态
			sBody = sHtmlEditorContent.value;
			break;
		case "Design":	//HTML编辑状态
			sBody = sHtmlEditor.document.body.innerHTML;
			break;	
		default:	//初始化状态
			sBody = document.getElementById("ContentEdit").value;
			break;
		}

		if (NewMode == "View")	//预览，弹出窗口显示
		{
			AttachSubmit();
			//window.open("editor_preview.asp","","height=450,width=600,resizable=yes,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no");
			return;
		}

		// 换图片
		document.getElementById("HtmlCode").src = "../../images/editor/html.gif";
		document.getElementById("HtmlDesign").src = "../../images/editor/editor.gif";

		switch(NewMode){
			case "Code":
				document.getElementById("HtmlCode").src = "../../images/editor/html2.gif";
				break;
			case "Design":
				document.getElementById("HtmlDesign").src = "../../images/editor/editor2.gif";
				break;
			default:
				document.getElementById("HtmlDesign").src = "../../images/editor/editor2.gif";
				break;
		}

		sCurrMode = NewMode;
		document.getElementById("ModeEdit").value = sCurrMode; //保存状态值
		//setHtmlEditToolStyle(sCurrMode) ;	//处理HTML工具栏状态
		setHtmlEditModeStyle(document.getElementById("ModeEdit").value);	//处理编辑框状态
		setHTML(sBody);
		
	}
}

// 设置编辑器的内容
function setHTML(html) {
	document.getElementById("ContentEdit").value = html;
	
	switch (sCurrMode){
	case "Code":
		sHtmlEditorContent.focus();
		sHtmlEditorContent.value = html ;
		bEditMode=false;
		break;
	case "Design":
		sHtmlEditor.document.open();
		sHtmlEditor.document.designMode="On";
		sHtmlEditor.document.write("<html><head></head><body style=\"font-size:14px;font-family:'宋体,Verdana,Arial';\" MONOSPACE>");
		sHtmlEditor.document.write(html);
		sHtmlEditor.document.write("</body></html>");
		sHtmlEditor.document.body.contentEditable="true";
		
		if (IsIE)
		{
		sHtmlEditor.document.oncontextmenu=new Function('return showContextMenu(sHtmlEditor.event);');
		}
		
		sHtmlEditor.document.close();

		sHtmlEditor.focus();

		if (html == "")
		{ FormatText('selectall',''); }

		bEditMode=true;
		break;
	}

	if ((borderShown != "0") && bEditMode) {
		borderShown = "0";
		showBorders();
	}


}

// 附加submit提交事件
function AttachSubmit() { 
	var oForm = oLinkField.form ;
	if (!oForm) return;
	
	var html = getHTML();

	document.getElementById("ContentEdit").value = html;

	if (sCurrMode=="Text"){
		html = HTMLEncode(html);
	}
	oLinkField.value = html;
} 

// 取编辑器的内容
function getHTML() {
	var html;
	if(sCurrMode=="Design"){
		html = sHtmlEditor.document.body.innerHTML;
	}else{
		html = sHtmlEditorContent.value;
	}
	
	if ((html.toLowerCase()=="<p>&nbsp;</p>")||(html.toLowerCase()=="<p></p>"))
	{	html = "";	}

	return html;
}


// 显示或隐藏指导方针
var borderShown = 0;

function showBorders() {
	if (!validateMode()) return;
	
	var allForms  = sHtmlEditor.document.body.getElementByTagName("FORM");
	var allInputs = sHtmlEditor.document.body.getElementByTagName("INPUT");
	var allTables = sHtmlEditor.document.body.getElementByTagName("TABLE");
	var allLinks  = sHtmlEditor.document.body.getElementByTagName("A");

	// 表单
	for (a=0; a < allForms.length; a++) {
		if (borderShown == "0") {
			allForms[a].runtimeStyle.border = "1px dotted #FF0000"
		} else {
			allForms[a].runtimeStyle.cssText = ""
		}
	}

	// Input Hidden类
	for (b=0; b < allInputs.length; b++) {
		if (borderShown == "0") {
			if (allInputs[b].type.toUpperCase() == "HIDDEN") {
				allInputs[b].runtimeStyle.border = "1px dashed #000000"
				allInputs[b].runtimeStyle.width = "15px"
				allInputs[b].runtimeStyle.height = "15px"
				allInputs[b].runtimeStyle.backgroundColor = "#FDADAD"
				allInputs[b].runtimeStyle.color = "#FDADAD"
			}
		} else {
			if (allInputs[b].type.toUpperCase() == "HIDDEN")
				allInputs[b].runtimeStyle.cssText = ""
		}
	}

	// 表格
	for (i=0; i < allTables.length; i++) {
			if (borderShown == "0") {
				allTables[i].runtimeStyle.border = "1px dotted #BFBFBF"
			} else {
				allTables[i].runtimeStyle.cssText = ""
			}

			allRows = allTables[i].rows
			for (y=0; y < allRows.length; y++) {
			 	allCellsInRow = allRows[y].cells
					for (x=0; x < allCellsInRow.length; x++) {
						if (borderShown == "0") {
							allCellsInRow[x].runtimeStyle.border = "1px dotted #BFBFBF"
						} else {
							allCellsInRow[x].runtimeStyle.cssText = ""
						}
					}
			}
	}

	// 链接 A
	for (a=0; a < allLinks.length; a++) {
		if (borderShown == "0") {
			if (allLinks[a].href.toUpperCase() == "") {
				allLinks[a].runtimeStyle.borderBottom = "1px dashed #000000"
			}
		} else {
			allLinks[a].runtimeStyle.cssText = ""
		}
	}

	if (borderShown == "0") {
		borderShown = "1"
	} else {
		borderShown = "0"
	}

	scrollUp()
}

// 返回页面最上部
function scrollUp() {
	sHtmlEditor.scrollBy(0,0);
}
//////////////////////////// 以上是HTML编辑框函数 ////////////////////////////