/*
	Some Functions for Form's
	Author: Everton Emilio Tavares
	Date: 28-08-2007
	Location: Cascavel - Paran? - Brazil
	
	http://forum.mootools.net/viewtopic.php?id=5286
*/
Element.extend({
	/* 
		Property: getFormValues
 
		Return a object
	 */
	getFormValues: function() {
		vals = {};
		this.getFormElements().each(function(el){
			var name = el.name;
			var val  = el.getValue();
			if (val === false || !name || el.disabled) return;
			//if exists value for input checkbox, append value in a new array
			if ($chk(vals[name])&&this.type=='checkbox') vals[name] = [vals[name]]; 
			if ($type(vals[name])=='array') vals[name].push(val)
			else vals[name] = val;
		});
		return vals;
	},
	/* 
		Property: getInputByName
	 */
	getInputByName : function(nome) {
		el = this.getFormElements().filterByAttribute('name','=',nome)
		return (el)?(el.length = 1)?el[0]:el:false;
	},
	/*
		Property: emptyValue
 
		Remove Value
	 */	
	emptyValue : function() {
		switch (this.getTag()){
			case 'select':
				$each(this.options, function(option){option.selected = false;});
				break;
			case 'input': 
				if (this.checked && ['checkbox', 'radio'].contains(this.type)) this.checked = false
				else if (['hidden', 'text', 'password'].contains(this.type)) this.value = '';
				break;
            		case 'textarea': this.value = '';
		}
		return this;
	},
	/* 
		Property: setValue
 
		Sets element value
	 */
		setValue : function(val) {
			switch (this.getTag()){
				case 'select':
					if ($type(val)=='array') val.each(function(v,i){val[i]=v.toString()});
					sel = function(option) {
						if (($type(val)=='array'&&val.contains(option.value))||(option.value==val))option.selected = true
						else option.selected = false;
					}
					$each(this.options,sel);
					break;
				case 'input': 
					if (['checkbox', 'radio'].contains(this.type))this.checked=(($type(val)=='array')?val.contains(this.value):(this.value==val));
					else if (['hidden', 'text', 'password'].contains(this.type.toString()))this.value=val;
					break;
	            		case 'textarea': this.value = val;
	            			break;
						//if element isn't a input, set the text
	            		default: if($type(val)!='array') this.setText(val);
			}
			return this;
		},
		
		
		/*
		added by Collin Green
		
		*/
		emptyForm : function() {
			this.getFormElements().each( function(el){
				el.emptyValue();
			});
		}
		
});
