
if (typeof(V1_GenericIncluded) == "undefined")
	alert("UPDATER.JS \ngeneric.js must be included first");

var _v1_updates ;
function _init_v1_updates()
{  
   if (_v1_updates == null)
      _v1_updates = new Object;  
}

function _V1_SubmitUpdates ()
{
   _init_v1_updates() ;
	var xml = '';
	for (var assetType in _v1_updates)
	{
		var table = _v1_updates[assetType];
		for (var assetKey in table)
		{
			xml += ('<' + assetType + ' id="' + assetKey + '"' + xmlWriteFields( table[assetKey] ) + '/>') ;
		}
	}
	document.forms[0].__V1_UPDATES.value = xml;
	_v1_updates = null ;
}

function xmlWriteFields(row)
{
	var fields = '';
	for (var fieldName in row)
		fields += (' ' + fieldName + '="' + _V1_EncodeValue(row[fieldName]) + '"') ;
   return fields ;
}

function _V1_EncodeValue (value)
{
	if (typeof(value) == "boolean")
		return value? 1: '';
	return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
}
function _findV1property(control, propname)
{
	while (control && !control.getAttribute(propname))
		control = control.parentNode;
	return control ;
}
function _getChild(parent, propname)
{
	return (parent[propname] == null) ? parent[propname] = new Object : parent[propname];
}
function _valuePropName(control)
{
	return (control.type == 'checkbox') ? 'checked' : 'value' ;
}

function _V1_Updater_Changed (e)
{
	_V1_Updater_OnChange(e.srcElement || e.target);
}

function _V1_Updater_OnChange (control)
{
	var value = control[_valuePropName(control)] ;
	var ctrlField = _findV1property(control, '_v1_field') ;
	var ctrlKey = _findV1property(ctrlField, '_v1_key') ;
	var ctrlType = _findV1property(ctrlKey, '_v1_type') ;

	if (ctrlType != null) 
	{
		_init_v1_updates() ;
		
		var field = ctrlField.getAttribute('_v1_field');
		var key = ctrlKey.getAttribute('_v1_key');
		var type = ctrlType.getAttribute('_v1_type');
		
		var table = _getChild(_v1_updates, type) ;
		var row = _getChild(table, key) ;
		row[field] = value;

		control.className += " changed";
	}
}

function _V1_GetUpdaterElements ()
{
	var updaterElements = new Array();
	var elements = document.forms[0].elements;
	for (var i = 0; i < elements.length; ++i)
	{
		var element = elements[i];
		if ((element.id && element.id == "_v1_updater") || (element.name && element.name == "_v1_updater"))
			updaterElements[ updaterElements.length ] = element;
	}
	return updaterElements;
}

function _V1_RestoreUpdates ()
{
	var updaters = _V1_GetUpdaterElements();
	for (var i = 0; i < updaters.length; ++i)
		_V1_Updater_Restore(updaters[i]);
}

function _V1_Updater_Restore (control)
{
	var ctrlField = _findV1property(control, '_v1_field') ;
	var ctrlKey = _findV1property(ctrlField, '_v1_key') ;
	var ctrlType = _findV1property(ctrlKey, '_v1_type') ;

	if (ctrlType != null) 
	{
		_init_v1_updates() ;

		var field = ctrlField.getAttribute('_v1_field');
		var key = ctrlKey.getAttribute('_v1_key');
		var type = ctrlType.getAttribute('_v1_type');

		var table = _v1_updates[type];
		var row = (table != null) ? table[key] : null ;
		var value = (row != null) ? row[field] : null ;

		if (value != null)
		{
			control[_valuePropName(control)] = value ;
			control.className += " changed";
		}
	}
}

function _V1_Updater_Init ()
{
	var updaters = _V1_GetUpdaterElements();
	for (var i = 0; i < updaters.length; ++i)
		V1_HookEvent(updaters[i], "change", _V1_Updater_Changed);
		
	V1_HookEvent(document.forms[0], "submit", _V1_SubmitUpdates);
}

_V1_Updater_Init();
