/**
 * WJForm handles the javascript interactions for the forms
 *
 * @since Thu Mar 18 2010
 * @author Nick van der Zwet
 * @package Windmill.Javascript.Wizhz
 **/
var WJForm = Class.create({
	
	/**
	 * initialize
	 *
	 * Creates a new WJForm
	 *
	 * @since Thu Mar 18 2010
	 * @access public
	 * @return WJForm
	 **/
	initialize: function() {
		this.otherOriginTrigger = "Anders, namelijk..";
		if (this.otherOrigin = $('otherorigin') ) {
			this.otherOrigin.hide();
		}
	},
	
	/**
	 * cloneExperience
	 *
	 * Clones an experience block and adds it to the document
	 *
	 * @since Thu Mar 18 2010
	 * @access public
	 * @param Event event
	 * @return void
	 **/
	cloneExperience: function(event) {
		var experience = event.element().up(1).select("div[class = 'experience']").pop();

		if ( !(this.checkForEmpty(experience.select('input') ) && this.checkForEmpty(experience.select('select') ) ) ) {
			var newexperience = experience.cloneNode(true);
			
			var inputs = newexperience.select("input");
			for (var i = 0; i < inputs.length; i++) {
				if (inputs[i].type != "checkbox") {
					inputs[i].value = "";
				}
			}
			var selects = newexperience.select("select");
			for (var i = 0; i < selects.length; i++) {
				selects[i].value = "";
			}
	
			var textareas = newexperience.select("textarea");
			for (var i = 0; i < textareas.length; i++) {
				textareas[i].value = "";
			}
			
			experience.insert({after: newexperience});
		}
	},
 
 	/**
	* cloneCourse
	*
	* Clones an course block and adds it to the document
	*
	* @since Wed Mar 31 2010
	* @access public
	* @param Event event
	* @return void
	**/
	cloneCourse: function(event) {
		var course = event.element().up(1).select("div[class = 'course']").pop();

		if ( !(this.checkForEmpty(course.select('input') ) && this.checkForEmpty(course.select('select') ) ) ) {
			var newcourse = course.cloneNode(true);
				
			var inputs = newcourse.select("input");
			for (var i = 0; i < inputs.length; i++) {
				if (inputs[i].type != "checkbox") {
					inputs[i].value = "";
				}
			}
	
			var selects = newcourse.select("select");
			for (var i = 0; i < selects.length; i++) {
				selects[i].value = "";
			}
	
			course.insert({after: newcourse});
		}
	},
	
	/**
	* toggleOtherOriginField
	*
	* Hide or show the "Other" field depending on the chosen option in "Origin" 
	*
	* @since Thu Aug 12 2010
	* @access public
	* @param Event event
	* @return void
	**/
	toggleOtherOriginField: function(event) {
		var origin = Event.element(event);
		if (typeof(origin) != "undefined") {
			if (origin.value === this.otherOriginTrigger) {
				this.otherOrigin.show();
			} else {
				this.otherOrigin.select('input').first().value = "";
				this.otherOrigin.hide();
			}
		}
		
	},
	
	/**
	* cloneSelect
	*
	* Handles the event when a clonable select field changes
	*
	* @since Thu Jan 13 2011 
	* @access public
	* @param Event event
	* @return void
	**/
	cloneSelect: function(event) {
		var select = event.element();
		this.cloneSelectByElement(select);
	},


	/**
	* cloneSelectByElement
	*
	* Handles the actual cloning functionality
	*
	* Clones a selectbox when a value is picked, 
	* or remove it when it's set to nothing and it's a clone
	*
	* @since Tue Aug 17 2010
	* @access public
	* @param Event event
	* @return void
	**/
	cloneSelectByElement: function(select, doreturn) {
		if (Form.Element.getValue(select) != "" && !select.isCloned && (select.retrieve("maxAmount") == 0 || select.up().childElements().size() < select.retrieve("maxAmount") ) ) {
			//clones the select after an option has been chosen
			var newselect = select.clone(true);
			select.isCloned = true;
			select.isClone = true;
			select.addClassName('clone');
			var selectid = select.readAttribute("id").split("_");
			var selectnumber = selectid.length > 1 ? new Number(selectid[1]) + 1 : 2;
			newselect.setAttribute("id", selectid[0] + "_" + selectnumber);	
			select.insert({after: newselect});

			//"Fix" for rendering bug in ie8
			el = select.up().up();
			el.setStyle({display: "block"});
			el.setStyle({display: "inline-block"});
	
			//Makes the nex select field cloneable
			newselect.store("maxAmount", select.retrieve("maxAmount") );
			newselect.observe("change", wjform.cloneSelect.bind(this) );
		}
		else if (select.isClone && Form.Element.getValue(select) == "") {
			var oldselect = Element.remove(select);
		}
		if (doreturn){
			return newselect;
		}
	},
		
	/**
	* cloneSelectInit
	*
	* Initialises a clone select field
	*
	* @since Tue Aug 17 2010
	* @access public
	* @param select
	* @param value
	* @return void
	**/
	cloneSelectInit: function(select, value, maxAmount) {
		maxAmount = maxAmount || 0;
		select.store("maxAmount", maxAmount);
		if(select && value && value.length > 0) {
			select.store("maxAmount", maxAmount);
			//shows the select with the option that previously were chosen and clones a new empty select
			var values = value.split(", ");
			values.each(function(v){
				select.select("option").each(function(option) {
					if(option.readAttribute("value") == v ){
						select.setValue(v);
						select = this.cloneSelectByElement(select, true);
					}
				}.bind(this) );
			}.bind(this) );
		}
		return select;
	},

	/**
	 * checkForEmpty
	 *
	 * Enumerates through a given enumeration $enum and returns false if any filled field is encountered
	 *
	 * @since Tue 31 Aug 2010
	 * @access private
	 * @param Enumeration $enum
	 * @return void
	 **/
	checkForEmpty: function($enum) {
		var isEmpty = true;
		$enum.each(
			function(expfield) {
				if (expfield.value !== "") {
					isEmpty = false;
				}
			}.bind(this)
		);
		return isEmpty;
	}

});

var wjform = new WJForm();

