// #PLib::PLoad('PLib.Dom.Client.prototype');
// #PLib::PLoad('PLib.Dom.Client.Scripts.lowpro');

/**
 * The submit event of all forms of the class "autostate_form" is catched
 * to fetch state information. Afterwards the form is submitted
 */

var AutoState = Class.create();

AutoState.prototype = {
	initialize: function() {
		this.listeners = new Array();
	//	Event.observe(window, 'load', this.bindToDocument.bindAsEventListener(this));
		Event.onReady(this.bindToDocument.bindAsEventListener(this));
	},
	
	bindToDocument: function(event) {
		var ctrls = $$('.autostate_form');

		var listener = this.formSubmitted.bindAsEventListener(this);
		ctrls.each(function(ctrl)
		{
			Event.observe(ctrl, 'submit', listener);
		});
	},
		
	formSubmitted: function(event)
	{
		var el = Event.element(event)
		this.fillAutoState(el);
	},
	
	submitForm: function(form)
	{
	 	this.fillAutoState(form);
 		form.submit();
	},
	
	fillAutoState: function(el)
	{
		var autoStates = el.getElementsBySelector('input[name$=_autostate]');
		var myThis = this;

		var doneMap = new Hash();
		
		this.listeners.each(function(listener) {
			// Your code working on item here...
			
			listener();
		}); 
				
		autoStates.each(function(item) {
			// Your code working on item here...
			if(typeof(doneMap.get(item.name)) == "undefined")
			{
				var name = item.name.substring(0, item.name.length - 10);
			
				myThis.copyFieldsTo(el, item.name, name);
				doneMap.set(item.name, true);
			}
		}); 
		
		// Needed e.g. for elements with an index afterwards
		autoStates = el.getElementsBySelector('input[name^=autostate_]');

		autoStates.each(function(item) {
			// Your code working on item here...
			if(typeof(doneMap.get(item.name)) == "undefined")
			{
				var name = item.name.substring(10, item.name.length);
				myThis.copyFieldsTo(el, item.name, name);
				doneMap.set(item.name, true);
			}
			/*
			var ctrl = document.getElementsByName(name);
			if(ctrl.length > 0)
			{
				item.value = ctrl[0].value;
			//	alert(ctrl[0].value);
			}*/
		}); 
					
	},
	
	copyFieldsTo: function(toParent, toName, fromName)
	{
		var to = toParent.select('[name="' + toName + '"]');
		var from = document.getElementsByName(fromName);
		var i = 0;
		var i3 = 0;
		var to2;
		while(i < from.length)
		{
//	alert(from[i].checked);
			if((from[i].checked == "undefined") || 
				((from[i].type != 'radio') && (from[i].type != 'checkbox'))
				 || (from[i].checked))
			{
				if(i3 < to.length)
				{
					to2 = to[i3];
				}
				else
				{
					to2 = document.createElement('input');
					to2.type = 'hidden';
					to2.name = toName;
				//	to[i3] = to2;
					toParent.appendChild(to2);
				}
				to2.value = from[i].value;
				i3++;
			}
			i++
		}
		if(i > 0)
		{
			var i2 = to.length - 1;
		//	alert(i);
		//	alert(i2);
		//	alert(i3);
			while(i2 >= i3)
			{
			//	alert(to[i2].name);
				to[i2].remove();
				i2--;
			}
		}
		/*
		var first = true;
		ctrls.each(function(ctrl)
		{
			if(first)
			{
				first = false;
			}
			else
			{
				
			}
			item.value = ctrl.value;
		//	alert(ctrl[0].value);
		}
		*/		
	},
	
	addFillListener: function(listener)
	{
		this.listeners.push(listener);
	}
}

var autoState = new AutoState();
