var lybbs_edit;
var lybbs_RangeType;
var lybbs_selection;
var lybbs_charset="UTF-8";
var lybbs_bLoad=false;
var lybbs_pureText=true;
var lybbs_bTextMode=1;//1-design,2-html
var ischeck=false;

function lybbs_InitDocument(contentValue, charset) {	
  if (charset!=null)
     lybbs_charset=charset;
  if (lybbs_bIsIE5){
    var lybbs_bodyTag="<style type=\"text/css\">.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px}.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style : oblique;line-height : normal ;font-weight:bold;}\n</style></head><body bgcolor=\"#FFFFFF\" title=\"Ctrl+Enter\u76f4\u63a5\u63d0\u4ea4\u8d34\u5b50\" onkeydown=\"ctlent();\">";
  }else{
    var lybbs_bodyTag="<style type=\"text/css\">.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px}.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style : oblique;line-height : normal ;font-weight:bold;}\n</style></head><body bgcolor=\"#FFFFFF\">";
  }
  var h=openner.theform.content.value;
  if (navigator.appVersion.indexOf("MSIE 6.0",0)==-1){
    IframeID.document.designMode="On"
  }
  IframeID.document.open();
  IframeID.document.write ('<html>');
  IframeID.document.write ('<head>');
  if (lybbs_bIsIE5){
    IframeID.document.write ('<script language="javascript" type="text/javascript">');
    IframeID.document.write ('function ctlent(eventobject) {');
    IframeID.document.write ('if(event.ctrlKey&&window.event.keyCode==13) {');
    IframeID.document.write ('parent.copyPost(); ');
    IframeID.document.write ('}}');
    IframeID.document.write ('function imgresize(o){');
    IframeID.document.write ('if(o.width > 500 ){');
    IframeID.document.write ('	o.style.width=\'500px\';');
    IframeID.document.write ('}');
    IframeID.document.write ('if(o.height > 800){');
    IframeID.document.write ('	o.style.height=\'800px\';');
    IframeID.document.write ('}');
    IframeID.document.write ('}');
    IframeID.document.write ('<\/script>');
  }
  IframeID.document.write(lybbs_bodyTag);
  IframeID.document.write("</body>");
  IframeID.document.write("</html>");
  if (h!=""){
    if(lybbs_bIsIE5) {
      IframeID.document.body.innerText=addScript(toBr(h));
    }
    else {
      IframeID.document.body.textContent=addScript(toBr(h));
    }
  }
  IframeID.document.close();
  if (navigator.appVersion.indexOf("MSIE 6.0")!=-1) {
    IframeID.document.body.contentEditable = "true";
  }
  IframeID.document.charset=lybbs_charset;
  lybbs_bLoad=true;
  IframeID.focus();
}
/**
function ctlent() {
  var ispost=0;
  if (document.all){
    if(event.ctrlKey && event.keyCode==13){
      lybbs_CopyData();
      lybbs_Checkdata()
      if (ischeck==true){this.document.submitok.submit();}
    }
  }
}
**/
function lybbs_setMode(n,v) {
  lybbs_setStyle();
  var content;
  var commonbar=document.getElementById("commonbar");
  var htmlbar=document.getElementById("htmlbar");
  switch (n){
    case 1:
      commonbar.style.display="";
      htmlbar.style.display="";
      if (lybbs_bIsIE5){
        content=IframeID.document.body.innerText;
        IframeID.document.body.innerHTML=content;
      }else{
        var html = IframeID.document.body.ownerDocument.createRange();
        html.selectNodeContents(IframeID.document.body);
        IframeID.document.body.innerHTML = html.toString();
      }
      break;
    case 2:
      commonbar.style.display="none";
      htmlbar.style.display="none";
      if(lybbs_bTextMode!=1){
        content=IframeID.document.body.innerText;
      }
      else{
        content=IframeID.document.body.innerHTML;
      }
      if (content){
        if (lybbs_bIsIE5){//IE
          IframeID.document.body.innerText=content;
        }else{//Nc
          var html=document.createTextNode(content);
          IframeID.document.body.innerHTML = "";
          IframeID.document.body.appendChild(html);
        }
      }
      break;
  }
  lybbs_bTextMode=n;
  lybbs_setTab(n);
}

