/********************************************************************

	FOREX.COM - LINKED FIELDS
	
	=========================================
	Author: Chris Erwin (chris@teehanlax.com)
	Agency: teehan+lax
	Date: July 15, 2008
	=========================================	
	
	
	Description
	-------------------------------------------------------------
	LinkedFields sets up a field so that it will change multiple other fields when it's value is changed based on a calculation you pass in
	
	
	Public Methods
	-------------------------------------------------------------
	initialize (constructor)
		Arguments:
			- sourceFieldID (string) : the id of the source field, the field that will trigger the calculations
			- arrLinkedFields (array) : an array that contains an array for each linked field ("two dimensional array")
				- linkedFieldID (string) : the id of the field that the source field is linked to
				- equation (string) : the equation that will get evaluated to determine the linked field's value when the source field is changed. The source field can be referenced in the equation using "this.source.value"
		Returns: null
				
	updateLinkedFields
		Description: iterates through the array of linked fields and runs the equation associated with them and updates the linked fields values
		Arguments: none
		Returns: null
		
		
	Events
	-------------------------------------------------------------
	valueChange
		- runs the updateLinkedFields method
		- fired from within the spinnerControl class
		
		
	HTML Sample File
	-------------------------------------------------------------
	/_widget_samples/linked_fields.html
	
	
	Dependencies
	-------------------------------------------------------------
	- MooTools 1.2
	
*********************************************************************/

var LinkedField = new Class({
    initialize: function(sourceFieldID, arrLinkedFields){
		this.sourceFieldID = sourceFieldID;
		this.arrLinkedFields = arrLinkedFields;
		
		this.source = $(this.sourceFieldID);
		
		this.source.addEvent('keyup', function(event){
			this.updateLinkedFields(event.key);
		}.bind(this));
		this.source.addEvent('valueChange', this.updateLinkedFields.bind(this, null));
    },
	
	updateLinkedFields : function(e) {
		// don't do the calculations if the tab key was pressed
		if(e != "tab") {				
			for(i=0; i<this.arrLinkedFields.length; i++) {
				var linkedField = $(this.arrLinkedFields[i][0]);
				var calculation = this.arrLinkedFields[i][1];
				var newLinkedFieldValue = eval(calculation);			
				linkedField.value = newLinkedFieldValue;
			}	
		}
	}
});
