var InputValidator = new Class({

  Implements: [Options, Events],
	
  options: {
		required: false,
		tests: []
	},
	
  initialize: function(input, options){
		this.setOptions(options);
		this.input = $(input);
		this.tests = new Array(this.options.tests).flatten();
		this.errors = [];
		this.input.addEvent('blur', function(){
		  this.validate();
		}.bind(this));
		
		this.errorMark = null;
		
		// events
		this.addEvent('error', function(event){
		  this.placeErrors();
		});
		this.addEvent('success', function(){
		  this.removeErrors();
		});
		
		return this;
	},
	
	addTest: function(test, message){
		this.tests.include(test, message);
	},
	
	validate: function(){
		this.errors.empty();
		var value = this.input.value.trim();
		if (value == ''){
			if (this.options.required) this.errors.include('Toto pole je povinné.');
		} else {
			this.tests.each(function(test){
				value.test(test.regexp) ? this.errors.erase(test.message) : this.errors.include(test.message);
			}, this);
		}
		this.errors.length ? this.fireEvent('error') : this.fireEvent('success');
	},
	
	getError: function(){
		return this.errors;
	},
	
	placeErrors: function(){
	  var parent = this.input.getParent('p');
		var message = this.errors.join('<br />');
		if (this.errorMark) this.errorMark.destroy();
		this.errorMark = new Element('span',{
		  'class': 'error-mark',
			'text': message
		}).inject(parent);
		parent.addClass('error');
	},
	
	removeErrors: function(){
	  var parent = this.input.getParent('p');
		parent.removeClass('error');
		if (this.errorMark) this.errorMark.destroy();
	}
	
});

var FormOrder = {
	
	init: function(){
		
		this.form = $('form-order');
		
		this.validatedItems = [
  		new InputValidator('form-order-name',    { required: true }),
			new InputValidator('form-order-city', 	 { required: false }),
			new InputValidator('form-order-email',   { required: true, tests: { regexp: /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/, message: 'Zadaná emailová adresa je neplatná.' } }),
			new InputValidator('form-order-phone',   { required: false, tests: { regexp: /^[0-9 ]{9,12}$|^[0-9\+ ]{13,16}$/, message: 'Neplatný formát telefonního čísla.' } })
		];
		
		
		this.form.addEvent('submit', function(event){
		  event.stop();
		  if (this.isValid()){
				this.form.submit();
			} else {
				alert('Prosím, vyplňte správně všechna požadovaná pole.');
			}
		}.bind(this));
		
		// anti
		this.form.getElement('noscript').destroy();
		new Element('input', {
		  'type': 'hidden',
			'name': 'anti',
			'value': 'etoile'
		}).inject(this.form);

	},
	
	isValid: function(){
		var errors = 0;
		this.validatedItems.each(function(item){
			item.validate();
			errors += item.getError().length;
		});
		return !errors;
	}
	
 };


var Etoile = {
	init: function(){
		if ($('form-order')) FormOrder.init();		
	}
};

window.addEvent('domready', function(){
   Etoile.init();
});
