﻿function LlistaMultiple_Cercador_KeyUp (e, c, l) {
    var i, found, objc, objl;

    i = 0;
    found = false;
    objc = document.getElementById(c);
    objl = document.getElementById(l);
    if (objc.value != '') {
        while ((!found) && (i < objl.options.length)) {
            found = (objl.options[i].text.toLowerCase().indexOf(objc.value.toLowerCase()) == 0) ;
            i ++;
        }
    }
    if (found) {
        objl.selectedIndex = i - 1;
    }
    else {
        objl.selectedIndex = -1;
    }
}

function LlistaMultiple_CercadorOrigen_KeyDown (e, c, o, d, h) {
    var objc;
    
    if (e.keyCode == 13) { 
        objc = document.getElementById(c);
        objc.value = '';
        return LlistaMultiple_AfegirUn_Click(o, d, h);
    }
}

function LlistaMultiple_CercadorDesti_KeyDown (e, c, o, d, h) {
    var objc;

    if (e.keyCode == 13) { 
        objc = document.getElementById(c);
        objc.value = '';
        return LlistaMultiple_EliminarUn_Click(o, d, h);
    }
}

function LlistaMultiple_AfegirTots_Click(o, d, h) {
    var objo, objd;
    
    objo = document.getElementById(o);
    objd = document.getElementById(d);
        
    while (objo.length > 0) {
        objd.options.length++;
        objd.options[objd.length-1].value = objo.options[0].value;
        objd.options[objd.length-1].text = objo.options[0].text;
        objo.options[0] = null;
    }
    objo.selectedIndex = -1;
    LlistaMultiple_Value(o, d, h);
    return false; 
}

function LlistaMultiple_AfegirUn_Click(o, d, h) {
    var objo, objd;
    
    objo = document.getElementById(o);
    if (objo.selectedIndex >= 0) {
        objd = document.getElementById(d);
        objd.options.length++;
        objd.options[objd.length-1].value = objo.options[objo.selectedIndex].value;
        objd.options[objd.length-1].text = objo.options[objo.selectedIndex].text;
        objo.options[objo.selectedIndex] = null;
        objo.selectedIndex = ((objo.options.length > objo.selectedIndex) ? objo.selectedIndex : objo.selectedIndex - 1);
    }
    LlistaMultiple_Value(o, d, h);
    return false; 
}

function LlistaMultiple_EliminarUn_Click(o, d, h) {
    var objo, objd;
    
    objd = document.getElementById(d);
    if (objd.selectedIndex >= 0) {
        objo = document.getElementById(o);
        objo.options.length++;
        objo.options[objo.length-1].value = objd.options[objd.selectedIndex].value;
        objo.options[objo.length-1].text = objd.options[objd.selectedIndex].text;
        objd.options[objd.selectedIndex] = null;
        objd.selectedIndex = ((objd.options.length > objd.selectedIndex) ? objd.selectedIndex : objd.selectedIndex - 1);
    }
    LlistaMultiple_Value(o, d, h);
    return false; 
}

function LlistaMultiple_EliminarTots_Click(o, d, h) {
    var objo, objd;
    
    objo = document.getElementById(o);
    objd = document.getElementById(d);
        
    while (objd.length > 0) {
        objo.options.length++;
        objo.options[objo.length-1].value = objd.options[0].value;
        objo.options[objo.length-1].text = objd.options[0].text;
        objd.options[0] = null;
    }
    objd.selectedIndex = -1;
    LlistaMultiple_Value(o, d, h);
    return false; 
}

function LlistaMultiple_Value(o, d, h) {
    var objd, objh, i;

    objd = document.getElementById(d);
    objh = document.getElementById(h);
    objh.value = ''; 
    for (i=0; i<objd.length; i++) {
        if (objh.value != '') {
            objh.value += '.';
        }
        objh.value += objd.options[i].value;
    }
}
