/*-- swap image --*/
function swapImage(element, newimage) {
	var oldsrc = element.src
	element.src = newimage
	if (!element.onmouseout)
		element.onmouseout = function (event) { swapImage(this, oldsrc); };
}

/*-- spam protection --*/
function getAdr(prefix, postfix, text) {
	document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
}

/*-- trim --*/
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, '');
}

/*-- check e-mail --*/
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;
		}
	}
}

/*-- validate form --*/
function validateForm(form,specialfields) {
	var errors = new Array();
	if($('form_inforequest')) {
		info_selected = false;
		infofields = form.getElementsByTagName('input');
	
		for (i=0;i<infofields.length;i++) {
			if (infofields[i].type.indexOf('checkbox') > -1 && infofields[i].checked) {
				info_selected = true;
			}
		}

		if  (!info_selected){
			errors.push('Bitte w&auml;hlen Sie ein oder mehrere Themen aus!');
		}
	}
	var fields = form.getElementsByTagName('span');
	for (i = 0; i < fields.length; i++) {
		var span = fields[i].getElementsByTagName('label')[0];
		if (span && span.firstChild) {
			if(span.firstChild.tagName == 'INPUT') {
				var label = span.childNodes[1].data;
			} else {
				var label = span.firstChild.data;
			}

			label = label.trim();
			if (label.charAt(label.length - 1) == '*') {
				label = label.substring(0, label.length - 1).trim();
				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 (obj_input && obj_input[0]) {
					input = obj_input[0];
					error = false;
					if (!input.value || input.value.trim().length==0) {
						error = true;
						errors.push(label);
					}
					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 + ' ' + check_message);
							}
						}
					}
					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;
}

myObjects = new Object();
function showFormErrors (errors, form) {
	error_message = '';
	if (form) {
		//$(form);
		error_message = '<strong>Diese Felder m&uuml;ssen ausgef&uuml;llt werden:</strong><ul>';
		for (i=0;i<errors.length;i++) {
			error_message += '<li>' + errors[i] + '</li>';
		}
		error_message += '</ul>';
		if (!myObjects.errorDiv) {
			myObjects.errorDiv = new Element('div', {'id':'formErrors'});
			myObjects.errorDiv.injectTop(form);
		}
		myObjects.errorDiv.innerHTML = error_message;
		setTimeout("location.href='#formErrors'", 100);
	}
	else {
		for (i=0;i<errors.length;i++) {
			error_message += errors[i] + '\n';
		}
	alert(error_message);
	}
}

/*-- popup --*/
function popup(url, typ, para1, width, height) {
	attrib = "";
	Y = (screen.height - width) / 2;
	X = (screen.width - height) / 2;
	X = Math.round(X);
	Y = Math.round(Y);
	if (para1 == 'CENTER') attrib += 'height=' + height + ',width=' + width + ',top=' + Y + ',left=' + X;
	if (typ == 'TYP1') attrib += ",scrollbars=no";
	if (typ == 'TYP2') attrib += ",scrollbars=yes";
	if (typ == 'TYP3') attrib += ",scrollbars=yes,menubar=yes";
	fenster = window.open(url, 'win', attrib);
	return false;
}

/*-- navigation --*/
window.addEvent('domready', function() {
	if ($('nav')) {
		var nav = $('nav');
		var imgs = nav.getElementsByTagName('img');
		for(i=0; i<imgs.length; i++) {
			imgs[i].removeAttribute('onmouseover');
			if(imgs[i].src.indexOf('_n.') > -1) {
				imgs[i].onmouseover = function(e) {
					this.src = this.src.split('_n').join('_h');
				}
				imgs[i].onmouseout = function(e) {
					this.src = this.src.split('_h').join('_n');
				}
			}
		}
	}
});

/*-- title fix --*/
window.addEvent('domready', function() {
	var tags = new Array('a', 'img', 'h1', 'h2');
	for(t=0; t<tags.length; t++) {
		var eles = document.getElementsByTagName(tags[t]);
		for(var i = 0; i < eles.length; i++) {
			if(eles[i].className.indexOf('tip') == -1) {
				eles[i].removeAttribute('title');
				eles[i].removeAttribute('alt');
			}
		}
	}
});

