function clean(html) {
  html = html.replace(/\n/g,'')
  html = html.replace(/<br>\s*/gi, '');
  html = html.replace(/<\/font>\s*/gi, '');
  html = html.replace(/<font[^>]*>\s*/gi, '');
  return html;
}

CForm = Class.create();
CForm.prototype = {
  initialize: function(f,cmds,ftxt,fnm,fb, frmStyle) {
    this.frmStyle = "<link rel='stylesheet' type='text/css' href="+frmStyle+" />";
    p = location.href.split('/');
    this.p = p[p.length-2] + '.' + p[p.length-1].replace(/[#|\?].*/,'');
    this.url = location.href;
    this.rf = $(f);
    this.cbar = $(cmds);
    this.ftxt = $(ftxt);
    this.fnm = $(fnm);
    this.fb = $(fb);
    this.mkRf();
    this.fnm.onclick = function(){
      this.fnm.value = '';
      this.fnm.className = 'ff up';
    }.bind(this);
    this.cmts = new Comments();
  },
  mkRf: function() {
    this.cbar.innerHTML = "<ul class='cmds'>"+mkb('bold','bold','Bold') + mkb('italic','italic','Italic') + mkb('createlink','Insert Link','A') + mkb('unlink','Remove Link','Unlink') + mkb('insertunorderedlist','Insert bullet list','UL') + mkb('insertorderedlist','Insert ordered list','OL') + mkb('indent','Quote/Indent','Blockquote') + mkb('removeFormat','Remove All Formating','Unformat')+"</ul>";
    
    this.fifrm = document.createElement('iframe');
    this.fifrm.id = 'fifrm';
    this.fifrm.name = 'fifrm';
    this.rf.insertBefore(this.fifrm,this.ftxt);
    this.fidoc = frames[this.fifrm.name].document;
    if (!document.all) {
      this.fifrm.contentDocument.designMode = 'on';
      this.fifrm.contentDocument.open();
      this.fifrm.contentDocument.write(this.frmStyle);
      this.fifrm.contentDocument.close();
      this.fifrm.contentDocument.execCommand('styleWithCSS', false, false);
    } else {
      this.fidoc.designMode = 'on';
      this.fidoc.open();
      this.fidoc.write(this.frmStyle);
      this.fidoc.close();
    }
    
    this.fcmds = lsCmds(this.cbar);
    this.fcmds.each(function(fci,i){
      fci.onclick = function() {
        if (fci.className == 'createlink') {
          lnUrl = prompt('Please enter a URL:', 'http://');
          if (lnUrl !=null && lnUrl != '') ins(this.fifrm,this.fidoc,fcmd,lnUrl);
        } else ins(this.fifrm,this.fidoc,fci.className,null);
      }.bind(this);
    }.bind(this));
    
    this.fb.onclick = this.update.bind(this);
  },
  update: function() {
    if ((/your name/.test(this.fnm.value)) || this.fnm.value == '') {
      this.fnm.value = "Please include your name";
      this.fnm.className = 'ff stress';
      return;
    }
    this.fb.innerHTML = 'Posting...';
    fidoc = (!document.all) ? this.fifrm.contentDocument.body.innerHTML : this.fidoc.body.innerHTML;
    fidoc = toWiki(fidoc.replace(/\n/g,'')).replace(/\n*$/gi,'').replace(/^\n*/gi,'') + "\n";
    
    fdata = "action=edit"
      + "&n=" + this.p
      + "&text=" + encodeURI(fidoc)
      + "&author=" + encodeURI(this.fnm.value)
      + "&s=0"  // Comment on top
      + "&pos=p0s0"  // General comment
      + "&cmt=1&post=1";
    
    this.xreq = mkXReq();
    this.xreq.onreadystatechange = function() { 
      if (this.xreq.readyState == 4) { if (this.xreq.status == 200) {
        this.fb.innerHTML = 'Post this comment';
        nurl = location.href.split('/');
        nln = nurl[nurl.length-1].replace(/\-/,'');
        nc = mke('div','','s',"<p class='ref'><a href='/Status/"+nln+"'>Document</a></p><p class='meta'><strong>"+this.fnm.value+"</strong>now</p>");
        nc.innerHTML += (!document.all) ? this.fifrm.contentDocument.body.innerHTML : this.fidoc.body.innerHTML;
        clist = $('cmts');
        if (!$('vyk')) clist.insertBefore(nc,clist.childNodes[0]);
        else {
          clist.innerHTML = '';
          clist.appendChild(nc);
        }
        this.cmts.chk();
      } else { alert('There was a problem with the request.'); } }
    }.bind(this);
    
    this.xreq.open('POST', this.url, true);
    this.xreq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    this.xreq.send(fdata);
  }
}

Comments = Class.create();
Comments.prototype = {
  initialize: function(ntag) {
    this.sel = -1;
    this.nm = '';
    p = location.href.split('/');
    this.p = p[p.length-2] + '.' + p[p.length-1].replace(/[#|\?].*/,'');
    this.url = location.href;
    this.chk();
  },
  close: function() {
    if (this.rte) this.rte.className = 'off';
    $('f').className = '';
    for (i = 0; i < this.n.length; i++)
      this.n[i].a.innerHTML = "<strong>Reply</strong>";
  },
  chk: function() {
    this.n = $$('cmts','div','s');
    if (this.n.length == 0) return;
    this.n.each(function(ni,i){
      ni.innerHTML += "<a href='javascript:;' id='c"+i+"' class='reply' title='Reply to this comment'><strong>Reply</strong></a>";
      ni.a = $('c'+i);
      ni.a.onclick = function(){
        if (this.sel == i) {
          this.close();
          this.sel = -1;
        } else {
          this.sel = i;
          this.pos = ni.getElementsByTagName('a')[0].id;
          this.close();
          ni.a.innerHTML = "<strong>Close</strong>";
          ni.a.title = '';
          if (!this.rte) this.mkRte();
          else this.mvRte();
          $('f').className = 'blur';
        }
      }.bind(this);
    }.bind(this));
  },
  mkRte: function() {
    this.rte = document.createElement('div');
    this.rte.id = 'rte';
    this.n[this.sel].appendChild(this.rte);
    
    hint = mke('p','','',"<strong>Reply to this comment</strong>");
    this.rte.appendChild(hint);
    
    this.cbar = mke('ul', 'ecmds', '', mkb('bold','bold','Bold') + mkb('italic','italic','Italic') + mkb("createlink","Insert link","A") + mkb('unlink','Remove link','Unlink') + mkb('insertunorderedlist','Insert bullet list','UL') + mkb('insertorderedlist','Insert ordered list','OL') + mkb('indent','Quote/Indent','Blockquote') + mkb('removeFormat','Remove all formating','Unformat'));
    this.rte.appendChild(this.cbar);
    
    // Rich text editor
    this.frmStyle = 
      "<link rel='stylesheet' type='text/css' href='http://sememe.org/pub/skins/greylady/includes/ifrm.css' />";
    this.ifrm = document.createElement('iframe');
    this.ifrm.id = 'ifrm';
    this.ifrm.name = 'ifrm';
    this.rte.appendChild(this.ifrm);
    this.idoc = frames[this.ifrm.name].document;
    this.sync();
    
    nm = mke('p','auth','',"<input id='authnm' value='Include your name' />");
    this.rte.appendChild(nm);
    $('authnm').onclick = function() { 
      $('authnm').value = ''; 
      $('authnm').className = 'on'; 
    }
    
    eb = mke('p','eb','',"<strong id='rec'>Post this comment</strong> or <em id='canc'>cancel</em>");
    this.rte.appendChild(eb);
    
    this.cmds = lsCmds(this.cbar);
    this.cmds.each(function(ci,i){
      ci.onclick = function() {
        if (ci.className == 'createlink') {
          lnUrl = prompt('Please enter a URL:', 'http://');
          if (lnUrl !=null && lnUrl != '') ins(this.ifrm,this.idoc,ci.className,lnUrl);
        } else ins(this.ifrm,this.idoc,ci.className, null);
      }.bind(this);
    }.bind(this));
    
    $('canc').onclick = function(){
      this.close();
      this.n[this.sel].a.innerHTML = "<strong>Reply</strong>";
      $('f').className = '';
    }.bind(this);
    $('rec').onclick = this.update.bind(this);
  },
  mvRte: function() {
    this.n[this.sel].appendChild(this.rte);
    $('authnm').value = (this.nm != '') ? this.nm : 'Include your name';
    this.rte.className = $('rec').className = '';
    $('rec').innerHTML = 'Post this comment';
    this.sync();
  },
  sync: function() {
    if (!document.all) {
      this.ifrm.contentDocument.designMode = 'on';
      this.ifrm.contentDocument.open();
      this.ifrm.contentDocument.write(this.frmStyle);
      this.ifrm.contentDocument.close();
      this.ifrm.contentDocument.execCommand('styleWithCSS', false, false);
    } else {
      this.idoc.designMode = 'on';
      this.idoc.open();
      this.idoc.write(this.frmStyle);
      this.idoc.close();
    }
  },
  update: function() {
    if (/name/.test($('authnm').value)) {
      $('authnm').className = 'stress';
      return;
    } else this.nm = $('authnm').value;
    idoc = (!document.all) ? this.ifrm.contentDocument.body.innerHTML : this.idoc.body.innerHTML;
    etxt = toWiki(idoc.replace(/\n/g,'')).replace(/\n*$/gi,'').replace(/^\n*/gi,'') + "\n";
    
    $('rec').innerHTML = 'Posting...';
    $('rec').className = 'busy';
    edata = "action=edit"
      + "&n=" + this.p
      + "&text=" + encodeURI(etxt)
      + "&author=" + encodeURI(this.nm)
      + "&s=" + (this.sel + 1)  // Reply to comment
      + "&pos=" + this.pos
      + "&cmt=1&post=1";
    
    this.xreq = mkXReq();
    this.xreq.onreadystatechange = function() { 
      if (this.xreq.readyState == 4) { if (this.xreq.status == 200) {
        nsel = this.n[this.sel];
        nreply = mke('div','','ss',"<p class='meta'><strong>"+this.nm+"</strong>now</p>");
        html = (!document.all) ? "<p>"+this.ifrm.contentDocument.body.innerHTML+"</p>" : this.idoc.body.innerHTML;
        nreply.innerHTML += clean(html);
        nsel.insertBefore(nreply,nsel.a);
        this.close();
        this.sel = -1;
      } else { alert('There was a problem with the request.'); } }
    }.bind(this);
    
    this.xreq.open('POST', this.url, true);
    this.xreq.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    this.xreq.send(edata);
  }
}

window.onload = function() {
	new Toggle('lnHeaderTrig', 0, ['lnHeaderSwitch']);
  frmStyle = 'http://traduwiki.org/pub/skins/greylady/includes/ifrm.css';
  new CForm('f','cmds','ftxt','fnm','fb', frmStyle);
}