function lybbs_setTab(n) {
  //the style of html and design button
  var mhtml=document.getElementById("lybbs_TabHtml");
  var mdesign=document.getElementById("lybbs_TabDesign");
  if(n==1){
    mhtml.className="lybbs_TabOff";
    mdesign.className="lybbs_TabOn";
  }else if(n==2){
    mhtml.className="lybbs_TabOn";
    mdesign.className="lybbs_TabOff";
  }
}

function lybbs_setStyle() {
  var bs = IframeID.document.body.style;
  bs.fontFamily="Arial";
  bs.fontSize="10.5pt";
  bs.scrollbar3dLightColor= '#D4D0C8';
  bs.scrollbarArrowColor= '#000000';
  bs.scrollbarBaseColor= '#D4D0C8';
  bs.scrollbarDarkShadowColor= '#D4D0C8';
  bs.scrollbarFaceColor= '#D4D0C8';
  bs.scrollbarHighlightColor= '#808080';
  bs.scrollbarShadowColor= '#808080';
  bs.scrollbarTrackColor= '#D4D0C8';
  bs.border='0';
}

function lybbs_validateMode() {
  if (lybbs_bTextMode==1) {
    return true;
  }
  else {
    alert("Error: HTML Mode");
    IframeID.focus();
    return false;
  }
}

var colour
function FormatText(command, option) {
  var codewrite
  if (lybbs_bIsIE5){
    if (option=="removeFormat"){
       command=option;
       option=null;
    }
    IframeID.focus();
    IframeID.document.execCommand(command, false, option);
    lybbs_pureText = false;
    IframeID.focus();
  }
  else {
    try {
      IframeID.focus();
      IframeID.document.execCommand(command, false, option);
      IframeID.focus();
    }
    catch (e) {}
  }
}

