﻿var sHeader='<meta http-equiv="Content-Language" content="zh-cn">' +
            '<meta http-equiv="Content-Type" content="text/html; charset=gb2312">' +
            '<style>' +
            'body{margin:0px;scrollbar-face-color:buttonface;scrollbar-highlight-color:buttonface;scrollbar-shadow-color:buttonface;scrollbar-3dlight-color:buttonhighlight;scrollbar-arrow-color:#797979;scrollbar-track-color:#EEEEEE;scrollbar-darkshadow-color:buttonshadow}' +
            'body,a,table,div,span,td,th,input,select{font:9pt;font-family: "Verdana", "新细明体", Verdana, Arial, Helvetica, sans-serif;}' +
            '</style>' +
            '<body topmargin="0"  bgcolor="#DAE7EF" leftmargin="0" onConTextMenu="event.returnValue=false;">';
var bMode=true,sel=null

function EditLoad(data) {
  sHeader="";
  if (data!=null) data=data.replace(/&amp;/ig,'&');
  else data='';
  bLoad=true;
  Dvbbs_Composition.document.designMode="On";
  idEdit = Dvbbs_Composition
  idEdit.document.open()
  idEdit.document.write(sHeader+data)
  idEdit.document.close()
  idEdit.focus();
}

function Error() {
  if (bMode) return true;
  alert("请取消“查看源代码”选项，才能使用系统编辑功能!");
  idEdit.focus();
  return false;
}

function format(what,opt) {
  if (!Error()) return;
  if (opt=="removeFormat"){
    what=opt;opt=null
  }
  if (opt==null) idEdit.document.execCommand(what)
  else           idEdit.document.execCommand(what,"",opt)
  idEdit.focus()
  sel=null
}

function getEl(sTag,start) {
  while ((start!=null) && (start.tagName!=sTag))
    start = start.parentElement
  return start
}

function create(what) {
  if (!Error()) return;
  idEdit.document.execCommand(what, true);
  idEdit.focus()
}

function specialtype(Mark){
  if (!Error()) return;
  var sel,RangeType
  sel = idEdit.document.selection.createRange();
  RangeType = idEdit.document.selection.type;
  if (RangeType == "Text"){
    sel.pasteHTML("<" + Mark + ">" + sel.text + "</" + Mark + ">");
    sel.select();
  }
  idEdit.focus();
}


//***************************************************
function inphoto(url){
  if (!Error()) return;
  idEdit.focus();
  if (url!="") {
    var sel,RangeType;
    var ispic = url.split("|");
    sel = idEdit.document.selection.createRange();
    if (ispic[1] == "1"){
    	//sel.pasteHTML("<center><img src=\"/info/pic.asp?id=" + url + "\"></center>");
    	sel.pasteHTML("<img src=\"pic.asp?id=" + ispic[0] + "\" border=0 onLoad=\"if( 500<this.width){ this.height=this.height*500/this.width; this.width=500;}\">");
    }else{
    	sel.pasteHTML("<a href=\"pic.asp?id=" + ispic[0] + "\" title=\"点击下载\"><img src=\"files/" + ispic[2] + ".gif\" border=0 hspace=10>" + ispic[3] + "</a>");
    }
    sel.select();
  }
  idEdit.focus();
}

//***************************************************
//首行缩进
function Dvbbs_forShsj(){
	if (document.all.bH.checked==true) {
	 alert('请取消"查看源代码"才能编辑!');
	 return false;
 }
 else {
		idEdit.focus();
		sel = idEdit.document.selection.createRange();
		sel.pasteHTML("<i>　　</i>");
		sel.select();
		idEdit.focus();
 }
}


//***************************************************
function clearEdit(){
  setMode(false)
  idEdit.document.open();
  idEdit.document.write(sHeader)
  idEdit.document.close();
}

function selectAll(){
  setMode(false)
  idEdit.document.all[0].innerHTML.select();
}

//**************************************************
function setMode() {
	if (document.all.bH.checked==true)
	{
		NewMode=false;
		document.all.bH.checked=true;
	}
	else
	{
		NewMode=true;
		document.all.bH.checked=false;
	}
  //showContent(false,NewMode)
  if (NewMode!=bMode) {
    if (NewMode) {
      //var sContents=document.all.SaveContent.value   //不能更改原内容
      var sContents=idEdit.document.body.innerText
      idEdit.document.body.innerHTML = sContents;
    }
  else {
    document.all.SaveContent.value=ReplaceUrl(idEdit.document.all.tags("HTML")[0].innerHTML);
    idEdit.document.body.innerText=ReplaceUrl(getPureHtml(idEdit.document.body.innerHTML));
  }
  bMode=NewMode
	for (var i=0;i<htmlOnly.children.length;i++)
		htmlOnly.children[i].disabled=(!bMode)
	}
  idEdit.focus()
}

