/**************************************************************

selectphp.js
   
***************************************************************/

// constants
var noValue = '-99';
// default values
var IDBook = noValue;
var IDPage = noValue;
var IDParcel = noValue;
var IDZipcode = noValue;
var IDStreet = noValue;
var IDStreetLetter = noValue;

// globals
var boolEnabled = true;
var curOption = new Array();
var isLoaded = new Array();

function initLists(){
  // initialize lists
  emptyList( 'lstBook' );
  emptyList( 'lstPage');
  emptyList( 'lstParcel' );
//  emptyList( 'lstZipcode' );
//  emptyList( 'lstStreet' );

  jsrsExecute( 'select_rs.php', cbFillBook, 'bookList' );

/* this takes a while, so don't preload the streets for now.  The top line works. */
//  jsrsExecute( 'select_rs.php', cbFillStreet, 'streetListStartsWith', '1,9');
//  jsrsExecute( 'select_rs.php', cbFillStreet, 'streetList' );
//  jsrsExecute( 'select_rs.php', cbFillZipcode, 'zipcodeList' );
}

function preselect(idBook,idPage,idParcel,idStreet,idZipcode){
  IDBook = idBook;
  IDPage = idPage;
  IDParcel = idParcel;
  IDStreet = idStreet;
  IDZipcode = idZipcode;
  initLists();
}

function lstBook_onChange(){
  var val = this.options[this.selectedIndex].value;
  IDBook = val;
  IDPage = noValue;
  IDParcel = noValue;

  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'lstPage' );
    emptyList( 'lstParcel');
    window.status = 'Loading Pages...';
    jsrsExecute( 'select_rs.php', cbFillPage, 'pageList', val );
  }  
}

function lstPage_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    emptyList( 'lstParcel');
    window.status = 'Loading Parcels...';
    jsrsExecute( 'select_rs.php', cbFillParcel, 'parcelList', '' + 
                 this.form.lstBook.options[this.form.lstBook.selectedIndex].value +':' + val + '' );
  }  
}

function lstParcel_onChange(){
  var val = this.options[this.selectedIndex].value;
  IDOptions = val;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    if(boolEnabled){
      document.getElementById('cmdSubmitAPN').disabled="";
    }  
  }
}

function lstZipcode_onChange() {
  var val = this.options[this.selectedIndex].value;
  IDZipCode = val;

  if (val == noValue) {
    selectOption( this.name, curOption[this.name])
  } else {
    curOption[this.name] = val;
    emptyList('lstStreet');
    window.status = 'Loading Streets...';
    jsrsExecute( 'select_rs.php', cbFillStreet, 'streetList', val );
  }
}
/*
function streetLetter_onClick(s, e) {
  var val = this.value;
  IDZipCode = val;

  emptyList('lstStreet');
  window.status = 'Loading Streets...';
  jsrsExecute( 'select_rs.php', cbFillStreet, 'streetListStartsWith', s + "," + e );
  d = document.getElementById('divStreetForm');
  if (d) {
    d.style.visibility = 'visible';
  }
}  

function lstStreet_onChange() {
  var val = this.options[this.selectedIndex].value;
  IDStreet = val;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    if(boolEnabled){
      document.getElementById('cmdSubmitAddress').disabled="";
    }  
  }
}
*/
function cbFillBook ( strBooks ){ 
  window.status = '';
  fillList( 'lstBook',  strBooks ); 
  if(IDBook != noValue){
    jsrsExecute( 'select_rs.php', cbFillPage, 'pageList', ''+IDBook+'' );
  }
}

function cbFillPage ( strPages ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstPage',  strPages ); 
  if(IDPage != noValue){
    jsrsExecute( 'select_rs.php', cbFillParcel, 'parcelList', ''+IDBook+':'+IDPage+'' );
  }
}

function cbFillParcel( strParcels ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstParcel', strParcels ); 
}

function cbFillZipcode ( strZipcodes ){ 
  window.status = '';
  fillList( 'lstZipcode',  strZipcodes ); 
  if(IDBook != noValue){
    jsrsExecute( 'select_rs.php', cbFillStreet, 'streetList', ''+IDZipcode+'' );
  }
}
/*
function cbFillStreet( strStreets ){
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstStreet', strStreets );
}
*/
function fillList( listName, strOptions ){
  // fill any list with options
  emptyList( listName );
  
  // always insert selection prompt
  var lst = document.forms['QForm'][listName];
  lst.disabled = true;
  lst.options[0] = new Option('', noValue);
  
  // options in form "value~displaytext|value~displaytext|..."
  var aOptionPairs = strOptions.split('|');
  for( var i = 0; i < aOptionPairs.length; i++ ){
    if (aOptionPairs[i].indexOf('~') != -1) {
      var aOptions = aOptionPairs[i].split('~');
      lst.options[i + 1] = new Option(aOptions[1], aOptions[0]);
    }  
  }

  switch(listName){
    case 'lstBook':
      ID = IDBook;
    break;
    case 'lstPage':
      ID = IDPage;
    break;
    case 'lstParcel':
      ID = IDParcel;
    break;
    case 'lstZipcode':
      ID = IDZipcode;
    break;
//    case 'lstStreet':
//      ID = IDStreet;
//    break;
  }

  // init to no value
  selectOption( listName, ID );
  isLoaded[listName] = true;
  lst.disabled = !boolEnabled;
  lst.onchange = eval( listName + "_onChange" );
  // eval( "document.forms['QForm']['" + listName + "'].onchange=" + listName + "_onChange;" );
}

function emptyList( listName ){
  var lst = document.forms['QForm'][listName];
  lst.options.length = 0;
  lst.onchange = null;
  lst.disabled = !boolEnabled;
  isLoaded[listName] = false;
  curOption[listName] = noValue;
}

function selectOption( listName, optionVal ){
  // set list selection to option based on value
  var lst = document.forms['QForm'][listName];
  for( var i = 0; i< lst.options.length; i++ ){
    if( lst.options[i].value == optionVal ){
      lst.selectedIndex = i;
      curOption[listName] = optionVal;
      return;
    }  
  }
}

