// 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);
}


// Mousetracker

/*
var links = 0;
var tmp_mouseposition = 0;
var tmp_clientdata = 0;
var adresse = 0;
var tmp_href = 0;
var tmp_link = 0;
var tmp_target = 0;
var inputs = 0;
var texts = 0;


function mouseposition(Ereignis) {
	if (window.netscape) {
		tmp_mouseposition = Ereignis.pageX+"_"+Ereignis.pageY;
	} else {
		tmp_mouseposition = event.clientX+"_"+event.clientY;
	}
	tmp_clientdata = mousetrackerurl+'&resolution='+screen.width+'x'+screen.height+'&innerres='+document.body.offsetWidth+'x'+document.getElementById('heightcontrol').offsetHeight+'&mousepos='+tmp_mouseposition;
}

function changeURL(adresse) {
	if (tmp_clientdata) {
		document.getElementById('counter').src = tmp_clientdata;
	}
	if (adresse) {
		window.setTimeout("window.location = '"+adresse+"'", 100);
	}
}

function openNewWindow(adresse) {
	if (tmp_clientdata) {
		document.getElementById('counter').src = tmp_clientdata;
	}
	fenster = window.open(adresse, "blank");
	fenster.focus();
}

function changeURLRightMouse(element, buttoncontrol) {
	justonetime = '0';
	if (tmp_clientdata && buttoncontrol == 'change') {
		document.getElementById('counter').src = tmp_clientdata;
	}
	if (buttoncontrol == 'back') {
		justonetime = '1';
	}
//	window.setTimeout("alert('"+adresse+"');", 100);
	
	var links = document.getElementsByTagName('a');
	for (i = 0; i < links.length; i++) {
		if (links[i].href.indexOf("mailto") == -1) {
			tmp_href = document.links[i].href;
			tmplink = '';
			if (justonetime == '0') {
				if (document.links[i].href.indexOf("openNewWindow") == -1) {
					tmplink = tmp_href.split("javascript:changeURL('");
					blanklink = 0;
				} else {
					tmplink = tmp_href.split("javascript:openNewWindow('");
					blanklink = 1;
				}
				tmplink = tmplink[1].split("');");
				document.links[i].href = tmplink[0];
				if(blanklink == '1') {
					document.links[i].target='_blank';
				}
			} else {
				tmp_href = document.links[i].href;
				if (document.links[i].target != '_blank') {
					document.links[i].href = "javascript:changeURL('"+tmp_href+"');";
				} else {
					document.links[i].href = "javascript:openNewWindow('"+tmp_href+"');";
					document.links[i].target = "";
				}
			}
		}
	}
	if (!element.onmouseout) {
		element.onmouseout = function (event) {
			if (justonetime != '1') {
				changeURLRightMouse(element, 'back');
			}
		};
	}
}
*/


/* Variablen für Menü */
var hoveritem = '';
var navi_speed = 7;
var max_height = 123;

onload = function () {

/*
// Links durch JS ersetzen
	var links = document.getElementsByTagName('a');
	for (i=0; i<links.length; i++) {
	
		// mailto-Links herausfiltern
		if (links[i].href.indexOf("mailto") == -1) {
			tmp_href = document.links[i].href;
			if (document.links[i].target != '_blank') {
				document.links[i].href = "javascript:changeURL('"+tmp_href+"');";
			} else {
				document.links[i].href = "javascript:openNewWindow('"+tmp_href+"');";
				document.links[i].target = "";
			}
		}
	}
*/

// 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';
			}
		}
	}
}


/*
document.onmousemove = mouseposition;
document.onmousedown = noLeftMouseButton;
*/

function noLeftMouseButton (Ereignis) {
	if (!Ereignis) {Ereignis = window.event;}
	if (Ereignis.button) {
		if (Ereignis.button == 1) {
			changeURL('');
		} else {
			changeURLRightMouse(this, 'change');
		}
	}
}

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>");
}