/*
      Title: Type Ahead JavaScript
      Written By: Eric Pascarello
      Version: 0.89B
      Version Date: 12/20/2004 10:52:35PM EST
      Email: A1ien51@hotmail.com
*/

/* You do not need to change anything in this file*/
      /* You do not need to change anything in this file*/
      //Create Reg Expression Flags
      	var ignoreCase = true;
	var matchAnywhere;
	var matchTextBoxWidth;
  	var theVisibleTime;
	var ShowNoMatchMessage;
	var noMatchingDataMessage;
	var theHiddenBox;
	var useTimeout;
 	var maxShowed;
 	    
      var regExFlags = "";
	  if(ignoreCase) regExFlags = "i";
	  var regExAny = "";
	  if(!matchAnywhere) regExAny = "^";

      //Writes the div element to the page
  	  if(useTimeout){
	    document.write('<span id="divOutput" class="spanTextDropdown" onmouseout="StartTimeout()" onmouseover="EraseTimeout();"></span>');
	  }
	  else{
	    document.write('<span id="divOutput" class="spanTextDropdown"></span>');
      }

	  var theTextBox;
      var currentValueSelected = -1;

      //Function recieves input from key press on text field
      function GiveOptions(e){

        if(useTimeout){
		  if(iAmTiming)EraseTimeout();
		  StartTimeout();
		}

        var nbr = 1;

        //Grab key press event
        if(document.all){ //IE
	      nbr = event.keyCode;
          theTextBox = event.srcElement;
		}
		else{  //Mozilla
		  nbr = e.which;
		  theTextBox = e.target;
		}

		xElem = theTextBox;
		if(theTextBox.value.length == 0){
			HideTheBox();            
			return false;
		}

		if(nbr==13){ //Enter Key
		  GrabHighlighted();
		  xElem.blur();
		  return false;
		}
		else if(nbr==38){ //Up Arrow
          MoveHighlight(-1);
		  return false;
		}
		else if(nbr==40){  //Down Arrow
          MoveHighlight(1);
		  return false;
		}
		else{}

        var theText = xElem.value;
		SetElementPostion(xElem,"divOutput");
		var theMatches = new Array();
		if(theText.length > 0){
		  theMatches = MakeMatches(theText,xElem.name);           //Determine matched array elements
		  theMatches = theMatches.join().replace(/\,/gi,"");  //Turn Array into String
		  document.getElementById("divOutput").innerHTML = theMatches;  //Set the document innerHTML
		  if(theMatches.length>0){
			document.getElementById("divOutput").innerHTML = theMatches;
		    document.getElementById("OptionsList_0").className="spanHighElement";  //make first item selected
			currentValueSelected = 0;
		  }
		  else{
	        currentValueSelected = -1;  //Remove any selection index
		    if(ShowNoMatchMessage)document.getElementById("divOutput").innerHTML = "<span class='noMatchData'>" + noMatchingDataMessage + "</span>";
			else HideTheBox();
		  }
		}


	  }

      //Variable used to number span element ids
      var countForId = 0;
     
      //Find all of the matches in the string
	  function MakeMatches(xCompareStr,xElemId){
	    countForId = 0;
	     var countShowed = 0;
	    var matchArray = new Array()
		var regExp = new RegExp(regExAny + xCompareStr,regExFlags);  //Createregualr expressions
		for(i=0;i<theOptions.length;i++){  //Loop through options
          var theMatch = theOptions[i][0].match(regExp);  //try to make a match
		  if(theMatch){
		    countShowed++;
		    if(countShowed < maxShowed){
		    	matchArray[matchArray.length] = CreateUnderline(theOptions[i][0],xCompareStr,i);  //Call function to make underline/span
		    }
		  }
		}
		return matchArray
	  }

      var iAmTiming = false;
      //Sets the position of the div under the text box in focus
	  function SetElementPostion(xElement,xPosElement){
	    var selectedPosX = 0;  //set defaults
		var selectedPosY = 0;
	    var theElement = xElement;
		var theElemHeight = theElement.offsetHeight; //Grab textbox width
		var theElemWidth = theElement.offsetWidth; //Grab textbox height
        while(theElement != null){  //Loop through the document tree
          selectedPosX += theElement.offsetLeft;  //Grab new X Position
          selectedPosY += theElement.offsetTop;   //Grab new Y Position
          theElement = theElement.offsetParent;   //Set to next element's parent
        }
		xPosElement = document.getElementById(xPosElement);  //get element reference
	    xPosElement.style.left = selectedPosX;  //Set X Position
		if(matchTextBoxWidth) xPosElement.style.width = theElemWidth;  //Set Witdh if required
        xPosElement.style.top = selectedPosY + theElemHeight;  //Set Y position
		xPosElement.style.display = "block";  //Display Block
	  }

      //Variables for create underline
      var undeStart = "<span class='spanMatchText'>";
	  var undeEnd = "</span>";

      //variables for span elements
	  var selectSpanStart = "<span style='width:100%;display:block;' class='spanNormalElement' onmouseover='SetHighColor(this)' ";
	  var selectSpanEnd ="</span>";

	  //Function makes underline under the text
      function CreateUnderline(xStr,xTextMatch,xVal){
	    selectSpanMid = "onclick='SetText(" + xVal + ")' id='OptionsList_" + countForId + "' theArrayNumber='"+ xVal +"'>"; //get click and ids
		var regExp = new RegExp(regExAny + xTextMatch,regExFlags);  //reg expression to match typed text
        var aStart = xStr.search(regExp)  //find start position of the text
	    var matchedText = xStr.substring(aStart,aStart + xTextMatch.length);  //grab the matched text (need to keep formatting)
		countForId++;
	    return selectSpanStart + selectSpanMid + xStr.replace(regExp,undeStart + matchedText + undeEnd) + selectSpanEnd; //return span elements
	  }

      //function sets the textbox and hidden textbox values
	  function SetText(xVal){

		theTextBox.value = theOptions[xVal][0];  //set text value
		theHiddenBox.value = theOptions[xVal][1];  //set the hidden value

		document.getElementById("divOutput").style.display = "none";  //hide the options list
		currentValueSelected = -1;  //remove the selected index

	  }

      //Gets value when option is clicked
	  function GrabHighlighted(){
		if(currentValueSelected != -1){
		  xVal = document.getElementById("OptionsList_" + currentValueSelected).getAttribute("theArrayNumber"); //grab the array index of the value
	      SetText(xVal);  //set value
		  document.getElementById("divOutput").style.display = "none"; //hide the options list
		}
	  }

      //Set High color when moused over
	  function SetHighColor(xElem){
	    if(xElem)currentValueSelected = xElem.id.slice(xElem.id.indexOf("_")+1,xElem.id.length); //Determine if returned full id name
		for(i=0;i<countForId;i++){ //loop through all selections
          document.getElementById('OptionsList_' + i).className = 'spanNormalElement'; //reset CSS class
		}
		document.getElementById('OptionsList_' + currentValueSelected).className = 'spanHighElement'; //Add highlight class
	  }

      //Handles the up an down arrows for moving highlight color
	  function MoveHighlight(xDir){
        if(currentValueSelected != -1){  //make sure options exist
		  newValue = parseInt(currentValueSelected) + parseInt(xDir); //Add direction
		  if(newValue >-1 && newValue <countForId){  //Make sure it stays within the bounds
		    currentValueSelected = newValue;  //set the selected index
		    SetHighColor();  //set the new CSS class
		  }
		}
	  }

	  function HideTheBox(){
		  document.getElementById("divOutput").style.display = "none"; //hide the options list
		  currentValueSelected = -1;
		  iAmTiming = false;
	  }

	  function EraseTimeout(){
	      clearTimeout(iAmTiming);
		  iAmTiming=false;
	  }
	  function StartTimeout(){
		iAmTiming = setTimeout("HideTheBox()",theVisibleTime);
	  }


