OptionManipulator = function(select)
{
    this.select = select;
}

OptionManipulator.prototype =
{
    select: null,

    clear: function()
    {
		while (this.select.options.length > 0)
        {
			this.select.remove(0);
		}        
    },

    getLessIndex: function(text, start, end)
    {
        var count = this.select.options.length;
        if (typeof(start) == "undefined") start = 0;
        if (typeof(end) == "undefined") end = count - 1;
		for (var i = start; i < count && i < end + 1; i++)
        {
            if (this.select.options[i].innerText > text)
            {
                return i;
            }
        }
        return -1;
    },

    insert: function(index, value, text)
    {
		var new_option = document.createElement("OPTION");
        this.select.add(new_option, index);
		new_option.value = value;
        new_option.innerText = text;
        return new_option;
    },
    
    insertHTML: function(index, value, text)
    {
		var new_option = document.createElement("OPTION");
        this.select.add(new_option, index);
		new_option.value = value;
        new_option.innerHTML = text;
        return new_option;
    },
    
    alphabeticInsert: function(value, text)
    {
        var i = this.getLessIndex(text);
        if (i != -1) this.insert(i, value, text);
        else this.append(value, text);
    },

    append: function(value, text)
    {
        try
        {
            return this.insert(this.select.options.length, value, text);
        }
        catch(err)
        {
            return this.insert(null, value, text);
        }
    },

    appendHTML: function(value, text)
    {
        try
        {
            return this.insertHTML(this.select.options.length, value, text);
        }
        catch(err)
        {
            return this.insertHTML(null, value, text);
        }
    },

    appendList: function(list)
    {
		for (var i = 0; i < list.length; i++)
        {
            this.append(list[i].value, list[i].text);
        }
    },

    appendListHTML: function(list)
    {
		for (var i = 0; i < list.length; i++)
        {
            this.appendHTML(list[i].value, list[i].text);
        }
    },

    selectedValue: function()
    {
        return this.select.options[this.select.selectedIndex].value;
    },

    selectValue: function(value)
    {
        var count = this.select.options.length;
        this.select.selectedIndex = 0;
		for (var i = 0; i < count; i++)
        {
            if (this.select.options[i].value == value)
            {
                this.select.selectedIndex = i;
                return;
            }
        }
    }
}
