// JavaScript Document
// auto complete library

var element = null;
var popup = null;
var eltname;

function autosuggest_init() {
  document.getElementById('catalognos').onkeyup = autosuggest_keyup;
  document.getElementById('catalognos').onblur = autosuggest_blur;
}

function autosuggest_keyup() {
  var elt = document.getElementById('catalognos');
	var value = elt.value;
	if (value == undefined && document.all) {
		value = document.all.catalogno.value;
  }
  if (value == undefined)
    value = document.forms['form2'].catalogno.value;

  var url = "ajax/catalogx.php?catalogno=" + escape(value);
  as_retrieveXML(url);
}

function autosuggest_blur() {
   if (!ignore_blur) hidePopup();
}


function findElementPosX(obj) {
  curleft = 0;
  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  }//if offsetParent exists
  else if (obj.x)
    curleft += obj.x
  return curleft;
}//findElementPosX

function setSelection(txt) {
  if (document.all) {
    if (txt.indexOf('more results') == -1)
		  document.forms['form2'].catalogno.value = txt;
    hidePopup();
    document.forms['form2'].submit();
/*
    for (var j = 0; j < document.forms.length; j++) {
      if (document.forms[j].catalogno)
        document.forms[j].submit();
    }
*/
   } else {
    var element = document.getElementById('catalognos');
    if (txt.indexOf('more results') == -1)
      element.value = txt;
    hidePopup();
    element.onblur = autosuggest_blur;
    element.form.submit();
  }
}

function hidePopup() {
  if (popup != null) {
    popup.style.visibility = "hidden";
    popup.style.display = "none";
    popup = null;
  }
}

function element() {
  return document.getElementById('catalognos');
}

var ignore_blur = 0;

function showPopup(list) {
  ignore_blur = 0;
  hidePopup();
  if (list.length <= 0) return;
  if (popup == null) {
    //popup = document.createElement('div');
    popup = document.getElementById('autosuges');
    //popup.className = "autosuggest";
    popup.style.position = "absolute";
    popup.style.zIndex = 999;
	popup.style.visibility = "visible";
    popup.style.display = "block";
	/*
    var pos = getPosition('catalognos');
		if (isNaN(pos.x)) {
			pos.x = findPosX(document.getElementById('catalognos'));
			pos.y = findPosY(document.getElementById('catalognos'));
		} else if (pos.x <= 5) {
      pos.x = findPosX(document.getElementById('searchpanel'));
      pos.y = findPosY(document.getElementById('searchpanel')) + document.getElementById('searchpanel').offsetHeight - 7;
      pos.x += 4;
    }
    popup.style.left = pos.x;
    popup.style.top = pos.y + document.getElementById('catalognos').offsetHeight;
    popup.style.width = document.getElementById('catalognos').offsetWidth;
	*/
    popup.style.border = "solid blue 1px;";
    popup.onmouseover = function() {
      ignore_blur = 1;
    }
    popup.onmouseout = function() {
      ignore_blur = 0;
    }
    //document.body.appendChild(popup);
  }
  popup.innerHTML = "";
  for (var j = 0; j < list.length; j++) {
    var item = list[j][0];
    var a = document.createElement('a');
    a.href = "javascript:setSelection('" + list[j][0] + "');";

//  list[j][0] = list[j][0].replace(/ /g, "");
    var tn = document.createTextNode(list[j][0]);
    a.appendChild(tn);

    popup.appendChild(a);
  }
}

function dump(arr) {
  var str = "Array(" + arr.length + "):\n";
  for (var j = 0; j < arr.length; j++) {
    str += "  #" + j + " = " + arr[j] + "\n";
  }
}

var first = 1;

function as_processXML(data) {
 var elements = data.getElementsByTagName('i');

 var list = Array();
 for (var j = 0; j < elements.length; j++) {
   var str = elements[j].getElementsByTagName('p')[0].firstChild.nodeValue;
   list.push(new Array(str));
 }
 showPopup(list); 
}

var req;

function as_retrieveXML(url) {
  if (window.XMLHttpRequest) { // Non-IE browsers
    req = new XMLHttpRequest();
    req.onreadystatechange = as_processStateChange;
    try {
      req.open("GET", url, true);
    } catch (e) {
//    alert(e);
      return;
    }
    req.send(null);
  } else if (window.ActiveXObject) { // IE
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
    if (req) {
      req.onreadystatechange = as_processXML;
      req.open("GET", url, true);
      req.send();
    }
  }
}

function as_processStateChange() {
  if (req.readyState == 4) { // Complete
    if (req.status == 200) { // OK response
      as_processXML(req.responseXML);
    } else {
//    alert("Problem: " + req.status + "/" + req.statusText);
    }
  }
}

function findPosX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
    curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}

function getPosition(elementId)
{

var element = document.getElementById(elementId);
var left = 0;
var top = 0;
				
if (element != null)
{
    // Try because sometimes errors on offsetParent after DOM changes.
    try
    {
        while (element.offsetParent)
        {
            // While we haven't got the top element in the DOM hierarchy
            // Add the offsetLeft
            left += element.offsetLeft;
            // If my parent scrolls, then subtract the left scroll position
            if (element.offsetParent.scrollLeft) {left -= element.offsetParent.scrollLeft; }
				
            // Add the offsetTop
            top += element.offsetTop;
            // If my parent scrolls, then subtract the top scroll position
            if (element.offsetParent.scrollTop) { top -= element.offsetParent.scrollTop; }
				
            // Grab
            element = element.offsetParent;
        }
    }
    catch (e)
    {
        // Do nothing
    }
				
    // Add the top element left offset and the windows left scroll and subtract the body's client left position.
    left += element.offsetLeft + document.body.scrollLeft - document.body.clientLeft;
				
    // Add the top element topoffset and the windows topscroll and subtract the body's client top position.
    top += element.offsetTop + document.body.scrollTop - document.body.clientTop;
}
return {x:left, y:top};

}
