
// 2 Noviembre del 2001
// autor: Miguel Ángel Buitrago Ramírez
// LIBRERIA JS PARA CONTROL DE SELECT

function selSelect(Object,value,by,inicio) {
	for (i=inicio;i<Object.options.length;i++) {
		if (eval("Object.options[i]." + by) == value) {
			Object.options[i].selected = true;
			break;
		}
	}
}

function incluirItem(textItem,valorItem,Object) {
// Añade una nueva opción al final del select referenciado por 'Object'.
// La opcion se compondrá de (Valor=valorItem,Texto=text=Item)
	Object.options[Object.options.length]=new Option(textItem,valorItem); 
}

function eliminarItem(valorItem,Object) {
// Elimina la opción del select cuyo valor sea igual a 'valorItem'.
// Si no existe la opción especificada devuelve -1, y no elimina nada.

	// 1. Obtener la posición del item a eliminar.
	i = 0;
	while (i<Object.options.length && Object.options[i].value != valorItem) {
		i++;
	}
	// 2. Control de la eliminación
	if (i == Object.options.length) { 
		// El elemento no está y se devuelve -1 indicando el error
		return -1; }
	else {
		// El objeto está en la posición 'i' y se elimina con remove.
		Object.remove(i);
	}
}

function destruirSelect(Object,PosicionInicio) {
// Destruye un Select entero referenciado por el parámetro Object
	while (Object.options.length != PosicionInicio) {
		eliminarItem(Object.options[PosicionInicio].value,Object);	
	}
}

function construirSelect(listaOpciones,Object) {
// A partir de las listade opciones contenida en un array bidimensional 
// con estructura [texto,valor] se construye el select.
	
	// 1. Se destruye el Select por seguridad. (No recomendado)
	// destruirSelect(Object);
	// 2. Construcción item a item usando la función 'incluirItem'
	i = 0;
	while (i < listaOpciones.length) {
		incluirItem(listaOpciones[i][0],listaOpciones[i][1],Object);
		i++;
	}
}

function select2Array(Object,inicio) {
	var arraySelect = new Array();
	
	for (i=inicio;i<Object.options.length;i++) {
		arraySelect[i] = new Array();
		arraySelect[i][0] = Object.options[i].value;
		arraySelect[i][1] = Object.options[i].text;
	}
	return arraySelect;
}

function array2Select(arraySelect,Object,inicio) {
	for (i=inicio;i<arraySelect.length;i++) {
		Object.options[i] = new Option(arraySelect[i][1],arraySelect[i][0]);
	}
	return 1;
}

function select2List(Object,inicio,sep) {
	var list = "";
	
	for (i2List=inicio;i2List<Object.options.length;i2List++) {
		if (Object.options[i2List].selected) {
			if (list != "") list += sep + Object.options[i2List].value;
			else list = Object.options[i2List].value;
		}
	}
	return list;
}

function InsertarOrdenadoSelect(objSelect,valueS,textS,inicio) {
// Esta función de inserción ordenado usa un array de apoyo
// El elemento por el que se ordena es el Text del select
	// 1. Leemos el select
	var arraySelect = select2Array(objSelect,inicio);
	var ordSelected = inicio;
	var insertado = false;
	
	// 2. Destruimos el select
	destruirSelect(objSelect,inicio);
	
	// 3. --- Insertamos de manera ordenada ---
	
	// 3.1 Inserción en object vacío
	if (arraySelect.length == 0) {
		objSelect.options[inicio]=new Option(textS,valueS);
		ordSelected = inicio;
		//alert('Inserta -vacío-');
	}
	// 3.2 Si hay elementos
	else {
		i = inicio;
		posSel = inicio;
		while (i<arraySelect.length) {
			// 3.3 Si es el sitio de insertar lo anteponemos
			if (!insertado && textS.localeCompare(arraySelect[i][1]) <= 0) {
				objSelect.options[posSel]=new Option(textS,valueS);
				ordSelected = posSel;
				insertado = true;
				//alert('Inserta -elemento-');
				posSel++;
			}
			// 3.4 Inserción del elemento del array (incondicional)
			objSelect.options[posSel]=new Option(arraySelect[i][1],arraySelect[i][0]);
			//alert('Inserta -del array-');
			posSel++;
			i++;
		}
		// Inserción en la última posición
		if (!insertado) {
			ordSelected = objSelect.options.length;
			objSelect.options[objSelect.options.length]=new Option(textS,valueS);
			//alert('Inserta -al fin-');
			insertado = true;
		}
	}
	
	// 4. Selección
	objSelect.options[ordSelected].selected = true;
}

