/*****************************************************************************/
/*  "Suchen" wird entfernt, wenn man es fokusiert.
/*****************************************************************************/
function clearField(currentObj) {
	var currentField = document.getElementById(currentObj.id);
	currentField.value = "";
}


/*****************************************************************************/
/*  Ersetzt alle Zeilenumbr�che im Code des Elements
/*****************************************************************************/

function fladCode (docId) {
	var currentElement = document.getElementById(docId);
	currentElement.innerHTML = currentElement.innerHTML.replace(/[\t\n\r\f\v]/g,'');
	return;
}

/*****************************************************************************/
/*  Blendet n�chstes/vorheriges Produkt ein
/*****************************************************************************/
var leftPosition = 0;

function showProducts(direction) {
	fladCode("mainBox");
	var jumpWidth = getFirstChild(document.getElementById("mainBox")).offsetWidth + 50;
	var jumps = getChildNodesLength(document.getElementById("mainBox"))+1; //document.getElementById("mainBox").childNodes.length;
	if (direction == "n")
	{
		leftPosition = leftPosition - jumpWidth;
		if (leftPosition <= jumps * jumpWidth * -1) {
			leftPosition = 0;
		}
	}
	
	else {
		leftPosition = leftPosition + jumpWidth;
		if (leftPosition > 0) {
			leftPosition = (jumps -1 ) * jumpWidth * -1;
		}
	}
	document.getElementById("mainBox").style.left = leftPosition + "px";
}

/*****************************************************************************/
/* Springt zu der gew�hlten Person
/*****************************************************************************/
function jumpTo(targetDiv) {
	document.getElementById("mainBox").style.left = 0;
	var peopleTeaser = getNextSibling(document.getElementById("mainBox").firstChild);
	var i = 0;
		while(peopleTeaser.id != targetDiv){
		peopleTeaser = getNextSibling(peopleTeaser);
		i++;
  }
	document.getElementById("mainBox").style.left = parseInt(i * - (peopleTeaser.offsetWidth + 50)) + "px";
}

/*****************************************************************************/
/* Zeichnet den Zoom-Layer
/*****************************************************************************/
var box = new Array();
var zoomImg = new Array();

function zoom(currentObj, fileSrc) {
	
	var divPosY = document.documentElement.scrollTop * 1.2 + 20;
	var divHeight = document.getElementsByTagName("BODY")[0].offsetHeight;
	var divWidth = document.getElementsByTagName("BODY")[0].offsetWidth;
	var newDiv = document.createElement("DIV");
	var divIdentity = document.createAttribute("id");
	divIdentity.nodeValue = "zoomBg";
	newDiv.setAttributeNode(divIdentity);
	document.getElementById("bd").insertBefore(newDiv, document.getElementById("main"));
	document.getElementById(divIdentity.nodeValue).innerHTML = "<div id=\"zoom\"><div><a href=\"javascript:void(0);\" onclick=\"javascript: removeZoom();\">schliessen</a></div><img id=\"zoomImage\" src=\"" + fileSrc + "\" /></div>";
	document.getElementById(divIdentity.nodeValue).style.height = divHeight + "px";
	document.getElementById(divIdentity.nodeValue).style.width = divWidth + "px";
	document.getElementById(divIdentity.nodeValue).style.padding =  divPosY + "px 0 0 0";
	
	imgWidth = document.getElementById("zoomImage").offsetWidth;
	document.getElementById("zoom").style.width = imgWidth + "px";
}

/*****************************************************************************/
/* Versteckt den Zoom-Layer
/*****************************************************************************/
function removeZoom() {
	var zoomNode = getFirstChild(document.getElementById("bd"));
	document.getElementById("bd").removeChild(zoomNode);
}

/*****************************************************************************/
/*  Paragraphen werden ein- oder ausgeblendet
/*****************************************************************************/

function showText(obj) {
	var paragraph = getNextSibling(obj.parentNode);
	var headlineRef = obj.parentNode.firstChild;
	var box = obj.parentNode.parentNode;
	
	if (paragraph != null &&
			headlineRef != null &&
			box != null) {
			switch (paragraph.className) {
	  	case "collapse":
	    	paragraph.className = "collapse active";
	    	headlineRef.className = "active";
	    	box.className = "collapseBox active"
	    	break;
			default:
	    	paragraph.className = "collapse";
	    	headlineRef.className = "";
	    	box.className = "collapseBox"
   	}
	}
}

/*****************************************************************************/
/* Firefox interpretiert Leerschl�ge und Zeilenumbr�che als #text
/* IE k�mmerts nicht
/*****************************************************************************/

