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

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

// deletes leading and trailing spaces in a string - adds the function directly to the String Object, so that all strings inherit this method
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, '');
}

//checkEmail - needed for forms
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;
		}
	}
}

/*
validates formfields if they have a value or not
to check for other options do the following
specialfields = new Object();
specialfields.fieldname = new Object();
specialfields.fieldname.check1 = 'function_to_call,error_message';
specialfields.fieldname.check2 = 'second_function_to_call,second_error_message';
specialfields.another_fieldname = new Object();
specialfields.another_fieldname.check1 = 'function_to_call,error_message';
*/
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 && span.firstChild) {
			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) == '*') {
				label = label.substring(0, label.length - 1).trim();
				// 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 + ' 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 + ' ' + 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);
}


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;
}

function activateNav(active_url, active_id) {
	active_image = document.getElementById("nav_"+active_id).getElementsByTagName('IMG')[0];
	active_image.src = active_url;
}




// FAQ-Scripts

function displayfaq(id) {
	var divisions = document.getElementById('faqnav').getElementsByTagName('div');
	for (i=0;i<divisions.length;i++) {
		if (divisions[i].className.indexOf('faq_answer') > -1) {
			divisions[i].style.display = 'none';
			divisions[i].parentNode.getElementsByTagName('H2')[0].className = "";
		}
	}
	if (id) {
		element = document.getElementById('answer_'+id);
		element.style.display = 'block';
		element.parentNode.getElementsByTagName('H2')[0].className = "activefaq";
	}
}

function init_faqs() {

	if (document.getElementById('faqnav')) {

		/* FAQs ausblenden und onclick hinzufügen */
		displayfaq();

		var faq_headlines = document.getElementById('faqnav').getElementsByTagName('H2');
		for (i=0; i<faq_headlines.length; i++) {
			if (faq_headlines[i].parentNode.parentNode.id == 'faqnav') {

				var headline_string = faq_headlines[i].innerHTML;
				headline_anchor = document.createElement('A');
				headline_anchor.innerHTML = headline_string;
				headline_anchor.href = '#';
				faq_headlines[i].innerHTML = "";
				faq_headlines[i].appendChild(headline_anchor);

				faq_headlines[i].getElementsByTagName('A')[0].onclick = function() {
					displayfaq(this.parentNode.id.split("navquestion_")[1]);
				};

			}
		}
	}
}






/* Startanimation */

var fade_interval;
var box_opacity = 0;

var open_interval;
var animation_box;
var box_width = 331;
var max_width = 988;
var open_speed = 10;

var new_location;
var sujet_loader = new Image();

function start_openanimation (new_href, my_number) {
	animation_box = document.getElementById('start_animation');

	sujet_loader.src = sujets[my_number];
	setOpacity(animation_box, 0);
	new_location = new_href;

	animation_box.style.display = 'block';
	document.getElementById('startnav').style.display = 'none';
	document.getElementById('startnav_bottom').style.display = 'none';
	fade_interval = window.setInterval("fadein_div(animation_box)", 50);
}

function fadein_div (element) {
	box_opacity += 10;
	setOpacity(element, box_opacity);
	if (box_opacity > 99) {
		window.clearInterval(fade_interval);
		document.getElementById('start_animation_sujet').style.background = 'url('+sujet_loader.src+')';
		open_interval = window.setInterval("open_animation()", 50);
	}
}

function open_animation () {

	box_width += open_speed;

	if (box_width >= (max_width - open_speed)) {
		box_width = max_width;
		animation_box.style.width = box_width + "px";
		animation_box.style.marginLeft = "-" + box_width/2 + "px";
		window.clearInterval(open_interval);
		window.location.href = new_location;
	} else {
		open_speed += open_speed/8;
	}

	animation_box.style.width = box_width + "px";
	animation_box.style.marginLeft = "-" + box_width/2 + "px";
}


var greenlight;
var greenlight_interval;
var greenlight_height = 2;
var greenlight_max_height = 168;

var logo;
var logo_interval;
var logo_opacity = 0;

var startnav;
var startnav_bottom;

function start_animation () {
	greenlight = document.getElementById('greenlight');
	logo = document.getElementById('logo');
	startnav = document.getElementById('startnav');
	startnav_bottom = document.getElementById('startnav_bottom');

	var hide_elements = new Array(greenlight, startnav, startnav_bottom, logo);
	for (i=0; i<hide_elements.length; i++) {
		hide_elements[i].style.display = 'none';
	}
	greenlight.style.height = '0px';
	greenlight.style.marginTop = '0px';
	greenlight.style.display = 'block';
	greenlight_interval = setInterval("start_greenlight_height ()", 50);

	setOpacity(logo, 0);
	logo.style.display = 'block';
}