function InsertarOrdenadoArray(objArray,valueS,textS,inicio) {
	// 1. Leemos el select
	var arraySelect = new Array();
	var insertado = false;
	var posSel = inicio;
	// 3. --- Insertamos de manera ordenada ---
	//alert('Entra en Insertar Array longitud: ' + objArray.length);
	// 3.1 Inserción en object vacío
	if (objArray.length <= inicio) {
		arraySelect[inicio] = new Array();
		arraySelect[inicio][0] = valueS;
		arraySelect[inicio][1] = textS;
		//alert('Inserta -vacío-');
	}
	// 3.2 Si hay elementos
	else {
		var i = inicio;
		while (i<objArray.length) {
			// 3.3 Si es el sitio de insertar lo anteponemos
			if (!insertado && textS.localeCompare(objArray[i][1]) <= 0) {
				arraySelect[posSel] = new Array();
				arraySelect[posSel][0] = valueS;
				arraySelect[posSel][1] = textS;
				insertado = true;
				//alert('Inserta -elemento-');
				posSel++;
			}
			// 3.4 Inserción del elemento del array (incondicional)
			arraySelect[posSel] = new Array();
			arraySelect[posSel][0] = objArray[i][0];
			arraySelect[posSel][1] = objArray[i][1];
			//alert('Inserta -del array-');
			posSel++;
			i++;
		}
		// Inserción en la última posición
		if (!insertado) {
			posSel = arraySelect.length;
			arraySelect[posSel] = new Array();
			arraySelect[posSel][0] = valueS;
			arraySelect[posSel][1] = textS;
			//alert('Inserta -al fin-');
			insertado = true;
		}
	}
	// Devolución del resultado
	return arraySelect;
}

function InsertarOrdenadoArrayBi(objArray,valueS,textS,inicio) {
	// 1. Leemos el select
	var arraySelect = new Array();
	var ordSelected = inicio;
	var insertado = false;
	
	// 3. --- Insertamos de manera ordenada ---
	
	// 3.1 Inserción en object vacío
	if (objArray.length == 0) {
		arraySelect[inicio] = new Array();
		arraySelect[inicio][0] = new Array();
		arraySelect[inicio][0][0] = valueS;
		arraySelect[inicio][0][1] = textS;
		//alert('Inserta -vacío-');
	}
	// 3.2 Si hay elementos
	else {
		i = inicio;
		posSel = inicio;
		while (i<objArray.length) {
			// 3.3 Si es el sitio de insertar lo anteponemos
			if (!insertado && textS.localeCompare(objArray[i][0][1]) <= 0) {
				arraySelect[posSel] = new Array();
				arraySelect[posSel][0] = new Array();
				arraySelect[posSel][0][0] = valueS;
				arraySelect[posSel][0][1] = textS;
				ordSelected = posSel;
				insertado = true;
				//alert('Inserta -elemento-');
				posSel++;
			}
			// 3.4 Inserción del elemento del array (incondicional)
			arraySelect[posSel] = new Array();
			arraySelect[posSel] = objArray[i];
			arraySelect[posSel] = objArray[i];
			//alert('Inserta -del array-');
			posSel++;
			i++;
		}
		// Inserción en la última posición
		if (!insertado) {
			ordSelected = arraySelect.length;
			arraySelect[posSel] = new Array();
			arraySelect[posSel][0] = new Array();
			arraySelect[posSel][0][0] = valueS;
			arraySelect[posSel][0][1] = textS;
			//alert('Inserta -al fin-');
			insertado = true;
		}
	}
	// Devolución del resultado
	return arraySelect;
}

function eliminarListaOpciones(listaOpciones,Object) {
// Se pasa una lista de índices y esos son los que se eliminan del select 
// referenciado en 'Object', debido a que al eliminar una opción el índice general
// también varia. Lista de opciones debe estar ordenada ascendentemente.

	var NumeroBorrados = 0;
	while (NumeroBorrados < listaOpciones.length) {
		if (Object.options.length>0) {
			if (NumeroBorrados==0) {
				// alert(listaOpciones[NumeroBorrados]);
				Object.remove(listaOpciones[NumeroBorrados]);
			}
			else {
				// alert(eval(listaOpciones[NumeroBorrados]-NumeroBorrados));
				Object.remove(eval(listaOpciones[NumeroBorrados]-NumeroBorrados));				
			}
		}
		NumeroBorrados++;	
	}	
}