/*
 * JavaScript to handle autocompleting dropdown list on station search page.
 *
 * Note: The variable locationList is defined in the main page which is an array of locations (strings).
 *
 */

//
// Set this to true if you want your text to match the start of the location name, 
// false will match anywhere in the name, not just the start.
//
var startingWith = true;

// 
// Show or hide dropdown div...
//
function ShowDropDown(toShow){
  if (toShow){
    var townDdl = document.getElementById('ddlTown');
    var suburbDdl = document.getElementById('ddlSuburb');
    if (townDdl){
      townDdl.style.display = 'none';
    }
    if (suburbDdl){
      suburbDdl.style.display = 'none';
    }
    document.getElementById('autoCompleteList').style.display = 'block';
  } else {
    document.getElementById('autoCompleteList').style.display = 'none';
    var townDdl = document.getElementById('ddlTown');    
    var  suburbDdl = document.getElementById('ddlSuburb');
    if (townDdl){
      townDdl.style.display = 'block';
    }
    if (suburbDdl){
      suburbDdl.style.display = 'block';
    }
  }
}

//
// This is the function that refreshes the list after a keypress.
// The maximum number to show can be limited to improve performance with
// huge lists (1000s of entries).
// The function clears the list, and then does a linear search through the
// globally defined array and adds the matches back to the list.
//
function HandleKeyUp(evt, maxNumToShow) {
    var selectObj, textObj, listLength;
    var i, searchPattern, numShown;
    
    var e = evt? evt : window.event; 
    if(!e) return; 
  
    var key;
    if (e.keyCode) { 
      key = e.keyCode; 
    } else if (typeof(e.which)!= 'undefined') { 
      // for moz/fb, if keyCode==0 use 'which' 
      key = e.which; 
    }
    
    if (key == 13){
      //key = 0;
      //document.getElementById('btnGo').click();      
    } else {
      // Set references to the form elements
      selectObj = document.getElementById('ddLocationList');
      textObj = document.getElementById('txtTown');

      // Remember the function list length for loop speedup
      listLength = locationList.length;

      // Set the search pattern depending
      if(startingWith == true)
      {
          searchPattern = "^"+textObj.value;
      }
      else
      {
          searchPattern = textObj.value;
      }

      // Create a regulare expression
      re = new RegExp(searchPattern,"gi");
      // Clear the options list
      selectObj.length = 0;

      // Loop through the array and re-add matching options
      numShown = 0;
      for(i = 0; i < listLength; i++)
      {
          if(locationList[i].search(re) != -1)
          {
              selectObj[numShown] = new Option(locationList[i],"");
              numShown++;
          }
          // Stop when the number to show is reached
          if(numShown == maxNumToShow)
          {
              break;
          }
      }
      // When options list whittled to one, select that entry and hide the dropdown
      if(selectObj.length == 1)
      {
          //selectObj.options[0].selected = true;
          //textObj.value = selectObj.options[0].text; 
          //showDropDown(false);
      }
      
      {
        // Show the div if more than one key pressed, otherwise hide it.
        if (textObj.value == ''){
          ShowDropDown(false);
        } else {
          ShowDropDown(true);
          if (numShown < 2){
            numShown = 2;
          } else if (numShown > 20){
            numShown = 20;
          }
          selectObj.size = numShown;
        }
      }
        
      // If down arrow key pressed then move to select object
      if (key == 40){
        if (selectObj.length > 0) {
          selectObj.focus();
          selectObj.selectedIndex = 0;
        }
      }
    }
}

//
// This function handles a keypress in the dropdown list.
//
function HandleSelectClick(){
  selectObj = document.getElementById('ddLocationList');
  textObj = document.getElementById('txtTown');

  if (selectObj.selectedIndex != -1){
    selectedValue = selectObj.options[selectObj.selectedIndex].text;
    textObj.value = selectedValue;
  }
  
  ShowDropDown(false);
  
  // Look up the id and location type for this value, copy to hidden fields for passing through to the search...
  /*for(i = 0; i < locationList.length; i++){
    if (locationList[i] == selectedValue){
      document.getElementById('locationtype').value = functionlocation[i];
      document.getElementById('locationid').value = functionids[i];
    }
  }*/
  
  textObj.focus();
}

//
// Handle enter key being pressed in dropdown list...
//
function HandleEnter(evt) { 
  var e = evt? evt : window.event; 
  if(!e) return; 
  
  var key;
  if (e.keyCode) { 
    key = e.keyCode; 
  } else if (typeof(e.which)!= 'undefined') { 
    // for moz/fb, if keyCode==0 use 'which' 
    key = e.which; 
  } 

  if (key == 13){
    // If enter key pressed in dropdown list then simulate a mouse click on that item.
    HandleSelectClick();
  } 
}




//
// Handles ticking of check boxes for services list...
//
function UpdateAllServices(anotherTicked){
  if (anotherTicked){
    document.getElementById('checkAll').checked = false;
  } else {
    // Don't know how many check boxes there are or what IDs they have 
    // (there could be gaps) so just assume they're all under 20.
    for (i=0; i < 20; i++){
      var obj = document.getElementById('chk' + i);
      if (obj != null){
        obj.checked = false;
      }
    }
  }
  
  return true;
}