/*!
 * Error Handling Function
 */

function errorHandling(inputs, errorBox, errorText){
	/* Config */
	this.divider = "<br />"; // Nach jeder Fehlerzeile wird das hinzugefügt
	this.colorLabel = "#E2001A"; // Textfarbe des Labels
	this.colorField = "#DCA2A2"; // Hintergrundfarbe des Inputfeldes

	this.input;
	this.inputs = inputs;
	this.errorBox = errorBox;
	this.errorText = errorText;
	this.output = ""; // Variable für Fehlertext
	this.state = true;

	this.checkField = function(field, type, text, label, required){
		var field = document.getElementById(field);
		var error = false; // Flag für Fehler
		if(required){
			if(field.value == ""){
				this.setError(text, label, field);
				error = true; // Flag setzen damit ein Fehler nicht zweimal auftauscht einmal für required und einmal für Plasibilität
			}
			else
				this.unsetError(label, field); // Farbliche Kennzeichnung entfernen
		}
		// Wenn für dieses Feld noch kein Fehler festgestellt wurde
		if(!error){
			switch(type){
				case "text":
					if(field.value.indexOf("<") != -1 || field.value.indexOf(">") != -1 || field.value.indexOf(";") != -1) // Prüfung ob es reiner Text ist
						this.setError(text, label, field);
					else
						this.unsetError(label, field);
					break;
				case "number":
					// Prüfung ob es eine Zahl ist
					if(isNaN(field.value))
						this.setError(text, label, field);
					else
						this.unsetError(label, field);
					break;
				case "mail":
					// Prüfung ob es eine Mail Adresse ist
					if(field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1)
						this.setError(text, label, field);
					else
						this.unsetError(label, field);
					break;
				default:
					this.state = false; // Wenn ein unbekannter Typ angegeben ist wird ein Fehler registriert
					break;
			}
		}
	}

	this.setError = function(text, label, field){
		this.state = false;
		this.output += text+this.divider;
		document.getElementById(label).style.color = this.colorLabel;
		field.style.backgroundColor = this.colorField;
	}

	this.unsetError = function(label, field){
		document.getElementById(label).style.color = "";
		field.style.backgroundColor = "";
	}

	this.main = function(){
		// Wenn letztes Zeichen ein Semikolon ist wird das weggeschnitten!
		if(this.inputs.substring(inputs.length, inputs.length-1) == ";")
			this.inputs = this.inputs.substr(0, this.inputs.length-1);

		this.input = this.inputs.split(";"); // Parameter auftrennen nach einzelnen Inputfeldern

		// Parameter durchlaufen
		for(i = 0; i < this.input.length; i++){
			this.input[i] = this.input[i].split(","); // Parameter auftrennen

			if(this.input[i][0] != undefined && this.input[i][1] != undefined && this.input[i][2] != undefined && this.input[i][3] != undefined && this.input[i][4] != undefined){
				this.checkField(this.input[i][0], this.input[i][1], this.input[i][2], this.input[i][3], this.input[i][4]);
			}
			else{
				this.output = "Programm Error: Syntax der Parameter nicht korrekt! <br />Input nr:"+ (i+1) +" <br />Attribute: <br />ID: " + this.input[i][0]+"<br />Typ: " + this.input[i][1]+"<br />Errortext: " + this.input[i][2]+"<br />Label ID: " + this.input[i][3]+"<br />Required: " + this.input[i][4];
				this.state = false;
				break;
			}
		}
		if(this.state == false){
			document.getElementById(errorText).innerHTML = this.output;
			document.getElementById(errorBox).style.display = "block";
			return false;
			
		}
		else{
			document.getElementById(errorBox).style.display = "none";
			return true;
		}
	}

	return this.main();
}
