/*
 This file is part of HiNii.

 Copyright (C) 2008 Carnino Claudio (jollyr0ger) <jollyr0g3r@gmail.com>
 */

/**
 * FormUtils
 * @author jollyr0ger
 *
 * Attention need:
 * -Prototype
 */
var FormUtils = Class.create({ });
Object.extend(FormUtils, {	

	
	
	/**
	 * Check if all the elements have the passed classNameCorrect and are filled
	 * @param	string		form					Form id
	 * @param	string		classNameObligatory		Css class to check
	 * @param	string		classNameCorrect		Css class that input must have
	 * @param	string		differentTo				Different from this value
	 * @return	Element[]	All the fields not filled
	 */
	fieldNotFilled: function(form, classNameObligatory, classNameCorrect, differentTo){
		
		var elements = $$('.'+ classNameObligatory);
		var elementsNotFilled = Array();
		
		
		// Delete dubplicate
		elements = FormUtils.elementNamesFiltered(elements);
		
		elements.each(function(item){
			
			item = $(item);
			
			// Check if the item is a children of the specified form
			var ancestors = item.ancestors();
			var isChildren = false;
			for(var ic = 0; ic < ancestors.size(); ic++){
				
				if(ancestors[ic].identify() == form){
					isChildren = true;
				}
			}
			
			// If is children
			if(isChildren){
				
				item = $(item);
				
				// Tag type
				var tagType = FormUtils.tagType(item);
				
				//
				// Input
				if(tagType == 'input'){
						
					// Get the item type
					var itemType = item.readAttribute('type');
					
					// If text and hidden
					if(itemType == 'text' || itemType == 'hidden' || itemType == 'password'){
						
						if(!item.hasClassName(classNameCorrect)) {
							elementsNotFilled.push(item);
						}
						
					// If checkbox
					}else if(itemType == 'checkbox'){
						
						var oneChecked = false;
						
						checkboxes = $(form).getInputs('checkbox');
						checkboxes.each(function(singlebox){
							
							// If is the checkbox searched
							if(singlebox.identify() == item.identify()){
								
								// If this one is checked, but is't equal to differentTo
								if(singlebox.checked && singlebox.value != differentTo){
									oneChecked = true;
								}
							}
						});
						
						// If no one is checked
						if(!oneChecked){
							elementsNotFilled.push(item);
						}
	
					// If radiobutton
					}else if(itemType == 'checkbox'){
						
						//console.log('Radiobutton filled? To implement!');
						elementsNotFilled.push(item);
						
					}
					
					
				//
				// Textarea
				}else if(tagType == 'textarea'){
	
					if(!item.hasClassName(classNameCorrect)) {
						elementsNotFilled.push(item);
					}
				}
			
			} // if(isChildren)
			
		});
		
		// All correct
		return elementsNotFilled;
	},
	
	
	
	/**
	 * Check if all the elements have the passed classNameCorrect and are filled
	 * @param	string		form					Form id
	 * @param	string		classNameObligatory		Css class to check
	 * @param	string		classNameCorrect		Css class that input must have
	 * @param	string		differentTo				Different from this value
	 * @return	bool
	 */
	allFieldFilled: function(form, classNameObligatory, classNameCorrect, differentTo){
		
		var elems = FormUtils.fieldNotFilled(form, classNameObligatory, classNameCorrect, differentTo);
		
		return (elems.size() == 0);
	},
	
	
	
	/**
	 * Return the type of the html tag. Does not support all the tags
	 * @param	string		item	Item to analyze
	 * @return	string
	 */
	tagType: function(item){
		
		var html = item.inspect();
		if( html.include('<input ') ){
			
			return 'input';
		
		}else if( html.include('<textarea ') ){
			
			return 'textarea';
		
		}else{
			
			return 'dont_know';
		}
	},
	
	
	
	/**
	 * Get the names, not repeated, of the passed html elements
	 * @param	string[] | DOMElements[]	elements	Elements to filter
	 * @return	string[]					Names of the elements
	 */
	elementNamesFiltered: function(elements){
		
		var names = Array();
		
		// Get the names
		elements.each(function(item){
			names.push( $(item).identify() );
		});
		
		// Remove duplicates
		return names.uniq();
	},
	
	
	
	/**
	 * Fire a passed event on each element passed
	 * @param	string[]|Element[]		elements	Elements on witch fire the event
	 * @param	string					event		Event to fire
	 */
	onloadFireFilled: function(elements, event){
		
		// On document loaded
		Event.observe(document, 'dom:loaded', function(){

			// On each fire
			elements.each(function(elem){
				
				elem = $(elem);
				
				// Fire just if is filled
				if(elem.value != ''){
					elem.fire(event);
				}
			});
		});
	},
	
	
	
	/**
	 * Fire an event on a group ef elements
	 * @param	string[]|Element[]		elements	Elements on witch fire the event
	 * @param	string					event		Event to fire
	 */
	fireEventOnElements: function(elements, event){
		
		// On each fire
		elements.each(function(elem){
			
			elem = $(elem);

			if(elem != null){
					
				// Fire just if is filled
				if(elem.value != ''){
					elem.fire(event);
				}
			}
		});
	},
	
	
	
	/**
	 * Observe an event on a pool of elements and when is catched
	 * will fire a new event on that elements
	 * @param	string[]|Elements[]		elements	Elements to observe
	 * @param	string					before		Event to observe
	 * @param	string					after		Event to fire
	 */
	transformEvent: function(elements, before, after){
		
		// On each element
		elements.each(function(elem){
			
			elem = $(elem);
			
			if(elem != null){
				
				// Observe
				Event.observe(elem, before, function(){
					
					elem.fire(after);
				});
			}
		});
	}
	
	
	
});

