// Globale Variable fuer Fehler-Flag
var errfound = false;


// Prueft Felder auf Laenge
function ValidLength(item, len) {
	return (item.length >= len);
}

// Prueft E-Mail-Adresse
// auf Laenge von min. 5 Zeichen, und 
// auf Vorkommen von Klammeraffen und Punkt
function ValidEmail(item) {
	if (!ValidLength(item, 6)) return false;
	if (item.indexOf ('@', 0) == -1) return false;
	if (item.indexOf ('.', 0) == -1) return false;
	return true;
} 


// Checkt, ob eines der drei Motive ausgewählt ist
function ValidMotiv(item){
	for (var ic = 0; ic < 6; ic++) {
		if (document.formular[ic].checked) {
		return true;
		}
	}
	return false;
}


// Zeigt eine Fehlermeldung an
// Funktion beendet, falls bereits ein Fehler gefunden wurde
function error(elem, text) {
	if (errfound) return;
	window.alert(text);
	errfound = true;
}


// Hauptpruefroutine
function Validate() {
	errfound = false;
	if (!ValidMotiv(document.formular.card_number.checked))
		error(document.formular.motive, "Please choose a postcard!\nUse the checkbox below the picture.");
	if (!ValidLength(document.formular.to_name.value,2))
		error(document.formular.to_name, "Please enter the recipient's name!");
	if (!ValidEmail(document.formular.to_mail.value))
		error(document.formular.to_mail, "Please enter a valid e-mail address for the recipient!");
	if (!ValidLength(document.formular.from_name.value,2))
		error(document.formular.from_name, "Pleas enter the sender's name (your name)!");
	if (!ValidEmail(document.formular.from_mail.value))
		error(document.formular.from_mail, "Please enter a valid e-mail address of the sender (your e-mail adress)!");
	return !errfound; /* true falls keine Fehler*/
}

