function AS_Init(obj)
{
  addEvent(obj, "blur", AS_Shutdown);
  addEvent(obj, "keydown", AS_KeyDown);
  addEvent(obj, "keypress", AS_Keypress);
  Make_Suggest(obj,0);
}

var asFocused = 0;
function AS_PannelFocus()
{
  asFocused = 1;
}

function AS_PannelBlur()
{
  asFocused = 0;
}

function AS_Shutdown(evt)
{
  if ( !evt ) evt = window.event;
  var obj = evt.srcElement ? evt.srcElement : evt.target;
  removeEvent(obj, "blur", AS_Shutdown);
  removeEvent(obj, "keydown", AS_KeyDown);
  removeEvent(obj, "keypress", AS_Keypress);
  if ( asFocused == 0 ) {
    AS_HidePannel(obj);
    var toid = obj.getAttribute("TOID");
    if ( toid ) {
      window.clearTimeout(toid);
    }
    AS_SetValue(obj,document.getElementById(obj.getAttribute("firstMatch")),true);
  }
}

function AS_KeyDown(evt)
{
  if ( !evt ) evt = window.event;
  var obj = evt.srcElement ? evt.srcElement : evt.target;
  var ch = evt.keyCode;
  if ( ch == 16 || ch == 17 || ch == 18 ) {
    return;
  }
  if ( ch == 9 ) {
    asFocused = 0;
    return;
  }
  var toid = window.setTimeout(function(){Make_Suggest(obj,ch)},50);
  obj.setAttribute("TOID", toid);
}

function AS_Keypress(evt)
{
  if ( !evt ) evt = window.event;
  var obj = evt.srcElement ? evt.srcElement : evt.target;
}

function AS_XmlLoaded(obj, ch, xmlhttp)
{
  if ( xmlhttp.status == 200 ) {
    var asData = xmlhttp.responseXML;
    if ( asData != null && asData.parseError != null && asData.parseError.errorCode != 0 ) {
      alert( asData.parseError.reason );
      return;
    }
    var asDIV = AS_CreatePannel(obj, asData);
    obj.setAttribute("asDIV", asDIV.id);
    obj.setAttribute("asData", asData);
    Make_Suggest(obj, ch);
  }
}

function AS_GetXmlData(obj,ch)
{
  var asData = obj.getAttribute("asData");
  if ( asData == null ) {
    var xmlhttp = obj.getAttribute("xmlhttp");
    if ( xmlhttp == null ) {
      xmlhttp = getXmlHttp();
      obj.setAttribute("xmlhttp", xmlhttp);
    }
    if ( xmlhttp ) {
      var url = document.location.href.replace(/tls\/.*$/,'tls/xml/loadList.xml');
      xmlhttp.open("POST", url);
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
          //AS_XmlLoaded(xmlhttp.status, xmlhttp.getAllResponseHeaders(), xmlhttp.responseText);
          AS_XmlLoaded(obj, ch, xmlhttp);
        }
      }
      xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
      var body = "dao="+obj.getAttribute("asClass")+"&field="+obj.getAttribute("asField");
      if ( obj.getAttribute("asType") != null ) {
        body+="&asType="+obj.getAttribute("asType");
      }
      xmlhttp.send(body);
    }
  }
  return asData;
}

function AS_SetValue(obj, oTR, flag)
{
  if ( oTR ) {
    obj.value = oTR.cells[0].getAttribute("value");
    dispatchEvent(obj, "change");
  } else if ( flag ) {
    obj.value = '';
    dispatchEvent(obj, "change");
  }
  var linkKey = obj.getAttribute("linkKey");
  if ( linkKey != null ) {
    var lKeys = document.getElementsByName(linkKey);
    for(var k=0; k<lKeys.length; k++) {
      lKeys[k].value = oTR!=null?oTR.cells[0].getAttribute("key"):'';
    }
  }
}

function AS_Click(evt)
{
  if ( !evt ) evt = window.event;
  var oTD = evt.srcElement ? evt.srcElement : evt.target;
  var oTR = oTD.parentNode;
  var oDIV = oTR.parentNode;
  while( oDIV != null && oDIV.tagName != 'DIV' ) 
    oDIV = oDIV.parentNode;
  var obj = document.getElementById(oDIV.getAttribute("srcElement"));
  if ( obj != null ) {
    AS_HidePannel(obj);
    AS_SetValue(obj, oTR);
  }
}

function AS_Select(obj, helpTable, oTR, l)
{
  obj.setAttribute("firstMatch", oTR.id);
  oTR.bgColor = "highlight";
  oTR.style.color = "white";
  AS_SetValue(obj,oTR);
  if ( obj.setSelectionRange ) {
    if ( l != null ) {
      obj.setSelectionRange(l, obj.value.length)
    } else {
      obj.setSelectionRange(obj.value.length, obj.value.length)
    }
  } else {
    var r = obj.createTextRange();
    if ( l != null ) {
      r.moveStart("character", l);
      r.moveEnd("character", obj.value.length);
    } else {
      r.moveStart("character", obj.value.length);
      r.collapse();
    }
    r.select();
  }
  //oTR.scrollIntoView();
}

