﻿var sCurrMode = null;
var errcode = '错误提示：在"代码"和"预览"模式下无法使用此功能，请在设计模式下运行！';
var L_TOOLBARGIF_TEXT = "editor/_toolbar.gif";

function initDocument()
{
	if (navigator.appVersion.indexOf("MSIE 6.0",0) == -1) {
		EditCtrl.document.designMode = "On"
	}
	EditCtrl.document.open();
	EditCtrl.document.write ("<html><head>");
	EditCtrl.document.write ("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
	EditCtrl.document.write ("</head><body bgcolor=\"#FFFFFF\"></body></html>");
	EditCtrl.document.close();
	EditCtrl.document.body.contentEditable = "True";
	EditCtrl.focus();
}

function document.onreadystatechange(){
	if (document.readyState != "complete") return;
	setMode('DESIGN');
	_drawToolbar();
}

//执行命令
function _Format(iwhat,opt,expt){
	EditCtrl.focus();
	if(sCurrMode!="DESIGN"){
		alert(errcode);
	}else{
		EditCtrl.document.execCommand(iwhat,"",opt);
	}
	EditCtrl.focus();
}

//执行命令
function _PFormat(iwhat,opt,expt){
	EditCtrl.focus();
	EditCtrl.document.execCommand(iwhat,"",opt);
	EditCtrl.focus();
}

//打开窗口命令
function _OpenWin(Url,dWidth,dHeight){
	if(sCurrMode!="DESIGN"){
		alert(errcode)
	}else{
		var arr = showModalDialog(""+ Url, window, "dialogWidth:"+ dWidth +"px;dialogHeight:"+ dHeight +"px;help:0;status:0;scroll:0");
		if (arr != undefined)
			insertHTML(arr);
	}
}

//修改对话框，打开窗口
function _EditOpenWin(Url,dWidth,dHeight){
	if(sCurrMode!="DESIGN"){
		alert(errcode)
	}else{
		var arr = showModalDialog("" + Url, window, "dialogWidth:"+ dWidth +";dialogHeight:"+ dHeight +";help:0;status:0;scroll:no");
		EditHTML(arr);
	}
}

//初始化菜单
function _drawToolbar()
{
	var aSizes = new Array(25,25,25,8,25,25,8,25,25,8,25,25,8,25,25,25,8,25,25,25,8,25,25,25,8,25,25,8,25,25,25,25,8,25,25,25,25,8,25,25,25,25,8,25,25,25,8,25,25,25,25,8,25,25,25,25,25,8,25,25,25,25,8,25,25,8,25,8);
	var aIds = new Array("cut","copy","paste","bar00","print","SpellingCheck","bar01","undo","redo","bar02","selectall","findreplace","bar03","delete","RemoveFormat","cleancode","bar04","bold","italic","underline","bar05","DeleteLine","superscript","subscript","bar06","color","backColor","bar07","CreateLink","DeleteLink","HorizontalRule","insertSymbol","bar08","JustifyLeft","JustifyCenter","JustifyRight","JustifyFusiform","bar09","InsertOrderedList","InsertUnorderedList","outDent","inDent","bar10","image","editimg","revertimg","bar11","table","EditTable","cleartable1","cleartable2","bar12","editcell","inrows","incols","outrows","outcols","bar13","upfile","upswf","upwm","uprm","bar14","markCode","markQuote","bar15","help","bar16");
	var aTips = new Array("剪切","复制","粘贴","","打印","拼写检查","","撤销","恢复","","全选","查找替换","","删除","删除格式","清理代码","","加粗","斜体","下划线","","删除线","上标","下标","","字体颜色","字体背景颜色","","超链接","删除链接","水平线","插入特殊符号","","左对齐","居中","右对齐","两端对齐","","编号","项目符号","减少缩进","增加缩进","","插入图片","修改图片","还原图片","","插入表格","修改表格","清理表格(text)","清理表格(html)","","修改单元格","插入行","插入列","删除行","删除列","","上传文件","插入Flash","插入Windows Media","插入Real Media","","插入代码","插入引用","","帮助","");
	var aCommand = new Array("_PFormat('cut')","_PFormat('copy')","_PFormat('paste')",null,"SelfPrint()","ieSpellCheck()",null,"_PFormat('undo')","_PFormat('redo')",null,"_PFormat('selectall')","_OpenWin('editor/findreplace.htm',310,170)",null,"_PFormat('delete')","_Format('RemoveFormat')","CleanCode()",null,"_Format('bold')","_Format('italic')","_Format('underline')",null,"_Format('strikethrough')","_Format('superscript')","_Format('subscript')",null,"foreColor()","backColor()","","_Format('CreateLink')","_Format('Unlink')","_Format('InsertHorizontalRule')","_OpenWin('editor/symbol.htm',350,240)",null,"_Format('JustifyLeft')","_Format('JustifyCenter')","_Format('JustifyRight')","_Format('JustifyFull')",null,"_Format('InsertOrderedList')","_Format('InsertUnorderedList')","_Format('outDent')","_Format('inDent')",null,"_OpenWin('editor/image.htm',350,350)","EditImg()","revertImg()",null,"_OpenWin('editor/table.htm',360,370)","EditTable()","ClearTable(1)","ClearTable(2)",null,"EditCell()","inrows()","incols()","delrows()","delcols()",null,"_OpenWin('editor/file.htm',400,250)","_OpenWin('editor/flash.htm',400,250)","_OpenWin('editor/wmv.htm',400,300)","_OpenWin('editor/rm.htm',400,300)",null,"markCode()","markQuote()","","_help()",null);
	var sz="<NOBR>";
	var iLeft=0, iHeight=24;
	for(var i=0; i<aSizes.length; i++)
	{
		sz+="<SPAN unselectable='On' CLASS='tbButton' ONKEYPRESS=\"if(event.keyCode==13){"+aCommand[i]+";event.keyCode=0}\" ID='tb"+aIds[i]+"' style='border-top: 1px solid #FFFFFF; border-bottom: 1px solid #808080; width:"+aSizes[i]+"px;height:"+(iHeight+2)+"px;'>" +"<SPAN STYLE=\"position:absolute;width:"+aSizes[i]+"px;height:"+iHeight+"px;clip:rect(0 "+aSizes[i]+" "+iHeight+" 0)\"><IMG ALT=\""+aTips[i]+"\" TITLE=\""+aTips[i]+"\" ONCLICK=\""+aCommand[i]+";event.cancelBubble=true\" ONMOUSEDOWN=\"if(event.button==1)this.style.pixelTop=-"+(iHeight*2)+"\" ONMOUSEOVER=\"this.style.pixelTop=-"+iHeight+"\" ONMOUSEOUT=\"this.style.pixelTop=0\" ONMOUSEUP=\"this.style.pixelTop=-"+iHeight+"\" SRC=\""+L_TOOLBARGIF_TEXT+"\" STYLE=\"position:absolute;top:0px;left:-"+iLeft+"px\"></SPAN></SPAN>" +(aTips[i]==""?"</NOBR><NOBR>":"")
		iLeft += aSizes[i]
	}
	sz += "</NOBR>";
	ToolBarID.innerHTML = sz;
}

function _help()
{
	window.open("http://www.light-star.net/");
}

function oblog_selectRange(){
	oblog_selection = EditCtrl.document.selection;
	oblog_edit	= oblog_selection.createRange();
	oblog_RangeType = oblog_selection.type;
}

function markCode()
{
	EditCtrl.focus();
	if (sCurrMode != "DESIGN") setMode('DESIGN');
	if(document.selection.type == "Control") return;
	var eobj = EditCtrl.document.selection.createRange();
	var codeHtml = "<div style='margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style:oblique;line-height:normal;font-weight:bold;cursor:pointer' title='点击运行该代码!' onclick=\"preWin=window.open('','','');preWin.document.open();preWin.document.write(this.innerText);preWin.document.close();\">"
	codeHtml = "<div style='margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style:oblique;line-height:normal;font-weight:bold;'>"
	codeHtml += eobj.htmlText;
	codeHtml += "</div>"
	eobj.pasteHTML(codeHtml);
}

function markQuote()
{
	EditCtrl.focus();
	if (sCurrMode != "DESIGN") setMode('DESIGN');
	if(document.selection.type == "Control") return;
	var eobj = EditCtrl.document.selection.createRange();
	var quoteHtml = "<div style='margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3'>"
	quoteHtml += eobj.htmlText;
	quoteHtml += "</div>"
	eobj.pasteHTML(quoteHtml);
}

//设置视图
function setMode(NewMode){
	var cont;
	if (NewMode!=sCurrMode){
		switch(NewMode){
			case "DESIGN":
				cont=EditCtrl.document.body.innerText;
				if(sCurrMode=="CODE")
					EditCtrl.document.body.innerHTML=cont;
				else
					EditCtrl.document.body.innerText=cont;
				resetbtn('designid')
				break;
			case "CODE":
				if(sCurrMode=="DESIGN")
					cont=EditCtrl.document.body.innerHTML;
				else
					cont=EditCtrl.document.body.innerText;
				EditCtrl.document.body.innerText=cont;
				resetbtn('codeid')
				break;
			case "TEXT":
				if ( !confirm("警告：切换到纯文本模式会丢失您所有的HTML格式，您确认切换吗？") )
					return false;
				if(sCurrMode=="DESIGN"){
					cont=EditCtrl.document.body.innerText;
					EditCtrl.document.body.innerText=cont;
				}else{
					cont=EditCtrl.document.body.innerText;
					EditCtrl.document.body.innerHTML=cont;
					cont=EditCtrl.document.body.innerText;
					EditCtrl.document.body.innerText=cont;
				}
				resetbtn('textid')
				break;
		}
	}

	sCurrMode = NewMode;
	if(sCurrMode=="DESIGN"){
		dMode = false;
	}else{
		dMode = true;
	}
	for (var i=0;i<FuncBarID.children.length;i++)
		FuncBarID.children[i].disabled=dMode;
	EditCtrl.focus();
}

//设置视图按钮
function resetbtn(obj){
	var btn = document.getElementById(obj)
	for (var i=0;i<FuncID.children.length;i++){
		FuncID.children[i].className="btn0";
	}
	btn.className="btn1";
}

//预览页面
function ViewCode()
{
	if (sCurrMode == "DESIGN") {
		cont=EditCtrl.document.body.innerHTML;
	} else {
		cont=EditCtrl.document.body.innerText;
	}
	cont=correctUrl(cont);
	bodyTag="<html><head><style type=text/css>.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px}.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style : oblique;line-height : normal ;font-weight:bold;}\nbody{boder:0px}</style></head><BODY bgcolor=\"#FFFFFF\" >";
	//if (oblog_filterScript)
	cont=FilterScript(cont);
	cont=rCode(cont,"\\[dvnews_ad]","<img src='editors/images/pic_ad.jpg' vspace=10 hspace=10 align=left border=1 title='Advertising'>");
	cont=rCode(cont,"\\[dvnews_page]","<br><br><hr size=2 width=95% align=left>&nbsp; <font color=red face='Tahoma,Arail' size=2><b>Next Page ...</b></font><br><hr size=2 width=95% align=left>");
	preWin=window.open('preview','','left=0,top=0,width=550,height=400,resizable=1,scrollbars=1, status=1, toolbar=1, menubar=0');
	preWin.document.open();
	preWin.document.write(bodyTag);
	preWin.document.write(cont);
	preWin.document.close();
	preWin.document.title="Preview";
	preWin.document.charset="UTF-8";
}
function correctUrl(cont)
{
	var regExp;
	var url=location.href.substring(0,location.href.lastIndexOf("/")+1);
	cont=rCode(cont,location.href+"#","#");
	cont=rCode(cont,url,"");
	return cont;
}
function rCode(s,a,b,i){
	//s原字串，a要换掉pattern，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 FilterScript(content)
{
	content = rCode(content, 'javascript:', '<b>javascript</b> :');
	var RegExp = /<script[^>]*>(.*)<\/script>/gi;
	content = content.replace(RegExp, "<div class=HtmlCode>&lt;!-- Script 代码开始 --&gt;<br>$1<br>&lt;!-- Script 代码结束 --&gt;</div>");
	RegExp = /<P>&nbsp;<\/P>/gi;
	content = content.replace(RegExp, "");
	return content;
}

//格式化粘贴
function onPaste() {
	if (sCurrMode=="DESIGN"){
		var sHTML = GetClipboardHTML() ;
		EditCtrl.document.selection.createRange().pasteHTML(sHTML) ;
		return false;
	}else{
		EditCtrl.document.selection.createRange().pasteHTML(HTMLEncode( clipboardData.getData("Text"))) ;
		return false;
	}
}
function GetClipboardHTML() {
	var oDiv = document.getElementById("Temp_HTML")
	oDiv.innerHTML = "" ;
	
	var oTextRange = document.body.createTextRange() ;
	oTextRange.moveToElementText(oDiv) ;
	oTextRange.execCommand("Paste") ;
	
	var sData = oDiv.innerHTML ;
	oDiv.innerHTML = "" ;
	
	return sData ;
}
function HTMLEncode(text){
	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 cleanHtml()
{
	var fonts = EditCtrl.document.body.all.tags("FONT");
	var curr;
	for (var i = fonts.length - 1; i >= 0; i--) {
		curr = fonts[i];
		if (curr.style.backgroundColor == "#ffffff") curr.outerHTML = curr.innerHTML;
	}
}

//删除格式
function CleanCode()
{
	var editor = EditCtrl;
	editor.focus();
	var body = editor.document.body;
	for (var index = 0; index < body.all.length; index++) {
		tag = body.all[index];
		tag.removeAttribute("className","",0);
		tag.removeAttribute("style","",0);
	}
	var html = editor.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>");
	editor.document.body.innerHTML = html;
}

function foreColor()
{
	if (sCurrMode != "DESIGN") return;
	var arr = showModalDialog("editor/color.htm", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0");
	if (arr != null)
		_Format('ForeColor', arr);
	else
		EditCtrl.focus();
}

function backColor()
{
	if (sCurrMode != "DESIGN") return;
	var arr = showModalDialog("editor/color.htm", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0; help:0");
	if (arr != null)
		_Format('backcolor', arr);
	else
		EditCtrl.focus();
}

//打印框架页
function SelfPrint()
{
	EditCtrl.focus();
	EditCtrl.window.print();
}

//拼写检查
function ieSpellCheck() {
	//if (htmlmode) return;
	try {
		var tspell = new ActiveXObject('ieSpell.ieSpellExtension');
		tspell.CheckAllLinkedDocuments(window.document);
	} catch (err){
		if (window.confirm('进行拼写检查需要安装 ieSpell 插件，您要安装吗？')){window.open('http://www.iespell.com/download.php');};
	}
}

//判断图片是否被选中
function isImageSelected() {
	EditCtrl.focus()
	if (EditCtrl.document.selection.type == "Control") {
		var oControlRange = EditCtrl.document.selection.createRange();
		if (oControlRange(0).tagName.toUpperCase() == "IMG") {
			selectedImage = EditCtrl.document.selection.createRange()(0);
			return true;
		}
	}
}

//编辑图片
function EditImg(){
	if (isImageSelected()) {
		//sPrePos = selectedImage.style.position;
		var res = window.showModalDialog('editor/image.htm', selectedImage, 'dialogWidth: 350px; dialogHeight: 350px; center: yes; resizable: no; scroll: no; status: no;');
		if (res) selectedImage.outerHTML = res;
	}else
		alert("请选择您要修改的图片！");
}

//恢复图片
function revertImg(){
	if(isImageSelected())
		selectedImage.outerHTML = '<img src=' + selectedImage.src + ' border=0>';
	else
		alert("请选择您要修改的图片！");
}

//判断表格是否被选中
function isTableSelected(){
	EditCtrl.focus()
	if(EditCtrl.document.selection.type == "Control"){
		var oControlRange = EditCtrl.document.selection.createRange();
		if(oControlRange(0).tagName.toUpperCase() == "TABLE"){
			selectedTable = EditCtrl.document.selection.createRange()(0);
			return true;
		}	
	}
}

//编辑表格
function EditTable(sNewTable){
  if (isTableSelected() || isCursorInTableCell()) {
   res = showModalDialog('editor/edittable.htm', selectedTable, 'dialogWidth: 360px; dialogHeight: 200px; center: yes; resizable: no; scroll: no; status: no;');
   if(res){
    if(res.width)
     selectedTable.width = res.width;
    else
     selectedTable.removeAttribute('width',0);

    if(res.cellPadding)
     selectedTable.cellPadding = res.cellPadding;
    else
     selectedTable.removeAttribute('cellPadding',0);
    
    if(res.bgColor)
     selectedTable.bgColor = res.bgColor;
    else
     selectedTable.removeAttribute('bgColor',0);

    if(res.background)
     selectedTable.background = res.background;
    else
     selectedTable.removeAttribute('background',0);

	if(res.cellSpacing)
     selectedTable.cellSpacing = res.cellSpacing;
    else
     selectedTable.removeAttribute('cellSpacing',0);

	if(res.border)
     selectedTable.border = res.border;
    else
     selectedTable.removeAttribute('border',0);
   }
  }
  else {
   alert("请选择您要修改的表格！");
  }
}

//删除表格格式，剩下文本
function ClearTable(vl)
{
	if(isTableSelected() || isCursorInTableCell()){
		var tbl = "";
		for (i=0; i<selectedTable.rows.length; i++)
		{
			tbl = tbl + "<tr>"
			for (j=0; j<selectedTable.rows(i).cells.length; j++)
			{
				tbl = tbl + "<td"
				if (selectedTable.rows(i).cells(j).colSpan != 1) tbl = tbl + " colSpan=" + selectedTable.rows(i).cells(j).colSpan
				if (selectedTable.rows(i).cells(j).rowSpan != 1) tbl = tbl + " rowSpan=" + selectedTable.rows(i).cells(j).rowSpan
				if (vl == 1)
					tbl = tbl + ">" + selectedTable.rows(i).cells(j).innerText + "</td>"
				else
					tbl = tbl + ">" + selectedTable.rows(i).cells(j).innerHTML + "</td>"
			}
			tbl = tbl + "</tr>"
		}
		selectedTable.outerHTML = "<table border=1 width='100%' cellpadding=5 style='border-collapse: collapse; font-size:12px' bordercolor='#666666'>"+ tbl +"</table>"
	}
	else
	{
		alert("请选择您要整理的表格！");
	}
}

//判断单元格是否被选中
function isCursorInTableCell(){
	EditCtrl.focus();
	if(document.selection.type != "Control"){
		var elem = 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 EditCell(){
  if(isCursorInTableCell()){
   res = showModalDialog('editor/editCell.htm', selectedTD, 'dialogWidth: 360px; dialogHeight: 225px; center: yes; resizable: no; scroll: no; status: no;');
   if(res){
    selectedTD.colSpan = res.colSpan;
    selectedTD.rowSpan = res.rowSpan;
    if(res.width)
     selectedTD.width = res.width;
    else
     selectedTD.removeAttribute('width',0);

    if(res.height)
     selectedTD.height = res.height;
    else
     selectedTD.removeAttribute('height',0);
    
    if(res.bgColor)
     selectedTD.bgColor = res.bgColor;
    else
     selectedTD.removeAttribute('bgColor',0);

    if(res.background)
     selectedTD.background = res.background;
    else
     selectedTD.removeAttribute('background',0);

	if(res.align && !res.align.match(/^None$/i))
     selectedTD.align = res.align;
    else
     selectedTD.removeAttribute('align',0);

	if(res.vAlign && !res.vAlign.match(/^None$/i))
     selectedTD.vAlign = res.vAlign;
    else
     selectedTD.removeAttribute('vAlign',0);

	if(res.noWrap)
     selectedTD.noWrap = true;
    else
     selectedTD.noWrap = false;

	if(''+ res.tagName == 'TH')
     selectedTD.name = res.tagName;
     //alert('TH')
   }
  }else{
   alert("请将光标停留在需要修改的单元格中！");
  }
}

//表格：插入列
function incols(){
	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 = " "
		}	
	}
}

//表格：删除列
function delcols(){
	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)
		}
	}
}

//表格：插入行
function inrows(){
	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 = " "
		}
	}
}