function start_greenlight_height () {
	greenlight_height += 5;
	if (greenlight_height >= greenlight_max_height) {
		greenlight_height = greenlight_max_height;
		clearInterval ("greenlight_interval");
		start_show_logo();
	}
	greenlight.style.height = greenlight_height + 'px';
	greenlight.style.marginTop = '-' + greenlight_height/2 + 'px';
}

function start_show_logo () {
	logo_opacity += 10;
	if (logo_opacity >= 100) {
		logo_opacity = 100;
		clearInterval('logo_interval');
		startnav.style.display = 'block';
		startnav_bottom.style.display = 'block';
	}
	setOpacity(logo, logo_opacity);
}



function rewrite_startpage() {

	start_animation();

	var nav_elements = document.getElementById('startnav').getElementsByTagName('LI');
	var counter = 0;

	for (i=0; i<nav_elements.length; i++) {
		anchor = nav_elements[i].getElementsByTagName('A')[0];
		anchor.my_number = i;
		anchor.onclick = function () {
			start_openanimation (this.href, this.my_number);
			this.href = '#';
		}
		counter = i;
	}

	var bottom_nav_elements = document.getElementById('startnav_bottom').getElementsByTagName('LI');

	for (i=0; i<bottom_nav_elements.length; i++) {
		anchor = bottom_nav_elements[i].getElementsByTagName('A')[0];
		counter ++;
		anchor.my_number = counter;
		anchor.onclick = function () {
			start_openanimation (this.href, this.my_number);
			this.href = '#';
		}
	}
}



/*--- Produkt-Seiten ---*/

	function init_tecspec() {
		if (document.getElementById('tec_spec_div')) {
			var tecspec = document.getElementById('tec_spec_div');
			if (document.getElementById('product_shot')) {
				var content = document.getElementById('product_shot');
			} else {
				var content = document.getElementById('content');
			}

			tecspec.style.display = 'none';
			var tecspec_link = document.createElement("a");
			tecspec_link.className = 'tecspec_link';
			tecspec_link.innerHTML = "Technische Daten";
			tecspec_link.href="#tec_spec_div";
			tecspec_link.onclick = function () {
				showPopup('tec_spec_div');
			}
			content.appendChild(tecspec_link);
		}
	}

	function init_additional_pics() {
		if (document.getElementById('additional_pics')) {
			if (document.getElementById('product_shot')) {
				var content = document.getElementById('product_shot');
			} else {
				var content = document.getElementById('content');
			}

			var additional_pics = document.getElementById('additional_pics');
			additional_pics.style.display = 'none';
			var additional_pic = additional_pics.getElementsByTagName('A');

			for (i=0; i<additional_pic.length; i++) {

				/* Link erzeugen */
				var additional_pic_link = document.createElement('a');
				additional_pic_link.className = 'tecspec_link';
				additional_pic_link.innerHTML = additional_pic[i].title;
				additional_pic_link.href="#additional_pics";
				additional_pic_link.iteration_number = i;
				additional_pic_link.onclick = function () {
					showPopup('additional_picture_' + this.iteration_number);
				}
				content.appendChild(additional_pic_link);

				/*Inhalts-DIV erzeugen*/
				pic_content = document.getElementById('content');
				var additional_pic_div = document.createElement('div');
				additional_pic_div.id = 'additional_picture_' + i;
				additional_pic_div.style.display = 'none';

				var additional_pic_img = document.createElement('img');
				additional_pic_img.src = additional_pic[i].href;
				var additional_pic_paragraph = document.createElement('div');
				additional_pic_paragraph.style.textAlign = "center";
				var additional_pic_title = document.createElement('h2');
				additional_pic_title.innerHTML = additional_pic[i].title;

				additional_pic_paragraph.appendChild(additional_pic_title);
				additional_pic_paragraph.appendChild(additional_pic_img);
				additional_pic_div.appendChild(additional_pic_paragraph);
				pic_content.appendChild(additional_pic_div);
			}
		}
	}



