<!--

var QSUG_MODE_SIMPLE = 1;
var QSUG_MODE_ADV = 2;
var QSUG_MODE_RESULT = 3;

var _boxName = "";
var _qsugObj = null;
var _userInput = "";
var _autoComUrl;
var _queryStr = "";
var _regPattern = /^[ ]+/g;
var _fUDArrow = false;
var _fIgnoreKey = false;
var _fDelKey = false;
var _boxTermWidth;
var _boxMode;
var _qsugTimer;
var _timerSec = 350;

function QSugInit(boxName, mode)
{
   if(!param['ShowQSug'] || param['ShowQSug'] != "1")
      return false;
   
   if((_qsugObj = new AutoMenu) == null)
      return false;
      
   _boxName = boxName;
   if(mode == "adv")
   {
      _boxMode = QSUG_MODE_ADV;
      _boxTermWidth = "240px";
   }
   else if(mode == "result")
   {
      _boxMode = QSUG_MODE_RESULT;
      _boxTermWidth ="160px";
   }
   else
   {
      _boxMode = QSUG_MODE_SIMPLE;
      _boxTermWidth = "185px";
   }   

   /* set autocomplete url */
   _autoComUrl = param['CGIPrefix'] + 'search/qsug.cgi';

   /* bind a re-position function when window re-size */
   window.onresize = QSugSetMenuPos;
   QSugSetMenuPos();

   /* set the bg-color of auto-menu */
   document.getElementById(_qsugObj.menuId).style.backgroundColor = "#FFFFFF";

   /* set the width of auto-menu to the width of input-text */
   /* IE only */
   if(document.all)
   {
      document.getElementById(_qsugObj.menuId).style.width =
      document.getElementById(_boxName).offsetWidth;
   }
   /* FireFox only */
   else
   {
      document.getElementById(_qsugObj.menuId).style.width =
      (parseInt(document.getElementById(_boxName).offsetWidth, 10) -
      parseInt(document.getElementById(_qsugObj.menuId).style.borderRightWidth, 10) -
      parseInt(document.getElementById(_qsugObj.menuId).style.borderLeftWidth, 10)) + "px";
   }

   return true;
}

/* move the auto menu to close the actived input text */
function QSugSetMenuPos()
{
   var inBox = document.getElementById(_boxName);
   var x, y;
   var count;

   /* if there is no input-text, then abort */
   if(null == inBox)
      return;

   /* find the offset between input-text to auto-menu */
   x = inBox.offsetLeft;
   y = inBox.offsetTop + inBox.offsetHeight;

   /* find out the distance offset from input-text to body */
   /* irene, 080623, avoid js error that inBox.offsetParent is undefined */
   for (; inBox = inBox.offsetParent; inBox)
   {
      if(inBox.tagName == "BODY")
         break;
      x += inBox.offsetLeft;
      y += inBox.offsetTop;
   }

   /* move the auto-menu*/
   _qsugObj.Move(x, y);
}

/* bind auto-menu to input box */
function QSugBind(event)
{
   var menu;

   /* null automenu obj */
   if(_qsugObj == null)
      return 0;

   /* bind the onclick event on auto-menu */
   menu = document.getElementById(_qsugObj.menuId);

   /* move the auto-menu */
   QSugSetMenuPos();

   return 0;
}

/* hide the auto-menu */
function QSugUnBind(event)
{
   if(_qsugObj == null)
      return 0;

   _qsugObj.Hide();

   return 0;
}

function QSugGetKey(e)
{
   /* null automenu obj */
   if(_qsugObj == null)
      return 0;
   
   var obj;
   var nKey;   
   var num;
   
   /* get srcObj and key from event */
   /* IE only */
   if(document.all)
   {
      e = event;
      nKey = e.keyCode;
      obj = e.srcElement;
   }
   /* FireFox only */
   else
   {
      nKey = e.which;
      obj = e.target;
   }

   /* parse key code to action */
   _fUDArrow = false;
   _fIgnoreKey = false;
   _fDelKey = false;
   switch(nKey)
   {
      /* enter */
      case 13:
         clearTimeout(_qsugTimer);
         break;

      /* esc */
      case 27:
         break;

      /* backspace, delete */
      case 8:
      case 46:
         _qsugObj.Select(-1);
         _fDelKey = true;
         break;

      /* pg up, pu down, home, end, left arrow, right arrow */
      case 33:
      case 34:
      case 35:
      case 36:
      case 37:
      case 39:
         _fIgnoreKey = true;
         break;

      /* up arrow */
      case 38:
         _fUDArrow = true;         
         itemNo = _qsugObj.SelectedIdx();
         if(itemNo < 0)
         {            
            if(document.all && _boxMode == QSUG_MODE_RESULT)
               _qsugObj.Select(_qsugObj.Length()-2);
            else
               _qsugObj.Select(_qsugObj.Length()-1);
         }
         else if(itemNo == 0)
            _qsugObj.Select(-1);
         else
            _qsugObj.Select(itemNo - 1);
         break;

      /* down arrow */
      case 40:
         _fUDArrow = true;
         itemNo = _qsugObj.SelectedIdx();
         if(document.all && _boxMode == QSUG_MODE_RESULT)
            num = _qsugObj.Length()-2;
         else
            num = _qsugObj.Length()-1;
         
         if(itemNo < 0)
            _qsugObj.Select(0);
         else if(itemNo >= num)
            _qsugObj.Select(-1);
         else
            _qsugObj.Select(itemNo+1);
         break;

      /* other keys */
      default:
         if(e.altKey)
            _fIgnoreKey = true;
         _qsugObj.Select(-1);         
         break;
   }

   return;
}

