/*
 *	FieldUtils helper utility to pass field information from one page to the other
 *	without having to interact with cross browser communication directly
 *
 *	This requires crossBrowser to be loaded
 */

function FieldUtils()
{
}

/*
 *	getFieldFromParent allows a field values to be retrieved from a parent form
 *
 *	formName - name of the form on the parent
 *	field - name of the field in the form
 *	func - the function to execute when the information has been retrieved
 */
FieldUtils.prototype.getFieldFromParent=function(formName,field,func)
{
	if (parentCrossBrowserCommunicationNotifier)
	{
		var notfunc=function(notifier,fieldparam,value)
		{
			if (field==fieldparam)
			{
				if (value)
				{
					func(field,value);
					//We are done so stop listening
					crossBrowserCommunicationNode.removeListener(formName+"_get",notfunc);
				}
			}
		};
		crossBrowserCommunicationNode.addListener(formName+"_getRet",notfunc);
		parentCrossBrowserCommunicationNotifier.notify(formName+"_get",field);
	}
}
/*
 *	putFormToParent allows a form to be propagated to the parent
 *
 *	formName - name of the form on the parent
 *	form - the actual form
 */
FieldUtils.prototype.putFormToParent=function(formName,form)
{
	if (parentCrossBrowserCommunicationNotifier)
	{
		parentCrossBrowserCommunicationNotifier.notify(formName+"_putForm",form);
	}
}

/*
 *	addForm allows a form to be a data source for parameter requests
 *
 *	form - the actual form
 *	put - the function that is called when the form needs to be updated, the function should accept a form from the source
 */
FieldUtils.prototype.addForm=function(form,put)
{
	//Values can be grabbed from this form, and set to this form 
	crossBrowserCommunicationNode.addListener(form.name+"_get",
	function(notifier,field)
	{
		if (form[field])
		{
			notifier.notify(form.name+"_getRet",field,form[field].value);
		}
	});
	crossBrowserCommunicationNode.addListener(form.name+"_putForm",
	function(notifier,formparam)
	{
		put(formparam);
	});
}

var fieldUtils=new FieldUtils();

