SEP_PADDING = 5
HANDLE_PADDING = 7
var bodyID,htmlableID;
initvar();
var yToolbars = new Array();
var YInitialized = false;

function initvar()
{
  var q=window.location.href;
  var a;
  q=q.substr(q.indexOf("?")+1);
  q=q.split("&");
  a=q[0].split("=");
  bodyID=a[1];
  a=q[1].split("=");
  htmlableID=a[1];
}

function document.onreadystatechange()
{
  if (YInitialized) return;
  YInitialized = true;
  var i, s, curr;
  for (i=0; i<document.body.all.length;  i++)
  {
    curr=document.body.all[i];
    if (curr.className == "yToolbar")
    {
      InitTB(curr);
      yToolbars[yToolbars.length] = curr;
    }
  }
  DoLayout();
  window.onresize = DoLayout;
  Composition.document.open()
  Composition.document.write("<head><style type=\"text/css\">body {font-size: 10pt}</style><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"></head><BODY bgcolor=\"#FFFFFF\" MONOSPACE></body>");
  Composition.document.close()
  Composition.document.designMode="On"
}

function InitBtn(btn)
{
  btn.onmouseover = BtnMouseOver;
  btn.onmouseout = BtnMouseOut;
  btn.onmousedown = BtnMouseDown;
  btn.onmouseup  = BtnMouseUp;
  btn.ondragstart = YCancelEvent;
  btn.onselectstart = YCancelEvent;
  btn.onselect = YCancelEvent;
  btn.YUSERONCLICK = btn.onclick;
  btn.onclick =  YCancelEvent;
  btn.YINITIALIZED = true;
  return true;
}

function InitTB(y)
{
  y.TBWidth = 0;
  if (!  PopulateTB(y)) return false;
  y.style.posWidth = y.TBWidth;
  return true;
}


function YCancelEvent()
{
  event.returnValue=false;
  event.cancelBubble=true;
  return false;
}

function BtnMouseOver()
{
  if (event.srcElement.tagName != "IMG") return  false;
  var image = event.srcElement;
  var element =  image.parentElement;
  if (image.className == "Ico")  element.className = "BtnMouseOverUp";
  else if (image.className == "IcoDown") element.className = "BtnMouseOverDown";
  event.cancelBubble = true;
}

function BtnMouseOut()
{
  if (event.srcElement.tagName != "IMG") {
    event.cancelBubble = true;
    return false;
  }
  var image = event.srcElement;
  var element =  image.parentElement;
  yRaisedElement = null;
  element.className = "Btn";
  image.className = "Ico";
  event.cancelBubble = true;
}

function BtnMouseDown()
{
  if (event.srcElement.tagName != "IMG") {
    event.cancelBubble = true;
    event.returnValue=false;
    return false;
  }
  var image = event.srcElement;
  var element =  image.parentElement;
  element.className = "BtnMouseOverDown";
  image.className = "IcoDown";
  event.cancelBubble = true;
  event.returnValue=false;
  return false;
}

function BtnMouseUp()
{
  if (event.srcElement.tagName != "IMG") {
    event.cancelBubble = true;
    return false;
  }
  var image = event.srcElement;
  var element =  image.parentElement;
  if (element.YUSERONCLICK) eval(element.YUSERONCLICK +  "anonymous()");
  element.className = "BtnMouseOverUp";
  image.className = "Ico";
  event.cancelBubble = true;
  return false;
}

function PopulateTB(y)
{
  var i, elements, element;
  elements = y.children;
  for (i=0; i<elements.length; i++) {
    element = elements[i];
    if (element.tagName  == "SCRIPT" || element.tagName == "!") continue;
    switch (element.className) {
    case "Btn":
      if (element.YINITIALIZED == null)  {
    if (! InitBtn(element))
      return false;
      }
      element.style.posLeft = y.TBWidth;
      y.TBWidth  += element.offsetWidth + 1;
      break;
    case "TBGen":
      element.style.posLeft = y.TBWidth;
      y.TBWidth  += element.offsetWidth + 1;
      break;
    case "TBSep":
      element.style.posLeft = y.TBWidth  + 2;
      y.TBWidth  += SEP_PADDING;
      break;
    case "TBHandle":
      element.style.posLeft = 2;
      y.TBWidth  += element.offsetWidth + HANDLE_PADDING;
      break;
    default:
      return false;
    }
  }
  y.TBWidth += 1;
  return true;
}

function DebugObject(obj)
{
  var msg = "";
  for (var i in  TB) {
    ans=prompt(i+"="+TB[i]+"\n");
    if (! ans) break;
  }
}

