if(typeof encodeURI=='undefined')
  encodeURI=function(s){
  s=s.replace(/\%/g,'%25')
  s=s.replace(/\^/g,'%5E')
  s=s.replace(/\[/g,'%5B')
  s=s.replace(/\]/g,'%5D')
  s=s.replace(/\{/g,'%7B')
  s=s.replace(/\}/g,'%7D')
  s=s.replace(/\</g,'%3C')
  s=s.replace(/\>/g,'%3E')
  s=s.replace(/\"/g,'%22')
  s=s.replace(/\|/g,'%7C')
  s=s.replace(/\\/g,'%5C')
  s=s.replace(/\ /g,'%20')
  s=s.replace(/\`/g,'%60')
  s=s.replace(/\r/g,'%0D')
  s=s.replace(/\n/g,'%0A')
  return s
}
function encodeSubmitData(s){
  s=s.replace(/\r\n/g,'\n') // IE textarea returns \r\n, firefox textarea returns \r => normalize to \r
  s=s.replace(/\n/g,'\r\n') // map back to \n\r
  s=encodeURI(s)
  return s
}
function xComment(isOwner){
  if (!Sarissa || !document.getElementsByTagName){
    return
  }
  this.rc = []
  this.rc['submit_missing_info'] = 'Please enter your name and a comment.'
  this.rc['remove_confirm'] = 'Delete this comment permanently?'
  this.state = []
  this.isOwner = typeof isOwner == 'undefined' ? false : isOwner
  this.xmlhttp_url = ''
  this.formName = 'fmComment'
  this.show = function(what){
    document.getElementById(what).style.display = 'inline'
    // if(what=='comment') this.show('commentForm')
  }
  this.hide = function(what){
    this.state[what] = document.getElementById(what).style.display
    document.getElementById(what).style.display = 'none'
  }
  this.restore = function(what){
    if(typeof this.state[what] != 'undefined')
      document.getElementById(what).style.display = this.state[what]
  }
  this.clear = function(what){
    if(what == 'comment'){
      document.getElementById('commentBody').innerHTML = '&nbsp;'
    }else if(what == 'commentForm'){
      // document.fmComment.authorName.value = ''
      document.fmComment.comment.value = ''
    }
  }
  this.load = function(){
    xmlhttp.open('POST', 'xcomment.asp', true)
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlhttp.send('src='+this.src)
    xmlhttp.onreadystatechange = function(){
      if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200){
          if(xmlhttp.responseXML){ // Firefox returns null if not found
            var items = xmlhttp.responseXML.getElementsByTagName('item')
            for(var s='', i=0; i<items.length; i++){
              s+=(s?'<HR>':'')
                +'<TABLE BORDER=0 CELLSAPCING=0 CELLPADDING=0>'
                +'<TR><TD vAlign=top>'
                +(xcomment.isOwner ?
                  '<A href="javascript:xcomment.remove('+items[i].getAttribute('xid')+')"><IMG border=0 src=images/delete.gif vspace=3 hspace=3></A></TD><TD>'
                  : ''
                )
              if(items[i].getAttribute('author')!=''){
                // s+='<A href="search.asp?username='+items[i].getAttribute('author')+'" target=_blank><B>'+items[i].getAttribute('authorName')+'</B></A><BR>'
                s+='<B>'+items[i].getAttribute('authorName')+'</B><BR>'
              }else{
                s+='<B>'+items[i].getAttribute('authorName')+'</B><BR>'
              }
              s+=items[i].getAttribute('datecreated')+'<BR>'
                +items[i].getAttribute('comment').replace(/\n/g,'<BR>')
                +'</TD></TR></TABLE>'                        
            }
            if(items.length>0){
              xcomment.show('comment')
            }else{
              xcomment.hide('comment')
            }
            document.getElementById('commentCount').innerHTML = items.length
            document.getElementById('commentBody').innerHTML = s
          }
        }
      }
    }
  }
  this.submit = function(){
    var sAuthorName = document[this.formName]['authorName'].value
    var sComment = document[this.formName]['comment'].value
    if(sAuthorName == '' || sComment == ''){
      alert(this.rc['submit_missing_info'])
      return
    }
    xmlhttp.open('POST', 'xcomment.action.asp', true)
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlhttp.send('action=add'
      +'&src='+this.src
      +'&authorName='+encodeURI(sAuthorName)
      +'&comment='+encodeSubmitData(sComment)
    )
    xmlhttp.onreadystatechange = function(){
      if (xmlhttp.readyState == 4){
        if (xmlhttp.status == 200){
          if(xmlhttp.responseXML){ // Firefox returns null if not found
            var x = xmlhttp.responseXML.getElementsByTagName('xcomment')
            if(x.length){
              if(x[0].getAttribute('status')=='ok')
                xcomment.load()
                // xcomment.show('comment')
            }
          }
        }
      }
    }
    this.clear('commentForm')
  }

  /*****************************************************************************
    .remove()
  *****************************************************************************/  
  this.remove = function(xid){
    if(!confirm(this.rc['remove_confirm'])) return
    xmlhttp.open('POST', 'xcomment.action.asp', true)
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
    xmlhttp.send('action=remove'
      +'&src='+this.src
      +'&xid='+xid
    )
    xmlhttp.onreadystatechange = function(){
      if (xmlhttp.readyState == 4){
        if (xmlhttp.status == 200){
          if(xmlhttp.responseXML){ // Firefox returns null if not found
            var x = xmlhttp.responseXML.getElementsByTagName('xcomment')
            if(x.length){
              if(x[0].getAttribute('status')=='ok')
                xcomment.load()
            }
          }
        }
      }
    }
  }

}

