// constants
var noValue = '-99'
var formid
// globals
var curOption = new Array();
var isLoaded = new Array();

function nPropId_onChange(id){
formid=id
  var val = document.forms[formid].nPropId.options[document.forms[formid].nPropId.selectedIndex].value;
   if(val == noValue){
    // don't allow novalue selection - revert to current
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'nSubPropId' );
    window.status = 'Loading Sub Property Types...';
    jsrsExecute( 'select_rs.asp', cbFillSubProp, 'SubPropList', val );
  }  
}
function nStateId_onChange(id){
  formid=id
  var val = document.forms[formid].nStateId.options[document.forms[formid].nStateId.selectedIndex].value;
   if(val == noValue){
    // don't allow novalue selection - revert to current
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'nCityId' );
    window.status = 'Loading City...';
    jsrsExecute( 'select_rs.asp', cbFillCity, 'CityList', val );
  }  
}

function cbFillSubProp ( strSubPropTypes ){ 
  // callback for dependent listbox
  //alert(strSubPropTypes);
  window.status = '';
  fillList( 'nSubPropId',  strSubPropTypes ); 
}
function cbFillCity ( strCityTypes ){ 
  // callback for dependent listbox
  //alert(strSubPropTypes);
  window.status = '';
  fillList( 'nCityId',  strCityTypes ); 
}

//common functions begin, do not modify

function fillList( listName, strOptions ){
  // fill any list with options
  emptyList( listName );
  
  // always insert selection prompt
  var lst = document.forms[formid][listName];
  lst.disabled = true;
  lst.options[0] = new Option('-- Please Select --', 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]);
    }  
  }
  
  // init to no value
  selectOption( listName, noValue );
  //lst.onchange = eval( listName + "_onChange" );
  isLoaded[listName] = true;
  lst.disabled = false;
}

function emptyList( listName ){
  var lst = document.forms[formid][listName];
  lst.options.length = 0;
  lst.onchange = null;
  isLoaded[listName] = false;
  curOption[listName] = noValue;
}

function selectOption( listName, optionVal ){
  // set list selection to option based on value
  var lst = document.forms[formid][listName];
  for( var i = 0; i< lst.options.length; i++ ){
    if( lst.options[i].value == optionVal ){
      lst.selectedIndex = i;
      curOption[listName] = optionVal;
      return;
    }  
  }
}
