﻿var _debug = false;


function RemoveAttributes(TagName, AttributeName) {
    if (!document.getElementById) return true;

    var elements = document.getElementsByTagName(TagName);
    for (var i = 0; i < elements.length; i++) {
    	var element = elements[i];
		
		/* IE<8 kennt hasAttribute nicht */
    	if (!element.hasAttribute) {
    		/* 
    			IE<8 kann auch mit verschiedenen Attributnamen nichts anfangen 
    			und nutzt lieber andere dafür. Daher erstmal ersetzen:
    		*/
    		AttributeName = (AttributeName == 'for') ? 'htmlFor' : AttributeName;
    		/* Dem Element jetzt noch die selbstgebaute hasAttribute-Funktion anhängen: */
			element.hasAttribute = function(a) {
    			return !!this.getAttribute(a);
    		};
    	}

		/* Und jetzt endlich für alle: */
    	if (element.hasAttribute(AttributeName)) {
    		element.removeAttribute(AttributeName);
    	}
    }

    return false;
}

String.prototype.replaceAll = function(strWhat, strWith) {
	var str = this;
	
	while (str.indexOf(strWhat) >= 0) {
		str = str.replace(strWhat, strWith);
	}
	
	return str;
}

function ToggleRows(hyperlink, tableId, rowClassName) {
	var table = document.getElementById(tableId);

	if (table != null) {
		var rows = table.getElementsByTagName("tr");

		if (rows != null) {
			for (var i = 0; i < rows.length; i++) {
				if (rows[i].className.indexOf(rowClassName) > -1) {
					if (rows[i].style.display == "none") {
						rows[i].style.display = "";
					} else {
						rows[i].style.display = "none";
					}
				}
			}
		}
	}

	if (hyperlink.className.indexOf(" Expand") > -1) {
		hyperlink.className = hyperlink.className.replaceAll(" Expand", " Collapse");
	} else {
		hyperlink.className = hyperlink.className.replaceAll(" Collapse", " Expand");
	}
}

function ToggleAllRows(sourceControl, tableId, rowIdentifierTop, rowIdentifierSub) {
	if (sourceControl.className.indexOf(" Expand ") >= 0) {
		sourceControl.className = sourceControl.className.replaceAll(" Expand ", " Collapse ");
	} else {
		sourceControl.className = sourceControl.className.replaceAll(" Collapse ", " Expand ");
	}
	var display = "block";
	var table = document.getElementById(tableId);
	if (table != null) {
		var rows = table.getElementsByTagName("tr");
		if (rows != null) {
			for (var i = 0; i < (rows.length - 1); i++) {
				if (rows[i].className.indexOf(rowIdentifierTop) >= 0) {
					var toggleLinks = rows[i].getElementsByTagName("a");
					if (toggleLinks != null) {
						for (var x = 0; x < (toggleLinks.length - 1); x++) {
							if (toggleLinks[x].className.indexOf("ToggleLink") >= 0) {
								if (sourceControl.className.indexOf(" Expand ") >= 0) {
									toggleLinks[x].className = toggleLinks[x].className.replaceAll(" Collapse", " Expand");
								}
								else {
									toggleLinks[x].className = toggleLinks[x].className.replaceAll(" Expand", " Collapse");
								}
								break;
							}
						}
					}
				} else if (rows[i].className.indexOf(rowIdentifierSub) >= 0) {
					if (sourceControl.className.indexOf(" Expand ") >= 0) {
						rows[i].style.display = "none";
					} else {
						rows[i].style.display = "";
					}
				}
			}
		}
	}
}



function ShowElement(id) {
	var element = document.getElementById(id);
	if (element != null && element.style != null && element.style.display != null) {
		element.style.display = "";
	}
}


var mouseX = 0;
var mouseY = 0;
var activeBox = "";

function ShowToolTip(toolTipId) {
	var toolTip = document.getElementById(toolTipId);

	if (toolTip != null) {
		activeBox = toolTipId;

		toolTip.style.display = "";
		toolTip.style.width = "auto";
		toolTip.style.whitespace = "no-wrap";
				
		// Damit der ToolTip nicht ausbricht, darf er max 450px breit sein
		if (toolTip.offsetWidth > 450) {
			toolTip.style.width = "450px";
			toolTip.style.whitespace = "normal";
		}
	}
}

function HideToolTip(toolTipId) {
	var toolTip = document.getElementById(toolTipId);

	if (toolTip != null) {
		activeBox = "";
		toolTip.style.display = "none";
	}
}

function InitMousePosition() {
	document.onmousemove = UpdateMsgBox;
}

function UpdateMsgBox(evt) {
	if (activeBox != "") {
		var msgBox;
		msgBox = document.getElementById(activeBox);

		if (msgBox.style.display != "none") {
			UpdateMousePosition(evt);

			msgBox.style.height = "auto";

			UpdateMsgBoxPos(mouseX, mouseY, msgBox.offsetWidth, msgBox.offsetHeight);
		}
	}
}


function UpdateMousePosition(evt) {
	//This works on Mozilla Firefox 1.5, Opera 9, Chrome 4
	if (evt) {
		mouseX = evt.pageX;
		mouseY = evt.pageY;
	}
	//This works on Internet Explorer 6 & 7
	else if (window.event) {
		mouseX = window.event.clientX + document.documentElement.scrollLeft;
		mouseY = window.event.clientY + document.documentElement.scrollTop;
	}
}

function UpdateMsgBoxPos(mouseX, mouseY, boxWidth, boxHeight) {	
	if (activeBox != "") {
		var msgBox = document.getElementById(activeBox);

		var bodyWidth = document.body.offsetWidth;
		var bodyHeight = document.body.offsetHeight;
		
		// ToolTip links/rechts ausrichten
		if (mouseX > (bodyWidth / 2)) {
			msgBox.style.left = (mouseX - boxWidth - 20) + "px";
			msgBox.style.top = mouseY + "px";
		} else {
			msgBox.style.left = (mouseX + 20) + "px";
			msgBox.style.top = mouseY + "px";
		}
	}
}

InitMousePosition();

function AddShoppingCartLine(sourceControl, targetControlId) {
	try {
		if (sourceControl.value != "") {
			var targetControl = document.getElementById(targetControlId);
			if (targetControl != null) {
				targetControl.style.display = "";
			}
		}
	}
	catch (ex) {
		if (_debug) {
			altert(ex.toString());
		}
	}
}

function ToggleDebugInfos(link) {
	if (link != null) {
		var fieldset = link.parentNode.parentNode;
		
		if (fieldset != null) {
			var table = fieldset.getElementsByTagName("table");

			if (table != null && table.length > 0) {
				if (table[0].style.display == "none") {
					table[0].style.display = "";
				} else {
					table[0].style.display = "none";
				}
			}
		}
	}
}