function QSugSelectTerm(e)
{
   /* null automenu obj */
   if(_qsugObj == null)
      return 0;
   
   var obj = document.getElementById(_boxName);
   var pos;
   
   if(_fIgnoreKey == true)
      return;

   if(_fUDArrow == false)
      _userInput = obj.value;

   if(_fDelKey == true)
   {
      clearTimeout(_qsugTimer);
      _qsugTimer = setTimeout("QSugGetTerms()", _timerSec);
      return;
   }

   if(_qsugObj.SelectedIdx() < 0)
   {  
      if(obj.value == "")
      {
         _qsugObj.Reset();
      }
      else
      {
         /* cancel the highlight, no need to get new terms */
         if(_fUDArrow == true)
         {
            obj.value = _userInput;
            return;
         }
            
         clearTimeout(_qsugTimer);
         _qsugTimer = setTimeout("QSugGetTerms()", _timerSec);
      }
   }
   else
   {      
      obj.value = _qsugObj.Value();
      _qsugObj.Show();
   }

   return;
}

function QSugGetTerms( )
{
   /* null automenu obj */
   if(_qsugObj == null)
      return 0;
   
   var curStr;

   curStr = document.getElementById(_boxName).value.replace(_regPattern, "");

   /* empty query string */
   if(curStr == "")
   {      
      _queryStr = "";
      _qsugObj.Reset();
      _qsugObj.Hide();
      return;
   }

   if(_queryStr == curStr)
   {
      return;
   }

   _queryStr = curStr;

   /* get suggestion terms */
   var url = _autoComUrl + "?magno=" + Math.random();
   var data = "&act=get&nterm=10&qstr=" + XMLHTTPEscPostValue(_queryStr);
   var autoComConn = null;

   /* append dbs */      
   if(_boxMode == QSUG_MODE_SIMPLE)
      dbs = GenDbList("");
   else if(_boxMode == QSUG_MODE_ADV)
      dbs = GenDbList("");
   else if(_boxMode == QSUG_MODE_RESULT)
      dbs = document.getElementsByName("dbs")[0].value;
   else
      dbs = "";
      
   if(dbs == "")
      return false;

   data += "&dbs="+dbs;

   if((autoComConn = XMLHTTPInit()) == null)
      return false;

   XMLHTTPAsyncResult(autoComConn, url , QSugShowTerm, null, data);

   return;
}

function QSugShowTerm(retBuf)
{
   var i;
   var result;
   var curStr;
   var itemList;
   var data;
   var nItem = 0;
   var infoStr = "";
   var attrStr = "";   

   curStr = document.getElementById(_boxName).value.replace(_regPattern, "");
   
   if(curStr.length == 1)
   {
      if(curStr.charCodeAt(0) < 128)
         return false;
   }
   
   if(_queryStr == curStr)
   {
      _qsugObj.Reset();

      /* parse dir list to auto-menu item */
      if(null != retBuf &&
         (result = retBuf.replace(/\r/g, "").replace(_regPattern, "")) != "" &&
          result.substr(0, 3) == "OK\n")
      {
         itemList = result.split("\n");

         for(i = 1; i < itemList.length; i++)
         {
            if((itemList[i] = itemList[i].replace(_regPattern, "")) != ""
               && (data = itemList[i].split("\t")).length == 2)
            {
               attrStr = ' onMouseDown="QSugSubmitTerm()" ';
               infoStr = '<div onMouseOver="QSugSelectTerm_Mouse(' +  nItem + ')" ' + attrStr + ' style="cursor:default;float:left;overflow:hidden;width:3px;">&nbsp;</div>'
                         + '<div onMouseOver="QSugSelectTerm_Mouse(' +  nItem + ')" ' + attrStr + ' style="cursor:default;float:left;overflow:hidden;width:'+_boxTermWidth+';">' + data[1] + '</div>'
                         + '<div onMouseOver="QSugSelectTerm_Mouse(' +  nItem + ')" ' + attrStr + ' style="cursor:default;float:left;text-align:left;width:5px;">&nbsp;</div>'
                         + '<div onMouseOver="QSugSelectTerm_Mouse(' +  nItem + ')" ' + attrStr + ' style="color:green;cursor:default;float:left;text-align:right;width:120px;">'+ data[0] + ' ' + TEXT_QSUG_MATCH + '</div>';
               _qsugObj.Add(infoStr, data[1]);
               nItem++;
            }
         }  

         if(document.all && _boxMode == QSUG_MODE_RESULT)
         {
            var menu = document.getElementById(_qsugObj.menuId);            
            menu.innerHTML += '<iframe style="position:absolute;z-index:-1;top:0;left:0;width:100%;height:100%;" \
                              frameborder="0" scrolling="No"></iframe>';
         }     
         
         _qsugObj.Show();
      }
   }

   return true;
}

function QSugSelectTerm_Mouse(nItem)
{
   /* null automenu obj */
   if(_qsugObj == null)
      return 0;

   _qsugObj.Select(nItem);

   return;
}

function QSugSubmitTerm()
{
   /* null automenu obj */
   if(_qsugObj == null)
      return 0;
   
   document.getElementById(_boxName).value = _qsugObj.Value();
   
   if(_boxMode == QSUG_MODE_SIMPLE)      
      SubmitSimple();
   else if(_boxMode == QSUG_MODE_ADV)
      SubmitAdv();
   else if(_boxMode == QSUG_MODE_RESULT)
      NewQuery('top');
   else 
      return false;
}

-->


