
var MaxIndex     = -1;
var MinIndex     = -1;
var CurrentIndex = -1;
var LocalList = new Array();
var SELECT_SIZE = 15;

var SelObj;  
var InputObj;

function init()
{
   SelObj = document.getElementById("all_list_id");
   InputObj = document.getElementById("txt_id");
  for(var i = 0; i < ObjectList.length; i++)
  {
    LocalList[i] = ObjectList[i]; //.toLowerCase();
  }
  LocalList.sort();
  MaxIndex = LocalList.length-1;
  MinIndex = 0;
  CurrentIndex = MinIndex;
  if(SELECT_SIZE < 1) { SELECT_SIZE = 1;}
  return buildAllSelect();
}

function showMatch(evt)
{
  evt = (evt) ? evt : new Object();
  var charCode = (evt.charCode)? evt.charCode : 0;
  var txt = InputObj.value;
  var lastChar = '';
  if ( (charCode > 32) && (charCode < 127))
  {
    lastChar = String.fromCharCode(charCode);
    txt = txt + lastChar;
  }

  SelObj.options[CurrentIndex].selected = false;

  CurrentIndex = findIndex(txt);
  //alert("Found index = " + CurrentIndex + " --> " + LocalList[CurrentIndex]);

  // alert("last char = " + lastChar + ", " + "entered text = " + txt + ", option substring = " + LocalList[CurrentIndex].substring(0, txt.length) + ", matching option = " + SelObj.options[CurrentIndex].value);
  if(  txt.toLowerCase() == LocalList[CurrentIndex].substring(0, txt.length).toLowerCase() )
  { 
    SelObj.options[CurrentIndex].selected = true;
  }
  else 
  {
    SelObj.options[CurrentIndex].selected = false;
  }
} 


function buildAllSelect()
{
  SelObj.options.length = 0;
  for(var i = MinIndex; i<=MaxIndex ; i++)
  {
    var arrayIndex = i;
    SelObj.options[i] = new Option( 
      LocalList[arrayIndex] + " ["  + ObjectCount[LocalList[arrayIndex]] +" matches]", //txt
      LocalList[arrayIndex], // val
      false,   // isDefaultSelectedFlag 
      false  // isSelectedFlag
    );
  }
  SelObj.options.length = MaxIndex-MinIndex+1;
  SelObj.size=SELECT_SIZE;
  return true;
}

function findIndex(txt)
{
  var length = LocalList.length;
  var s = (new String(txt)).toLowerCase();
  var index = Math.floor(MaxIndex/2); // MaxIndex=5 --> index = 2
  if (s.length == 0) {return index; }

  if(CurrentIndex != -1) {index = CurrentIndex;}

  var bigDelta = Math.ceil(length/25);
  var firstChar = s.charAt(0);
  var previous = 0;
  while(true)
  {
    compChar = LocalList[index].charAt(0).toLowerCase();
    if (firstChar.localeCompare(compChar) == 0 ) {break; }
    if (firstChar.localeCompare(compChar) > 0 ) 
    {  
      if(previous < 0) { break; }
      index = index + bigDelta; 
      if(index > MaxIndex) 
      { 
        index = MaxIndex;
        break; 
      }
      previous = 1;
    }
    else 
    {
      if(previous > 0) { break; }
      index = index - bigDelta; 
      if(index < MinIndex) 
      { 
        index = MinIndex;
        break; 
      }
      previous = -1;
    }
  }
//alert(txt + " should be near "+ LocalList[index]);
  previous = 0;
  while(true)
  { 
    var x =s.length >  LocalList[index].length ? LocalList[index].length : s.length;
    comp = LocalList[index].substring(0, x).toLowerCase();
  
    if(s.localeCompare(comp)== 0 ) {return index;}
    if(s.localeCompare(comp) > 0 ) 
    { 
//alert(txt + " >  "+ comp + ", index = " +index);
      if (previous < 0) { return index; }
      index++; 
      previous = 1;
    }
    else 
    { 
//alert(txt + " <  "+ comp + ", index = " +index);
      if (previous > 0) { return index; }
      index--; 
      previous = -1;
    }
    if(index < MinIndex) {return MinIndex;}
    if(index > MaxIndex) {return MaxIndex;}
  }
}

function newOption(val, selectedFlag)
{
  return new Option(val, val, false, selectedFlag);
}

function copyVal()
{
  // Copy the val of selected option to the input text.
  if( SelObj.selectedIndex != -1 ) 
  {
    InputObj.value= SelObj.options[SelObj.selectedIndex].value;
  }
  return true;
}

function selectVal(name)
{
  for(var i = 0; i < SelObj.options.length; i++)
  {
    if( SelObj.options[i].value.localeCompare(name) == 0 )
    {
      SelObj.options[i].selected = true;
      return true;
    }
  }
  return false;
}

function checkSolarBody()
{
  copyVal();
  // In case the user cut-and-pasted a name in the input text field:
  if (SelObj.selectedIndex == -1)
  {
    selectVal(InputObj.value);
  }
    
  if (SelObj.selectedIndex != -1)
  {
    // return confirm("You are interested in the Solar Body: "+ SelObj.options[SelObj.selectedIndex].value);
    return true;
  }
  else
  {
    // Required by AM 28 Sep 2006 (his last day before moving to Spain!):
    // Allow submission even if no solar body is selected.
    // // alert("Please select the Solarbody you are interested in.");
    // // return false;
    return true;
  }
}