/*-- tooltip --*/
window.addEvent('domready', function() {
	new Tips($$('.tip'), {
		initialize:function(){
			this.toolTip.effects({wait:false}).set({'opacity':[0]});
		},
		onShow: function(toolTip) {
			this.toolTip.effects({}).set({'opacity':[1]});
		},
		onHide: function(toolTip) {
			this.toolTip.effects({}).set({'opacity':[0]});
		}
	});
});

/*-- ul li background fix --*/
window.addEvent('domready', function() {
	var uls = $$('#content ul');
	uls.each(function(item) {
		if(item.parentNode.tagName == 'LI') {
			splithtml = item.parentNode.innerHTML.toLowerCase().split('<ul');
			if(splithtml[0].split(' ').join('').length <= 1) {
				item.parentNode.style.background='none';
			}
		}
	});
});

/*-- search --*/
oldonload = window.onload;
window.onload = function() {
	if ($('fld_search')) {
	var search_string = 'Seite durchsuchen …'
		var searchinput = $('fld_search');
		searchinput.onfocus = function(e) { 
			if (searchinput.value==search_string) {
				searchinput.value = '';
			}
		}
		searchinput.onblur = function(e) { 
			if (searchinput.value=='') {
				searchinput.value = search_string;
			}
		}
		if(oldonload)
			oldonload();
	}
}

/*-- mootools popup --*/
function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "hidden"; }
	if(!window.ie)
		document.getElementById('content').style.overflow='hidden';
}

function showSelectBoxes() {
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) { selects[i].style.visibility = "visible"; }
	if(!window.ie)
		document.getElementById('content').style.overflow='auto';
}

function showPopup(id, width, height, popupinnerHTML) {
	if(popupinnerHTML == undefined)
		popupinnerHTML = "";

	hideSelectBoxes();
	if(document.getElementById('popup_background')) {
		objTemp = document.getElementById('popup_background');
	}
	else {
		objTemp = document.createElement("div");
		objTemp.id = 'popup_background';
		objTemp.style.visibility='hidden';
	}

	objTemp.onclick=function() { }

	if(objTemp.style.display != 'block') {
		objTemp.style.display='block';
		popupBackground = document.body.appendChild(objTemp);
		$(popupBackground).effects({duration:400,onComplete:function(){ $(popupBackground).effects({}).set({'opacity':[0.5]}); showPopupContent(id, width, height, popupinnerHTML); }}).start({'opacity':[0,0.5]});
	}
	else {
		$(popupBackground).effects({duration:0,onComplete:function(){ $(popupBackground).effects({}).set({'opacity':[0.5]}); showPopupContent(id, width, height, popupinnerHTML); }}).start({'opacity':[0.5,0.5]});
	}
}

function showPopupContent(id, width, height, popupinnerHTML) {
	if(document.getElementById('popup_content')) {
		objTemp = document.getElementById('popup_content');
	}
	else {
		objTemp = document.createElement("div");
		objTemp.id = 'popup_content';
	}
	objTemp.innerHTML = '';
	$('popup_background').onclick=function() { popupClose(); }

	if(width > 0 && height > 0) {
		objTemp.style.width=width+'px';
		objTemp.style.height=height+'px';
		objTemp.style.marginLeft='-' + Math.round(width/2)-4 +'px';
		objTemp.style.marginTop='-' + Math.round(height/2) +'px';
	}

	objTemp.style.display='block';
	newinnerhtml = '<div id="popup_close"><a href="javascript:popupClose()"><span>X</span></a></div><div id="popup_text">' + document.getElementById(id).innerHTML + popupinnerHTML + '</div>';
	objTemp.innerHTML = newinnerhtml.split('\n').join('');
	popupContent =  document.body.appendChild(objTemp);
}

function popupClose() {
	popupContent = document.getElementById('popup_content');
	popupContent.style.display='none';
	popupBackground = document.getElementById('popup_background');
	$(popupBackground).effects({duration:400,onComplete:function(){ popupBackground.style.display='none'; showSelectBoxes(); }}).start({'opacity':[0.5,0]});
}