/*=====================================================================
'代码版权说明
'=====================================================================
' 文件名: editdata.js
*/
//初始化编辑器
var edit;
var RangeType;
var bLoad=false;
var pureText=true;
var bTextMode=false;
var bIsIE5 = navigator.userAgent.indexOf("IE 5")> -1;
var content=typeof(parent.EditContent)=="undefined"?"":parent.EditContent;

//初始化完毕
function SetEdit()
{
	try{
		Composition.document.body.contentEditable="true";
		InitDocument();
	}catch(E){
		setTimeout("SetEdit();",1000);
	}
}
SetEdit();

function GetContent()
{   
    var strText;
	copyContent();
	
	if(bTextMode)
	{	strText=Composition.document.body.innerText;	}
	else
	{	strText=Composition.document.body.innerHTML;}
	if (strText.indexOf("lang=EN-US")!=-1 & $("autoClearWord").checked){
		strText=clearWord(strText);
	}
	return strText;
  		
}

function copyContent(){
	formatExecute('SelectAll');
	formatExecute('copy');
}

function SetContent(str)
{
	Composition.document.body.innerHTML=str;
}

function AppendContent(str)
{
	str=str.replace(/\[cc\](.*?)\[\/cc\]/ig,"<embed allowScriptAccess=always src=http://union.bokecc.com/$1 width=\"438\" height=\"387\"></embed>")
	Composition.document.body.innerHTML+=str;
}
//======================================================================
function ValidateMode()
{
  if (!	bTextMode) return true;
  alert("请取消“使用 HTML 语法书写”选项再使用系统编辑功能!");
  Composition.focus();
  return false;
}

function formatExecute(CommandText,Option)
{
	 Composition.focus();
	 if (Option=="removeFormat")
	 {
		CommandText=Option;
		Option=null;
		
		
		//range.pasteHTML(arr);
		
	 }
	 
	 if (Option==null) Composition.document.execCommand(CommandText);
	 else Composition.document.execCommand(CommandText,"",Option);
	if (CommandText=="RemoveFormat"){
		 var range = Composition.document.selection.createRange();
		 arr=range.htmlText;
		 //var arr=Composition.document.body.innerHTML;
		// alert(arr)
		 //arr=clearWord(arr)
		 //Composition.document.body.innerHTML=arr;
		 range.pasteHTML(arr);
	}
	 pureText = false;
	 Composition.focus();
}

