// constants
var noValue = '-99'

// globals
var curOption = new Array();
var isLoaded = new Array();

function body_onLoad(){

  // initialize lists
  emptyList( 'lstMake' );
  emptyList( 'lstModel');
  emptyList( 'lstOptions' );
  jsrsExecute( 'select_rs-teste.asp', cbFillMake, 'makeList' );
}

function lstMake_onChange(){
  var val = this.options[this.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( 'lstModel' );
    emptyList( 'lstOptions');
    window.status = 'Carregando as opções para Seleção...';
    jsrsExecute( 'select_rs-teste.asp', cbFillModel, 'modelList', val );
  }  
}

function lstModel_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    emptyList( 'lstOptions');
    window.status = 'Carregando as opções para Seleção...';
    jsrsExecute( 'select_rs-teste.asp', cbFillOptions, 'optionsList', val );
  }  
}

function lstOptions_onChange(){
  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    var msg = "Você selecionou: \n\n";
    msg += this.form.lstMake.options[this.form.lstMake.selectedIndex].text + "\n";
    msg += this.form.lstModel.options[this.form.lstModel.selectedIndex].text + "\n";
    msg += this.options[this.selectedIndex].text + "\n";
    alert (msg);
	document.QForm.submit();
  }
}

function cbFillMake ( strMakes ){ 
  window.status = '';
  fillList( 'lstMake',  strMakes ); 
}

function cbFillModel ( strModels ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstModel',  strModels ); 
}

function cbFillOptions( strOptions ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstOptions', strOptions ); 
}

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('-- Selecione Aqui --', 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['QForm'][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['QForm'][listName];
  for( var i = 0; i< lst.options.length; i++ ){
    if( lst.options[i].value == optionVal ){
      lst.selectedIndex = i;
      curOption[listName] = optionVal;
      return;
    }  
  }
}


