var ani_cert = -1;

function init()
{
	var as = document.getElementsByTagName("a");
	for(var i = 0; i < as.length; i++)
	{
		if(!as[i].href)
			continue;
		var a = as[i].href;
		if(
			(
					a.substr(0, 7) == "http://"
				&&
					a.substr(0, 17) != "http://www.k-m.at"
				&&
					a.substr(0, 16) != "http://localhost"
			)
			||
			(
					a.substr(0, 8) == "https://"
				&&
					a.substr(0, 18) != "https://www.k-m.at"
				&&
					a.substr(0, 17) != "https://localhost"
			)
		)
			as[i].onclick = new Function('', 'window.open(this.href); return false;');
	}

	var as = document.getElementsByName("m-addr");
	for(var i = 0; i < as.length; i++)
	{
		if(as[i].nodeName == "a" || as[i].nodeName == "A")
		{
			as[i].href = as[i].href.replace(mail_mask_at, '@');
			as[i].href = as[i].href.replace(mail_mask_dot, '.');
		}else if(as[i].nodeName == "span" || as[i].nodeName == "SPAN")
		{
			as[i].firstChild.nodeValue = as[i].firstChild.nodeValue.replace(mail_mask_at, '@');
			as[i].firstChild.nodeValue = as[i].firstChild.nodeValue.replace(mail_mask_dot, '.');
		}
	}

	if(document.URL.lastIndexOf("#infobriefanmeldung") != -1)
		open_newsletter();

	$("#certificates li").css('position', 'absolute');
	$("#certificates li").css('left', '80%');
	$("#certificates li").css('margin', '0');
	$("#certificates li").css('display', 'block');
	$("#certificates li").animate(
	{
		opacity:	0
	}, 100);
	window.setTimeout(animate_certificate, 1000);
}

function animate_certificate()
{
	var elem = 0;
	do
		elem = Math.floor(Math.random() * $("#certificates li").size());
	while(elem == $("#certificates li").size() || elem == ani_cert);
	ani_cert = elem;
	$($("#certificates li").get(ani_cert)).animate(
		{
			left:		'0',
			opacity:	1
		}, 500, 'linear', function()
		{
			window.setTimeout(function()
			{
				$($("#certificates li").get(ani_cert)).animate(
				{
					left:		'80%',
					opacity:	0
				}, 500, 'linear', function()
				{
					$(this).css('right', '0');
					window.setTimeout(animate_certificate, 1000);
				});
			}, 10000);
		}
	);
}

function PrivateClick(elementID)
{
	obj = document.getElementById(elementID);

	obj.value = '';
	obj.disabled = "disabled";
	
	document.getElementById("zusendung_td").style.color = "#CCCCCC";
	
	document.getElementById(elementID).style.backgroundColor = "#CCCCCC";
}

function PrivateClickNl(elementID)
{
	obj = document.getElementById(elementID);

	obj.value = '';
	obj.disabled = "disabled";
	
	document.getElementById("nl_zusendung_td").style.color = "#CCCCCC";
	
	document.getElementById(elementID).style.backgroundColor = "#CCCCCC";
}

function FirmaClick(elementID)
{
	obj = document.getElementById(elementID);
	
	obj.disabled = false;

	document.getElementById(elementID).style.backgroundColor = "#FFFFFF";
	
	document.getElementById("zusendung_td").style.color = "#87888A";
}

function FirmaClickNl(elementID)
{
	obj = document.getElementById(elementID);
	
	obj.disabled = false;

	document.getElementById(elementID).style.backgroundColor = "#FFFFFF";
	
	document.getElementById("nl_zusendung_td").style.color = "#87888A";
}

function SetMailActive()
{
	document.getElementById("contactViaTel").innerHTML = 'Telefonnummer:';
	document.getElementById("contactViaMail").innerHTML='*E-Mail Adresse:';
}

function SetTelActive()
{
	document.getElementById("contactViaTel").innerHTML = '*Telefonnummer:';
	document.getElementById("contactViaMail").innerHTML='E-Mail Adresse:';

}

function Kontakt_set_date_and_time()
{
	var time=new Date();
	var hour = time.getHours(); hour=(hour<10)? '0'+hour:hour;
	var minute = time.getMinutes();minute=(minute<10)? '0'+minute:minute;
	var travelTime = hour+':'+minute;
	document.getElementById("anfahrt_formular").REQ0JourneyTime.value=travelTime;
	var yy = time.getFullYear();
	var mm = time.getMonth()+1; mm=(mm<10)?'0'+mm:mm;
	var dd = time.getDate(); dd=(dd<10)?'0'+dd:dd;
	var travelDate=dd+"."+mm+"."+yy;
	document.getElementById("anfahrt_formular").REQ0JourneyDate.value=travelDate;
}

function open_print_popup()
{
	window.open(document.getElementById('print').href, "Printview", "width=420,height=613,top=100,left=100,resizable=no,scrollbars=yes");
	return false;
}

var dont_close = false;

