/**************************************************************
   
result.php by Milfson (milf@milfcz.com) 17.04.2004

Milfson added preselect(parameters...) to pre-populate dropdowns with default values.

Thanks for the code! - Brent.
 
***************************************************************/

// constants
var noValue = '-99';
// default values
var IDJudet = noValue;
var IDOras = noValue;
//selects disabled true/false
var boolEnabled = true;

// globals
var curOption = new Array();
var isLoaded = new Array();

function initLists(){
  // initialize lists
  emptyList( 'lstJudet' );
  emptyList( 'lstOras');
  jsrsExecute( '/select/select_rs.php', cbFillJudet, 'judetList');
}

function preselect(IdJudet,IdOras,selectable){
  boolEnabled = selectable;
  IDJudet = IdJudet;
  IDOras = IdOras;
  initLists();
}

function lstJudet_onChange(){
  var val = this.options[this.selectedIndex].value;
	//MODIFICARE: SA APARA NUMAI DACA APARE SI TARA
  document.cautare["lstOras"].className = "vis";
    IDJudet = val;
    IDOras = noValue;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
    curOption[this.name] = val;
    // init dependent lists
    emptyList( 'lstOras' );
    window.status = 'Incarc Orasele...';
    jsrsExecute( '/select/select_rs.php', cbFillOras, 'orasList', val);
  }  
}

function lstOras_onChange(){

  var val = this.options[this.selectedIndex].value;
  IDOras = val;
  if(val == noValue){
    selectOption( this.name, curOption[this.name] )
  } else {
     if(boolEnabled){
    document.getElementById('cmdSubmit').disabled="";
    document.getElementById('show').style.backgroundColor="#FFCC99";
    }
  }
}

function cbFillJudet ( strJudete ){
  window.status = '';
  fillList( 'lstJudet',  strJudete );
  if(IDJudet != noValue){
    emptyList( 'lstOras' );
    jsrsExecute( '/select/select_rs.php', cbFillOras, 'orasList', IDJudet);
  }
}

function cbFillOras ( strOrase ){
  // callback for dependent listbox
  window.status = '';
  fillList( 'lstOras',  strOrase );
}

function fillList( listName, strOptions ){
  // fill any list with options
  emptyList( listName );
  // always insert selection prompt
  var lst = document.forms['cautare'][listName];
  lst.disabled = true;
    switch(listName){
      case 'lstJudet':
          ID = IDJudet;
          lst.options[0] = new Option('Alege tara', noValue);
        break;
      case 'lstOras':
          ID = IDOras;
//          lst.options[0] = new Option('Alege orasul', noValue);
        break;
    }

  // 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('~');
	  if (listName == 'lstOras')
	  {
		lst.options[i] = new Option(aOptions[1], aOptions[0]);
	  }
	  else
	    lst.options[i + 1] = new Option(aOptions[1], aOptions[0]);
    }
  }

  // init to no value
  selectOption( listName, ID );
  isLoaded[listName] = true;
  lst.disabled = !boolEnabled;
  lst.onchange = eval( listName + "_onChange" );
   //eval( "document.forms['cautare']['" + listName + "'].onchange=" + listName + "_onChange;" );
}

function emptyList( listName ){
  var lst = document.forms['cautare'][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['cautare'][listName];
  for( var i = 0; i< lst.options.length; i++ ){
    if( lst.options[i].value == optionVal ){
      lst.selectedIndex = i;
      curOption[listName] = optionVal;
      return;
    }  
  }
}
