// JavaScript Document

var vornameOK = false;		// Variablen deren Inhalt aufschluss gibt ob die Formularfelder richtig ausgefüllt wurden (Namen Selbstredend)
var nameOK = false;
var uidOK = false;
var strasseOK = false;
var hnrOK = false;
var ortOK = false;
var plzOK = false;
var passwordSet = false;
var passwordSet2 = false;
var passwordValid = false;
var emailOK = false;
var telefonVorwahlOK = true;
var telefonOK = true;
var telefaxVorwahlOK = true;
var telefaxOK = true;
var okColor='#32CD32';		// Farbe die Das Formularfeld Bekommen soll wenn es richtig ausgefüllt wurde
var falseColor='#FF0000';	// Farbe die Das Formularfeld Bekommen soll wenn es falsch ausgefüllt wurde
var emptyColor='#CCCCCC';	// Farbe die Das Formularfeld Bekommen soll wenn es nicht ausgefüllt wurde


// Funktion überprüft ob im Vornamen Zahlen vorkommen oder ob er leer ist.

function checkVorname(vorname){
vornameOK = true;
if (vorname != ""){
	var erg = vorname.match(/\d/);
	if (erg != null) {
		vornameOK = false;
		document.getElementById('vorname').style.backgroundColor=falseColor;
		}
	}
else {
	document.getElementById('vorname').style.backgroundColor=emptyColor;
	vornameOK = false;
	}
if (vornameOK) {
	document.getElementById('vorname').style.backgroundColor=okColor;
	}
sendOK();
}

// Funktion überprüft ob im Nachnamen Zahlen vorkommen oder ob er leer ist.

function checkNachname(nachname){
nameOK = true;
if(nachname != "") {
	var erg = nachname.match(/\d/);
	if (erg != null) {
		nameOK = false;
		document.getElementById('name').style.backgroundColor=falseColor;
		}	
	}
else {
	nameOK = false;
	document.getElementById('name').style.backgroundColor=emptyColor;
	}
if (nameOK) {
	document.getElementById('name').style.backgroundColor=okColor;
	}
sendOK();
}

function checkUID(uid){
uidOK = true;
if(uid != "") {
	var erg = uid.match(/\d/);
	if (erg != null) {
		uidOK = false;
		document.getElementById('uid').style.backgroundColor=falseColor;
		}	
	}
else {
	uidOK = false;
	document.getElementById('uid').style.backgroundColor=emptyColor;
	}
if (uidOK) {
	document.getElementById('uid').style.backgroundColor=okColor;
	}
sendOK();
}

// Funktion überprüft ob in der Straße Zahlen vorkommen oder ob sie leer ist.

function checkStrasse(strasse){
strasseOK = true;
if(strasse != "") {
	var erg = strasse.match(/\d/);
	if (erg != null) {
		strasseOK = false;
		document.getElementById('strasse').style.backgroundColor=falseColor;
		}	
	}
else {
	strasseOK = false;
	document.getElementById('strasse').style.backgroundColor=emptyColor;
	}
if (strasseOK) {
	document.getElementById('strasse').style.backgroundColor=okColor;
	}
sendOK();
}

// Funktion überprüft ob in der Haußnummer mindestens eine Zahl vorkommt oder ob sie leer ist.			

function checkHnr(hnr){
hnrOK = true;
if (hnr == "") {
	hnrOK = false;
	document.getElementById('hnr').style.backgroundColor=emptyColor;
	}
else {
	var erg = hnr.match(/\d/);
	if (erg == null) {
		document.getElementById('hnr').style.backgroundColor=falseColor;
		hnrOK = false;
		}
	else {
		document.getElementById('hnr').style.backgroundColor=okColor;
		}
	}
sendOK();
}

// Funktion überprüft ob im Ort Zahlen vorkommen oder ob er leer ist.

function checkOrt(ort){
ortOK = true;
if(ort != "") {
	var erg = ort.match(/\d/);
	if (erg != null) {
		ortOK = false;
		document.getElementById('ort').style.backgroundColor=falseColor;
		}	
	}
else {
	ortOK = false;
	document.getElementById('ort').style.backgroundColor=emptyColor;
	}
if (ortOK) {
	document.getElementById('ort').style.backgroundColor=okColor;
	}
sendOK();
}

// Funktion überprüft ob in der Postleitzahl 5 Zahlen vorkommen oder ob sie leer ist.

function checkPlz(plz) {
plzOK = true;
if (plz != "") {
	var erg = plz.match(/\d\d\d\d\d/)
	if (erg == null) {
		plzOK = false;
		document.getElementById('plz').style.backgroundColor=falseColor;
		}
	}
else {
	plzOK = false;
	document.getElementById('plz').style.backgroundColor=emptyColor;
	}
if (plzOK) {
	document.getElementById('plz').style.backgroundColor=okColor;
	}
sendOK();
}
			

// Funktion überprüft ob das Passwort leer ist und ob es länger ist als 5 Zeichen.

function checkPassword(password) {
passwordSet = true;
if (password == ""){
	passwordSet = false;
	document.getElementById('passwort').style.backgroundColor=emptyColor;
	}
else {
	if (password.length < 5) {	
		passwordSet = false;
		document.getElementById('passwort').style.backgroundColor=falseColor;
		}
	else {
		document.getElementById('passwort').style.backgroundColor=okColor;
		}
	}
		checkPassword2(document.getElementById('passwort2').value);
		validatePassword(document.getElementById('passwort').value, document.getElementById('passwort2').value);
sendOK();
}

