
function fvRow(title, form, fields, masterFieldNames)
{
	this.title = title;
	this.form = form;
	this.fields = fields;
	this.masterFieldNames = masterFieldNames;
	
	this.ready = false;
}


fvRow.prototype.calculate = function()
{
	// TODO: Implement me!
	
	/* this will have to check empty counts and all
	   that fun stuff. Can use old calculators as a
	   source for some example code (but don't even
	   think about copying it straight across).
	*/
}

fvRow.prototype.setReady = function()
{
	this.ready = true;
}


fvRow.prototype.calculateField = function(fieldName)
{
	if (this.ready)
	{
		var emptyCount = this.emptyCount();
		
		if (fieldName == null || emptyCount == 0 || (emptyCount == 1 && this.fields[fieldName].isEmpty(this.form)))
		{
			try
			{
				var valueList = this.getValueList();
				
				// A null fieldname means to just calculate the masters.
				if (fieldName != null)
				{
					valueList[fieldName] = this.fields[fieldName].solve(valueList);
				
					valueList = this.cycleValueList(valueList);
				}
				
				for (var i=0; i<this.masterFieldNames.length; i++)
				{
					if (this.masterFieldNames[i] != fieldName)
					{
						valueList[this.masterFieldNames[i]] = this.fields[this.masterFieldNames[i]].solve(valueList);
					}
				}
		
				this.setValueList(valueList);
			}
			catch (ex)
			{
				alert("Error occured while calculating '" + this.fields[fieldName].title +
				      "' for '" + this.title + "': \n\n" + ex);
			}
			
			this.ready = false;
		}
	}
}


fvRow.prototype.getValueList = function()
{
	var valueList = new Object;
	
	for (field in this.fields)
	{
		valueList[field] = this.fields[field].getValue(this.form);
	}
	
	return valueList;
}

fvRow.prototype.setValueList = function(valueList)
{
	for (field in valueList)
	{
		this.fields[field].setValue(this.form, valueList[field]);
	}
}

fvRow.prototype.cycleValueList = function(valueList)
{
	this.setValueList(valueList);
	
	return this.getValueList();
}

fvRow.prototype.emptyCount = function()
{
	var emptyCount = 0;
	
	for (field in this.fields)
	{
		if (this.fields[field].isEmpty(this.form))
			emptyCount++;
	}
	
	return emptyCount;
}
