
// constants
var noValue = '-99';
// default values
var IDSector = noValue;
var IDActividad = noValue;
var IDOptions = noValue;
//selects disabled true/false
var boolEnabled = true;

// globals
var curOption = new Array();
var isLoaded = new Array();

function initLists(){
  // initialize lists
  emptyList( 'lstSector' );
  emptyList( 'lstActividad');
  jsrsExecute( 'select_rs.php', cbFillMake, 'sectorList' );
}

function preselect(IDSector,IDActividad,selectable){
  boolEnabled = selectable;
  IDSector = IDSector;
  IDActividad = IDActividad;
  initLists();
}

function lstSector_onChange(){
  var val = this.options[this.selectedIndex].value;
    IDSector = val;
    IDActividad = noValue;
    IDOptions = noValue;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'lstActividad' );
    window.status = 'Cargando Actividades...';
    jsrsExecute( 'select_rs.php', cbFillModel, 'actividadList', val );
  }  
}

function lstActividad_onChange(){

  var val = this.options[this.selectedIndex].value;
 
}


function cbFillMake ( strMakes ){ 
  window.status = '';
  fillList( 'lstSector',  strMakes ); 
  if(IDSector != noValue){
    jsrsExecute( 'select_rs.php', cbFillModel, 'actividadList', ''+IDSector+'' );
  }
}

function cbFillModel ( strModels ){ 
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstActividad',  strModels ); 
}


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 'lstSector':
		  ID = IDSector;
		break;
  	case 'lstActividad':
		  ID = IDActividad;
		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;
    }  
  }
}