function checkPassword2(password) {
passwordSet2 = true;
if (password == ""){
	passwordSet2 = false;
	document.getElementById('passwort2').style.backgroundColor=emptyColor;
	}
else {
	if (password.length < 5 ) {	
		passwordSet2 = false;
		document.getElementById('passwort2').style.backgroundColor=falseColor;
		}
	else {
		document.getElementById('passwort2').style.backgroundColor=okColor;
		}
	}
validatePassword(document.getElementById('passwort').value, document.getElementById('passwort2').value);
sendOK();
}
	
// Funktion überprüft ob das Password mir der Passwortbestätigung übereinstimmt

function validatePassword(password, passwordconfirm) {
passwordValid = true;
if (password == "" && passwordconfirm == "") {
	passwordValid = false;
	}
else {
	if (password != passwordconfirm) {
		passwordValid = false;
		document.getElementById('passwort2').style.backgroundColor=falseColor;
		}
	if (passwordSet == true && passwordSet2 == true && password == passwordconfirm) {
		passwordValid = true;
		document.getElementById('passwort').style.backgroundColor=okColor;
		document.getElementById('passwort2').style.backgroundColor=okColor;	
		}
	}
sendOK();
}	

// Funktion die die Emailadresse auf formale Korrektheit überprüft

function checkEmail(email) {
emailOK = true;
if (email != "") {
	var erg = email.match(/.+@.+\....?.?\b/);
	email.indexOf(' ')
	if (erg == null || email.indexOf(' ') != -1) {
		emailOK = false;
		document.getElementById('email').style.backgroundColor=falseColor;
		}
	}
else {
	emailOK = false;
	document.getElementById('email').style.backgroundColor=emptyColor;
	}
if (emailOK) {
	document.getElementById('email').style.backgroundColor=okColor;
	}
sendOK();
}


function checkTelefonVorwahl(telefon_vorwahl){
telefonVorwahlOK = true;
if (telefon_vorwahl == "") {
	document.getElementById('telefon_vorwahl').style.backgroundColor=emptyColor;
	}
else {
	var erg = telefon_vorwahl.match(/0\d+/);
	if (erg != null) {
		if (telefon_vorwahl.match(/\D/)) {
			document.getElementById('telefon_vorwahl').style.backgroundColor=falseColor;
			telefonVorwahlOK = false;
		}
		else {
			document.getElementById('telefon_vorwahl').style.backgroundColor=okColor;
		}
	}
	else {
		document.getElementById('telefon_vorwahl').style.backgroundColor=falseColor;
		telefonVorwahlOK = false;
		}
	}
sendOK();
}


function checkTelefaxVorwahl(telefax_vorwahl){
telefaxVorwahlOK = true;
if (telefax_vorwahl == "") {
	document.getElementById('telefax_vorwahl').style.backgroundColor=emptyColor;
	}
else {
	var erg = telefax_vorwahl.match(/0\d+/);
	if (erg != null) {
		if (telefax_vorwahl.match(/\D/)) {
			document.getElementById('telefax_vorwahl').style.backgroundColor=falseColor;
			telefaxVorwahlOK = false;
		}
		else {
			document.getElementById('telefax_vorwahl').style.backgroundColor=okColor;
		}
	}
	else {
		document.getElementById('telefax_vorwahl').style.backgroundColor=falseColor;
		telefaxVorwahlOK = false;
		}
	}
sendOK();
}


function checkTelefon(telefon){
telefonOK = true;
if (telefon == "") {
	document.getElementById('telefon').style.backgroundColor=emptyColor;
	}
else {
	var erg = telefon.match(/\d/);
	if (erg != null) {
		if (telefon.match(/\D/)) {
			document.getElementById('telefon').style.backgroundColor=falseColor;
			telefonOK = false;
		}
		else {
		document.getElementById('telefon').style.backgroundColor=okColor;
		}
	}
	else {
		document.getElementById('telefon').style.backgroundColor=falseColor;
		telefonOK = false;
		}
	}
sendOK();
}


function checkTelefax(telefax){
telefaxOK = true;
if (telefax == "") {
	document.getElementById('telefax').style.backgroundColor=emptyColor;
	}
else {
	var erg = telefax.match(/\d/);
	if (erg != null) {
		if (telefax.match(/\D/)) {
			document.getElementById('telefax').style.backgroundColor=falseColor;
			telefaxOK = false;
		}
		else {
		document.getElementById('telefax').style.backgroundColor=okColor;
		}
	}
	else {
		document.getElementById('telefax').style.backgroundColor=falseColor;
		telefaxOK = false;
		}
	}
sendOK();
}


// Funktion die überprüft ob alle Plichtfelder richtig ausgefüllt sind und den Submit-Button steuert
function sendOK() {
if (!(vornameOK && nameOK && uidOK && strasseOK && hnrOK && ortOK && plzOK && passwordValid && emailOK && passwordSet && passwordSet2 && telefonOK && telefaxOK && telefonVorwahlOK && telefaxVorwahlOK)) {
	document.getElementById('submit').disabled=true;
	}
else {
	document.getElementById('submit').disabled=false;
	}
}
