// form_validation v. 2.02c
//  ----------------------------
// | Euroweb Internet GmbH     |
//  ----------------------------
//	Last changed:	19. 08. 2008

function resetForm(){
	$('fehlermeldung').style.display = 'none';
	for (var i = 0; i < $$("label").length; i++) {
		currentLabel = $$("label")[i];
		if(currentLabel.className.match(/ error/, ''))
		{	currentLabel.className = currentLabel.className.replace(/ error/gi, '');
		}
		unmarkField(currentLabel);		
	}
}

function markField(theLabel){
	if(theLabel.htmlFor){
		$(theLabel.htmlFor).className+=' errorfield';
	}
}
function unmarkField(theLabel){
	if(theLabel.htmlFor){
		$(theLabel.htmlFor).className=$(theLabel.htmlFor).className.replace(/errorfield/gi,'');
	}
}

function validateForm(formular) {
	var error = 0;
	var currentLabel = '';
	var currentField = '';
	var klasse = '';
	var newClass = '';
	
	// Inspect all of the document's labels ...
	for (var i = 0; i < $$("label").length; i++) {
		currentLabel = $$("label")[i];
		if (currentLabel.htmlFor) {
			currentField = $(currentLabel.htmlFor);
		}
		klasse = currentLabel.className;
		newClass = currentLabel.className.replace(/ error/, '');
		
		// Check if the current label belongs to the form we want to validate
		if (currentLabel.form == formular && currentField) {

			// ...  and if it is required at all, or has to be a number, or an e-mail address.
				
				// required field (but neither numeric nor an e-mail)
				if (klasse.match(/required/)) {
					if (currentField.value == '') {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						markField(currentLabel);
						error = 1;
					} else {
						currentLabel.className = newClass;
						unmarkField(currentLabel);
					}
				}
				
				// numeric field
				if (klasse.match(/number/)) {
					var numeric = isNumber(currentField);
					if (!numeric && !klasse.match(/required/) && currentField.value != '') {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						markField(currentLabel);
						error = 1;
					} else {
						currentLabel.className = newClass;
						unmarkField(currentLabel);
					}
					if (!numeric && klasse.match(/required/)) {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						markField(currentLabel);
						error = 1;
					} else {
						if (error == 0) {
							currentLabel.className = newClass;
							unmarkField(currentLabel);
						}
					}
				}
				
				// e-mail address
				if (klasse.match(/mail/)) {
					var valid = isMailValid(currentField);
					if (!valid && currentField.value != '') {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						markField(currentLabel);
						error = 1;
					} else {
						currentLabel.className = newClass;
						unmarkField(currentLabel);
					}
					if (!valid && klasse.match(/required/)) {
						currentLabel.className = newClass;
						currentLabel.className += ' error';
						markField(currentLabel);
						error = 1;
					} else {
						if (error == 0) {
							currentLabel.className = newClass;
							unmarkField(currentLabel);
						}
					}
				}
		}
		
	}
	if (error === 0) {
		return true;
	} else {
		$("fehlermeldung").style.display = 'block';
		$$(".meldung").each(function(el){el.style.display = 'block';});
		return false;
	}
		
}

// Additional functions for numeric and e-mail validation
function isNumber(field) {
	var returnvar = (isNaN(parseInt(field.value)) == true) ? false : true;
	return returnvar;
}

function isMailValid(field) {
	var returnvar = (field.value.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/)) ? true : false;
	return returnvar;
}