//Colour top offset
function getOffsetTop(elm) {
  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;
  while(mOffsetParent){
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
  return mOffsetTop;
}

//Colour left offset
function getOffsetLeft(elm) {
  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;
  while(mOffsetParent) {
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
  return mOffsetLeft;
}

function AddText(text) {
  if (IframeID.document.body.createTextRange && IframeID.document.body.caretPos) {      
    var caretPos = IframeID.document.body.caretPos;      
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
  }
  else {
    IframeID.document.body.innerHTML += text;
  }
  IframeID.focus(caretPos);
}

function fixtag(text){
  text = text.replace(/&|\/|<|>|\*|#|:|;|=|\?|\)|\(|%|\[|\]|\$| /g, "") ;
  return text ;
}

function escapeUrl(text){
  text = text.replace(/&#/g, "");
  text = text.replace(/ /g, "%20");
  text = text.replace(/"/g, "");
  text = text.replace(/'/g, "");
  text = text.replace(/</g, "");
  text = text.replace(/>/g, "");
  text = text.replace(/\*/g, "");
  return text ;
}

function checkInvalidUrl(text) {
  text=text==null?"":text.toLowerCase();
  if(text.indexOf("onerror")!=-1) { return true; }
  else if(text.indexOf("script")!=-1) { return true; }
  return false;
}

function HTML(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;") ;
  return text ;
}

function ReHTML(text){
  text = text.replace(/&amp;/g, "&") ;
  text = text.replace(/&quot;/g, '"') ;
  text = text.replace(/&lt;/g, "<") ;
  text = text.replace(/&gt;/g, ">") ;
  text = text.replace(/&#146;/g, "'") ;
  return text ;
}

function toBr(text){
  text = text.replace(/\r/g, "") ;
  text = text.replace(/\n/g, "<br>") ;
  return text;
}

function toBlank(text){
  return text.replace(/\xA0/g, " ");
}

function addScript(text){
  return "<pre>"+text+"</pre>";
}

function delScript(text){
  text=text.replace("<pre>","");
  text=text.replace("</pre>","");
  return toBlank(text);
}

function lybbs_inserttable() {
  if (!lybbs_validateMode()) return;
  IframeID.focus();
  var arr = showModalDialog("bbs_commonForward.do?forward=inserttable", window, "dialogWidth:22em; dialogHeight:19em; status:0; help:0;scroll:no;");
  if (arr){
    IframeID.document.body.innerHTML+=arr;
  }
  IframeID.focus();
}

function lybbs_CopyData(){
  ischeck=true;
  if(lybbs_bTextMode==2){
    if (lybbs_bIsIE5){
      content=IframeID.document.body.innerText;
      IframeID.document.body.innerHTML=content;
    }else{
      var html = IframeID.document.body.ownerDocument.createRange();
      html.selectNodeContents(IframeID.document.body);
      IframeID.document.body.innerHTML = html.toString();
    }
    IframeID.focus();
  }
  try {
    openner.theform.content.value=delScript(OSWEBXHTML.GetXHTML(IframeID.document.body));
    //support html checked
    if(openner.theform.htmlSupport!=null) openner.theform.htmlSupport.checked=true;
    return true;
  }
  catch(ex) {
    alert(ex.message);
    return false;
  }
}

function lybbs_CheckData(){
  IframeID.document.charset="GBK";
  IframeID.document.body.innerHTML=addScript(toBr(theform.content.value));
  try {
    theform.content.value=delScript(OSWEBXHTML.GetXHTML(IframeID.document.body));
    return true;
  }
  catch(ex) {
    alert(ex.message);
    theform.content.value="";
    return false;
  }
}

function checkattributeName(attributeName){
  var Re = new RegExp(/&|\/|<|>|\*|#|:|;|=|\?|\)|\(|%|\[|\]|\$| /gi);
  if(Re.test(attributeName)){
    return false
  }else{
    var Re = new RegExp(/[0-9]/gi);
    if(Re.test(attributeName.substring(0,1))){
      return false
    }else{
      return true;
    }
  }
}

var checkword=0;
var OSWEBXHTML=new Object();
OSWEBXHTML.GetXHTML=function(node){
  if (window.ActiveXObject){
    var prefix = ["MSXML3","MSXML2","MSXML","Microsoft"];
    for (var i=0;i<prefix.length;i++) {
      try {
        this.XML=new ActiveXObject(prefix[i] + ".DOMDocument");
        if (XML){
          return XML;
        }
      } catch (e) {}
    }
  }else{
    this.XML=document.implementation.createDocument('', '', null);
    Node.prototype.__defineGetter__('xml', OSWEBXHTML._Node_Getxml);
  }
  this.MainNode=this.XML.appendChild(this.XML.createElement( 'XHTML' ));
  this._AppendChildNodes(this.MainNode, node);
  var sXHTML=this.MainNode.xml;
  sXHTML=sXHTML.replace(/<embed><\/embed>/g,"") ;
  return sXHTML.substr(7, sXHTML.length - 15);
}

OSWEBXHTML._Node_Getxml=function(){
  var oSerializer=new XMLSerializer();
  return oSerializer.serializeToString(this);
}

OSWEBXHTML._AppendChildNodes=function(xmlNode, htmlNode){
  var oChildren=htmlNode.childNodes;
  var i=0;
  while (i < oChildren.length){
    i +=this._AppendNode(xmlNode, oChildren[i]);
  }
}

OSWEBXHTML._AppendNode=function(xmlNode, htmlNode){
  var iAddedNodes=1;
  switch (htmlNode.nodeType){
    case 1 : var sNodeName=fixtag(htmlNode.nodeName.toLowerCase());
             if(sNodeName=='') break;
             else if(sNodeName=='plaintext'||sNodeName=='xmp') break;//filter plaintext and xmp to clear
             else if(sNodeName=='iframe') break;//filter iframe
             else if(sNodeName=='meta') break;//filter iframe
             else if(sNodeName=='form') break;//filter iframe
             if(checkword==1){
               if (sNodeName=='colgroup') break;
             }
             var oAttributes=htmlNode.attributes;
             var oNode=xmlNode.appendChild(this.XML.createElement( sNodeName ));
             for (var n=0 ; n < oAttributes.length ; n++){
               var oAttribute=oAttributes[n];
               if (oAttribute.specified) {
                 if(oAttribute.nodeName.toLowerCase()=='onerror') {
                    break;
                 }
                 else if (oAttribute.nodeName.toLowerCase()=='style') {
                   ;
                 }
                 else if (oAttribute.nodeName.toLowerCase()=='src') {
                   var nodeValue=escapeUrl(oAttribute.nodeValue+'');
                   if(checkInvalidUrl(nodeValue)) {
                     break;
                     //this._AppendAttribute(oNode,fixtag(oAttribute.nodeName.toLowerCase()+''),'');
                   }
                   else {
                     this._AppendAttribute(oNode, fixtag(oAttribute.nodeName.toLowerCase()+''), nodeValue);
                   }
                 }
                 else {
                   this._AppendAttribute(oNode, fixtag(oAttribute.nodeName.toLowerCase()+''), oAttribute.nodeValue+'');
                 }
               }
             }
             /**
             var cssText=htmlNode.style.cssText;
             if (cssText!='') {
               if (navigator.appVersion.indexOf("MSIE")!=-1){
                 cssText=cssText+';';
               }
               this._AppendAttribute(oNode, 'style', cssText);
             }
             **/
             if (htmlNode.childNodes.length == 0){
                switch (sNodeName){
                  case "img" :case "input" :case "br" : case "hr" :case "param":break;
                  default : oNode.appendChild(this.XML.createTextNode( '' ));
                            break;
                }
             }
             switch (sNodeName){
               case "abbr" : if (document.all){
                               var oNextNode=htmlNode.nextSibling;
                               while (true){
                                 iAddedNodes++;
                                 if (oNextNode && oNextNode.nodeName !='/ABBR'){
                                   this._AppendNode(oNode, oNextNode);
                                   oNextNode=oNextNode.nextSibling;
                                 }else{
                                   break;
                                 }
                               }
                               break;
                             }
               case "area" : if (document.all && ! oNode.attributes.getNamedItem( 'coords' )){
                               var sCoords=htmlNode.getAttribute('coords', 2);
                               if (sCoords && sCoords !='0,0,0')
                                 this._AppendAttribute(oNode, 'coords', sCoords);
                             }
               case "img" : if (! oNode.attributes.getNamedItem( 'alt' )) {
                              this._AppendAttribute(oNode, 'alt', 'img');
                            }
               default :this._AppendChildNodes(oNode, htmlNode);
                        break;
             }
    case 3 : if (htmlNode.nodeValue!=null) {
               xmlNode.appendChild(this.XML.createTextNode( htmlNode.nodeValue ));
             }
             break;
    default : xmlNode.appendChild(this.XML.createComment( "Element not supported - Type: " + htmlNode.nodeType + " Name: " + htmlNode.nodeName ));
              break;
  }
  return iAddedNodes;
}

OSWEBXHTML._AppendAttribute=function(xmlNode, attributeName, attributeValue){
  if(checkattributeName(attributeName) && attributeName.indexOf('_moz') ==-1 && attributeName.substring(0,2)!='on' && attributeValue.indexOf('_moz') ==-1 && attributeName!=''){
    if(checkword==1){
      if (attributeName=='classname') return;
      if (attributeName=='style') return;
      if (attributeName=='class') return;
    }
    if (attributeName=='xstr') return;
    if (attributeName=='xnum') return;
    if (attributeName=='u1num') return;
    var oXmlAtt=this.XML.createAttribute(attributeName);
    if ((typeof( attributeValue )=='boolean' && attributeValue == true ) || attributeValue == 'true'){
      oXmlAtt.nodeValue=attributeName;
    }else{
      oXmlAtt.nodeValue=attributeValue;
    }
    xmlNode.attributes.setNamedItem(oXmlAtt);
  }
}
