/* Konstruktor für neue Formular-Validation */
var ax_forms = new Array();
function ax_form(form)
{
	/*##### Parameter #####*/
	/* Verbindung */
	this.valid = null;
	this.name = form['name'];
	eval('this.error = '+((form['error'] == '' || form['error'] == 'standard') ? 'function(){ ax_forms[\''+this.name+'\'].errorHandler(); }' : form['error']))
	this.inputs = form['inputs'];
	
	this.invalidInputs = new Array();
	this.invalidInputs['mandatory'] = new Array();
	this.invalidInputs['email'] = new Array();
	this.invalidInputs['password'] = new Array();
	this.invalidInputs['spammath'] = new Array();

	this.validate = function()
	{
		this.valid = null;
		var this_input = '';

		/* Input-Felder validieren */
		for(var i = 0; i<this.inputs.length; i++)
		{
			this_input = this.inputs[i];

			if(document.getElementById(this_input['id']) && typeof(this_input['mandatory']) != 'undefined' && this_input['mandatory'] == true) 
			{
				var missing = false;
				switch(this_input.type)
				{
					case 'checkbox': missing = (document.getElementById(this_input['id']).checked == false) ? true : false;
								 	 break;

					case 'textarea': missing = (document.getElementById(this_input['id']).innerText == '') ? true : false;
								 	 break;
						
					case 'file':
					case 'hidden':
					case 'password':
					case 'select':			 
					case 'textfield':
					case 'text': missing = (document.getElementById(this_input['id']).value == '') ? true : false;
								 break;
				}
						
				if(missing)
				{
					this.valid = false;
					/* leeres Pflichtfeld */
					this.invalidInputs['mandatory'].push(this_input);
				}
			}
			
			if(typeof(this_input['validate']) != 'undefined')
			{
				if(this_input['validate'] == 'email')
				{
					/* Email-Adresse validieren */
					if(document.getElementById(this_input['id']))
					{
						var emailvalid = true;
						var string = document.getElementById(this_input['id']).value;
						var at = "@"
						var dot = "."
						var lat = string.indexOf(at)
						var lstr = string.length
						var ldot = string.indexOf(dot)
				
						if (string.indexOf(at) == -1) emailvalid = false;
						else if (string.indexOf(at) == -1 || string.indexOf(at) == 0 || string.indexOf(at) == lstr) emailvalid = false;
						else if (string.indexOf(dot) == -1 || string.indexOf(dot) == 0 || string.indexOf(dot) == lstr) emailvalid = false;
						else if (string.indexOf(at,(lat+1))!=-1) emailvalid = false;
						else if (string.substring(lat-1,lat)==dot || string.substring(lat+1,lat+2) == dot) emailvalid = false;
						else if (string.indexOf(dot,(lat+2)) == -1) emailvalid = false;
						else if (string.indexOf(" ") != -1) emailvalid = false;
						else emailvalid = true;
						
						if(!emailvalid) 
						{
							this.valid = false;
							/* ungültige Email-Adresse */
							this.invalidInputs['email'].push(this_input);
						}
					}

				} else if(this_input['validate'] == 'password'){
					
					/* Passwort überprüfen */
					if(document.getElementById(this_input['id']))
					{
						var passwordsvalid = true; this_input['invalidMinlength'] = false; this_input['invalidConfirmation'] = false;
						var pass1 = document.getElementById(this_input['id']).value;
						if(typeof(this_input['compinput']) != 'undefined' && this_input['compinput'] != '') var pass2 = (document.getElementById(this_input['compinput'])) ? document.getElementById(this_input['compinput']).value : null;					
						else var pass2 = null;
						
						/* Passwort auf Mindestlänge überprüfen) */
						if(typeof(this_input['minlength']) != 'undefined' && pass1.length < this_input['minlength']){ passwordsvalid = false; this_input['invalidMinlength'] = true; }
						
						/* Passwort mit Passwort-Wiederholung vergleichen */
						if(pass2 != null && pass1 != pass2){ passwordsvalid = false; this_input['invalidConfirmation'] = true; }

						if(!passwordsvalid)
						{
							this.valid = false;
							/* ungültiges Passwort */
							this.invalidInputs['password'].push(this_input);
						}
					}
				} else if(this_input['validate'] == 'spammath'){
					
					/* Passwort überprüfen */
					if(document.getElementById(this_input['id']))
					{
						var mathvalid = true;
						var math1 = document.getElementById(this_input['id']).value;
						var math2 = (document.getElementById(this_input['id']+'_math').value) ? document.getElementById(this_input['id']+'_math').value : null;					

						if(math2)
						{
							math2 = math2.replace(/op1/, '+');
							math2 = math2.replace(/op2/, '-');
							
							/* Rechenaufgabe auf Ergebnis überprüfen */
							if(parseInt(math1) != parseInt(eval(math2)))
							{
								mathvalid = false;
							}
						}

						if(!mathvalid)
						{
							this.valid = false;
							/* ungültiges Passwort */
							this.invalidInputs['spammath'].push(this_input);
						}
					}
				}
			}
		}

		if(this.valid == false) 
		{
			if(window.event) window.event.returnValue = false; /* Form-Submit im IE verhindern */
			
			/* Error-Handler aufrufen */
			this.error();
		} else this.valid = true;
		return (this.valid == false) ? false : true;
	}
	
	this.errorHandler = function()
	{
		var errormsg = ''; var missingMandatoryfields = new Array();
		if(this.invalidInputs['mandatory'].length > 0)
		{
			errormsg += "\r\nBitte füllen Sie alle Pflichtfelder aus:\r\n\r\n";
			for(var i=0; i<this.invalidInputs['mandatory'].length; i++) 
			{
				missingMandatoryfields.push(this.invalidInputs['mandatory'][i]['id']);
				errormsg += this.invalidInputs['mandatory'][i]['label']+"\r\n";
			}
		}

		if(this.invalidInputs['email'].length > 0)
		{
			var msg = '';
			for(var i=0; i<this.invalidInputs['email'].length; i++) 
			{
				if(!in_array(this.invalidInputs['email'][i]['id'], missingMandatoryfields)) msg += this.invalidInputs['email'][i]['label']+"\r\n";
			}

			if(msg != '') errormsg += "\r\nBitte geben Sie eine gültige Email-Adresse ein:\r\n\r\n"+msg;
		}
		
		if(this.invalidInputs['password'].length > 0)
		{
			for(var i=0; i<this.invalidInputs['password'].length; i++) 
			{	
				if(!in_array(this.invalidInputs['password'][i]['id'], missingMandatoryfields))
				{				
					if(this.invalidInputs['password'][i]['invalidMinlength']) errormsg += "\r\nDas Passwort mus mindestens "+this.invalidInputs['password'][i]['minlength']+" Zeichen lang sein:\r\n\r\n";
					else if(this.invalidInputs['password'][i]['invalidConfirmation'])  errormsg += "\r\nDas Passwort entspricht nicht der Wiederholung:\r\n\r\n";
					errormsg += this.invalidInputs['password'][i]['label']+"\r\n";
				}
			}
		}

		if(this.invalidInputs['spammath'].length > 0)
		{
			var msg = '';
			for(var i=0; i<this.invalidInputs['spammath'].length; i++) 
			{
				if(!in_array(this.invalidInputs['spammath'][i]['id'], missingMandatoryfields)) msg += this.invalidInputs['spammath'][i]['label']+"\r\n";
			}

			if(msg != '') errormsg += "\r\nDas Ergebnis der Rechenaufgabe ist falsch:\r\n\r\n"+msg;
		}
		
		this.reset(); 
		alert(errormsg);
		return false;
	}
	
	this.reset = function()
	{
		/* Inputs auf Voreinstellung zurücksetzen */
		for(var i = 0; i<this.inputs.length; i++)
		{
			this_input = this.inputs[i];
			var invalid_input = false;
			if(this_input['type'] == 'spammath')
			{
				for(var a = 0; a<this.invalidInputs['spammath'].length; a++)
				if(this_input == this.invalidInputs['spammath'][a]) invalid_input = true;

				if(invalid_input && document.getElementById(this_input['id']))
				{
					var math = (document.getElementById(this_input['id']+'_math').value) ? document.getElementById(this_input['id']+'_math').value : null;					
				
					if(math)
					{
						math = math.replace(/op1/, 'p l u s');
						math = math.replace(/op2/, 'm i n u s');
						document.getElementById(this_input['id']).value = math;
					}
				}				
			}	
		}

		this.invalidInputs = new Array();
		this.invalidInputs['mandatory'] = new Array();
		this.invalidInputs['email'] = new Array();
		this.invalidInputs['password'] = new Array();		
		this.invalidInputs['spammath'] = new Array();		
	}
}

function in_array(needle, haystack)
{
	var hay = haystack.toString();
	if(hay == ''){
		return false;
	}
	var pattern = new RegExp(needle, 'g');
	var matched = pattern.test(haystack);
	return matched;
}