function LayoutTBs()
{
  NumTBs = yToolbars.length;
  if (NumTBs ==  0) return;
  var i;
  var ScrWid = (document.body.offsetWidth) - 6;
  var TotalLen = ScrWid;
  for (i = 0 ; i < NumTBs ; i++) {
    TB = yToolbars[i];
    if (TB.TBWidth > TotalLen) TotalLen  = TB.TBWidth;
  }
  var PrevTB;
  var LastStart  = 0;
  var RelTop = 0;
  var LastWid, CurrWid;
  var TB = yToolbars[0];
  TB.style.posTop = 0;
  TB.style.posLeft = 0;
  var Start = TB.TBWidth;
  for (i = 1 ; i < yToolbars.length ; i++) {
    PrevTB = TB;
    TB = yToolbars[i];
    CurrWid = TB.TBWidth;
    if ((Start + CurrWid) > ScrWid) {
      Start = 0;
      LastWid =  TotalLen - LastStart;
    }
    else {
      LastWid =  PrevTB.TBWidth;
      RelTop -=  TB.offsetHeight;
    }
    TB.style.posTop = RelTop;
    TB.style.posLeft = Start;
    PrevTB.style.width = LastWid;
    LastStart =  Start;
    Start += CurrWid;
  }
  TB.style.width = TotalLen - LastStart;
  i--;
  TB = yToolbars[i];
  var TBInd = TB.sourceIndex;
  var A  = TB.document.all;
  var item;
  for (i in A) {
    item = A.item(i);
    if (! item)  continue;
    if (! item.style) continue;
    if (item.sourceIndex <= TBInd) continue;
    if (item.style.position == "absolute") continue;
    item.style.posTop =  RelTop;
  }
}

function DoLayout() { LayoutTBs(); }

function validateMode()
{
  if (!bTextMode) return true;
  alert("请取消“查看HTML源代码”选项再使用系统编辑功能或者提交!");
  Composition.focus();
  return false;
}

function format1(what,opt)
{
  if (opt=="removeFormat") { what=opt; opt=null; }
  if (opt==null) Composition.document.execCommand(what);
  else Composition.document.execCommand(what,"",opt);
  pureText = false;
  Composition.focus();
}

function format(what,opt)
{
  if (!validateMode()) return;
  format1(what,opt);
}

function setMode(newMode)
{
  bTextMode = newMode;
  var cont;
  var url=location.href;
  url=url.replace("html_edit.htm","");  //用来去掉转换时自动添加的路径部分
  if (bTextMode) {
    cleanHtml();
    cleanHtml();
    cont=Composition.document.body.innerHTML;
    cont=cont.replace(url,"");
    Composition.document.body.innerText=cont;
  } else {
    cont=Composition.document.body.innerText;
    cont=cont.replace(url,"");
    Composition.document.body.innerHTML=cont;
  }
  Composition.focus();
}

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

function UserDialog(what)
{
  if (!validateMode()) return;
  Composition.focus();
  Composition.document.execCommand(what, true);
  //去掉添加图片时的src="file://
  if(what=="InsertImage")
  { Composition.document.body.innerHTML=(Composition.document.body.innerHTML).replace("src=\"file://","src=\""); }
  pureText = false;
  Composition.focus();
}

