var Qs_Form_OrderSelect = qs.createObject(); Qs_Form_OrderSelect.prototype = { initialize: function (opt) { for (var i in opt) { this[i] = opt[i]; } var elements = document.getElementsByName(this.name + '[]'); if (!elements.length) { alert('Select with name "' + this.name + '" not found'); return false; } else { this.hiddenElement = elements[0]; } elements = document.getElementsByName('_' + this.name + '[]'); if (!elements.length) { alert('Select with name "' + this.name + '" not found'); return false; } else { this.element = elements[0]; } this.btnUp = document.getElementById(this.name + '_move_up'); this.btnDown = document.getElementById(this.name + '_move_down'); var obj = this; this.btnUp.onclick = function() { obj.onUp(); }; this.btnDown.onclick = function() { obj.onDown(); }; }, switchOptions: function (select, topIndex, currentIndex) { var tmp = select.options[topIndex].text; select.options[topIndex].text = select.options[currentIndex].text; select.options[topIndex].label = select.options[currentIndex].label; select.options[currentIndex].text = tmp; select.options[currentIndex].label = tmp; var tmpValue = select.options[topIndex].value; select.options[topIndex].value = select.options[currentIndex].value; select.options[currentIndex].value = tmpValue; var selected = select.options[topIndex].selected; select.options[topIndex].selected = select.options[currentIndex].selected; select.options[currentIndex].selected = selected; }, onUp: function () { var topIndex = null; for (var i = 0; i < this.element.options.length; i++) { if (this.element.options[i].selected && topIndex !== null && !this.element.options[topIndex].selected) { this.switchOptions(this.element, topIndex, i); this.switchOptions(this.hiddenElement, topIndex, i); } topIndex = i; } }, onDown: function () { var topIndex = null; for (var i = this.element.options.length -1; i >= 0; i--) { if (this.element.options[i].selected && topIndex !== null && !this.element.options[topIndex].selected) { this.switchOptions(this.element, topIndex, i); this.switchOptions(this.hiddenElement, topIndex, i); } topIndex = i; } } }; function initFormOrderSelect(name) { var select = new Qs_Form_OrderSelect({name: name}); }