/**
* beispiel:
* 	<form id="contact_form">
*		<label for="contact[name]">Name</label>
*		<input type="text" name="contact[name]" class="form_checker_type_text" />
*		
*		<label for="contact[mail]">Mail</label>
*		<input type="text" name="contact[mail]" class="form_checker_type_mail" />
*		
*		<label for="contact[telefon]">Telefon</label>
*		<input type="text" name="contact[telefon]" class="form_checker_type_phone" />
*		
*		<label for="contact[anfrage]">Anfrage</label>
*		<textarea name="contact[anfrage]" class="form_checker_type_text"></textarea>
*		
*		<input type="submit" value="Abschicken" />
*	</form>
*
*	<script type="text/javascript">
*		document.observe('dom:loaded', function(){
*			var form_checker = new Form_Checker('contact_form', ['contact[name]', 'contact[mail]', 'contact[anfrage]']);
*			form_checker.check();
*		});
*	</script>
*/
var Form_Checker = Class.create({
	flag_submit: false,
	flag_init: false,
	array_form: [],
	must_fields: [], // speichert die namen der auszufüllenden elemente
	array_valid_elements: [], // speichert name der elemente
	form_id: '',
	
	color_green: '#62BD2D',
	color_red: '#D73233',
	
	
	/**
	* wird beim erstellen des objekts aufgerufen. 
	*/
	initialize: function(id, array) {
		this.form_id = id;
		this.must_fields = array;
		
		$$('#'+this.form_id+' input[type="text"]', '#'+this.form_id+' textarea').each((function(item, i){
			var type = 'optional';
			if(this.in_array(this.must_fields, item.name)){
				type = 'must';	
			}
			this.array_form[i] = [item.name, type];  // name value type (must, optional)
		}).bind(this));
		
		this.flag_init = true;
		
		Event.observe(this.form_id, 'keyup', (function(){ this.check(); }).bind(this) );
	},
	
	check: function(){
		if(this.flag_init){
			this.array_valid_elements.clear();
			this.array_form.each((function(item){
				$$('#'+this.form_id+' input[name="'+item[0]+'"]', '#'+this.form_id+' textarea[name="'+item[0]+'"]').each((function(item1){
					this.check_value(item1.value, item1.className, item[1], item[0]);
				}).bind(this));
			}).bind(this));
			
			this.update_form();
		}
	},
	
	update_form: function(){
		this.array_form.each((function(item){
			var item_element = $$('#'+this.form_id+' input[name="'+item[0]+'"]', '#'+this.form_id+' textarea[name="'+item[0]+'"]')[0];
			if(this.in_array(this.array_valid_elements, item[0])){
				item_element.setStyle({
					'borderLeft': '5px solid '+this.color_green	
				});
			}else{
				item_element.setStyle({
					'borderLeft': '5px solid '+this.color_red
				});
			}
		}).bind(this));
		
		if(this.array_form.size() == this.array_valid_elements.size()){
			$$('#'+this.form_id+' input[type="submit"]')[0].setStyle({
				'border': '1px solid '+this.color_green,
				'cursor': 'pointer'
			});
			$$('#'+this.form_id+' input[type="submit"]')[0].setOpacity('1.0');
			$(this.form_id).onsubmit = function(){return true;}
		}else{
			$$('#'+this.form_id+' input[type="submit"]')[0].setStyle({
				'border': '1px solid '+this.color_red,
				'cursor': 'auto'	
			});
			$$('#'+this.form_id+' input[type="submit"]')[0].setOpacity('0.5');
			$(this.form_id).onsubmit = function(){return false;}
		}
	},
	
	check_value: function(value, data_type, type, name){
		var valid = true;
		
		if(type == 'must' || value != ''){
			valid = false;
			switch(data_type){
				case "form_checker_type_text":
					valid = this.is_text(value);
					break;	
				case "form_checker_type_mail":
					valid = this.is_mail(value);
					break;	
				case "form_checker_type_phone":
					valid = this.is_phone(value);
					break;	
			}
			
		}
		
		if(valid){
			this.array_valid_elements.push(name);
		}
	},
	
	is_text: function(value){
		var result = true;
		var trimmed = value.replace(/^\s+|\s+$/g, '') ;
		if(trimmed == ''){
			result = false;	
		}
		
		return result;
	},
	
	is_mail: function(value){
		var a = false;
		var res = false;
	
		if(typeof(RegExp) == 'function') {
			var b = new RegExp('abc');
	 		if(b.test('abc') == true) {
	 			a = true;
	 		}
	 	}
	
		if(a == true){
			reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
								'(\\@)([a-zA-Z0-9\\-\\.]+)'+
								'(\\.)([a-zA-Z]{2,4})$');
	 		res = (reg.test(value));
		}else {
	  		res = (value.search('@') >= 1 && value.lastIndexOf('.') > value.search('@') && value.lastIndexOf('.') >= value.length-5)
	  	}
		
		return res;
	},
	
	is_phone: function(value){
		var result = false;
		if(value.match(/^[0-9,.\ ]+$/)){
			result = true;
		}
		return result;
	},
	
	
	/* ARRAY FUNKTIONEN */
	in_array: function(array, item){
		var result = false;
		array.each(function(item1){
			if(item == item1){
				result = true;	
			}
		});
		
		return result;
	},
	
	remove_from_array: function(array, item){
		var result = array;
		array.each(function(item1, i){
			if(item == item1){
				array[i] = null;	
			}
		});
		array.compact();
		
		return result;
	}
});
