// JavaScript Document
//Aquest arxiu javscript serveix per treballar amb llistes <select>
//F.X Escarmís

//afegir_valor(select_usuaris_membres, select_usuaris, v_usuaris_membres)
function afegir_valor(select_desti, select_origen, llista_desti) {
	if (select_origen.selectedIndex < 0) return;
	valor_insert = select_origen.options[select_origen.selectedIndex].value;
	if (!valor_insert || valor_insert=="" || valor_insert=="0") return;
	text = select_origen.options[select_origen.selectedIndex].text;
	valor = '['+valor_insert+']';
	text_llista = llista_desti.value;
	if (text_llista.indexOf(valor) < 0){
		select_desti.options[select_desti.options.length] = new Option(text, valor_insert);
		llista_desti.value += valor;
	}
	if (select_desti.options.length > 5){
		select_desti.size = select_desti.options.length;
	}
	else {
		select_desti.size = 5;
	}
}

//afegir_valor_unic(id_cap, v_nom_cap, select_usuaris_membres, select_usuaris, v_usuaris_membres)
function afegir_valor_unic(valor_unic, text_unic, select_desti, select_origen, llista_desti) {
	if (select_origen.selectedIndex < 0) return;
	valor_insert = select_origen.options[select_origen.selectedIndex].value;
	if (!valor_insert || valor_insert=="" || valor_insert=="0") return;
	text = select_origen.options[select_origen.selectedIndex].text;
	valor = '['+valor_insert+']';
	text_llista = llista_desti.value;
	if (text_llista.indexOf(valor) < 0){
		select_desti.options[select_desti.options.length] = new Option(text, valor_insert);
		llista_desti.value += valor;
	}
	if (select_desti.options.length > 5){
		select_desti.size = select_desti.options.length;
	}
	else {
		select_desti.size = 5;
	}
	valor_unic.value = valor_insert;
	text_unic.value = text;
}

//afegir_valor_dos_llistes
function afegir_valor_dos_llistes(select_01, select_02, select_origen, llista_01, llista_02) {
	if (select_origen.selectedIndex < 0) return;
	valor_insert = select_origen.options[select_origen.selectedIndex].value;
	text = select_origen.options[select_origen.selectedIndex].text;
	if (!valor_insert || valor_insert=="" || valor_insert=="0") return;
	valor = '['+valor_insert+']';
	text_llista_02 = llista_02.value;
	text_llista_01 = llista_01.value;
	if (text_llista_01.indexOf(valor) < 0){
		select_01.options[select_01.options.length] = new Option(text, valor_insert);
		llista_01.value += valor;
	}
	abans = text_llista_02.indexOf(valor);
	if (abans >= 0){
		llarg = select_02.options.length;
		despres = (abans+valor.length);
		text_abans = text_llista_02.substring(0,abans);
		text_despres = text_llista_02.substring(despres); 
		llista_02.value = text_abans + text_despres; 
		trobat = false;
		for (i=0;(i<llarg && !trobat);i++){
			valor_02 = select_02.options[i].value;
			if (valor_02 == valor_insert){
				select_02.options[i] = null;
				trobat = true;
			}
		}
	}
	if (select_01.options.length > 5){
		select_01.size = select_01.options.length;
	}
	else {
		select_01.size = 5;
	}
	if (select_02.options.length > 5){
		select_02.size = select_02.options.length;
	}
	else {
		select_02.size = 5;
	}
}


//afegir_tots_valors(select_usuaris_membres, select_usuaris, v_usuaris_membres)
function afegir_tots_valors(select_desti, select_origen, llista_desti) {
	llarg = select_origen.options.length;
	for (i=0;i<llarg;i++){
		valor_insert = select_origen.options[i].value;
		text = select_origen.options[i].text;
		valor = '['+valor_insert+']';
		text_llista = llista_desti.value;
		if (text_llista.indexOf(valor) < 0){
			select_desti.options[select_desti.options.length] = new Option(text, valor_insert);
			llista_desti.value += valor;
		}
	}
	if (select_desti.options.length > 5){
		select_desti.size = select_desti.options.length;
	}
	else {
		select_desti.size = 5;
	}
}

//afegir_tots_valors_dos_llistes
function afegir_tots_valors_dos_llistes(select_01, select_02, select_origen, llista_01, llista_02) {
	llarg = select_origen.options.length;
	for (i=0;i<llarg;i++){
		valor_insert = select_origen.options[i].value;
		text = select_origen.options[i].text;
		valor = '['+valor_insert+']';
		text_llista = llista_01.value;
		if (text_llista.indexOf(valor) < 0){
			select_01.options[select_01.options.length] = new Option(text, valor_insert);
			llista_01.value += valor;
		}
	}
	if (select_01.options.length > 5){
		select_01.size = select_01.options.length;
	}
	else {
		select_01.size = 5;
	}
	select_02.length = 0;
	llista_02.value = '';
	select_02.size = 5;
}

//eliminar_valor
function eliminar_valor_llistes(select_desti,llista_desti) {
	if (select_desti.selectedIndex < 0) return;
	valor_delete =  select_desti.options[select_desti.selectedIndex].value;
	valor = '['+valor_delete+']';
	select_desti.options[select_desti.selectedIndex] = null;
	text_llista = llista_desti.value;
	abans = text_llista.indexOf(valor);
	despres = (abans+valor.length);
	text_abans = text_llista.substring(0,abans);
	text_despres = text_llista.substring(despres); 
	llista_desti.value = text_abans + text_despres; 
	if (select_desti.options.length > 5){
		select_desti.size = select_desti.options.length;
	}
	else {
		select_desti.size = 5;
	}
}


//eliminar_valor(select_usuaris_membres, v_usuaris_membres, id_cap, v_nom_cap)
function eliminar_valor(select_desti,llista_desti,valor_unic,text_unic) {
	if (select_desti.selectedIndex < 0) return;
	valor_delete =  select_desti.options[select_desti.selectedIndex].value;
	valor = '['+valor_delete+']';
	select_desti.options[select_desti.selectedIndex] = null;
	text_llista = llista_desti.value;
	abans = text_llista.indexOf(valor);
	despres = (abans+valor.length);
	text_abans = text_llista.substring(0,abans);
	text_despres = text_llista.substring(despres); 
	llista_desti.value = text_abans + text_despres; 
	if (select_desti.options.length > 5){
		select_desti.size = select_desti.options.length;
	}
	else {
		select_desti.size = 5;
	}
	if (valor_delete == valor_unic.value){
		valor_unic.value = '';
		text_unic.value = '';
	}
}

//eliminar_tots_valors_llista
function eliminar_tots_valors_llista(select_desti,llista_desti) {
	select_desti.length = 0;
	llista_desti.value = '';
	select_desti.size = 5;
}

//eliminar_valor_unic(id_cap, v_nom_cap)
function eliminar_valor_unic(valor_unic, text_unic) {
	valor_unic.value = '';
	text_unic.value = '';
}

//eliminar_tots_valors(select_usuaris_membres, select_usuaris, v_usuaris_membres, id_cap, v_nom_cap)
function eliminar_tots_valors(select_desti,llista_desti,valor_unic,text_unic) {
	select_desti.length = 0;
	llista_desti.value = '';
	select_desti.size = 5;
	valor_unic.value = '';
	text_unic.value = '';
}