/* Inline Popup */

	var popup_fader;

	function showPopup(div_id) {

		if (!document.getElementById('inline_popup_background')) {
			var inline_popup_background = document.createElement('div');
			inline_popup_background.id = 'inline_popup_background';
			document.body.appendChild(inline_popup_background);
		}

		if (!document.getElementById('inline_popup')) {
			var inline_popup = document.createElement('div');
			inline_popup.id = 'inline_popup';
			document.body.appendChild(inline_popup);
		}

		var inline_popup = document.getElementById('inline_popup');
		inline_popup.innerHTML = "<a id='inline_popup_close' href='#top'>schließen</a><div id='inline_popup_content'>" + document.getElementById(div_id).innerHTML + "<div class='bottomspace'></div></div>";

		var inline_popup_background = document.getElementById('inline_popup_background');
		var inline_popup_close = document.getElementById('inline_popup_close');

		inline_popup_background.onclick = inline_popup_close.onclick = function() {
			inline_popup_background.fader = window.setInterval("fadeoutElement('inline_popup_background')", 50);
			inline_popup.fader = window.setInterval("fadeoutElement('inline_popup')", 50);
		}

		setOpacity(inline_popup_background, 0);
		setOpacity (inline_popup, 0);

		inline_popup.opacity = 0;
		inline_popup.max_opacity = 100;
		inline_popup.style.display = 'block';
		inline_popup.fader = window.setInterval("fadeinElement('inline_popup')", 35);

		inline_popup_background.opacity = 0;
		inline_popup_background.max_opacity = 75;
		inline_popup_background.style.display = 'block';
		inline_popup_background.fader = window.setInterval("fadeinElement('inline_popup_background')", 35);
	}

	function fadeinElement (element_id) {
		if (element = document.getElementById(element_id)) {
			element.opacity += 10;
			if (element.opacity >= element.max_opacity) {
				setOpacity (element, element.max_opacity);
				window.clearInterval(element.fader);
			} else {
				setOpacity (element, element.opacity);
			}
		}
	}

	function fadeoutElement (element_id) {
		if (element = document.getElementById(element_id)) {
			element.opacity -= 10;
			if (element.opacity <= 0) {
				setOpacity (element, 0);
				element.style.display = 'none';
				window.clearInterval(element.fader);
			} else {
				setOpacity (element, element.opacity);
			}
		}
	}

	function setOpacity(e,opacity){
		var o=e.style;
		o.opacity=(opacity/100); //Opera
		o.MozOpacity=(opacity/100); //Mozilla+Firefox
		o.KhtmlOpacity=(opacity/100); //Konqueror
		o.filter="alpha(opacity="+opacity+")"; //IE
	}





/* Linktip */

function init_linktip() {
	var anchors = document.getElementsByTagName("A");
	for (i=0; i<anchors.length; i++) {
		if (anchors[i].className.indexOf("linktip") > -1) {
			anchors[i].onmouseover = function() {
				var anchor_name = this.href.split('#')[1];
				var linktip_content = document.getElementById(anchor_name).getElementsByTagName('SPAN')[0].innerHTML;
				displayLinktip (this, linktip_content);
			};
		}
	}

	var linktip_hide = document.getElementsByTagName("P");
	for (i=0; i<linktip_hide.length; i++) {
		if (linktip_hide[i].className.indexOf('linktip_hide') > -1) {
			linktip_hide[i].style.display = 'none';
		}
	}
}


function displayLinktip (element, content) {
	if (!document.getElementById('linktip')) {
		newelement('linktip');
	}
	var linktip_box = document.getElementById('linktip');
	linktip_box.innerHTML = content;
	document.onmousemove = getmouseposition;
	linktip_box.style.display = 'block';

	element.onmouseout = function () {
		closeTip(element);
	}
}

function newelement (newid) {
	if (document.createElement) {
		var el = document.createElement('div');
		el.id = newid;
		with (el.style) {
			display = 'block';
			position = 'absolute';
		}
		el.innerHTML = '&nbsp;';
		document.body.appendChild(el);
	}
}

function closeTip (element) {
	document.getElementById('linktip').style.display = 'none';
}

var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);
var offsetx = 12;
var offsety =  8;

function getmouseposition(e) {
	if (document.getElementById) {
		mousex = (ie5)?event.x:(ns6)?clientX = e.clientX:false;
		mousey = (ie5)?event.y:(ns6)?clientY = e.clientY:false;
		var linktip_box = document.getElementById('linktip');
		linktip_box.style.left = (mousex+offsetx+10) + 'px';
		linktip_box.style.top = (mousey+offsety-10) + 'px';
	}
}



















function domready() {
	if (document.getElementById('startpage')) {
		rewrite_startpage();
	} else {
		init_tecspec();
		init_additional_pics();
		init_linktip();
		init_faqs();
	}
}
