// Formulareingabe überprüfen
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');
}

function checkEmail(val) {
	if (val) {
		var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
		var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
		var regex = "^"+usr+"\@"+domain+"$";
		var myrxp = new RegExp(regex);
		var check = (myrxp.test(val));
		if (check!=true) {
			return false;
		}
		else {
			return true;
		}
	}
}

function validateForm(form,specialfields) {
	var errors = new Array();
	var fields = form.getElementsByTagName('label');
	for (i = 0; i < fields.length; i++) {
		var span = fields[i].getElementsByTagName('span')[0];
		if (span) {
			var label = span.firstChild.data;
			label = label.trim();
			// if there is a '*' in the label - this indicates the inputfield has to be filled
			if (label.charAt(label.length - 1) == '*'){
				// get the inputfield
				var obj_input = fields[i].getElementsByTagName('input');
				if (!obj_input[0])
					obj_input = fields[i].getElementsByTagName('select');
				if (!obj_input[0])
					obj_input = fields[i].getElementsByTagName('textarea');

				// if there is an inputfield
				if (obj_input && obj_input[0]) {
					input = obj_input[0];
					error = false;
					
					// check if the inputfield has a value
					if (!input.value || input.value.trim().length==0) {
						error = true;
						errors.push(label.substring(0, label.length -1) + ' nicht eingegeben');
					}
					
					// check the inputfield for special things (email, ...)
					if (!error && specialfields[input.name]){
						specialfield = specialfields[input.name];
						for (check in specialfield){
							check_function = specialfield[check].split(',')[0];
							check_message = specialfield[check].split(',')[1];
							if (!eval(check_function)(input.value)){
								error = true;
								errors.push(label.substring(0, label.length -1) + check_message);
							}
						}
					}
		            
					// on error give the label the className 'error' otherwise delete the className 'error' (if exists)
					if (error){
						className = fields[i].className;
						if (className.length>0){
							className = className + ' ';
						}
						fields[i].className = className + 'error';
					} else {
		            	className = fields[i].className;
		                if (className.indexOf('error')>-1){
							className = className.replace(' error', '');
							className = className.replace('error', '');
							fields[i].className = className;
						}
		            }
				}
			}
		}
	}
	return errors;
}

function showFormErrors (errors) {
	error_message = '';
	for (i=0;i<errors.length;i++){
		error_message += errors[i] + '\n';
	}
	alert(error_message);
}



/* Variablen für Menü */
var hoveritem = '';
var navi_speed = 7;
var max_height = 123;

onload = function () {


// Eingabe- und Textfelder bei focus ändern
	var inputs = document.getElementsByTagName('input');
	var texts = document.getElementsByTagName('textarea');

	for (i=0; i<inputs.length; i++) {
		inputs[i].onfocus = function () {
			this.style.borderColor='#e29002';
			this.style.backgroundColor='#f2e9ac';
		}
		inputs[i].onblur = function () {
			this.style.borderColor='white';
			this.style.backgroundColor='transparent';
		}

	}

	for (i=0; i<texts.length; i++) {
		texts[i].onfocus = function () {
			this.style.borderColor='#e29002';
			this.style.backgroundColor='#f2e9ac';
		}
		texts[i].onblur = function () {
			this.style.borderColor='white';
			this.style.backgroundColor='transparent';
		}
	}
	
	if (document.getElementById('searchfield')) {
		obj = document.getElementById('searchfield');

		obj.onfocus = function () {
			this.style.borderColor='#e29002';
			this.style.backgroundColor='#f2e9ac';
			if (obj.value=='Suchbegriff eingeben...') {
				this.value='';
			}
		}
		obj.onblur = function () {
			this.style.borderColor='white';
			this.style.backgroundColor='transparent';
			if (this.value=='') {
				this.value='Suchbegriff eingeben...';
			}
		}
	}
	
// Animiertes Menü inizialisieren
	nav_interval = window.setInterval("moveMenu()", 25);
	var navi_lis = document.getElementById('navi').getElementsByTagName('li');
	document.getElementById('content').onmouseover = function () {
		hoveritem = 'none';
	}

	for (i=0; i<navi_lis.length; i++) {
		navi_lis[i].onmouseover = function() {
			hoveritem = this;
		}
		var navi_div = navi_lis[i].getElementsByTagName('div')[0];
		if (navi_div) {
			if (navi_lis[i].id == active_id) {
				navi_div.style.height = max_height+'px';
			} else {
				navi_div.style.height = '0px';
				navi_div.style.paddingTop = '0px';
				navi_div.style.display = 'block';
			}
		}
	}
}

function moveMenu() {
	var navi_lis = document.getElementById('navi').getElementsByTagName('li');
	if (hoveritem) {
		var completeElements = 0;
		for (i=0; i<navi_lis.length; i++) {
			var navi_div = navi_lis[i].getElementsByTagName('div')[0];
			if (navi_div && navi_lis[i].id != active_id) {
				navi_div.style.paddingTop = '10px';
				var old_height = parseInt(navi_div.style.height.split('px').join(''));
				if (navi_lis[i].id == hoveritem.id) {
					// Menü ausfahren
					if (old_height < max_height) {
						new_height = old_height + navi_speed;
						if (new_height > max_height) {
							new_height = max_height;
						}
						navi_div.style.height = new_height + 'px';
					} else {
						completeElements++;
					}
				} else {
					// Menü einfahren
					if (old_height > 0) {
						new_height = old_height - navi_speed;
						if (new_height < 0) {
							new_height = 0;
						}
						navi_div.style.height = new_height + 'px';
					} else {
						navi_div.style.paddingTop = '0px';
						completeElements++;
					}
				}
			}
		}
		if (completeElements > navi_lis.length) {
			hoveritems='';
		}
	}
}



// Mailadressen verstecken

function mailsafe(name, domain) {
	document.write("<a href=\"javascript:changeURL('mailto:"+name+"@"+domain+"')\">"+name+"@"+domain+"</a>");
}
