
//
// ghSelectHelper Object -- used to help with manipulation of Select boxes
//
function ghSelectHelper(opts)
{
	//	opts should be an object with:
	//		id: idvalue  -- where idvalue can be passed to getElementById
	//      element: obj -- where obj is the DOM element of the select boxx
	// alert('Create ghSelectHelper object');
	this.id = null;
	this.element = null;

	if (typeof opts.id != 'undefined') {
		this.setSelectElementById(opts.id);
	} else if (typeof opt.element != 'undefined') {
		this.setSelectElement(element);
	}
}


// Set the Select element in this object & id if available
ghSelectHelper.prototype.setSelectElement = function(element) 
{
	// alert('setSelectElement called');
	this.element = element;
	if (typeof element.id != 'undefined') {
		this.id = element.id;
	} else {
		this.id = null;
	}
}

// Set the element & id of this object
ghSelectHelper.prototype.setSelectElementById = function(id)
{
	// alert('setSelectId called');
	this.element = document.getElementById(id);
	this.id = id;
}

// Return the Select element in the DOM
ghSelectHelper.prototype.getSelectElement = function() 
{
	// alert('getSelectElement called');
	return this.element;
}

// Return the value of the "id" tag on the element
ghSelectHelper.prototype.getSelectId = function()
{
	// alert('getSelectId called');
	return this.id;
}

// Return the index of the indicated option, or -1 if not found
ghSelectHelper.prototype.getOptionIndex = function(opt)
{
	// alert('getOptionIndex called');
	var i;
	if (typeof opt.index != 'undefined') {
		if (opt.index <= this.element.length) {
			return opt.index;
		} else {
			return -1;
		}
	} else if (typeof opt.index_value != 'undefined') {
		// Search by value
		for (i = 0; i < this.element.length; i++) {
			if (this.element.options[i].value == opt.index_value) {
				// alert('found value at: ' + i);
				return i;
			}
		}
		return -1;
	} else if (typeof opt.index_text != 'undefined') {
		// Search by text
		for (i = 0; i < this.element.length; i++) {
			if (this.element.options[i].text == opt.index_text) {
				// alert('found text at: ' + i);
				return i;
			}
		}
		return -1;
	}
	// alert('getOptionIndex usage needs index/value/text passed as object pair');
	return -1;
}

// Remove an option, may add option to not chain to onchange(), but currently calls it
// note:  onchange only gets called if the option being removed was selected
ghSelectHelper.prototype.removeOption = function(opt)
{
	// alert('removeOption called');
	var index = this.getOptionIndex(opt);
	if (index != -1) {
		if (this.element.selectedIndex == index) {
			// Need to call onchange
			this.element.remove(index);
			if (typeof this.element.onchange != 'undefined') {
				this.element.onchange();
			}
		} else {
			this.element.remove(index);
		}
	}
}


// Select an option, may add option to not chain to onchange(), but currently calls it
// note:  onchange only gets called if the option being selected was not already selected
ghSelectHelper.prototype.selectOption = function(opt)
{
	// alert('selectOption called');
	var index = this.getOptionIndex(opt);
	if (index != -1) {
		if (this.element.selectedIndex != index) {
			this.element.selectedIndex = index;
			// alert('Selected new option: ' + index);
			if (typeof this.element.onchange != 'undefined') {
				this.element.onchange();
			}
		}
	}
}


// Insert Option before specified option
ghSelectHelper.prototype.insertOptionBefore = function(opt)
{
	// alert('insertOptionBefore called');
	var index = this.getOptionIndex(opt);
	if (index != -1) {
		var option = document.createElement('option');
		if (typeof opt.value != 'undefined') {
			option.value = opt.value;
		}
		if (typeof opt.text != 'undefined') {
			option.text = opt.text;
		}
		try {
			this.element.add(option, this.element.options[index]);
		}
		catch (e) {
			this.element.add(option, index);
		}
	}
}

// Create alias function for insertOption
ghSelectHelper.prototype.insertOption = ghSelectHelper.prototype.insertOptionBefore;


// Insert Option After specified option
ghSelectHelper.prototype.insertOptionAfter = function(opt)
{
	// alert('insertOptionAfter called');
	var index = this.getOptionIndex(opt);
	if (index != -1) {
		var option = document.createElement('option');
		if (typeof opt.value != 'undefined') {
			option.value = opt.value;
		}
		if (typeof opt.text != 'undefined') {
			option.text = opt.text;
		}
		if (index == this.element.length) {
			// Insert at End
			try {
				this.element.add(option, null);
			}
			catch (e) {
				this.element.add(option);
			}
		} else {
			// Insert before index + 1
			try {
				this.element.add(option, this.element.options[index+1]);
			}
			catch (e) {
				this.element.add(option, index+1);
			}
		}
	}
}


// Append Option at end
ghSelectHelper.prototype.appendOption = function(opt)
{
	// alert('appendOption called');
	var option = document.createElement('option');
	if (typeof opt.value != 'undefined') {
		option.value = opt.value;
	}
	if (typeof opt.text != 'undefined') {
		option.text = opt.text;
	}
	// Insert at End
	try {
		this.element.add(option, null);
	}
	catch (e) {
		this.element.add(option);
	}
}


// getSelected -- Return { index, value, text } if known
ghSelectHelper.prototype.getSelected = function()
{
	// alert('getSelected called');
	var obj = new Object;
	obj.index = this.element.selectedIndex;
	if (obj.index >= 0) {
		if (typeof this.element.options[obj.index].value != 'undefined') {
			obj.value = this.element.options[obj.index].value;
		}
		if (typeof this.element.options[obj.index].text != 'undefined') {
			obj.text = this.element.options[obj.index].text;
		}
	}
	return obj;
}

// getSelectedIndex - return the index to the selected item
ghSelectHelper.prototype.getSelectedIndex = function()
{
    return this.element.selectedIndex;
}

// clearOptions -- remove from [lower, upper) in opts list or all
// you may specify either lower or upper or both or neither
ghSelectHelper.prototype.clearOptions = function(opts)
{
    var i;
    var upperbound = this.element.length;
    var lowerbound = 0;

    if (typeof opts.lower != 'undefined') {
        lowerbound = opts.lower;
    }
    if (typeof opts.upper != 'undefined') {
        upperbound = opts.upper;
    }
    for (i = upperbound - 1; i >= lowerbound ; i--) {
        this.removeOption({ index: i });
    }
}

