
function catAjax() {
  var text;
  var max;
  var time;

  function go(word,ename) {
  var cmd="catword?word="+word+"&max="+this.max+"&count";
  var max=this.max;
  var e=$("#"+ename);

  $.ajax({
    url:cmd,
    global:false,
    type:"get",
    dataType:"xml",
    timeout:5000,
    success: function(xml){
        var html="";
        var root=$(xml).find('parfile');
        if (root.length<1) {e.html("<span class='error'>ERROR catajax</span>"); return;}
        var t=root.find('count');
        e.html("");
        if (t.length>0){
            html="<b>"+t.text()+" elements found: </b>";
        }
        var i,n,cat=root.find('cat');
        for (i=0;i<cat.length;i++){
            t=cat.eq(i).find("title");
            if (t.length>0) {t=t.text().replace(/["']/," ");}
            else {t="";}
            n=cat.eq(i).attr('name');
            if (!n) {n=cat.eq(i).attr('id');}
            if (i>0) html+=" .";
            html+=" <a href='Cat?cat="+encodeURIComponent(cat.eq(i).attr('id'))+"' title='"+t+"'>"+n+"</a>";
        }
        if (cat.length>=max) html+=" ...";
        e.html(html);
    },
    error:function(request,status, err){e.html("<span class='error'>ERROR catajax</span>"); return;}
  });
  }
  this.go=go;

  function getTime(){
    var dt=new Date();
    return dt.getTime();
  }
  this.getTime=getTime;


  function trap(eword,ename) {
  var word=$("#"+eword).val();
  if (word[word.length]==' '){
    this.go(word,ename);
    this.text=word;
    this.time=this.getTime();
  }
  if (word.length<3) {this.text=word;return;}
  var t=this.getTime();
  if (t-this.time>100) {
    this.go(word,ename);
    this.text=word;
    this.time=this.getTime();
  }
  }
  this.trap=trap;
}

var catajax=new catAjax();
catajax.text="";
catajax.max=5;
catajax.time=catajax.getTime();