//表格：删除行
function delrows(){
	if(isCursorInTableCell()){
		selectedTable.deleteRow(selectedTR.rowIndex)
	}
}

//插入数据
function insertHTML(str){
	EditCtrl.focus();
	var range = EditCtrl.document.selection.createRange();
	if (str != null){
		range.pasteHTML(str);
	}
}
function EditHTML(str){
	EditCtrl.focus();
	var range = EditCtrl.document.selection.createRange();
	if (str != null){
		range.pasteHTML(str);
	}
}

function getText()
{
	if (sCurrMode != "DESIGN") setMode('DESIGN');
	cleanHtml();
	return EditCtrl.document.body.innerText;
}

function getHTML()
{
	if (sCurrMode != "DESIGN")
		return EditCtrl.document.body.innerText;
	else
	{
		cleanHtml();
		return EditCtrl.document.body.innerHTML;
	}
}

//插入高级项目
function Advfunc(NewStr){
	if (sCurrMode!="DESIGN") return;

	switch(NewStr){
		case "nowdate":
			var d = new Date();
			insertHTML(d.toLocaleDateString());
			break;
		case "nowtime":
			var d = new Date();
			insertHTML(d.toLocaleTimeString());
			break;
		case "fieldset":
			_OpenWin("editor/fieldset.htm",'300pt','140pt');
			break;
	}
}

