var OPTION_SIZE=14;
//*******************************************************************
function sub() {
    var s = $F('symbol_input').toUpperCase();
    var i = symbols.indexOf(s);
    if (i >= 0) {
    	var url = "trading/cik" + ciks[i] + ".html";
    	window.location = url;
    } else {
        Element.update($('lookmsg'), "Symbol " + s + " not found.");
    }
    return false;
}
//*******************************************************************
function doclick() {
    // sub();
    return false;
}
//*******************************************************************
function SetProperties(xElem,xHidden,xserverCode,
    xignoreCase,xmatchAnywhere,xmatchTextBoxWidth,
    xshowNoMatchMessage,xnoMatchingDataMessage,xuseTimeout, xtheVisibleTime){
        var props={
	        elem: xElem,
	        hidden: xHidden,
	        serverCode: xserverCode,
	        regExFlags: ( (xignoreCase) ? "i" : "" ),//case
	        regExAny: ( (xmatchAnywhere) ? "" : "^" ),
	        matchAnywhere: xmatchAnywhere,
	        matchTextBoxWidth: xmatchTextBoxWidth,
	        theVisibleTime: xtheVisibleTime,
	        showNoMatchMessage: xshowNoMatchMessage,
	        noMatchingDataMessage: xnoMatchingDataMessage,
	        useTimeout: xuseTimeout
    };
    AddHandler(xElem);
    return props;
}
//*******************************************************************
function AddHandler(objText){
  objText.onkeyup = GiveOptions;
  objText.onblur = function(){
    //alert("this.obj.useTimeout=" + this.obj.useTimeout + "  );
    if(this.obj.useTimeout)StartTimeout();
  }   
}

//*******************************************************************
   var strLastValue = "";
  var bMadeRequest;
  var theTextBox;
  var objLastActive;
  var currentValueSelected = -1;
  var bNoResults = false;
  var isTiming = false;

//*******************************************************************
function GiveOptions(e){
   Element.update($('lookmsg'), "");
    var intKey = -1;
    if(window.event){
      intKey = event.keyCode;
      theTextBox = event.srcElement;
    }  else {
      intKey = e.which;
      theTextBox = e.target;
    }
     if(theTextBox.value.length == 0  ){
      HideTheBox();
      strLastValue = "";
          return false;
    }
    if(theTextBox.obj.useTimeout){
      if(isTiming)EraseTimeout();
      StartTimeout();
    }
   
  
    if(intKey == 13){
            GrabHighlighted();
            theTextBox.blur();
            return false;
    }   else if(intKey == 38){
            MoveHighlight(-1);
            return false;
    }   else if(intKey == 40){
            MoveHighlight(1);
            return false;
     }  else {
     }
     
    BuildList(theTextBox.value);
    strLastValue = theTextBox.value;
}
 
//*******************************************************************
function BuildList(theText){
    SetElementPosition(theTextBox);
    var theMatches = MakeMatches(theText);
    theMatches = theMatches.join().replace(/\,/gi,"");
    if(theMatches.length > 0){
      document.getElementById("spanOutput").innerHTML = theMatches;
      document.getElementById("OptionsList_0").className="highEl";
      currentValueSelected = 0;
      bNoResults = false;
    }  else {
      currentValueSelected = -1;
      bNoResults = true;
      if(theTextBox.obj.showNoMatchMessage)
        document.getElementById("spanOutput").innerHTML = "<span class='noMatchData'>" +
          theTextBox.obj.noMatchingDataMessage +"</span>";
      else HideTheBox();
    }
}
//*******************************************************************
      function SetElementPosition(theTextBoxInt){
        var selectedPosX = 0;
        var selectedPosY = 0;
        var theElement = theTextBoxInt;
        if (!theElement) return;
        var theElemHeight = theElement.offsetHeight;
        var theElemWidth = theElement.offsetWidth;
        while(theElement != null){
          selectedPosX += theElement.offsetLeft;
          selectedPosY += theElement.offsetTop;
          theElement = theElement.offsetParent;
        }
        xPosElement = document.getElementById("spanOutput");
        xPosElement.style.left = selectedPosX;
        if(theTextBoxInt.obj.matchTextBoxWidth)
          xPosElement.style.width = theElemWidth;
        xPosElement.style.top = selectedPosY + theElemHeight
        xPosElement.style.display = "block";
        if(theTextBoxInt.obj.useTimeout){
          xPosElement.onmouseout = StartTimeout;
          xPosElement.onmouseover = EraseTimeout;
        } else {
          xPosElement.onmouseout = null;
          xPosElement.onmouseover = null;
        }
      }
//*******************************************************************
      var countForId = 0;
      function MakeMatches(xCompareStr){
        countForId = 0;
        var matchArray = new Array();
        var regExp = new RegExp(theTextBox.obj.regExAny + xCompareStr,theTextBox.obj.regExFlags);
        for(i=0;i<symbols.length;i++){
          var theMatch = symbols[i] .match(regExp);
          if(theMatch){
            matchArray[matchArray.length]= CreateUnderline(symbols[i] , xCompareStr,i);
          }
          if (matchArray.length > OPTION_SIZE) {
              break;
          };
        }
        return matchArray;
      }
//*******************************************************************
      var hiStart = "<span class='spanMatchText'>";
      var hiEnd = "</span>";
      var selStart = "<span style='width:100%;display:block;' class='normalEl' onmouseover='SetHighColor(this)'";
      var selEnd ="</span>";
      function CreateUnderline(xStr,xTextMatch,xVal){
        selMid = "onclick='SetText(" + xVal + ")'" + "id='OptionsList_" +
          countForId + "' theArrayNumber='"+ xVal +"'>";
        var regExp = new RegExp(theTextBox.obj.regExAny + xTextMatch,theTextBox.obj.regExFlags);
        var aStart = xStr.search(regExp);
        var matchedText = xStr.substring(aStart, aStart + xTextMatch.length);
        countForId++;
        return selStart + selMid + xStr.replace(regExp,hiStart + matchedText + hiEnd) + selEnd;
      }
 
      function MoveHighlight(xDir){
        if(currentValueSelected >= 0){
          newValue = parseInt(currentValueSelected) + parseInt(xDir);
          if(newValue > -1 && newValue < countForId){
            currentValueSelected = newValue;
            SetHighColor (null);
          }
        }
      }
      function SetHighColor(theTextBox){
        if(theTextBox){
          currentValueSelected = theTextBox.id.slice(theTextBox.id.indexOf("_")+1,
          theTextBox.id.length);
        }
        for(i = 0; i < countForId; i++){
          document.getElementById('OptionsList_' + i).className ='normalEl';
        }
        document.getElementById('OptionsList_' + currentValueSelected).className = 'highEl';
      }
 
      function SetText(xVal){
        theTextBox.value = symbols[xVal] ; //set text value
     
        document.getElementById("spanOutput").style.display = "none";
        currentValueSelected = -1; //remove the selected index
      }
      function GrabHighlighted(){
        if(currentValueSelected >= 0){
          xVal = document.getElementById("OptionsList_" +
          currentValueSelected).getAttribute("theArrayNumber");
          SetText(xVal);
          HideTheBox();
        }
      }
      function HideTheBox(){
        document.getElementById("spanOutput").style.display = "none";
        currentValueSelected = -1;
        EraseTimeout();
      }
 
      function EraseTimeout(){
        clearTimeout(isTiming);
        isTiming = false;
      }
function StartTimeout(){
  // alert("theTextBox.obj.theVisibleTime=" );
  if (theTextBox)
  isTiming = setTimeout("HideTheBox()",theTextBox.obj.theVisibleTime);
}