var chAry = new Array(8,35,36,37,39,45,46,46,91);
function AS_Match(obj,ch)
{
  //AS_SetValue(obj,null);
  var asDIV = obj.getAttribute("asDIV");
  if ( asDIV != null ) {
    asDIV = document.getElementById(asDIV);
    var helpTable = document.getElementById(asDIV.getAttribute("helpTable"));
    if ( helpTable != null ) {
      var firstMatch = null;
      if ( ch == 40 || ch == 38 ) {
        var oTR = null; 
        firstMatch = document.getElementById(obj.getAttribute("firstMatch"));
        if ( firstMatch != null ) {
          firstMatch.bgColor = '';
          firstMatch.style.color = '';
          oTR = (ch==40)?firstMatch.nextSibling:firstMatch.previousSibling;
        } else {
          oTR = (ch==40)?helpTable.rows[0]:helpTable.rows[helpTable.rows.length-1];
        }
        while( oTR != null && oTR.style.display == 'none' ) {
          oTR = (ch==40)?oTR.nextSibling:oTR.previousSibling;
        }
        if ( oTR != null ) {
          AS_Select(obj, helpTable, oTR);
        } else {
          obj.removeAttribute("firstMatch");
        }
        return;
      }
      obj.removeAttribute("firstMatch");
      var curVal = obj.value;
      var rep = new RegExp("^"+curVal.toUpperCase());
      for(var i=0; i<helpTable.rows.length; i++) {
        var oTR = helpTable.rows[i];
        oTR.bgColor = '';
        oTR.style.color = '';
        oTR.style.display = '';
        if ( curVal.length > 0 ) {
          var oTD = oTR.cells[0];
          var val = oTD.getAttribute("value");
          if ( val.toUpperCase().match(rep) ) {
            if ( firstMatch == null ) {
              firstMatch = oTR;
            }
          } else {
            oTR.style.display = 'none';
          }
        }
      }
      obj.setAttribute("matchValue", curVal.id);
      for(var j=0; j<chAry.length; j++) {
        if ( ch == chAry[j] )
          return;
      }
      if ( firstMatch ) {
        if ( ch == 0 )
          AS_Select(obj, helpTable, firstMatch, 0);
        else 
          AS_Select(obj, helpTable, firstMatch, curVal.length);
      }
    }
  }
}

var count=0;
function Make_Suggest(obj, ch)
{
  //window.status = obj.name + "=" + obj.value + "/" + ch + "(" + (count++) + ")" + obj.value.length;
  var asData = AS_GetXmlData(obj, ch);
  if ( asData != null ) {
    var asDIV = document.getElementById(obj.getAttribute("asDIV"));
    if ( asDIV == null ) {
      asDIV = AS_CreatePannel(obj, asData);
      obj.setAttribute("asDIV", asDIV.id);
    }
    AS_Match(obj,ch);
    AS_ShowPannel(obj);
  }
}

function AS_ShowPannel(srcE)
{
  var asDIV = document.getElementById(srcE.getAttribute("asDIV"));
  if ( asDIV != null ) {
    var iframe = document.getElementById(asDIV.getAttribute("iframe"));
    var pos = findXY(srcE, iframe);
    iframe.style.left = pos.left;
    iframe.style.top = pos.top + srcE.offsetHeight + 1;
    iframe.style.width = srcE.offsetWidth;
    iframe.style.height = "200px";
    asDIV.style.left = pos.left;
    asDIV.style.top = pos.top + srcE.offsetHeight + 1;
    asDIV.style.width = srcE.offsetWidth;
    asDIV.style.height = "200px";
    asDIV.style.display = '';
    iframe.style.display = '';
  }
}

function AS_HidePannel(srcE)
{
  var asDIV = document.getElementById(srcE.getAttribute("asDIV"));
  if ( asDIV != null ) {
    var iframe = document.getElementById(asDIV.getAttribute("iframe"));
    iframe.style.display = 'none';
    asDIV.style.display = 'none';
  }
}

function AS_CreatePannel(srcE, xmlDoc)
{
  if ( srcE.id == '' || srcE.id == null ) {
    srcE.id = getUniqueID();
  }
  var iframe = document.createElement("IFRAME");
  iframe.id = getUniqueID();
  iframe.style.display = 'none'
  iframe.style.position = "absolute";
  iframe.setAttribute("frameBorder", "0"); 
  iframe.tabIndex = -1;
  //document.appendChild(iframe);
  srcE.parentNode.insertBefore(iframe, srcE);

  var helpE = document.createElement("DIV")
  helpE.id = getUniqueID();
  helpE.setAttribute("iframe", iframe.id);
  helpE.setAttribute("srcElement", srcE.id);
  helpE.style.display = 'none'
  helpE.style.position = "absolute";
  helpE.style.border = "1px solid gray";
  helpE.style.height = "200px";
  helpE.style.overflow = "auto";
  helpE.style.background = 'white';
  //helpE.onmouseover = AS_PannelFocus;
  //helpE.onmouseout = AS_PannelBlur;
  //document.appendChild(helpE);
  srcE.parentNode.insertBefore(helpE, srcE);

  var helpTable = helpE.appendChild(document.createElement("TABLE"));
  helpTable.id = getUniqueID();
  helpE.setAttribute("helpTable", helpTable.id);
  helpTable.width ="100%";
  var datas = xmlDoc.getElementsByTagName("data")
  for(var i=0; i<datas.length; i++) {
    var child = datas[i];
    var oTR = helpTable.insertRow(-1);
    oTR.id = getUniqueID();
    var oTD = oTR.insertCell(-1);
    oTD.innerHTML = child.firstChild.nodeValue;
    oTD.style.cursor = "pointer";
    oTD.onmouseover = AS_PannelFocus;
    oTD.onmouseout = AS_PannelBlur;
    oTD.onclick = AS_Click;
    oTD.setAttribute("key", child.getAttribute("id"));
    oTD.setAttribute("value", child.firstChild.nodeValue);
  }
  iframe.width = helpE.clientWidth;
  iframe.height = helpE.clientHeight;
  return helpE;
}