var SelUsuario = {
Evento: function (elemento,nomevento,funcion) {
if (elemento.attachEvent)
{
var f=function(){
funcion.call(elemento,window.event);
}
elemento.attachEvent('on'+nomevento,f);
return true;
}
else
if (elemento.addEventListener)
{
elemento.addEventListener(nomevento,funcion,false);
return true;
}
else
return false;
},
init: function(){
var elem1 = document.getElementById("txt");
var elem2 = document.getElementById("contenedor");
SelUsuario.Evento(elem1, 'keyup', SelUsuario.Empieza);
SelUsuario.Evento(elem2, 'mouseover', SelUsuario.Seleccionar);
SelUsuario.Evento(elem1, 'keydown', SelUsuario.Pegar2);
SelUsuario.Evento(window, 'click', SelUsuario.Cerrar);
},
creaAjax: function (){
var ajaxs = ["Msxml2.XMLHTTP","Msxml2.XMLHTTP.4.0","Msxml2.XMLH TTP.5.0","Msxml2.XMLHTTP.3.0","Microsoft.XMLHTTP"];
var ajax = false;
for(var i=0 ; !ajax && i<ajaxs.length ; i++){
try{
ajax = new ActiveXObject(ajaxs[i]); // Internet Explorer
}
catch(e) {
ajax = false;
}
}
if(!ajax && typeof XMLHttpRequest!='undefined') {
ajax = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
return ajax;
},
Empieza: function (ev) {
var str = document.getElementById(this.id).value;
var obj = document.getElementById("contenedor");
var ref = obj.getElementsByTagName('DIV');
var keyCode = document.layers ? ev.which : document.all ? event.keyCode : document.getElementById ? ev.keyCode : 0;
if (keyCode == 40) {
SelUsuario.BajaOpt ()
} else if (keyCode == 38) {
SelUsuario.SubeOpt ()
} else if (keyCode == 13) {
for (var i = 0; i <= ref.length-1; i++) {
if (ref[i].className == "seleccionado") {
document.getElementById("txt").value = ref[i].id;
if (obj.style.display == "block"); obj.style.display= "none";
break;
}
}
try{ // DOM;
ev.preventDefault();
}catch(e){ // iexplore;
ev.returnValue = false;
}
} else {
if (str.length < 2) {
//obj.innerHTML = "";
if (obj.style.display == "none") obj.style.display= "none";
else obj.style.display= "none";
return;
}
ajax = SelUsuario.creaAjax()
if (ajax==null) {
alert ("Tu navegador no soporta Ajax");
return;
}
var url="SelecUsuarios.asp?q="+escape(str);
ajax.open("GET",url,true);
ajax.onreadystatechange = SelUsuario.Despliega;
//ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajax.send(null);
return
}
},
Despliega: function () {
var obj = document.getElementById("contenedor");
if (ajax.readyState==1 || ajax.readyState==2 || ajax.readyState==3) {
obj.innerHTML = "<p align='center'><img src='precarga.gif' border='0px' widht='15px' height='15px'/></p>";
obj.style.display= "block";
}
else if (ajax.readyState==4) {
if (ajax.status==200) {
obj.innerHTML = unescape(ajax.responseText);
}else {
obj.innerHTML = "<p>Se ha producido un error</p>";
}
if (obj.style.display == "none") obj.style.display= "block";
}
},
Seleccionar: function () {
var ref = document.getElementById(this.id).getElementsByTagName('DIV');
for (var i = 0; i < ref.length; i++) {
SelUsuario.Evento(ref[i], 'click', function() {
SelUsuario.Pegar(this.id);
})
SelUsuario.Evento(ref[i], 'mouseover', function() {
SelUsuario.Marcar(this.id);
})
}
},
Pegar: function (valor) {
var obj = document.getElementById("contenedor")
document.getElementById("txt").value = valor;
if (obj.style.display == "block"); obj.style.display= "none";
},
Pegar2: function (ev) {
var obj = document.getElementById("contenedor")
var ref = obj.getElementsByTagName('DIV');
var keyCode = document.layers ? ev.which : document.all ? event.keyCode : document.getElementById ? ev.keyCode : 0;
if ((keyCode == 13) || (keyCode == 9)) {
for (var i = 0; i <= ref.length-1; i++) {
if (ref[i].className == "seleccionado") {
document.getElementById("txt").value = ref[i].id;
if (obj.style.display == "block"); obj.style.display= "none";
break;
}
}
}
},
BajaOpt: function() {
var ref = document.getElementById("contenedor").getElementsByTagName('DIV');
for (var i = 0; i <= ref.length-1; i++) {
if (ref[i].className == "seleccionado") {
ref[i].className = "deseleccionado";
if (i < ref.length-1){
i++;
}else{
i = 0;
}
ref[i].className = "seleccionado"
}
}
},
SubeOpt: function() {
var ref = document.getElementById("contenedor").getElementsByTagName('DIV');
for (var i = 0; i <= ref.length-1; i++) {
if (ref[i].className == "seleccionado") {
ref[i].className = "deseleccionado";
if (i < ref.length && i > 0){
i--;
}else{
i = ref.length-1;
}
ref[i].className = "seleccionado"
}
}
},
Marcar: function (id) {
var selecionado = document.getElementById(id);
var ref = document.getElementById("contenedor").getElementsByTagName('DIV');
for (var i = 0; i <= ref.length-1; i++) {
if (ref[i].id != id) {
ref[i].className = "deseleccionado";
selecionado.className = "seleccionado";
}
}
},
Cerrar: function () {
var obj = document.getElementById("contenedor")
if (obj.style.display == "block"); obj.style.display= "none";
}
}
SelUsuario.Evento(window, 'load', SelUsuario.init,false);