function comboValue(value, key)
{
	this.text = value;
	this.value = key;
}

function ComboFieldData(formField, parentTable, watchField, names)
{
	this.myName = names;	

	this.id = g_id++;
	this.fld = formField;
	this.hash = parentTable;
	this.currentValue = "";

	var parentVal
	
	try {
		// causes exception in non-ie browsers
		var e = new Enumerator(parentTable)
		parentVal = '0';
	} catch( notieexception ) {
		for( parentVal in parentTable ) {
			// get first value;
			break;
		}
	}

	this.values = parentTable[parentVal];
	this.listeners = new Array(0);
	this.index = new Array(0);
	this.buildIndex();
	this.populate();
	
	if( watchField != null )
		watchField.addListener(this);
}

// set up methods
ComboFieldData.prototype.addListener = addListener;
ComboFieldData.prototype.notifyListeners = notifyListeners;
ComboFieldData.prototype.notifyValueChanged = comboNotifyValueChanged;
ComboFieldData.prototype.notifykeyReleased = notifykeyReleased;
ComboFieldData.prototype.valueChanged = valueChanged;
ComboFieldData.prototype.notify = comboNotify;
ComboFieldData.prototype.populate = populate;
ComboFieldData.prototype.getValue = comboGetValue;
ComboFieldData.prototype.setValue = comboSetValue;
ComboFieldData.prototype.buildIndex = buildIndex;

function notifykeyReleased()
{
	this.notifyValueChanged();
}

function comboNotifyValueChanged()
{
	var idx = -1;
	var i;
	
	for( i = 0; i < this.fld.options.length; i++ )
	{
		if( this.fld.options[i].selected )
		{
			idx = i;
			break;
		}
	}

	if( idx != -1 && this.fld.options[idx].value != this.currentValue )
		this.valueChanged();
}

function comboNotify(obj)
{
	var parentVal = obj.getValue();
	
	if( this.values != this.hash[parentVal] )
	{
		this.values = this.hash[parentVal];
		this.buildIndex();
		this.populate();
		this.notifyListeners();
	}
}

function buildIndex()
{
	var i;
	for( i = 0; i < this.values.length; i++ )
		this.index[this.values[i].value] = i;
}

function populate()
{
	this.fld.selectedIndex = -1;

	for(i = this.fld.length - 1; i >= 0; i--)
	{
		this.fld.options[i] = null;
	}
	
	for( i = 0; i < this.values.length; i++ )
	{
		var opt = this.values[i];
		this.fld.options[i] = new Option(opt.text, opt.value);
	}
	
	this.fld.selectedIndex = 0;
}

function comboGetValue()
{
	if( this.fld.selectedIndex == - 1)
		return null;
	else
		return this.fld.options[this.fld.selectedIndex].value;
}


function comboSetValue(newVal)
{
	this.currentValue = newVal;
	var idx = this.index[newVal];
	this.fld.options[idx].selected = true;
	this.notifyListeners();	
}
