//*** cambia el combo hijo con respecto al padre (combo anidado)
function selectChange(control,controlToPopulate,datos,valorInicial) {
  valorInicial = ( valorInicial != null ? valorInicial : "Seleccione" );
  var myEle;
  var x ;
  // Limpia el segundo combo;
  for (var q=controlToPopulate.options.length;q>=0;q--) {
    controlToPopulate.options[q]=null;
  }
  myEle = document.createElement("option");
  myEle.value = 0;
  myEle.text = valorInicial;
  controlToPopulate.length = 1;
  controlToPopulate[0] = myEle;
  
  // Rutina que accesa a los elementos hijos
  // adiciona la descripcion y el valor a el select option.
  var nuevo_Control = 0;
  for ( x = 0 ; x < datos.length  ; x++ ){
    if (datos[x][2] == control.value) {
      myEle = document.createElement("option");
      myEle.value = datos[x][0];
      myEle.text = datos[x][1] ;
      controlToPopulate.length = (++nuevo_Control);
      controlToPopulate[nuevo_Control] = myEle;
    }
  }
}

//*** verifica si un valor ya no está incluido en un combo
function incluido(valor,control){
  	for (var i=0; i<control.options.length;i++) {
		if (control.options[i].value==valor){
			return true;
			break;
		}
	}
	return false;
}
  
//*** inicialmente carga todas las opciones en un combo hijo, sin diferenciar por el padre
function cargarTodos(control,controlToPopulate,datos,valorInicial){
	valorInicial = ( valorInicial != null ? valorInicial : "Seleccione" );

 	var myEle;
	var x ;
	// Limpia el segundo combo;
	for (var q=controlToPopulate.options.length;q>=0;q--) {
		controlToPopulate.options[q]=null;
	}
	myEle = document.createElement("option");
	myEle.value = 0;
	myEle.text = valorInicial;
	controlToPopulate.length = 1;
	controlToPopulate[0] = myEle;
	  
	// Rutina que accesa a los elementos hijos
	// adiciona la descripcion y el valor a el select option.
	var nuevo_Control = 0;
	for ( x = 0 ; x < datos.length  ; x++ ){
		if (!incluido(datos[x][0],controlToPopulate)){
		  	myEle = document.createElement("option");
		   	myEle.value = datos[x][0];
		   	myEle.text = datos[x][1] ;
		   	controlToPopulate.length = (++nuevo_Control);
		   	controlToPopulate[nuevo_Control] = myEle;
		}
	} 	
}
 
 
//*** cambia el combo hijo con respecto al padre (combo anidado), con valores distintos
function selectChangeDistintos(control,controlToPopulate,datos,valorInicial) {
  valorInicial = ( valorInicial != null ? valorInicial : "Seleccione" );
  
  if (control.value==0){
  	//se cargan todos
  	cargarTodos(control,controlToPopulate,datos,valorInicial);
  }
  else{
	  var myEle;
	  var x ;
	  // Limpia el segundo combo;
	  for (var q=controlToPopulate.options.length;q>=0;q--) {
	    controlToPopulate.options[q]=null;
	  }
	  myEle = document.createElement("option");
	  myEle.value = 0;
	  myEle.text = valorInicial;
	  controlToPopulate.length = 1;
	  controlToPopulate[0] = myEle;
	  
	  // Rutina que accesa a los elementos hijos
	  // adiciona la descripcion y el valor a el select option.
	  var nuevo_Control = 0;
	  for ( x = 0 ; x < datos.length  ; x++ ){
	    if ((datos[x][2] == control.value)&&(!incluido(datos[x][0],controlToPopulate))) {
	      myEle = document.createElement("option");
	      myEle.value = datos[x][0];
	      myEle.text = datos[x][1] ;
	      controlToPopulate.length = (++nuevo_Control);
	      controlToPopulate[nuevo_Control] = myEle;
	    }
      }
  }
}