function clearWord(arr){  //清除WORD中的垃圾字符
//alert("WORD清除开始")
var l1=arr.length;
arr=arr.replace(/<\!--[\s\S]*?-->/g,'');
arr=arr.replace(/<o:p>\s*<\/o:p>/g,'');
arr=arr.replace(/<o:p>[\s\S]*?<\/o:p>/g,'&nbsp;');
arr=arr.replace(/\s*mso-[^:]+:[^;"]+;?/gi,'');
arr=arr.replace(/\s*MARGIN: 0(?:cm|in) 0(?:cm|in) 0pt\s*;/gi,'');
arr=arr.replace(/\s*MARGIN: 0(?:cm|in) 0(?:cm|in) 0pt\s*"/gi,'"');
arr=arr.replace(/\s*TEXT-INDENT: 0cm\s*;/gi,'');
arr=arr.replace(/\s*TEXT-INDENT: 0cm\s*"/gi,'"');
arr=arr.replace(/\s*TEXT-ALIGN: [^\s;]+;?"/gi,'"');
arr=arr.replace(/\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi,'"');
arr=arr.replace(/\s*FONT-VARIANT: [^\s;]+;?"/gi,'"');
arr=arr.replace(/\s*tab-stops:[^;"]*;?/gi,'');
arr=arr.replace(/\s*tab-stops:[^"]*/gi,'');
//if(d){
//arr=arr.replace(/\s*face="[^"]*"/gi,'');
//arr=arr.replace(/\s*face=[^ >]*/gi,'');
//arr=arr.replace(/\s*FONT-FAMILY:[^;"]*;?/gi,'');
//}
arr=arr.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi,'<$1$3');
//if(e){
//arr=arr.replace(/<(\w[^>]*) style="([^\"]*)"([^>]*)/gi,'<$1$3');
//}

arr=arr.replace(/<STYLE[^>]*>[\s\S]*?<\/STYLE[^>]*>/gi,'');
arr=arr.replace(/<(?:META|LINK)[^>]*>\s*/gi,'');
arr=arr.replace(/\s*style="\s*"/gi,'');
arr=arr.replace(/<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi,'&nbsp;');
arr=arr.replace(/<SPAN\s*[^>]*><\/SPAN>/gi,'');
arr=arr.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi,'<$1$3');
arr=arr.replace(/<SPAN\s*>([\s\S]*?)<\/SPAN>/gi,'$1');
arr=arr.replace(/<FONT\s*>([\s\S]*?)<\/FONT>/gi,'$1');
arr=arr.replace(/<\\?\?xml[^>]*>/gi,'');
//if(f)
//这里用于清CSS
//arr=arr.replace(/<v:shape[\s\S]*?style="[\s\S]*?width:([\s\S]*?)pt[\s\S]*?height:([\s\S]*?)pt[\s\S]*?"[\s\S]*?(src="[\s\S]*?")[\s\S]*?<\/v:shape>/gi,function(h,i,j,k){return '<img width="'+i*4/3+'"  height=" '+j*4/3+'" '+k+' />';});


arr=arr.replace(/<w:[^>]*>[\s\S]*?<\/w:[^>]*>/gi,'');
arr=arr.replace(/<\/?\w+:[^>]*>/gi,'');
arr=arr.replace(/<(U|I|STRIKE)>&nbsp;<\/\1>/g,'&nbsp;');

arr=arr.replace(/<H\d>\s*<\/H\d>/gi,'');
arr=arr.replace(/<(\w+)[^>]*\sstyle="[^"]*DISPLAY\s?:\s?none[\s\S]*?<\/\1>/ig,'');
arr=arr.replace(/<(\w[^>]*) language=([^ |>]*)([^>]*)/gi,'<$1$3');
arr=arr.replace(/<(\w[^>]*) onmouseover="([^\"]*)"([^>]*)/gi,'<$1$3');
arr=arr.replace(/<(\w[^>]*) onmouseout="([^\"]*)"([^>]*)/gi,'<$1$3');
//if(b.config.pasteFromWordKeepsStructure){\
//arr=arr.replace(/<H(\d)([^>]*)>/gi,'<h$1>');
//arr=arr.replace(/<(H\d)><FONT[^>]*>([\s\S]*?)<\/FONT><\/\1>/gi,'<$1>$2</$1>');
//arr=arr.replace(/<(H\d)><EM>([\s\S]*?)<\/EM><\/\1>/gi,'<$1>$2</$1>');
//}else{
arr=arr.replace(/<H1([^>]*)>/gi,'<div$1><b><font size="6">');
arr=arr.replace(/<H2([^>]*)>/gi,'<div$1><b><font size="5">');
arr=arr.replace(/<H3([^>]*)>/gi,'<div$1><b><font size="4">');
arr=arr.replace(/<H4([^>]*)>/gi,'<div$1><b><font size="3">');
arr=arr.replace(/<H5([^>]*)>/gi,'<div$1><b><font size="2">');
arr=arr.replace(/<H6([^>]*)>/gi,'<div$1><b><font size="1">');
arr=arr.replace(/<\/H\d>/gi,'</font></b></div>');
var g=new RegExp('(<P)([^>]*>[\\s\\S]*?)(</P>)','gi');
arr=arr.replace(g,'<div$2</div>');
arr=arr.replace(/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g,'');
arr=arr.replace(/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g,'');
arr=arr.replace(/<([^\s>]+)(\s[^>]*)?>\s*<\/\1>/g,'');






/*		arr=arr.replace(/style="[^"]*"/g,"");
		 arr=arr.replace(/style='[^']*'/g,"");
		 arr=arr.replace(/<SPAN[^>]*>/g,"");
		 arr=arr.replace(/<\/SPAN>/g,"");
		 arr=arr.replace(/class=MsoNormal/g,"");
		 arr=arr.replace(/<o:p>[^\n]*<\/o:p>/g,"");
		 arr=arr.replace(/<v:[^>]*>/g,"");
		 arr=arr.replace(/<\/v:[^\n]*>/g,"");
		 arr=arr.replace(/<script[^>]*>/g,"")
		 arr=arr.replace(/<SCRIPT[^>]*>/g,"")
		 arr=arr.replace(/<\/SCRIPT>/g,"")
		 arr=arr.replace(/<\/script>/g,"")
		 
*/		 
	var l2=arr.length;
	//alert(l1+"|"+l2)
	return arr;
}

function format(CommandText,Option)
{
	 if (!ValidateMode()) return;
	 formatExecute(CommandText,Option);
}

function myformat()
{
	if (!ValidateMode()) return;
	
	  Composition.focus();
	  var range = Composition.document.selection.createRange();
	  var arr="<HR id=[PAGE]>";
	  range.pasteHTML(arr);
	
	
	
	//Composition.document.execCommand("InsertHorizontalRule","dddd","[PAGE]");
}


function setMode(newMode)
{
	 bTextMode = newMode;
	 var cont;
	 if (bTextMode) {
		cont=Composition.document.body.innerHTML;
		Composition.document.body.innerText=cont;
	 } else {
		cont=Composition.document.body.innerText;
		Composition.document.body.innerHTML=cont;
	 }
	 Composition.focus();
}

function getElement(sTag,start)
{
	while	((start!=null) && (start.tagName!=sTag)) start = start.parentElement;
	return start;
}

function UserDialog(what)
{
	if (!ValidateMode()) return;
	Composition.document.execCommand(what, true);
	pureText = false;
	Composition.focus();
}

function foreColor()
{
	if (!ValidateMode())	return;
	var arr = showModalDialog("palette.html", "", "dialogWidth:350px; dialogHeight:280px; toolbar=no;location=no;directories=no;status=no;menubar=no;scroll=no;resizable=0;help=0");
	if (arr != null) format('forecolor', arr);
	else Composition.focus();
}


function fortable()
{
	if (!ValidateMode())	return;
	var arr = showModalDialog("table.html", "", "dialogWidth:20.5em; dialogHeight:16.5em; status:0");
	  
	if (arr != null){
		var ss;
		ss=arr.split("*")
		row=ss[0];
		col=ss[1];
		bord=ss[2]
		colo=ss[3]
		var string;
		string="<table width='100%' border="+ss[2]+">";
		for(i=1;i<=row;i++){
			string=string+"<tr>";
			for(j=1;j<=col;j++){
				string=string+"<td  bgcolor="+colo+"></td>";
			}
			string=string+"</tr>";
		}
		string=string+"</table>";
		content=Composition.document.body.innerHTML;
		content=content+string;
		Composition.document.body.innerHTML=content;
	}
	else Composition.focus();
}


function getText()
{
	if (bTextMode)
		return Composition.document.body.innerText;
	else
	{
		return Composition.document.body.innerHTML;
	}
}

function putText(str)
{
	if (bTextMode)
		Composition.document.body.innerText = str;
	else
		Composition.document.body.innerHTML = str;
}

function InitDocument()
{
	bLoad=true;
	putText(content);
}

function doSelectClick(str, el) {
	var Index = el.selectedIndex;
	if (Index != 0){
		el.selectedIndex = 0;
		if (el.id == "specialtype")
			specialtype(el.options[Index].value);
		else
			format(str,el.options[Index].value);
	}
}

function specialtype(Mark){
	var strHTML;
	if (bIsIE5) selectRange();	
	if (RangeType == "Text"){
		strHTML = "<" + Mark + ">" + edit.text + "</" + Mark + ">"; 
		edit.pasteHTML(strHTML);
		Composition.focus();
		edit.select();
	}		
}

function selectRange(){
	edit = Composition.document.selection.createRange();
	RangeType =  Composition.document.selection.type;
}


function Insermytable()
{
	  if (!	ValidateMode())	return;
	  Composition.focus();
	  var range = Composition.document.selection.createRange();
	  var arr = showModalDialog("table.htm",window, "dialogWidth:300pt;dialogHeight:236pt;help:0;status:0");
	  Composition.focus();
}


function Inserflash()
{
	  if (!	ValidateMode())	return;
	  Composition.focus();
	  var range = Composition.document.selection.createRange();
	  var arr = showModalDialog("flash.htm", "", "dialogWidth:300pt;dialogHeight:125pt;help:0;status:0");
	
	  if (arr != null){
		range.pasteHTML(arr);
	  }
	  Composition.focus();
}


function Insermenu(id)
{
	  if (!	ValidateMode())	return;
	  Composition.focus();
	  var range = Composition.document.selection.createRange();
	  var ran = Composition.document.selection.createRange("").text;
	  var arr = showModalDialog("insmenu.htm?id="+id, "", "dialogWidth:450pt;dialogHeight:186pt;help:0;status:0");
	
	  if (arr != null){
		range.pasteHTML(arr);
	  }
	  Composition.focus();
}

function Insertlr(filename,width,whei,myid){
	  if (!	ValidateMode())	return;
	  Composition.focus();
	  var range = Composition.document.selection.createRange();
	  var arr = showModalDialog(filename+"?temp="+create_id(), window, "dialogWidth:"+width+"pt;dialogHeight:"+whei+"pt;toolbar=no;location=no;directories=no;status=no;menubar=no;scroll=no;resizable=0;help=0;");
	  if (arr != null){	
		if (filename.toLowerCase()=="../sysupfile/selectfile.asp"){
			var fen=exName(arr)
			if (fen=="bmp" ||fen=="jpg" ||fen=="gif" ||fen=="tif" ){
			 arr="<img src='"+arr+"'>"
			}else{
				 arr="<a href='"+arr+"'>"+fen+"格式附件</a>"
			}
		}
		range.pasteHTML(arr);
	  }
	  Composition.focus();
}

function  exName(fName){  //取文件的扩展名
     var str=fName.lastIndexOf(".")
     if (str<=0){
        return ""
     }else{
         var exn=fName.substr(str+1)
         exn=exn.toLowerCase()
         return exn
     }
}
    

function create_id(){  //生成对象的ID
   var objdate=new Date()
   var o_datetime
   o_datetime=""
   o_datetime=o_datetime+objdate.getHours()
   o_datetime=o_datetime+objdate.getMinutes()
   o_datetime=o_datetime+objdate.getSeconds()
   var o_r=Math.random()
   o_r=o_datetime+parseInt(o_r*1000)
   return o_r
}

function PasteText(){
   if (! ValidateMode()) return;
	 Composition.focus();
         var range = Composition.document.selection.createRange();
	     var sText = HTMLEncode( clipboardData.getData("Text") ) ;
        if (sText != null){
		 	range.pasteHTML(sText);
         }
	Composition.focus();
}


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 addSmile(){
	if(!ValidateMode()) return;
	Composition.focus();
	var range=Composition.document.selection.createRange();
	var reVal=showModalDialog("smile.htm?temp="+create_id(), window, "dialogWidth:200px;dialogHeight:230px;toolbar=no;location=no;directories=no;status=no;menubar=no;scroll=no;resizable=0;help=0;");
	if (reVal!=null){
		reVal="<img src='"+reVal+"'border=0>";
		try{
				range.pasteHTML(reVal);
		}catch(E){
				
		}
	}else{
		Composition.focus();
	}
}

function IsSelectedObject()
{
	var theObj=Composition.document.selection.createRange();
	if(typeof(theObj.text)=="undefined")
		return true;
	
	if(theObj.text.length>0)
		return true;
	else
		return false;
}

function SetImgAlign(align)
{
	PopupPanel.EventTarget.setAttribute("align",align);
}


//查找替换字符串 2007-07-06新增
function replaceStr(){
	if(!ValidateMode()) return;
	Composition.focus();
	var range=Composition.document.selection.createRange();
	var reVal=showModalDialog("replace.htm", window, "dialogWidth:320px;dialogHeight:165px;toolbar=no;location=no;directories=no;status=no;menubar=no;scroll=no;resizable=0;help=0;");
	if (reVal!=null){
		reVal="<img src='"+reVal+"'border=0>";
		try{
				range.pasteHTML(reVal);
		}catch(E){
				
		}
	}else{
		Composition.focus();
	}
}