var browser					= "";
var browserWidth 		= 0;
var browserHeight 	= 0; 
var scrollbarHeight	= 0; 
var bodyWidth 			= 0; 
var bodyHeight 			= 0;
var currentFrame		= 0;
var waitingTime			= 0;
var fadeSteps				= 8;
var fadeSpeed				= 20;
var codeDivWidth		= 400;
var codeDivHeight		= 200;
var fadingIn				= false;
var fadingOut				= false;
var opacityFilter		= 7;			
var formFocus				= "";

function fileName (file)
	{
	return file.replace(/_/g,"-");	
	}

function windowSize() 
	{	
	//BROWSERÜBERGREIFENDE DOKUMENTENHÖHE
  bodyHeight = Math.max(Math.max(document.body.scrollHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.documentElement.offsetHeight), Math.max(document.body.clientHeight, document.documentElement.clientHeight));

	//INTERNET EXPLORER
	if (self.innerHeight) 
		{ 
		//INTERNET EXPLORER
		browserWidth  	= window.innerWidth;
		browserHeight 	= window.innerHeight;
		scrollbarHeight = window.pageYOffset;
		
		opacityFilter = 70;
		} 
	else if (document.documentElement && document.documentElement.clientHeight) 
		{ 
		//INTERNET EXPLORER 6
		browserWidth  	= document.documentElement.clientWidth;
		browserHeight 	= document.documentElement.clientHeight;
		scrollbarHeight = document.documentElement.scrollTop;
		
		opacityFilter = 70;
		} 
	else if (document.body) 
		{ 
		//INTERNET EXPLORER 7
		browserWidth  	= document.body.clientWidth;
		browserHeight	  = document.body.clientHeight;
		scrollbarHeight = document.body.scrollTop;
		
		opacityFilter = 70;
		}

	//BODYABMESSUNGEN
	if (window.innerHeight && window.scrollMaxY) 
		{	
		bodyWidth  = document.body.scrollWidth;
		} 
	else if (document.body.scrollHeight > document.body.offsetHeight) 
		{ 
		//ALLE AUSSER MAC
		bodyWidth  = document.body.scrollWidth;
		}
	else 
		{
		//MAC
		bodyWidth  = document.body.offsetWidth;
		}
	}

function buttonValue (id, original, valueText)
	{
	document.getElementById(id).value = valueText;
	
	formFocus = document.getElementById(id).parentNode.parentNode.id;
	}

function fadeInCC (headline, description, dirRelImgCodes)
	{
	if (!document.getElementById("divPreloader"))
		{
		if (currentFrame == 0)
			{
			windowSize();	
	
			htmlBody = document.getElementsByTagName("body").item(0);		
			divLayer = document.createElement("divLayer");
			
			divLayer.setAttribute("id","divLayer");		

			divLayer.style.width	 	 	= bodyWidth  + "px";
			divLayer.style.height	 	 	= bodyHeight + "px";
			divLayer.style.background	= "#222222";
			divLayer.style.filter	 	 	= "alpha(opacity=0)";
			divLayer.style.opacity 	 	= "0.0";
			divLayer.style.MozOpacity	= "0.0";
			divLayer.style.position 	= "absolute"; 
			divLayer.style.left	 	 		= "0px";
			divLayer.style.top				= "0px";	
			divLayer.style.width 			= bodyWidth  + "px";
			divLayer.style.height 		= bodyHeight + "px";
			divLayer.style.zIndex 	 	= "9997";
			divLayer.style.display 	 	= "none";
			divLayer.className		 	 	= "layer0";		
				
			htmlBody.insertBefore(divLayer, htmlBody.firstChild);
			}
		}
	else
		{
		document.getElementById("divLayer").style.display = "block";
		}
		
	if (!fadingOut)
		{
		fadingIn = true;
			
		currentFrame++;		
	
		if (currentFrame <= fadeSteps)
			{
			divLayer.style.filter	 	 	= "alpha(opacity=" + currentFrame + "0)";
			divLayer.style.opacity 	 	= "0." + currentFrame;
			divLayer.style.MozOpacity	= "0." + currentFrame;
			
			divLayer.style.display 	 	= "block";

			setTimeout("fadeInCC(\"" + headline + "\", \"" + description + "\", \"" + dirRelImgCodes + "\")",fadeSpeed);
			}
		else if (currentFrame > fadeSteps) 
			{
			fadingIn = false;		
			
			captchaCodeLayer(headline, description, dirRelImgCodes);
			
			divLayer.setAttribute("onclick","fadeOutCC(false);");
			}
		}
	}
	