function getNextSibling(startBrother){
  var endBrother = startBrother.nextSibling;
  while(endBrother.nodeType!=1){
 		endBrother = endBrother.nextSibling;
  }
  return endBrother;
}

function getFirstChild(startNode){
  var child = startNode.firstChild;
  while(child.nodeType!=1){
    child = child.nextSibling;
  }
  return child;
}

function getChildNodesLength(startNode){
	var child = startNode.firstChild;
	var j = 0;
	var i = 0;
	while(j < startNode.childNodes.length && child.nextSibling != null){
		while(child.nodeType != 1){
		  child = child.nextSibling;
	    i++;
	  }
	 	child = child.nextSibling;
	  j++;
	 }
	return j;
}
//document.getElementById("mainBox").childNodes.length

/*****************************************************************************/
/* Onload
/*****************************************************************************/
function allOnLoads() {
}

//window.onload = allOnLoads;

/*****************************************************************************/
/* BMI Defaultvalue löschen
/*****************************************************************************/
function clearField(fieldName) {
	document.forms["BMIForm"].elements[fieldName].value = "";
	document.getElementById("BMIAlert").style.left = "-2000px";
}

/*****************************************************************************/
/* BMI Defaultvalue setzen falls keine Eingabe
/*****************************************************************************/
function defaultValueField(fieldName) {
	if (document.forms["BMIForm"].elements[fieldName].value == "")
	{
		document.forms["BMIForm"].elements[fieldName].value = bmiEnterHere;
	}
}

/*****************************************************************************/
/* Prüft die Eingaben des Users
/*****************************************************************************/
function checkInput() {
   
  alertText ="";
	if (isNaN(document.forms["BMIForm"].elements[0].value) == true)
	{
		var alertText = bmiNoticeHigh;
	}
	
	else if (isNaN(document.forms["BMIForm"].elements[1].value) == true)
	{
		var alertText = bmiNoticeWeight;
	}
	
	return alertText;	
}

/*****************************************************************************/
/* Berechnet den BMI
/*****************************************************************************/
function calculateBMI() {
	var size = document.forms["BMIForm"].elements[0].value * 0.01;
	var weight = document.forms["BMIForm"].elements[1].value;
	if (checkInput() == "") {
		var BMI = roundBMI(weight / Math.pow(size, 2));
		var layerPosition = 348 + ((BMI - 15) * 12.8);

		if (BMI < 15) {
			var layerPosition = 348;
			var textColor = 000;
			var BMIText = bmibis20;
		}
		
		if (BMI >= 15 && BMI < 20) {
			var layerPosition = 348 + ((BMI - 15) * 12.8);
			var textColor = 000;
			var BMIText = bmibis20;
		}
		
		if (BMI >= 20 && BMI < 25) {
			var textColor = "54952a";
			var BMIText = bmibis249;
		}
		
		if (BMI >= 25 && BMI < 30) {
			var textColor = "f2a30f";
			var BMIText = bmibis299;
		}

		if (BMI >= 30 && BMI < 40) {
			var textColor = "ff6a23";
			var BMIText = bmibis389;
		}
		
		if (BMI >= 40) {
			var layerPosition = 645;	
			var textColor = "ff0005";
			var BMIText = bmiab40;
	}
		
		document.getElementById("BMIInfoText").innerHTML = "<span style=\" color:#" + textColor + "\"><strong>" + BMI + "</strong>"+ BMIText +"</span>";
		document.getElementById("BMIPointer").style.left = layerPosition + "px";
	}
	else {
		document.getElementById("BMIAlert").style.left = "60px";		
		document.getElementById("BMIAlert").innerHTML = checkInput();		
		}
	
}

function roundBMI(x) {
  var k = (Math.round(x * 10) / 10).toString();
  k += (k.indexOf('.') == -1)? '' : '';
  return k.substring(0, k.indexOf('.') + 3);
}

/*****************************************************************************/
/* öffnet popup
/*****************************************************************************/

windowopen = false;

function open_window(url,width,height,scroll)
{
	if (windowopen) {
		if (hwin.closed) {
			hwin = window.open(url,"popup","alwaysRaised=yes,width=" + width + ",height=" + height +","+ scroll);
		}
		else {
			close_window()
			hwin = window.open(url,"popup","alwaysRaised=yes,width=" + width + ",height=" + height +","+ scroll);
		}
	}
	else {
		hwin = window.open(url,"popup","alwaysRaised=yes,width=" + width + ",height=" + height +","+ scroll);
		if (hwin) windowopen = true;
	}
}

function close_window()
{
	if (windowopen) {
		if (!hwin.closed)
			hwin.close();
	}
} 
