var isNaN = function(tester)
 {
 	return (tester.toString() === 'NaN');
 }

var Form = {};

Form.validator = Class.create({
	
	form: null,
	elements: [],
	
	initialize: function()
	{
		if (arguments.length > 0) {
			this.form = arguments[0];
		}
	},
	
	setForm: function(form)
	{
		this.form = form;
	},
	
	enable: function(properties)
	{
		var i = this.elements.length;
		this.elements[i] = properties;
		if (properties.start) {
			var el = $(properties.element); // we want to be sure that the element IS a element
			el.value = properties.start;
			Event.observe(el, 'focus', function(){
				this.focused(i);
			}.bind(this));
			Event.observe(el, 'blur', function(){
				this.blured(i);
			}.bind(this));
		}
	},
	
	focused: function(i)
	{
		var property = this.elements[i];
		var el = $(property.element);
		if (el.value == property.start) {
			el.value = '';
		}
	},
	
	blured: function(i)
	{
		var property = this.elements[i];
		var el = $(property.element);
		if (el.value == '') {
			el.value = property.start;
		}
	},
	
	validate: function()
	{
		for (var i = 0; i < this.elements.length; i++) {
			var prop = this.elements[i];
			var el = $(prop.element);
			var value = el.value;
			var exact = prop.exact;
			
			if (value === '') {
				if(prop.onValueless) {
					alert(prop.onValueless);
					el.focus();
				}
				return false;
			}
			
			if (exact && value.length !== exact) {
				if(prop.onWrong) {
					alert(prop.onWrong);
					el.focus();
				}
				return false;
			}
			
			switch (prop.type) {
				case 'integer':
					if ( isNaN( parseInt(value) ) ) { // it is not numeric
						if(prop.onWrong) {
							alert(prop.onWrong);
							el.focus();
						}
						return false;
					}
					break;
				case 'decimal':
					if ( isNaN( parseFloat(value) ) ) { // it is not numeric
						if(prop.onWrong) {
							alert(prop.onWrong);
							el.focus();
						}
						return false;
					}
					break;
				case 'text':
					if (!isNaN( parseInt(value) ) && !isNaN( parseFloat(value) ) ) { // it is not text
						if(prop.onWrong) {
							alert(prop.onWrong);
							el.focus();
						}
						return false;
					}
					break;
				case 'boolean':
					if (value !== true || value !== false || value !== 'true' || value !== 'false'){
						if(prop.onWrong) {
							alert(prop.onWrong);
							el.focus();
						}
						return false;
					}
					break;
			}
		}
		
		return true;
	},
	
	validateAndSend: function()
	{
		if (this.validate()) {
			this.form.submit();
		}
	}
	
});

var validator = new Form.validator();

Event.observe(window, 'load', function(){
	
	validator.setForm($('loginForm'));
	
	validator.enable({
		type: 'integer',
		element: $('customerNumber'),
		onValueless: 'Du mangler at indtaste dit kundenummer',
		onWrong: 'Du har indtastet et forkert kundenummer'
	});
	
	validator.enable({
		type: 'integer',
		element: $('orderNumber'),
		onValueless: 'Du mangler at indtaste dit ordrenummer',
		onWrong: 'Du har indtastet et forkert ordrenummer'
	});
	
});