function captchaCodeLayer (headline, description, dirRelImgCodes)
	{
	windowSize();
		
	htmlBody = document.getElementsByTagName("body").item(0);		
	divCaptchaCode = document.createElement("div");
		
	divLeft = (bodyWidth		 / 2) - (codeDivWidth  / 2) - 100;
	divTop	= (browserHeight / 2) - (codeDivHeight / 2) - 100;
	
	divCaptchaCode.setAttribute("id","ccDiv");
	
	divCaptchaCode.style.position 	= "fixed"; 
	divCaptchaCode.style.left	 	 		= divLeft + "px";
	divCaptchaCode.style.top				= divTop  + "px";	
	divCaptchaCode.style.filter	 	 	= "alpha(opacity=" + opacityFilter + ")";
	divCaptchaCode.style.opacity 	 	= "0.7";
	divCaptchaCode.style.MozOpacity	= "0.7";
	divCaptchaCode.style.width 			= codeDivWidth  + "px";
	divCaptchaCode.style.height 		= codeDivHeight + "px";
	divCaptchaCode.style.background = "#000000";
	divCaptchaCode.style.color			= "#CCCCCC";
	divCaptchaCode.style.border 	 	= "1px solid #777777";
	divCaptchaCode.style.padding 	 	= "100px";
	divCaptchaCode.style.zIndex 	 	= "9999";
	divCaptchaCode.style.display 	 	= "block";

	divCaptchaCode.innerHTML			 	= "<h3 style='color:#FFF; margin:0 0 30px 0;'>" + headline + "</h3>" + 
																		"<div><img src='" + dirRelImgCodes + ccImage + "' alt='' /></div>" +
																		"<div><input type='text' id='code' name='code' style='width:270px; text-align:center; font-weight:bold; letter-spacing:5px;' maxlength='8' /></div>" +
																		"<div style='margin:30px 0 0 0'>" + description + "</div>";

	htmlBody.insertBefore(divCaptchaCode, htmlBody.firstChild);
	
	document.getElementById("code").focus();
	}
	
function stringToUpper ()
	{
	if (document.getElementById("code"))
		{
		document.getElementById("code").value = document.getElementById("code").value.toUpperCase()
		}
	}

function checkCode ()
	{
	if (document.getElementById("code"))
		{
		if (ccText == document.getElementById("code").value)
			{
			document.getElementById("code").style.background = "green";
				
			document.getElementById(formFocus + "_chars").value = ccText;
						
			fadeOutCC ("OK");
			}
		else if (document.getElementById("code").value.length == 8)
			{
			document.getElementById("code").style.background = "red";	
			}
		else
			{
			document.getElementById("code").style.background = "#FFF";	
			}
		}
	}
	
function fadeOutCC (confirmation)
	{
	if (!fadingIn)
		{	
		if (currentFrame == 8)
			{
			if (document.getElementById("ccDiv"))
				{
				document.getElementById("ccDiv").style.display = "none";	
				}
			}
		
		fadingOut = true;	
		
		currentFrame--;	
	
		if (currentFrame > 0)
			{	
			divLayer.style.filter	 	 	= "alpha(opacity=" + currentFrame + "0)";
			divLayer.style.opacity 	 	= "0." + currentFrame;
			divLayer.style.MozOpacity	= "0." + currentFrame;
			
			setTimeout("fadeOutCC(\"" + confirmation + "\")",fadeSpeed);
			}
		else
			{
			fadingOut = false;

			document.getElementById("divLayer").style.display	= "none";	
						
			if (confirmation == "OK")
				{			
				document.getElementById(formFocus).submit();
				}
			}		
		}
	}
