/* Functions for implementating "auto-suggest" entry widgets */

var HUI_suggestCache = new Object();
var HUI_suggestMax = 7;

function HUIsuggestCback(x,cback) {
  HUIsuggestResults(cback.suggest,cback.search,cback.op,cback.key,x);
}

function HUIsuggestUpdate(o) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o) {
    var search = o.entryObj.value;
    var op = o.suggestOp;
    var field = o.suggestArg;
    var key = op + ":" + field + ":" + search;
    if (search == "") {
      HUIsuggestClose(o);
      return;
    }
    if (o.suggestCache && o.suggestCache[key])
      HUIsuggestResults(o,search,op,key,o.suggestCache[key]);
    else {
      var cback = new Object();
      cback.onsuccess = HUIsuggestCback;
      cback.search = search;
      cback.field = field;
      cback.op = op;
      cback.key = key;
      cback.suggest = o;
      var arg = '';
      arg += ajaxMakeArg("search",search);
      arg += ajaxMakeArg("max",HUI_suggestMax+1);
      arg += ajaxMakeArg("arg",o.suggestArg);
      arg += ajaxMakeArg("op",o.suggestOp);
      if (self.huiDebug)
	huiDebug("suggest call arg = " + arg);
      if (o.suggestCall) {
	ajaxCancel(o.suggestCall);
	o.suggestCall = false;
      }
      o.suggestCall = ajaxDoCall(o.suggestURI,o.suggestAction,arg,cback);
    }
  }
}

function HUIsuggestFocus(o) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o)
    o.isFocused = true;
}

function HUIsuggestBlur(o) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o) {
    HUIsuggestClose(o);
    o.isFocused = false;
  }
}

function HUIsuggestOpen(o) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o)
    o.style.display = 'block';
}

function HUIsuggestClose(o) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o)
    o.style.display = 'none';
}

function HUIsuggestSelect(o,data) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o) {
    o.entryObj.value = unescape(data);
    HUIsuggestClose(o);
  }
}

function HUIsuggestSafe(x) {
  x = x.replace('&','&amp;');
  x = x.replace('<','&lt;');
  return x;
}

function HUIsuggestRegexp(x) {
  x = x.replace("\\","\\\\");
  x = x.replace("/","\\/");
  x = x.replace("*","\\*");
  x = x.replace("+","\\+");
  x = x.replace(".","\\.");
  return "/" + x + "/i";
}

function HUIsuggestMake(o,search,data,count) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o) {
    var x = '<div class="huisuggestnorm"';
    x += ' onmouseover="HUIsuggestHighlight(this)"';
    x += ' onmouseout="HUIsuggestUnhighlight(this)"';
    x += ' onmousedown="HUIsuggestSelect(\'' + o.id + '\',\'' + escape(data) + '\')"';
    x += '>';
    x += '<table><tr><td style="text-align: left;">';
    x += HUIsuggestSafe(data);
    x += '</td><td style="text-align: right;">';
    x += '<span class="huisuggestcount">';
    x += count + ' matches';
    x += '</span>';
    x += '</td></tr></table>';
    x += '</div>';
    o.innerHTML += x;
  }
}

function HUIsuggestResults(o,search,op,key,data) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o) {
    if (!o.suggestCache)
      o.suggestCache = new Object();
    o.suggestCache[key] = data;
    o.innerHTML = "";
    var n = 0;
    for (d in data) {
      n++;
      if (n > HUI_suggestMax) {
	if (self.huiDebug)
	  huiDebug("suggest: maximum count reached");
	o.innerHTML += '<div class="huisuggestnorm"><span class=\"unknown\">...</span></div>';
	break;
      }
      HUIsuggestMake(o,search,data[d].value,data[d].count);
    }
    if (n == 0) {
      o.innerHTML += "<div class=\"huisuggestnone\">No results</div>";
    }
    if (o.isFocused)
      HUIsuggestOpen(o);
  }
}

function HUIsuggestHighlight(o) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o)
    o.className= 'huisuggesthigh';
}

function HUIsuggestUnhighlight(o) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o)
    o.className = 'huisuggestnorm';
}

function HUIsuggestInit(suggestId,entryId,uri,action,arg,op) {
  var entry = document.getElementById(entryId);
  var suggest = document.getElementById(suggestId);
  if (entry && suggest) {
    suggest.entryObj = entry;
    suggest.suggestURI = uri;
    suggest.suggestAction = action;
    suggest.suggestArg = arg;
    suggest.suggestOp = op;
    suggest.isFocused = false;
  }
}

function HUIsuggestSetArg(o,arg) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o) {
    o.suggestArg = arg;
    // Hide it
    HUIsuggestClose(o);
  }
}

function HUIsuggestSetOp(o,op) {
  if (typeof o != "object")
    o = document.getElementById(o);
  if (o) {
    o.suggestOp = op;
    // Hide it
    HUIsuggestClose(o);
  }
}
