var W3CDOM = (document.getElementsByTagName && document.createElement);
var cHeight = 335;
var flyouts;

var errorMessages = {};
errorMessages.nameMissing = 'this is a mandatory field!';
errorMessages.emailMissing = 'this is a mandatory field!';
errorMessages.emailInvalid = 'please doublecheck your email adress!';
errorMessages.commentMissing = 'this is a mandatory field!';

window.addEvent('domready', initFormValidation );


function initFormValidation () {
	var formular = document.forms[0];
	if (formular) 
		formular.onsubmit = function () { return validate() };
}


// Formvalidation blog comments

function validate() {
	validForm = true;
	firstError = null;
	errorstring = '';
	var x = document.forms[0].elements;
	if (!x['name'].value)
		writeError(x['name'],errorMessages.nameMissing);
	if (!x['email'].value)
		writeError(x['email'],errorMessages.emailMissing);
	if (x['email'].value.indexOf('@') == -1)
		writeError(x['email'],errorMessages.emailInvalid);
	if (!x['comment'].value)
		writeError(x['comment'],errorMessages.commentMissing);
	if (!W3CDOM)
		alert(errorstring);
	if (firstError)
		firstError.focus();
	if (validForm)
		return true;
	return false;
}

function writeError(obj,message) {
	validForm = false;
	if (obj.hasError) return;
	if (W3CDOM) {
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else {
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}
