var indiceseleccion = -1;
var pulsoMovimiento = false;

var ant_a=0;
var ult_tec_pob = 0;

function BorraDesplegable(desplegable)
{
	var total_items_actual=desplegable.options.length;
	for (var i=0;i<total_items_actual;i++)
	{
		desplegable.options[i]=null;
	}
	desplegable.options.length=0;

}

function selecpobq()
{
//document.getElementById('capamare')
	if (document.listadoForm2.selpobq2.options[document.listadoForm2.selpobq2.selectedIndex].value=="+")
	{
//		document.listadoForm2.selpobq2.options[i]= new Option( pobz[a],  pobz[a], false);
		fillformq(ant_a);
	}
	else
	{
		document.listadoForm2.poblacionq.value=document.listadoForm2.selpobq2.options[document.listadoForm2.selpobq2.selectedIndex].value;
		ocultarq('capaq1');
	}
}
function fillformq(a)
{
            var sortir = false;
            var i=0;

            var cadena = document.listadoForm2.poblacionq.value;
            cadena = cadena.toUpperCase();
            document.listadoForm2.poblacionq.value= cadena;

            BorraDesplegable(document.listadoForm2.selpobq2);

            if (cadena.length>-1)
            {
                        while (a<pobz.length && !sortir)
                        {
                                   if ( Semblantq(pobz[a],cadena)>-1 )
                                   {
                                               document.listadoForm2.selpobq2.options[i]= new Option( pobz[a],  pobz[a], false);
                                               i++;
                                               if ((i)>58)
                                               {
                                                           document.listadoForm2.selpobq2.options[i]= new Option( "+",  "+", false);
                                                           sortir=true;
                                                           ant_a = a;
                                                           i++;
                                               }
                                   }
                                   a++;
                        }
            }


                        if (document.listadoForm2.selpobq2.selectedIndex==-1 && pulsoMovimiento==false){
                                   document.listadoForm2.selpobq2.selectedIndex=0; //seleccionamos el combo
                                   indiceseleccion = 0;
                        }
                        if (indiceseleccion!=-1){
                                    document.listadoForm2.selpobq2.selectedIndex=indiceseleccion;
                        }


            if (document.listadoForm2.selpobq2.options.length==0) ocultarq('capaq1');

}


function Semblantq(cad1,cad2)
{
	myString = new String(cad1);
	results = myString.search(cad2);
	return results;
}

function mostrarq(nombreCapaq){
document.getElementById(nombreCapaq).style.visibility="visible";
}
function ocultarq(nombreCapaq){
document.getElementById(nombreCapaq).style.visibility="hidden";
}
function capturaTeclaq(e)
{
            comboPobs = document.getElementById('selpobq2');
            if(document.all) tecla=event.keyCode;
            else tecla=e.which;
            ult_tec_pob=tecla;

            if(tecla==13)
            {
                        pulsoMovimiento = false;
                        ocultarq('capaq1');
                        if (comboPobs.selectedIndex>-1){
                                   document.listadoForm2.poblacionq.value = comboPobs.options[comboPobs.selectedIndex].value;
                        }

            } else{
                        mostrarq('capaq1');
                        if (tecla==40){ //indica pulsarq abajo

                                               if (comboPobs.length>0){



                                                           pulsoMovimiento = true;
                                                           if (indiceseleccion + 1<comboPobs.length){
                                                                       indiceseleccion = indiceseleccion + 1;
                                                           }
                                               }else{

                                                           pulsoMovimiento = false;
                                                           ocultarq('capaq1');
                                               }

                        } else if(tecla==38){ //pulso arriba
                                   pulsoMovimiento = true;
                                               if (comboPobs.length>0){

                                                           if (indiceseleccion>0){
                                                                       indiceseleccion = indiceseleccion - 1;
                                                           }
                                               } else{
                                                           pulsoMovimiento = false;
                                                           ocultarq('capaq1');
                                               }

                        } else{
                                   pulsoMovimiento = false;
                        }
                                    }
}

function pulsarq(e) {
  tecla = (document.all) ? e.keyCode : e.which;
  return (tecla != 13);
}