/*
 *	author: WebKitchen.cz; http://www.webkitchen.cz
 */
String.prototype.hasStar = function() {
	return (this.indexOf('*') != -1)
}
String.prototype.trim = function() {
	return this.replace(/^\s*/g, '').replace(/\s*$/g, '')
}
String.prototype.isEmpty = function() {
	return (this == '' || this.trim() == '');
}
String.prototype.isNumber = function() {
	return /^\d+$/.test(this);
}
String.prototype.checkFormat = function(format) {
	switch (format) {
		case 'dbDate' :
			return /^2[0-9]{3}-[0-9]{1,2}-[0-3][0-9]$/.test(this);
		break;
		case 'email' :
			return /^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]{2,6}/i.test(this);
		break;
		case 'number' :
			return !isNaN(this);
		break;
		case 'uri' :
			return /^[a-z-]+$/.test(this);;
		break;
		default:
			alert('Unkown format: '+format);
			return false;
		break;
	}
}
var formCheck = {
	init: function() {
		var fs = document.getElementsByTagName('form');
		for (var i = 0; i < fs.length; i++) {
			if (!/noCheck/.test(fs[i].className)) {
				new Listener(fs[i], 'submit', formCheck.checkForm).listen();
			}
		}
	},
	checkForm: function(e) {
		var els, el, labels, label, validation;
		f = e.currentTarget;
		if (f.noCheck) {
			return; // zkontrolovano jinde a nalezeny chyby
		}
		labels 	= f.getElementsByTagName('label');
		for (var i = 0; i < labels.length; i++) {
			labels[labels[i].htmlFor] = labels[i].firstChild.data;
		}
		els = f.elements;
		for (var i = 0; i < els.length; i++) {
			el = els[i];
			label = labels[el.id];
			if ((label && (label.hasStar()) || /check/.test(el.className)) && !/noCheck/.test(el.className)) {
				if (/^text|textarea$/.test(el.type)) {
					formCheck.validation = formCheck.checkTxtFiled;
				} 
				else if (el.tagName == 'SELECT') {
					formCheck.validation = formCheck.checkSelect;
				}
				if (!formCheck.validation(el)) {
						e.preventDefault();
						return;
				}
			}
		}
		return true;
	},
	checkTxtFiled: function(field, checkFormat) {	
		var fieldErr, msg, format;
		
		fieldErr = this.err[field.name] ? this.err[field.name] : this.err.defaultErr;
		
		if (typeof fieldErr == 'string') {
			msg = fieldErr;
			format = null;
		} 
		else {
			msg		= (checkFormat ? fieldErr[2] : fieldErr [0]);
			format 	= fieldErr[1];	
		}
		field.value = field.value.trim();
		if (!checkFormat) {
			if (field.value.isEmpty()) {
				return this.tellIt(field, msg);
			}
			if (format) {
				return this.checkTxtFiled(field, true);
			}
		} 
		else	{
			if (!field.value.checkFormat(format)) {
				return this.tellIt(field, msg);
			}
		}
		return true;
	},
	checkSelect: function (sel) {             
		if (sel.selectedIndex == 0) {
			return this.tellIt(sel, this.err[sel.name]);
		}
		return true;
	},
	
	tellIt: function(el, msg) {
			alert(msg);
			el.focus();
			return false;
	}
}
new Listener(window, 'load', formCheck.init).listen();

// Coaching.cz
formCheck.err = {
		defaultErr: 'Field is incorrect, please check it',
		name: 'Please enter your name and / or Company',
		email: ['Please, enter your e-mail address', 'email', 'Please enter a valid e-mail address'],
		message: 'Please enter your message',
		firstName: 'Please enter your first name',
		lastName: 'Please enter your last name',
		occupation: 'Please enter your occupation',
		address: 'Please enter your address',
		phone: 'Please enter your telephone'
}