function open_newsletter()
{
	$("#gray-box").fadeIn("slow");
}

function close_newsletter()
{
	if(dont_close)
		dont_close = false;
	else
		$("#gray-box").fadeOut("slow");
		
	return false;
}

function dont_close_function()
{
	dont_close = true;
}

function subscribe_newsletter()
{
	var nl = document.getElementById("newsletter");
	anrede = '';
	for(var i = 0; i < $("#newsletter input[name=anrede]").toArray().length; i++)
		if($("#newsletter input[name=anrede]").toArray()[i].checked)
			anrede = $("#newsletter input[name=anrede]").toArray()[i].value;
	title = $("#newsletter input[name=title]").attr("value");
	vorname = $("#newsletter input[name=vorname]").attr("value");
	nachname = $("#newsletter input[name=nachname]").attr("value");
	zusendung = '';
	for(var i = 0; i < $("#newsletter input[name=zusendung]").toArray().length; i++)
		if($("#newsletter input[name=zusendung]").toArray()[i].checked)
			nl = $("#newsletter input[name=zusendung]").toArray()[i].value;
	firma = $("#newsletter input[name=firma]").attr("value");
	strasse = $("#newsletter input[name=strasse]").attr("value");
	plz = $("#newsletter input[name=plz]").attr("value");
	ort = $("#newsletter input[name=ort]").attr("value");
	land = $("#newsletter input[name=land]").attr("value");
	tel = $("#newsletter input[name=tel]").attr("value");
	email = $("#newsletter input[name=email]").attr("value");
	sicherheitsfrage = $("#newsletter input[name=sicherheitsfrage]").attr("value");

	$("#nl_answer").html("Ihre Anfrage wird verarbeitet. Bitte warten...");
	$("#nl_answer").css("color", "#87888A");
	document.getElementById("nl_submit").disabled = "disabled";
	$.post(	"/subscribe_newsletter.php",
		{
			anrede			:	anrede,
			title			:	title,
			vorname			:	vorname,
			nachname		:	nachname,
			zusendung		:	zusendung,
			
			firma			:	firma,
			strasse			:	strasse,
			plz			:	plz,
			ort			:	ort,
			land			:	land,
			
			tel			:	tel,
			email			:	email,
			sicherheitsfrage	:	sicherheitsfrage
		},
		function(data)
		{
			var begin = data.substr(0, data.indexOf(':'));
			var end = data.substr(data.indexOf(':') + 1);
			if(begin == "Error")
			{
				$("#nl_answer").html(end);
				$("#nl_answer").css("color", "#FF0000");
				document.getElementById("nl_submit").removeAttribute("disabled");
			}else if(begin == "Success")
			{
				$("#nl_answer").html(end);
				$("#nl_answer").css("color", "#87888A");
				window.setTimeout('close_newsletter()', 5000);
			}else
			{
				$("#nl_answer").html("Ein Fehler ist aufgetreten. Bitte versuchen Sie es später erneut." + data);
				$("#nl_answer").css("color", "#FF0000");
				document.getElementById("nl_submit").removeAttribute("disabled");
			}
		});
	return false;
}

function highlight_this_pressebox(box)
{
	pressebox_color = box.style.backgroundColor;
	box.style.backgroundColor = "#e5e5e5";
	pressebox_h_color = box.getElementsByTagName("h3")[0].style.color;
	box.getElementsByTagName("h3")[0].style.color = "#003684";
	box.onclick = function()
	{
		document.location = box.getElementsByTagName("a")[0].href;
	}
}

function unhighlight_this_pressebox(box)
{
	box.style.backgroundColor = pressebox_color;
	box.getElementsByTagName("h3")[0].style.color = pressebox_h_color;
}

function close_smart_home()
{
	if(dont_close)
		dont_close = false;
	else
		$("#interactive-smart-home").fadeOut("slow");
}

function oeffne_smart_home()
{
	if(!document.getElementById("interactive-smart-home"))
	{
		var box = document.createElement('div');
		box.className = 'gray-box';
		box.id = 'interactive-smart-home';
		box.onclick = close_smart_home;

		var flash_text = AC_FL_GetContent
		( 'classid','clsid:D27CDB6E-AE6D-11cf-96B8-444553540000',
		  'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0',
		  'width','690',
		  'height','500',
		  'wmode','transparent',
		  'src','/interaktives-smart-home/Interaktives-Smart-Home',
		  'menu','false',
		  'allowScriptAccess','always'
		);

		box.innerHTML = flash_text;

		box.firstChild.onclick = dont_close_function;
		box.firstChild.style.position = "absolute";
		box.firstChild.style.top = "50%";
		box.firstChild.style.left = "50%";
		box.firstChild.style.marginTop = "-250px";
		box.firstChild.style.marginLeft = "-345px";
		box.firstChild.style.width = "690px";
		box.firstChild.style.height = "500px";
	    
		document.getElementsByTagName("body")[0].appendChild(box);
	}
	$("#interactive-smart-home").fadeIn("slow");
}
