var DivWinMulti;

/*
 * Widget que lista un conjunto de opciones que concuerdan con lo que uno escribe.
 * Las opciones aparecen paginadas.
 * 
 * Permite elegir múltiples opciones que se separan por coma.   
 */
DivWinMulti = function() {};

DivWinMulti.prototype = new DivWin();

DivWinMulti.prototype.separador = ',';


/*
 * Asignación de un valor a un campo del formulario.  
 */
DivWinMulti.prototype.asignacion = function(field, fieldValue) {
    if(field.className.match(/multiple/g)) {
        field.value = this.ultimoValorCambiadoPor(field.value, fieldValue) + ',';
        this.cursorAlFinal(field);
    }
    else {
        field.value = fieldValue;
    }
};

DivWinMulti.prototype.ultimoValor = function(cadena) {
    var valores = cadena.split(this.separador);
    return valores[valores.length - 1];
};

DivWinMulti.prototype.ultimoValorCambiadoPor = function(cadena, nuevoValor) {
    var valores = cadena.split(this.separador);
    valores[valores.length - 1] = nuevoValor;
    return valores.join(this.separador);
};

DivWinMulti.prototype.cursorAlFinal = function(field) {
    field.focus();
    if(field.setSelectionRange)
    {
        // Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
        var len = $(field).val().length * 2;
        field.setSelectionRange(len, len);
    }
    else {
        // ... otherwise replace the contents with itself
        // (Doesn't work in Google Chrome)
        $(field).val($(field).val());
    }
    field.scrollTop = 999999;
}

divWinMulti = new DivWinMulti();

//alert('carga ok');

