// constants
var noValue = '-99';
// default values
var IDEscola = noValue;
var IDTurma = noValue;
var IDAluno = noValue;
//selects disabled true/false
var boolEnabled = true;

// globals
var curOption = new Array();
var isLoaded = new Array();

function initLists(){
  // initialize lists
  emptyList( 'lstEscola' );
  emptyList( 'lstTurma');
  emptyList( 'lstAluno' );
  jsrsExecute( 'select_rs.php', cbFillMake, 'makeList');
}

function preselect(idEscola,idTurma,idAluno,selectable){
  boolEnabled = selectable;
  IDEscola = idEscola;
  IDTurma = idTurma;
  IDAluno = idAluno;
  initLists();
}

function lstEscola_onChange(){
  var val = this.options[this.selectedIndex].value;
    IDEscola = val;
    IDTurma = noValue;
    IDAluno = noValue;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'lstTurma' );
    emptyList( 'lstAluno');
    window.status = 'Loading Model Selections...';
    jsrsExecute( 'select_rs.php', cbFillModel, 'modelList', val);
  }  
}

function lstTurma_onChange(){

  var val = this.options[this.selectedIndex].value;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    emptyList( 'lstAluno');
    window.status = 'Loading Options Selections...';
    jsrsExecute( 'select_rs.php', cbFillOptions, 'optionsList', val);
  }  
}

function lstAluno_onChange(){
  var val = this.options[this.selectedIndex].value;
  IDAluno = val;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    var msg = "You have selected: \n\n";
    msg += this.form.lstEscola.options[this.form.lstEscola.selectedIndex].text + "\n";
    msg += this.form.lstTurma.options[this.form.lstTurma.selectedIndex].text + "\n";
    msg += this.options[this.selectedIndex].text + "\n";
    //alert (msg);
    
    if(boolEnabled){
    document.getElementById('cmdSubmit').disabled="";
    document.getElementById('show').style.backgroundColor="#00FF7F";
    }
    
  }
}

function cbFillMake ( strMakes ){ 
  window.status = '';
  fillList( 'lstEscola',  strMakes );
  if(IDEscola != noValue){
    jsrsExecute( 'select_rs.php', cbFillModel, 'modelList', ''+IDEscola+'');
  }
}

function cbFillModel ( strModels ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstTurma',  strModels );
  if(IDTurma != noValue){
    jsrsExecute( 'select_rs.php', cbFillOptions, 'optionsList', ''+IDTurma+'');
  }
}

function cbFillOptions( strOptions ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstAluno', 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('=>  Escolha  <=', 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 'lstEscola':
		  ID = IDEscola;
		break;
  	case 'lstTurma':
		  ID = IDTurma;
		break;
	case 'lstAluno':
		  ID = IDAluno;
		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;
    }  
  }
}