//保存文档
function SaveContentValue(){
	//使用相对路径处理好图像路径的内容,必须保证调用内容和本页面的级别相同
	var edtDtl;
	if (sCurrMode == "CODE"){
		edtDtl = EditCtrl.document.body.innerText;
	}else{
		edtDtl = EditCtrl.document.body.innerHTML;
	}
	var theURL = self.document.URL;
	theURL = theURL.substr(0, theURL.indexOf("?", theURL.indexOf("/", 8)))
	var urlTemp = theURL;
	var startI = 8, urlAll = "", m = 0;
	while (urlTemp.indexOf("/", startI) > 0)
	{
		urlAll = urlAll + urlTemp.substring(startI, urlTemp.indexOf("/", startI) + 1) + " "
		m = m + 1;
		urlTemp = urlTemp.substr(urlTemp.indexOf("/", startI));
		startI = 1;
	}
	urlAll = theURL.substr(0, 8) + urlAll.substr(0, urlAll.length - 1);
	var urlArray = urlAll.split(" ");
	for (i = 1; i < urlArray.length; i++)
		urlArray[i] = urlArray[i - 1] + urlArray[i];
	var re, j, jj, rplcto
	for (i = urlArray.length - 1; i >= 0; i--)
	{
		jj = urlArray.length - 1 - i;
		rplcto = "";
		for (j = 0; j <= jj; j++)
			rplcto = rplcto + "../";
		rplcto = rplcto.substr(3);
		re = new RegExp(urlArray[i], "gi");
		edtDtl = edtDtl.replace(re, rplcto);
	}
	return edtDtl;
}

