// constants
var noValue = '-99'
// default values
var IDMake = noValue;
var IDModel = noValue;
//selects disabled true/false
var boolEnabled = true;

// globals
var curOption = new Array();
var isLoaded = new Array();

function body_onLoad(){

  // initialize lists
  emptyList( 'lstMake' );
  emptyList( 'lstModel');
  //emptyList( 'lstOptions' );
  jsrsExecute( 'select_rs.asp', cbFillMake, 'makeList' );
}

function preselect(idMake,idModel,selectable){
  boolEnabled = selectable;
  IDMake = idMake;
  IDModel = idModel;
  //alert(IDMake);
  //alert(IDModel);  
  body_onLoad();
}

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 = 'Cargando Modelos ...';
    jsrsExecute( 'select_rs.asp', cbFillModel, 'modelList', val );
  }  
  cbmarcOtro();
}

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 = 'Loading Options Selections...';
//    jsrsExecute( 'select_rs.asp', cbFillOptions, 'optionsList', val );
  }  
  cbmod();
}

//function lstOptions_onChange(){
//  var val = this.options[this.selectedIndex].value;
//  if(val == noValue){
//    selectOption( this.name, curOption[this.name] )
//  } else {
//    var msg = "You have selected: \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);
//  }
//}
//

function cbFillMake ( strMakes ){ 
  window.status = '';
  fillList( 'lstMake',  strMakes ); 
  if(IDMake != noValue){
    jsrsExecute( 'select_rs.asp', cbFillModel, 'modelList', ''+IDMake+'');
  }
}

//function cbFillMake ( strMakes ){ 
//  window.status = 'Cargando Marcas';
//  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['form1'][listName];
  lst.disabled = true;
  lst.options[0] = new Option('=> Seleccione Uno <=', 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]);
    }  
  	lst.options[aOptionPairs.length] = new Option('Otros', 9999);
  }
  
  switch(listName){
  	case 'lstMake':
		  ID = IDMake;
		break;
  	case 'lstModel':
		  ID = IDModel;
		break;
	}

  selectOption( listName, ID );
  
  if (listName == "lstModel")  
  	{
		if (form1.lstMake.value == "9999")
			{
				selectOption( listName, "9999" );
			}
	}
  // init to no value
  //selectOption( listName, noValue );
  isLoaded[listName] = true;
  lst.disabled = !boolEnabled;
  lst.onchange = eval( listName + "_onChange" );
}

function emptyList( listName ){
  var lst = document.forms['form1'][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['form1'][listName];
  for( var i = 0; i< lst.options.length; i++ ){
    if( lst.options[i].value == optionVal ){
      lst.selectedIndex = i;
      curOption[listName] = optionVal;
      return;
    }  
  }
}

// 

