﻿/******************************************************************************************
©2009苏州社易资讯科技有限公司，保留所有权利。
本产品受《计算机软件保护条例》保护，未经著作权人合法授权不得用于商业用途。举报电话：0512-62621588(星期一至星期五8:30-17:30)
******************************************************************************************/
var isCompatible = (is_ie || is_moz);
if (is_ie)
{
	var browserVersion = userAgent.match(/msie (.\..)/)[1];
	isCompatible       = (browserVersion >= 6.0);
}
if (is_moz)
{
	var browserVersion = userAgent.match(/firefox\/(.\..)/)[1];
	isCompatible       = (browserVersion >= 3.0);
}

function editor(pInstanceName)
{
	if(!pInstanceName)
	{
		alert("错误：编辑器没有指定实例！");
		return;
	}
	this.InstanceName = pInstanceName;
	this.Height       = "300px";
	this.Width        = "100%";
	this.ToolbarHolder;
	this.EditorHolder;
	this.SourceHolder;
	if (isCompatible)
	{
		document.write('<div style="border:solid 1px #999999; overflow:hidden;"><div id="ToolbarHolder_' + this.InstanceName + '" class="ToolbarHolder" style="width:' + this.Width + ';"></div><div id="PreviewHolder" style="display:none; height:' + this.Height + '; width:' + this.Width + '; position:relative; overflow:auto;"><div style="padding:6px; font-size:14px; font-weight:bold; color:#999999; background-color:#FFFFEE; border-bottom:solid 1px #999999;">你正在预览&nbsp;<input type="button" value="返回" style="padding:0; text-decoration:underline; color:#0000FF; background:none; border:none;" onclick="$Id(\'EditorHolder_\' + edt.InstanceName).style.display=\'\';$Id(\'PreviewHolder\').style.display=\'none\';" /></div><div id="PreviewContent" style="padding:6px; font-size:12px; font-family:\'宋体\';"></div></div><iframe id="EditorHolder_' + this.InstanceName + '" frameborder="no" style="height:' + this.Height + '; width:' + this.Width + '; overflow:hidden;"></iframe></div>');
		this.ToolbarHolder                         = $Id("ToolbarHolder_" + this.InstanceName);
		this.EditorHolder                          = $Id("EditorHolder_" + this.InstanceName).contentWindow;
		this.SourceHolder                          = $Id(this.InstanceName);
		this.SourceHolder.style.display            = "none";
		this.EditorHolder.document.designMode      = 'On';
		this.EditorHolder.document.open();
		this.EditorHolder.document.write('<html>\n<head>\n<title></title>\n</head>\n<body style="margin:0; padding:6px; font-size:12px; font-family:\'宋体\';"></body>\n</html>');
		this.EditorHolder.document.close();
		this.EditorHolder.document.body.innerHTML  = edtCode(this.SourceHolder.value);
		this.EditorHolder.document.contentEditable = true;
		var s1 = '<div class="l"><div class="font_family"><input type="button" value="仿宋_GB2312" class="FontName" style="font-family:\'仿宋_GB2312\';" title="仿宋_GB2312" onclick="edt.execCmd(\'FontName\',\'仿宋_GB2312\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="黑体" class="FontName" style="font-family:\'黑体\';" title="黑体" onclick="edt.execCmd(\'FontName\',\'黑体\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="楷体_GB2312" class="FontName" style="font-family:\'楷体_GB2312\';" title="楷体_GB2312" onclick="edt.execCmd(\'FontName\',\'楷体_GB2312\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="宋体" class="FontName" style="font-family:\'宋体\';" title="宋体" onclick="edt.execCmd(\'FontName\',\'宋体\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="微软雅黑" class="FontName" style="font-family:\'微软雅黑\';" title="微软雅黑" onclick="edt.execCmd(\'FontName\',\'微软雅黑\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="新宋体" class="FontName" style="font-family:\'新宋体\';" title="新宋体" onclick="edt.execCmd(\'FontName\',\'新宋体\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="Arial" class="FontName" style="font-family:\'Arial\';" title="Arial" onclick="edt.execCmd(\'FontName\',\'Arial\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="Times New Roman" class="FontName" style="font-family:\'Times New Roman\';" title="Times New Roman" onclick="edt.execCmd(\'FontName\',\'Times New Roman\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="Courier New" class="FontName" style="font-family:\'Courier New\';" title="Courier New" onclick="edt.execCmd(\'FontName\',\'Courier New\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="Georgia" class="FontName" style="font-family:\'Georgia\';" title="Georgia" onclick="edt.execCmd(\'FontName\',\'Georgia\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="Verdana" class="FontName" style="font-family:\'Verdana\';" title="Verdana" onclick="edt.execCmd(\'FontName\',\'Verdana\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /><input type="button" value="Geneva" class="FontName" style="font-family:\'Geneva\';" title="Geneva" onclick="edt.execCmd(\'FontName\',\'Geneva\')" onmousemove="this.className=\'FontName FontName_MouseOver\'" onmouseout="this.className=\'FontName\'" /></div></div>';
		var s2 = '<div class="c"><div class="font_size"><input type="button" value="7号" class="FontSize" style="font-size:44px;" title="7号" onclick="edt.execCmd(\'FontSize\',7)" onmousemove="this.className=\'FontSize FontSize_MouseOver\'" onmouseout="this.className=\'FontSize\'" /><input type="button" value="6号" class="FontSize" style="font-size:30px;" title="6号" onclick="edt.execCmd(\'FontSize\',6)" onmousemove="this.className=\'FontSize FontSize_MouseOver\'" onmouseout="this.className=\'FontSize\'" /><input type="button" value="5号" class="FontSize" style="font-size:23px;" title="5号" onclick="edt.execCmd(\'FontSize\',5)" onmousemove="this.className=\'FontSize FontSize_MouseOver\'" onmouseout="this.className=\'FontSize\'" /><input type="button" value="4号" class="FontSize" style="font-size:18px;" title="4号" onclick="edt.execCmd(\'FontSize\',4)" onmousemove="this.className=\'FontSize FontSize_MouseOver\'" onmouseout="this.className=\'FontSize\'" /><input type="button" value="3号" class="FontSize" style="font-size:17px;" title="3号" onclick="edt.execCmd(\'FontSize\',3)" onmousemove="this.className=\'FontSize FontSize_MouseOver\'" onmouseout="this.className=\'FontSize\'" /><input type="button" value="2号" class="FontSize" style="font-size:13px;" title="2号" onclick="edt.execCmd(\'FontSize\',2)" onmousemove="this.className=\'FontSize FontSize_MouseOver\'" onmouseout="this.className=\'FontSize\'" /><input type="button" value="1号" class="FontSize" style="font-size:10px;" title="1号" onclick="edt.execCmd(\'FontSize\',1)" onmousemove="this.className=\'FontSize FontSize_MouseOver\'" onmouseout="this.className=\'FontSize\'" /></div></div>';
		var s3 = '<div class="r"><div class="t"><input type="button" value="" class="Bold" title="粗体" onclick="edt.execCmd(\'Bold\')" onmousemove="this.className=\'Bold Bold_MouseOver\'" onmouseout="this.className=\'Bold\'" /><input type="button" value="" class="Italic" title="斜体" onclick="edt.execCmd(\'Italic\')" onmousemove="this.className=\'Italic Italic_MouseOver\'" onmouseout="this.className=\'Italic\'" /><input type="button" value="" class="Underline" title="下划线" onclick="edt.execCmd(\'Underline\')" onmousemove="this.className=\'Underline Underline_MouseOver\'" onmouseout="this.className=\'Underline\'" /><input type="button" value="" class="StrikeThrough" title="删除线" onclick="edt.execCmd(\'StrikeThrough\')" onmousemove="this.className=\'StrikeThrough StrikeThrough_MouseOver\'" onmouseout="this.className=\'StrikeThrough\'" /><input type="button" value="" class="Superscript" title="上标" onclick="edt.execCmd(\'Superscript\')" onmousemove="this.className=\'Superscript Superscript_MouseOver\'" onmouseout="this.className=\'Superscript\'" /><input type="button" value="" class="Subscript" title="下标" onclick="edt.execCmd(\'Subscript\')" onmousemove="this.className=\'Subscript Subscript_MouseOver\'" onmouseout="this.className=\'Subscript\'" /><input type="button" value="" class="ForeColor" title="标红" onclick="edt.execCmd(\'ForeColor\',\'Red\')" onmousemove="this.className=\'ForeColor ForeColor_MouseOver\'" onmouseout="this.className=\'ForeColor\'" /><input type="button" value="" class="RemoveFormat" title="移除格式" onclick="edt.execCmd(\'RemoveFormat\')" onmousemove="this.className=\'RemoveFormat RemoveFormat_MouseOver\'" onmouseout="this.className=\'RemoveFormat\'" /></div>';
		var s4 = '<div class="m"><input type="button" value="" class="Indent" title="缩进" onclick="edt.execCmd(\'Indent\')" onmousemove="this.className=\'Indent Indent_MouseOver\'" onmouseout="this.className=\'Indent\'" /><input type="button" value="" class="Outdent" title="减少缩进" onclick="edt.execCmd(\'Outdent\')" onmousemove="this.className=\'Outdent Outdent_MouseOver\'" onmouseout="this.className=\'Outdent\'" /><input type="button" value="" class="InsertUnorderedList" title="项目列表" onclick="edt.execCmd(\'InsertUnorderedList\')" onmousemove="this.className=\'InsertUnorderedList InsertUnorderedList_MouseOver\'" onmouseout="this.className=\'InsertUnorderedList\'" /><input type="button" value="" class="InsertOrderedList" title="编号列表" onclick="edt.execCmd(\'InsertOrderedList\')" onmousemove="this.className=\'InsertOrderedList InsertOrderedList_MouseOver\'" onmouseout="this.className=\'InsertOrderedList\'" /><input type="button" value="" class="JustifyLeft" title="居左" onclick="edt.execCmd(\'JustifyLeft\')" onmousemove="this.className=\'JustifyLeft JustifyLeft_MouseOver\'" onmouseout="this.className=\'JustifyLeft\'" /><input type="button" value="" class="JustifyCenter" title="居中" onclick="edt.execCmd(\'JustifyCenter\')" onmousemove="this.className=\'JustifyCenter JustifyCenter_MouseOver\'" onmouseout="this.className=\'JustifyCenter\'" /><input type="button" value="" class="JustifyRight" title="居右" onclick="edt.execCmd(\'JustifyRight\')" onmousemove="this.className=\'JustifyRight JustifyRight_MouseOver\'" onmouseout="this.className=\'JustifyRight\'" /></div>';
		var s5 = '<div class="b"><input type="button" value="" class="CreateLink" title="插入超链接" onclick="edt.execCmd(\'CreateLink\',null,true)" onmousemove="this.className=\'CreateLink CreateLink_MouseOver\'" onmouseout="this.className=\'CreateLink\'" /><input type="button" value="" class="Unlink" title="移除超链接" onclick="edt.execCmd(\'Unlink\')" onmousemove="this.className=\'Unlink Unlink_MouseOver\'" onmouseout="this.className=\'Unlink\'" /><input type="button" value="" class="InsertImage" title="插入图片" onclick="edt.execCmd(\'InsertImage\',null,true)" onmousemove="this.className=\'InsertImage InsertImage_MouseOver\'" onmouseout="this.className=\'InsertImage\'" /><input type="button" value="" class="InsertFlash" title="插入Flash" onclick="alert(\'错误：功能研发中！\');" onmousemove="this.className=\'InsertFlash InsertFlash_MouseOver\'" onmouseout="this.className=\'InsertFlash\'" /><input type="button" value="" class="InsertTable" title="插入表格" onclick="alert(\'错误：功能研发中！\');" onmousemove="this.className=\'InsertTable InsertTable_MouseOver\'" onmouseout="this.className=\'InsertTable\'" /><input type="button" value="" class="Preview" title="预览" onclick="$Id(\'PreviewContent\').innerHTML=edt.EditorHolder.document.body.innerHTML;$Id(\'EditorHolder_\' + edt.InstanceName).style.display=\'none\';$Id(\'PreviewHolder\').style.display=\'\';" onmousemove="this.className=\'Preview Preview_MouseOver\'" onmouseout="this.className=\'Preview\'" /></div></div>';
		this.ToolbarHolder.innerHTML = s1 + s2 + s3 + s4 +s5;
	}
	else
	{
		alert("错误：编辑器与浏览器不兼容！");
		return;
	}
}
editor.prototype.execCmd = function(pCmd,pValue,pAsk)
{
	if (pAsk==true&&is_moz)
	{
		alert("错误：浏览器不支持！");
		return;
	}
	this.EditorHolder.focus();
	if (pAsk==true)
	{
		this.EditorHolder.document.execCommand(pCmd, true, pValue);
	}
	else
	{
		this.EditorHolder.document.execCommand(pCmd, false, pValue);
	}
	this.SourceHolder.value = sourceCode(this.EditorHolder.document.body.innerHTML);
}

function sourceCode(txt)
{
	var edtTxt=txt;
	while(edtTxt.indexOf("<")>=0)
	{
		edtTxt=edtTxt.replace("<","&lt;");
	}
	while(edtTxt.indexOf(">")>=0)
	{
		edtTxt=edtTxt.replace(">","&gt;");
	}
	while(edtTxt.indexOf(" ")>=0)
	{
		edtTxt=edtTxt.replace(" ","&nbsp;");
	}
	return edtTxt;
}

function edtCode(txt)
{
	var edtTxt=txt;
	while(edtTxt.indexOf("&lt;")>=0)
	{
		edtTxt=edtTxt.replace("&lt;","<");
	}
	while(edtTxt.indexOf("&gt;")>=0)
	{
		edtTxt=edtTxt.replace("&gt;",">");
	}
	while(edtTxt.indexOf("&nbsp;")>=0)
	{
		edtTxt=edtTxt.replace("&nbsp;"," ");
	}
	return edtTxt;
}