var oPopupMenu = window.createPopup();
var sMenu1="<table border=0 cellpadding=0 style=\"border-collapse: collapse\" width=118 class=\"ContextMenuTable\"><tr><td><table width=\"100%\" border=0 cellspacing=1 cellpadding=0 style=\"border-collapse: collapse\">";
var sMenu2="<\/TABLE><\/td><\/tr><\/TABLE>";

function getFormatMenuRow(menu, html, image){
	var s_Disabled = "";
	//命令是否可用
	if (!EditCtrl.document.queryCommandEnabled(menu)){
		s_Disabled = "disabled";
	}
	var s_Event = "_PFormat('"+menu+"')";
	var s_Image = menu+".gif";
	if (image){
		s_Image = image;
	}
	return getMenuRow(s_Disabled, s_Event, s_Image, html)
}

function getMenuRow(s_Disabled, s_Event, s_Image, s_Html) {
	var s_MenuRow = "";
	s_MenuRow = "<tr "+s_Disabled+"><td class=MouseOut onMouseOver=this.className='MouseOver'; onMouseOut=this.className='MouseOut';";
	if (s_Disabled==""){
		s_MenuRow += " onclick=\"parent."+s_Event+";parent.oPopupMenu.hide();\"";
	}
	s_MenuRow += " height=20>"

	if (s_Image !=""){
		s_MenuRow += "&nbsp;<img border=0 src='"+s_Image+"' width=20 height=20 align=absmiddle "+s_Disabled+">&nbsp;";
	}else{
		s_MenuRow += "&nbsp;";
	}

	s_MenuRow += s_Html+"<\/td><\/tr>";
	return s_MenuRow;
}

function sizeChange(size){
	var h = parseInt(EditCtrl.document.body.offsetHeight);
	if (h + size >= 300){
		EditCtrl.frameElement.overflow = "auto";
		EditCtrl.frameElement.height = h + size;
	}
	EditCtrl.focus();
}
