/*=====================================================================
'代码版权说明
'=====================================================================
' 文件名: 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;
	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();
		 var arr=range.htmlText;
		 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,"")
		 
		 
		 range.pasteHTML(arr);
	}
	 pureText = false;
	 Composition.focus();
}

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

function myformat()
{
	if (!ValidateMode()) return;
	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();
	}
}