function foreColor()
{
  if (!validateMode())  return;
  var arr = showModalDialog("selcolor.html", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0");
  if (arr != null) format('forecolor', arr);
  else Composition.focus();
}

function fortable()
{
  if (!validateMode())  return;
  var arr = showModalDialog("table.html", "", "dialogWidth:13.5em; dialogHeight:12.5em; status:0; ");
  if (arr != null)
  {
    var ss;
    ss=arr.split("*")
    row=ss[0];
    if (row=="") row=1;
    col=ss[1];
    if (col=="") col=1;
    tbwidth=ss[2];
    if (tbwidth=="") tbwidth=500;
    tbborder=ss[3];
    if (tbborder=="") tbborder=1;
    tbalign=ss[4];
    if (tbalign=="") tbalign="center";
    var string;
    string="<table border="+tbborder+" width="+tbwidth+" align="+tbalign+" style='border-collapse:collapse;' bordercolor='#CCCCCC'>";
    for(i=1;i<=row;i++){
      string=string+"<tr>";
      for(j=1;j<=col;j++) { string=string+"<td>&nbsp;</td>"; }
      string=string+"</tr>";
    }
    string=string+"</table>";
    content=Composition.document.body.innerHTML;
    content=content+string;
    Composition.document.body.innerHTML=content;
  }
  else
    Composition.focus();
}

function forswf()
{
  var arr = showModalDialog("swf.htm", "", "dialogWidth:15em; dialogHeight:15em; status:0;help:0");
  if (arr != null){
    var ss;
    ss=arr.split("*")
    path=ss[0];
    row=ss[1];
    col=ss[2];
    var string;
    string="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'  codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0' width="+row+" height="+col+"><param name=movie value="+path+"><param name=quality value=high><embed src="+path+" pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width="+row+" height="+col+"></embed></object>"
    content=Composition.document.body.innerHTML;
    content=content+string;
    Composition.document.body.innerHTML=content;
  }
  else Composition.focus();
}

function forwmv()
{
  var arr = showModalDialog("wmv.htm", "", "dialogWidth:16.5em; dialogHeight:15em; status:0;help:0");
  if (arr != null){
    var ss;
    ss=arr.split("*")
    path=ss[0];
    width=ss[1];
    height=ss[2];
    var string;
    //string="<object align=center classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 hspace=5 vspace=5 width="+ width +" height="+ height +"><param name=Filename value="+ path +"><param name=ShowStatusBar value=1><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src="+ path +"  width="+ width +" height="+ height +"></embed></object>";
    string="<embed src='"+ path+"' width="+ width +" height="+ height +" autostart=true loop=false ></embed>";
    content=Composition.document.body.innerHTML;
    content=content+string;
    Composition.document.body.innerHTML=content;
  }
  else Composition.focus();
}

function forrm()
{
  var arr = showModalDialog("rm.htm", "", "dialogWidth:16.5em; dialogHeight:15em; status:0;help:0");
  if (arr != null){
    var ss;
    ss=arr.split("*")
    path=ss[0];
    row=ss[1];
    col=ss[2];
    var string;
    string="<object classid='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA' width="+row+" height="+col+"><param name='CONTROLS' value='ImageWindow'><param name='CONSOLE' value='Clip1'><param name='AUTOSTART' value='-1'><param name=src value="+path+"></object><br><object classid='clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA'  width="+row+" height=60><param name='CONTROLS' value='ControlPanel,StatusBar'><param name='CONSOLE' value='Clip1'></object>"
    content=Composition.document.body.innerHTML;
    content=content+string;
    Composition.document.body.innerHTML=content;
  }
  else Composition.focus();
}

function cleanHtml()
{
  var fonts = Composition.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 getPureHtml()
{
  var url=location.href;
  url=url.replace("html_edit.htm","");  //用来去掉转换时自动添加的路径部分
  var str = "";
  //var paras = Composition.document.body.all.tags("P");
  //if (paras.length > 0){
  //  for  (var i=paras.length-1; i >= 0; i--) str= paras[i].innerHTML + "\n" + str;
  //} else {
    str  = Composition.document.body.innerHTML;
  //}
  str=str.replace(url,"");
  return str;
}

var bLoad=false
var pureText=true
var bodyTag="<head><style type=\"text/css\">body {font-size:  10pt}</style><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"></head><BODY bgcolor=\"#FFFFFF\" MONOSPACE>"
var bTextMode=false
public_description=new Editor

function Editor()
{
  this.put_HtmlMode=setMode;
  this.put_value=putText;
  this.get_value=getText;
}

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

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

function InitDocument()
{
  Composition.document.open();
  Composition.document.write(bodyTag);
  Composition.document.close();
  bLoad=true;
  eval("Composition.document.body.innerHTML+=(self.opener."+ htmlableID +".checked)?(self.opener."+bodyID+".value):(ubb2html(self.opener."+ bodyID +".value))");
}

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);
  }
}
var bIsIE5 = navigator.userAgent.indexOf("IE 5")  > -1;
var edit;
var RangeType;

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 rCode(s,a,b){
  var r = new RegExp(a,"gi");
  return s.replace(r,b); 
}

function handin()
{
  if (!validateMode()) return;
  var strHTMLbegin;
  var strHTMLend;
  strHTMLbegin = "";
  strHTMLend = "";
  eval("self.opener."+bodyID+".value=strHTMLbegin + getPureHtml(Composition.document.body.innerHTML) + strHTMLend;self.opener."+htmlableID+".checked=true;");
  self.close();
}

function ubbcode(){
  if (!validateMode()) return;
  cont=getPureHtml(Composition.document.body.innerHTML);
  var aryCode0 = new Array("<STRONG>","[b]","</STRONG>","[/b]","<p","[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 color=","[color=","<font face=","[face=","<font size=","[size=");
  var aryCode9 = new Array(">","]","<","[","</","[/");
  var aryCode = aryCode0.concat(aryCode1).concat(aryCode2).concat(aryCode9);
  for (var i=0;i<aryCode.length;i+=2){
    cont=rCode(cont,aryCode[i],aryCode[i+1]);
  }
  eval("self.opener."+bodyID+".value=cont;");
  self.close();
}

function ubb2html(str){
  if (str=="")
    return str;
  var aryCode0 = new Array("<br>","\n","<b>","\\[b]","</b>","\\[/b]","<p","\\[p","</p>","\\[/p]","<a href=","\\[url=","</a>","\\[/url]");
  var aryCode1 = new Array("<i>","\\[i]","</i>","\\[/i]","<u>","\\[u]","</u>","\\[/u]","<ul>","\\[list]","</ul>","\\[/list]","<ol>","\\[list=1]","</ol>","\\[/list]");
  var aryCode = aryCode0.concat(aryCode1);
  for (var i=0;i<aryCode.length;i+=2){
    str=rCode(str,aryCode[i+1],aryCode[i]);  
  }
  return str;
}

function help()
{
    var helpmess;
    helpmess="-------------填写帮助-------------　　\r\n\r\n"+
         "1.请不要发表有危险性的脚本。\r\n\r\n"+
         "2.如果要书写源代码，请选中\r\n\r\n"+
         "　查看HTML源代码书写.\r\n\r\n"+
         "3.需要你自己运行,才能看效果.\r\n\r\n"+
         "4.如果书写js，尽量不要在这儿书写.";
    alert(helpmess);
}