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 selecpoby()
{
//document.getElementById('capamare')
	if (document.listadoForm.selpoby2.options[document.listadoForm.selpoby2.selectedIndex].value=="+")
	{
//		document.listadoForm.selpoby2.options[i]= new Option( poby[a],  poby[a], false);
		fillformy(ant_a);
	}
	else
	{
		document.listadoForm.poblaciony.value=document.listadoForm.selpoby2.options[document.listadoForm.selpoby2.selectedIndex].value;
		ocultary('capay1');
	}
}
function fillformy(a)
{
            var sortir = false;
            var i=0;

            var cadena = document.listadoForm.poblaciony.value;
            cadena = cadena.toUpperCase();
            document.listadoForm.poblaciony.value= cadena;

            BorraDesplegable(document.listadoForm.selpoby2);

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


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


            if (document.listadoForm.selpoby2.options.length==0) ocultary('capay1');

}


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

function mostrary(nombreCapay){
document.getElementById(nombreCapay).style.visibility="visible";
}
function ocultary(nombreCapay){
document.getElementById(nombreCapay).style.visibility="hidden";
}
function capturaTeclay(e)
{
            comboPobs = document.getElementById('selpoby2');
            if(document.all) tecla=event.keyCode;
            else tecla=e.which;
            ult_tec_pob=tecla;

            if(tecla==13)
            {
                        pulsoMovimiento = false;
                        ocultary('capay1');
                        if (comboPobs.selectedIndex>-1){
                                   document.listadoForm.poblaciony.value = comboPobs.options[comboPobs.selectedIndex].value;
                        }

            } else{
                        mostrary('capay1');
                        if (tecla==40){ //indica pulsary abajo

                                               if (comboPobs.length>0){



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

                                                           pulsoMovimiento = false;
                                                           ocultary('capay1');
                                               }

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

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

                        } else{
                                   pulsoMovimiento = false;
                        }
                                    }
}

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