
// OPERACIONES CON SELECTS

var abcMas = new Array();
abcMas = ["","A","B","C","D","E","F","G","H","I","J","K","L","M","N","Ñ","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
var abcMenos = new Array();
abcMenos = ["","a","b","c","d","e","f","g","h","i","j","k","l","m","n","ñ","o","p","q","r","s","t","u","v","w","x","y","z"];

function selectRangoNum(name,ini,fin,funcion,defecto,paso) {
	if (paso==null) {paso=1;}
	document.write("<select name='"+name+"' size='1' onchange='"+funcion+";'>");
	for (i=ini;i<=fin;i=(i*1)+paso){
		document.write("<option value='"+i+"'");
		if (defecto==i) document.write("selected");
		document.write(">"+i+"</option>");
	}
	document.write("</select>");
}

var meses = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
function selectRangoMeses(name,funcion,defecto) {
	document.write("<select name='"+name+"' size='1' onchange='"+funcion+";'>");
	for (i=1;i<=meses.length;i++){
		document.write("<option value='"+i+"'");
		if (defecto==i) document.write("selected");
		document.write(">"+meses[i-1]+"</option>");
	}
	document.write("</select>");
}

function selectRangoArray(name,a,funcion,defecto,disabled,isNull,tam) {
	if (isNull==null) isNull = "false";
  if ((disabled==null) || (disabled=='false')) disabled = "";
  if (disabled=='true') disabled = "DISABLED";
  if ((disabled=='DISABLED') || (disabled=='disabled')) {
		document.write("<input type='hidden' name='"+name+"' value='"+defecto+"'>");
  	name = name+"_"+disabled;
  }
  if (tam==null) {
  	var tamano = "style='width:382'";
  } else {
  	var tamano = "style='width:"+tam+"'";
  }
	document.write("<select name='"+name+"' size='1' class='input"+disabled+"' "+disabled+" onchange='"+funcion+";' "+tamano+" lang='nulo:"+isNull+"' chk='"+name+"'>");
	for (i=0;i<a.length;i++){
		par=a[i];
		document.write("<option value='"+par[1]+"'");
		if (defecto==par[1]) document.write("selected");
		document.write(">"+par[0]+"</option>");
	}
	document.write("</select>");
}

function selectMultipleRangoArray(name,a,funcion,defecto,disabled,isNull){
  if (isNull==null) isNull = "false";
  if ((disabled==null) || (disabled=='false')) disabled = "";
  if (disabled=='true') disabled = "DISABLED";
  var tamano = "style='width:382'";
	document.write("<select multiple name='"+name+"' size='10' class='input"+disabled+"' "+disabled+" onclick='"+funcion+";' ondblclick='"+funcion+";' "+tamano+" lang='nulo:"+isNull+"' chk='"+name+"'>");
	for (i=0;i<a.length;i++){
		par=a[i];
		document.write("<option value='"+par[1]+"'");
		for (j=0; j<defecto.length;j++) {
			if (defecto[j]==par[1]) document.write("selected");
		}
		document.write(">"+par[0]+"</option>");
	}
	document.write("</select>");
}

function selectRangoBool(name,funcion,defecto){
	selectRangoArray(name,[["falso","false"],["cierto","true"]],funcion,defecto);
}

function selectRangoVocales(funcion){
	var res = new Array();
	for (i=0; i<abcMas.length;i++){
		res[i] = new Array();
		if (abcMas[i] != "") {
			res[i][0] = "Empieza por "+abcMas[i];
		} else {
			res[i][0] = "Todos";
		}
		res[i][1] = abcMas[i];
	}
	selectRangoArray('',res,funcion,'','','','','');
}

function removeSelected(nameSelect, nameForm) {
	var f = getForm(nameForm);
	var nameSelect = eval("f."+nameSelect);
	for(i = 0; i < nameSelect.length; i++) {
		if (nameSelect.options[i].selected) {		
			nameSelect.remove(i--);
		}	
	}
}

function removeAll(nameSelect, nameForm) {
	var f = getForm(nameForm);
	var nameSelect = eval("f."+nameSelect);
	for(i = 0; i < nameSelect.length; i++) {
		nameSelect.remove(i--);
	}
}

function selectAll(nameSelect, nameForm) {
	var f = getForm(nameForm);
	var nameSelect = eval("f."+nameSelect);
	for(i = 0; i < nameSelect.length; i++) {
		nameSelect.options[i].selected = true;	
	}
}

function changeRangoArray(nameSelect,a,nameForm) {
	var f = getForm(nameForm);
	removeAll(nameSelect);
	var nameSelect = eval("f."+nameSelect);
	for (i=0;i<a.length;i++){
		par=a[i];
		item_opt = document.createElement("OPTION");
		item_opt.text = par[0];
		item_opt.value = par[1];
		nameSelect.add(item_opt);
	}
}

function marcarSelect(formName,selectName,valor) {
	var sel=eval(formName+"."+selectName);
	for(i=0;i<sel.length;i++){
		if (sel.options[i].value==valor){
			sel.options[i].selected=true;
		}
	}
}

function filtrarArrayRecurso(a,nombre) {
	var b = new Array();
	var j = 0;
	for (i=0;i<a.length;i++) {
		if (nombre != '') {
			par = a[i][0];
			if (par != "") {
				var ext = par.split(" - ");
				ext = ext[ext.length - 1];
				if (ext != nombre) {
					continue;
				}
			}
		}
		b[j++] = a[i];
	}
	return b;
}

function filtrarArrayInicio(a,inicio) {
	var indiceMas;
	for (i=0;i<abcMas.length;i++) {
		if (abcMas[i] == inicio) {
			indiceMas = i;
			break;
		}
	}
	var b = new Array();
	var j = 0;
	for (i=0;i<a.length;i++) {
		if (inicio != '') {
			par = a[i][0];
			if ((par != "") && (par != " ")) {
				if ((par.indexOf(inicio)) && (par.indexOf(abcMenos[indiceMas]))) {
					continue;
				}
			}
		}
		b[j++] = a[i];
	}
	return b;
}

//////////////////////////////////////////////////////////////////////
// OPERACIONES CON SELECTS MULTIPLES
function moveup(src)
{
	var i, temp;
	for (i=0; i<src.options.length; i++)
	{
		if (src.options[i].selected)
		{
			if (i!=0)
			{
				temp = src.options[i-1];
				items=new Option(temp.text,temp.value);
				otheritem=new Option(src.options[i].text,src.options[i].value);
				otheritem.selected=1;
				var j=i-1;
				src.options[j]=otheritem;
				src.options[i]=items;
			}
		}
	}
}

function movedown(src)
{
	var i, temp;
	for (i=src.options.length-1; i>=0; i--)
	{
		if (src.options[i].selected)
		{
			if (i!=src.options.length-1)
			{
				temp = src.options[i+1];
				items=new Option(temp.text,temp.value);
				otheritem=new Option(src.options[i].text,src.options[i].value);
				otheritem.selected=1;
				var j=i+1;
				src.options[j]=otheritem;
				src.options[i]=items;
			}
		}
	}
}

function moveleft(src,target)
{
	var sc, itemText, i, newIndex, listy;
	for (i=0; i<src.options.length; i++)
	{
		if (src.options[i].selected)
		{
			var sc=i;
			var o=src.options[sc]
         items=new Option(o.text,o.value);
			
			newIndex=target.options.length;
			target.options[newIndex]=items;
			target.options[newIndex].selected=true
		}
	}
	i = 0;
	while (i<src.options.length)
	{
		if (src.options[i].selected)
			src.options[i]=null
		else
			i = i + 1;
	}
	delNul(src)
	delNul(target)
}

function moveright(src,target,prepend,required)
{
	var sc, itemText, i, oldIndex, numSelected, newIndex, listy;
	numSelected=0;
	for (i=0; i<src.options.length; i++)
	{
		if (src.options[i].selected)
		{
			var sc=i;
			var o=src.options[sc];
         items=new Option(o.text,o.value);
			
			newIndex=target.options.length;
			target.options[newIndex]=items;
			
			target.options[newIndex].selected=true
			oldIndex=i;
			numSelected++;
		}
	}
	i = 0;
	while (i<src.options.length)
	{
		if (src.options[i].selected)
			src.options[i]=null
		else
			i = i + 1;
	}
	delNul(src);
	delNul(target);
	
	if (src.options.length>0)
	{
		if (src.options.length>(oldIndex-numSelected+1))
			src.options[oldIndex-numSelected+1].selected=true;
		else
			src.options[src.options.length-1].selected=true;
	}
}

function delNul(lst){
	i = 0;
	while (i<lst.options.length) {
		if (lst.options[i].value==-1)
			lst.options[i]=null
		else
			i = i + 1;
	}
}

function selAll(lst){
 if (!lst) return;
	for (i=0; i<lst.options.length; i++)
		lst.options[i].selected=true
	delNul(lst)
}
