
/******************************************
******************************************
    SUGGESTION BOX FUNCTIONS
******************************************
******************************************/


var sdobj = getobj('searchdiv');

var curID = null;

var sdindex = null;
var timer = null;
var cachedres = new Array();

function myblur()
{
  timer = window.setTimeout("myhide()", 300);
}
function myhide()
{
  clearTimeout(timer);
  if (curID!=null) objShowHide('sd['+curID+']', 'none');
  objShowHide('searchdiv', 'none');
  sdindex = null;
}
function mysubmit()
{
  obj = getobj('theform');
  //obj.submit();
}
function myhandle()
{
  timer = window.setTimeout("mysubmit()", 300);
  return false;
}
function chkenter(e)
{
  oEvent = e?e:(window.event?window.event:event);
  if ( oEvent.keyCode==13 && sdindex!=null && sdindex<sdobj.childNodes.length )
  {
    chgval(sdobj.childNodes[sdindex]);
    return false;
  }
  return true;
}
function mysearch(e, newID)
{
  oEvent = e?e:(window.event?window.event:event);
  
  if ( typeof(cachedres[newID])=="undefined" )
  {
    cachedres[newID] = new Array();
  }
  
  if ( newID!=curID )
  {
    newobj = getobj('sd['+newID+']');
    if ( curID!=null )
    {
      oldobj = getobj('sd['+curID+']');
      oldobj.innerHTML = '';
      objShowHide('sd['+curID+']', 'none');
    }
    sdobj.innerHTML = '';
    try
    {
      sdobj.outerHTML = '';
      if ( typeof(sdobj.removeNode)!="undefined" ) 
      {
        sdobj.removeNode(true);
      }
      else
      {
        sdobj.parentNode.removeChild(sdobj);
      }
    }
    catch (ex) { }
    sdobj = document.createElement("DIV");
    sdobj.id = "searchdiv";
    sdobj.onmouseout = "myfocus";
    newobj.appendChild(sdobj);
    curID = newID;
  }
  
  if ( oEvent.keyCode==13 )
  {
    // event coming from pressing enter while an option from the suggestions is selected
    myhide();
    return false;
  }
  
  obj = getobj('sub_titles_'+newID+'');
  q = obj.value;
  target = sdobj;
  newstate = (q!=null)?(q!=""?"block":"none"):"none";
  
  
  eq = escape(q);
  
  if ( newstate=="block" && oEvent.keyCode==38 )
  {
    // key up, choose previous result, if any
    if ( sdindex==null ) 
    {
      sdindex = target.childNodes.length-1;
    }
    else
    {
      sdindex--;
      if ( sdindex<0 ) sdindex = target.childNodes.length-1;
    }
    myfocus(null, sdindex);
  }
  else if ( newstate=="block" && oEvent.keyCode==40 )
  {
    // key down, choose next result, if any
    if ( sdindex==null ) 
    {
      sdindex = 0;
    }
    else
    {
      sdindex++;
      if ( sdindex>target.childNodes.length-1 ) sdindex = 0;
    }
    myfocus(null, sdindex);
  }
  else if ( newstate=="block" && xmlhttp )
  {
    if ( cachedres[newID][eq]==null || typeof(cachedres[newID][eq])=="undefined" || cachedres[newID][eq]=="undefined" )
    {
      // have something to search, and the objects to do so
      url = "job_vals.php?id="+newID+"&q=" + eq;
      myxmlreq = new myXMLHttpRequest();
      if (myxmlreq)
      {
        myxmlreq.open ("GET", url, true);
        myxmlreq.setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');
        myxmlreq.send ("");
        myxmlreq.onreadystatechange = function () 
        {
          if ( myxmlreq.readyState == 4 && myxmlreq.responseText.indexOf("404 Not Found")<0 ) 
          {
            // re-add the cached content, if any, because with multiple divs, multiple results can show up
            try
            {
              cachedres[newID][eq] = myxmlreq.responseText;
              target.innerHTML = cachedres[newID][eq];
            }
            catch(ex) {}
            // show/hide the results div, depending on the value of the search field
            objShowHide('searchdiv', newstate);
            objShowHide('sd['+newID+']', 'block');
          }
        }
      }
    }
    else
    {
      // re-add the cached content, if any, because with multiple divs, multiple results can show up
      target.innerHTML = cachedres[newID][eq];
      // show/hide the results div, depending on the value of the search field
      objShowHide('searchdiv', newstate);
      objShowHide('sd['+newID+']', 'block');
    }
  }
  else 
  {
		target.innerHTML = "";
		sdindex = null;
    myblur();
  }
  
  return true;
}
function chgval(obj)
{
  if ( curID!=null )
  {
    getobj('sub_titles_'+curID+'').value = obj.innerHTML.replace(/&amp;/g, "&");
  }
  myhide();
}
function myfocus(obj, n)
{
  foundi = null;
  for ( i=0; i<sdobj.childNodes.length; i++ )
  {
    sdobj.childNodes[i].className = "entry";
    if ( sdobj.childNodes[i]==obj )
    {
      foundi = i;
    }
  }
  if ( typeof(n)!="undefined" && typeof(sdobj.childNodes[n])!="undefined" ) sdobj.childNodes[n].className="entryon";
  else if ( typeof(obj)!="undefined" && obj!=null ) 
  {
    obj.className="entryon";
    if (foundi!=null) sdindex = foundi;
  }
  else sdindex = null;
}


/******************************************
******************************************
    SUGGESTION BOX FUNCTIONS END
******************************************
******************************************/

