/**************************************************************
   
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 IDDepartamento = noValue;
var IDCiudad = noValue;
//selects disabled true/false
var boolEnabled = true;

// globals
var curOption = new Array();
var isLoaded = new Array();

function initLists()
{
  // initialize lists
  emptyList( 'lstDepartamento' );
  emptyList( 'lstCiudad');
  jsrsExecute( 'select_rs.php', cbLlenarDepartamento, 'departamentoList');
}

function preselect(idDepartamento,idCiudad,selectable)
{
  boolEnabled = selectable;
  IDDepartamento = idDepartamento;
  IDCiudad = idCiudad;
  initLists();
}

function lstDepartamento_onChange()
{
  var val = this.options[this.selectedIndex].value;
    IDDepartamento = val;
    IDCiudad = noValue;
  if(val == noValue)
  {
    selectOption( this.name, curOption[this.name] )
  } 
	else 
	{
	  curOption[this.name] = val;
      // init dependent lists
      emptyList( 'lstCiudad' );
      window.status = 'Loading Model Selections...';
      jsrsExecute( 'select_rs.php', cbLlenarCiudad, 'ciudadList', val);
	}  
}

function lstCiudad_onChange()
{
  var val = this.options[this.selectedIndex].value;
  IDCiudad=val;
  if(val == noValue){
   selectOption( this.name, curOption[this.name] )
   }
   else {
    var msg = "You have selected: \n\n";
    msg += this.form.lstDepartamento.options[this.form.lstDepartamento.selectedIndex].text + "\n";
    msg += this.form.lstCiudad.options[this.form.lstCiudad.selectedIndex].text + "\n";
    msg += this.options[this.selectedIndex].text + "\n";
    //alert (msg);
    
    if(boolEnabled){
    document.getElementById('cmdSubmit').disabled="";
    }
    
  }
}

function cbLlenarDepartamento ( strDepartamento )
{ 
  window.status = '';
  llenarList( 'lstDepartamento',  strDepartamento ); 
  if(IDDepartamento != noValue){
    jsrsExecute( 'select_rs.php', cbLlenarCiudad, 'ciudadList', ''+IDDepartamento+'');
  }
}

function cbLlenarCiudad ( strCiudades )
{ 
  // callback for dependent listbox
  window.status = '';
  llenarList( 'lstCiudad',  strCiudades );
}

function llenarList( listName, strOptions )
{
  // llenar any list with options
  emptyList( listName );
  
  // always insert selection prompt
  var lst = document.forms['QForm'][listName];
  lst.disabled = true;
  lst.options[0] = new Option('Seleccione una Opción', noValue);
  
  // options in form "value~displaytext|value~displaytext|..."
  var aOptionPairs = strOptions.split('|');
  switch(listName)
	{
		case 'lstDepartamento':
		{
			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]);
				}
			}
		}
   		break;
  
		case 'lstCiudad':
		 {
			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[2], aOptions[1]);
				}
			}
		}
		break;
	}
  
   switch(listName)
	{
  	case 'lstDepartamento':
		  ID = IDDepartamento;
		break;
  	case 'lstCiudad':
		  ID = IDCiudad;
		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;
    }  
  }
}