function ReplaceUrl(str){
    var sFld=""
    var sFlp=window.location.pathname;
    sFlp=sFlp.replace('\\','/');
    sFlp=sFlp.replace('//','/');
    if(sFlp.substring(0,1)=='/'){
        sFlp=sFlp.substring(1);
    }
    var As=sFlp.split('/');
    var rs='';
    if(As.length>1){
       //此处不处理http://demain/folder/这样的路径，只处理带页面名称的路径
      for(var i=0;i<As.length-1;i++){
         rs=rs+'/'+As[i];
      }
    }
	var re = new RegExp("(=)(\"|')?(http://"+location.host+rs+"/)","gi"); 
	str=str.replace(re,'$1$2');
	str=str.replace("=\"http://"+location.host + rs+"/","=\"");
	return str;
}

function showContent(show,NewMode){
  document.all.bW.checked=false;
  document.all.bH.checked=false;
  document.all.bE.checked=false;
  document.all.bF.checked=false;
  document.all.EditContent.style.display='';
  document.all.SaveContent.style.display='';
  if (show) {
    document.all.EditContent.style.display='none';
    if (NewMode) document.all.bE.checked=true;
    else         document.all.bF.checked=true;
  }
  else {
    document.all.SaveContent.style.display='none';
    if (NewMode) document.all.bH.checked=true;
    else         document.all.bH.checked=false;
  }
}

function getPureHtml(){
  var str = idEdit.document.body.innerHTML;
  return str;
}

function rCode(s,a,b){
  var r = new RegExp(a,"gi");
  return s.replace(r,b); 
}

function setSave(NewMode){
  setMode(true)
  showContent(true,NewMode)
  if (NewMode) {
    var sContents=getPureHtml(idEdit.document.body.innerHTML);
    sContents=sContents.replace(/<(\/|)(td|th|tr|tbody|table)(.[^\<]*|)>/ig,'\[$1$2]');
    sContents=sContents.replace(/\[(td|th)]/ig,'    \[$1]');
    sContents=sContents.replace(/\[tr]/ig,'  \[TR]');
    sContents=sContents.replace(/\[tbody]\r\n  \[TR]/ig,'\[tbody]\n  \[TR bgcolor="efefef"]');
    sContents=sContents.replace(/\[table]/ig,'\[table width="1" border="0" cellpadding="1" cellspacing="1" style="border:1 ridge #eaeaea" align=right]');
    var aryCode0 = new Array("<strong>","[b]","</strong>","[/b]","<p","[p","</p>","","<a href=","[url=","</a>","[/url]");
    var aryCode1 = new Array("<em>","[i]","</em>","[/i]","<u>","[u]","</u>","[/u]","<ul>","[list]","</ul>","[/list]","<ol>","[list=1]","</ol>","[/list]");
    var aryCode2 = new Array("<li>","[*]","</li>","","<font size=","[size=","<font color=","[color=","<font face=","[face=");
    var aryCode9 = new Array(">","]","<","[","</","[/");
    var aryCode = aryCode0.concat(aryCode1).concat(aryCode2).concat(aryCode9);
    for (var i=0;i<aryCode.length;i+=2){
      sContents=rCode(sContents,aryCode[i],aryCode[i+1]);	
    }
    sContents=sContents.replace(/\[p([^\]]*|)]/ig,'[BR]');
  } else {
    var sContents=idEdit.document.all[0].innerText
    sContents=sContents.replace(/\r\n/ig,'[BR]\n');
  }
  sContents=sContents.replace(/\ \[BR]/ig,'[BR]');
  document.all.SaveContent.value=sContents.replace(/([0-9a-zA-Z@]{32})/ig,"$1 ");
}

// 将选中的内容设为关键字
function SetKeywords(){
	if (document.all.bH.checked==true) {
	 alert('请取消"查看源代码"才能编辑!');
	 return false;
	}
	else {
		idEdit.focus();
		oTextRange=idEdit.document.selection.createRange();
		var putObj=GetEle('keyword');
		if (oTextRange!=null && oTextRange.text.length>0) {
			//alert(oTextRange.text);
			var tempstr = putObj.value;
			if (tempstr.length>0)
				tempstr = tempstr + "|"
			putObj.value = tempstr + oTextRange.text;
		}
		else {
			alert('请选中要设为关键词的文本!');
		}
		idEdit.focus();
 }
}
