/**
 * @author jdreissig
 */



if (typeof(mobile) == "undefined") var mobile = {};

/**
 * Some utility functions for form elements.
 */
mobile.FormHelper = Object.extend({}, {

	/**
	 * Enable an input/select and its corresponding label.
	 * The label's "for" attribute can be specified explicitly, also the class to
	 * mark a label as disabled (default: "inactive").
	 * @param {Element} field The Form field to disable.
	 * @param {String} labelFor	The "for" field to identify the label by. Defaults to field id.
	 * @param {String} disabledClass The CSS class to apply to disabled labels. Defaults to "inactive".
	 */
	enableFieldAndLabel: function(field, labelFor, disabledClass) {
		field = $(field);

		if (field) {
			// defaults
			labelFor = labelFor || field.id;
			disabledClass = disabledClass || "inactive";

			field.enable();
			var label = $$('label[for="' + labelFor + '"]').first();
			if (label)
				label.removeClassName(disabledClass);
		}
	},

	/**
	 * @see #enableFieldAndLabel.
	 * @param {Object} field
	 * @param {Object} labelFor
	 * @param {Object} disabledClass
	 */
	disableFieldAndLabel: function(field, labelFor, disabledClass) {
		field = $(field);

		if (field) {
			// defaults
			labelFor = labelFor || field.id;
			disabledClass = disabledClass || "inactive";

			field.disable();
			var label = $$('label[for="' + labelFor + '"]').first();
			if (label)
				label.addClassName(disabledClass);
		}
	},
	
	/**
	 * @see #checkAll
	 * @param {String} form - id or of the form or HtmlElement 
	 */
	checkBoxes: function(form, check) {
		form = $(form);
		form.select('input[type=checkbox]').each(function(checkbox) {
			checkbox.checked = check;
		});
	},
	
	/**
	 * @param {String} select with event onchange attached - id of the select 
	 * @param {String} select that changes the selected index - id of the select 
	 */
	connectSelects: function(masterSelect, slaveSelect){
		masterSelect = $(masterSelect);
		slaveSelect = $(slaveSelect);
		
		if(masterSelect && slaveSelect){
			masterSelect.observe("change", function(event){
				slaveSelect.setValue(this.value);
			});
		}
	},
	/**
	 * @param {String} select with event onchange attached - id of the select 
	 * @param {Array} selects that changes the selected index - array of select ids 
	 */
	connectMultiSelects: function(masterSelect, slaveSelects){
		slaveSelects.each(function(slaveSelect){this.connectSelects(masterSelect,slaveSelect)}.bind(this));
	},
	
	/**
	 * @param {String} text input with event onchange attached - id of the text input 
	 * @param {String} text input that is connected - id of the text input 
	 */
	connectTextInputs: function(masterInput, slaveInput){
		masterInput = $(masterInput);
		slaveInput = $(slaveInput);
		if (masterInput && slaveInput){
			masterInput.observe("change", function(){
				slaveInput.value = this.value;
			});
		}
	},
	/**
	 * @param {String} text input with event onchange attached - id of the text input 
	 * @param {Array} text inputs that are connected - ids of the text inputs 
	 */
	connectMultiTextInputs: function(masterInput, slaveInputs){
		slaveInputs.each(function(slaveInput){this.connectTextInputs(masterInput,slaveInput)}.bind(this));
	},
	
	/**
	 * disable input type=submit on submit
	 * @param {Object} form element 
	*/
	submitOnce: function(form){
		form = $(form);
		form.observe("submit", function(event){
			form.down("input[type=submit]").disable();
		});
	} 
});

