/**
 * Multiselect.
 * Version: 1.0
 *
 * The use of this script is strictly forbidden without prior written
 * consent. You must obtain permission before copying, modifying, 
 * redistributing, deriving or selling any part of this program.
 *
 * Copyright (c) 2008 HYPERZOID
 * All Rights Reserved
 */


// The Multiselect object.
var multiselect = null;


/**
 * Initializes this program.
 */
function initMultiselect() {
    multiselect = new Multiselect();
}


/**
 * The constructor.
 */
function Multiselect() {
    this.options = new Array();
    for (var i = 0; i < 9; i++) {
        this.options[i] = false;
    }

    this.form = document.getElementById('informationForm');
}


/**
 * Handles onclick events.
 */
Multiselect.prototype.select = function() {
    // Select or deselect the clicked option.
    var index = this.form.productsSelect.selectedIndex;
    if (index != -1) {
        this.options[index] = !this.options[index];
    }

    // Highlight selected items.
    for (var i = 0; i < 9; i++) {
        this.form.productsSelect.options[i].selected = this.options[i];
    }

    // Set hidden products field.
    var value = '';
    var numSelected = 0;
    for (var i = 0; i < 9; i++) {
        if (this.options[i]) {
            if (numSelected > 0) {
                value += ', ';
            }
            value += this.form.productsSelect[i].value;
            numSelected++;
        }
    }
    this.form.products.value = value;
}


// Run this function upon the onload event.
addOnLoad(initMultiselect);
