define("chy/form/FormManager", [
	"dojox/form/Manager",
	"dojo/_base/declare",
	"dojo/_base/array",
	"dojo/dom-attr",
	"dijit/registry"
], function(Manager, declare, array, domAttr, reg){

return declare("chy.form.FormManager", [ Manager ], {
	
	events: {},
	
	constructor: function(args){
		declare.safeMixin(this,args);
		this.inherited(arguments);
	},
		
	startup: function(){
		console.log('Start FormManager',this);
		this.inherited(arguments);
		domAttr.remove(this.domNode,'data-dojo-type');
		var _this = this;
		array.forEach(this.events, function(event){
			require(['chy/event/'+event.action], function(eventClass){
				//js object reflection, instantiate event class and instance
				 var eventObj = new eventClass;
				 eventObj.init(event, _this);
			});
		},this);
	},
	
	formNodeValue: function(name, value){
		//console.log('formNodeValue',this,name,value);
		//register dynamic included form elements (set horizontal etc.)
		this.registerWidgetDescendants(this);
		this.registerNodeDescendants(this.domNode);
		//if widget, set/get widget value by widgetid, otherwise set/get form element by name.
		var targetWidget = reg.byId(this.id+'-'+name);
		if(typeof value == 'undefined'){//get value
			if(typeof targetWidget != 'undefined'){
				return targetWidget.get('value');
			} else {
				return this.inherited(arguments);
			}
		} else {//set value
			if(typeof targetWidget != 'undefined'){
				targetWidget.set('value',value);
			} else {
				this.inherited(arguments);
			}
		}
	},
	
	validate: function(){
		//make sure all dynamic include widgets are registered
		this.registerWidgetDescendants(this);
		this.registerNodeDescendants(this.domNode);
		return this.inherited(arguments);
	},
	
	isValid: function(){
		//make sure all dynamic include widgets are registered
		this.registerWidgetDescendants(this);
		this.registerNodeDescendants(this.domNode);
		return this.inherited(arguments);
	}
});
});