/*
* addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
function addEvent(obj, type, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(type, fn, false);
	} else if (obj.attachEvent)	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
function removeEvent(obj, type, fn) {
	if (obj.removeEventListener) {
		obj.removeEventListener(type, fn, false);
	} else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}



/*
* More accessible compact forms
*/
function initOverLabels () {

	if (!document.getElementById) return;
	var labels, id, field, dynamic;
	dynamic = false;
	// Set focus and blur handlers to hide and show
	// LABELs with 'overlabel' class names.
	labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		if (labels[i].className == 'overlabel' || labels[i].className == 'overlabel dynamic') {
			// Skip labels that do not have a named association
			// with another field.
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id))) {
				continue;
			}

			labels[i].style.display = "none";
			offsetTop = (field.offsetTop+4);
			offsetLeft = (field.offsetLeft+5);
			labels[i].style.display = "";

			// Change the applied class to hover the label
			// over the form field.
			if(labels[i].className == "overlabel") {
				labels[i].className = 'overlabel-apply';
			}
			if(labels[i].className == "overlabel dynamic") {
				labels[i].className = 'overlabel-apply dynamic';
				dynamic = true;
			}

			if(!isIE7 && !isIE6 && dynamic) {
				labels[i].setAttribute("style","position: absolute; top: " + offsetTop + "px; left: " + offsetLeft + "px;");
			} else if(dynamic) {
				pos = getPos(id);

				if(isIE6) {
					addTop = 4;
					addLeft = 5;
				}

				if(isIE7) {
					addTop = 12;
					addLeft = 13;
				}


				labels[i].style.position = "absolute";
				labels[i].style.top = (pos.top+addTop) + "px";
				labels[i].style.left = (pos.left+addLeft) + "px";
			}


			// Hide any fields having an initial value.
			if (field.value !== '') {
				hideLabel(field.getAttribute('id'), true);
			}
			// Set handlers to show and hide labels.
			field.onfocus = function () {
				hideLabel(this.getAttribute('id'), true);
			};
			field.onkeydown = function () {
				hideLabel(this.getAttribute('id'), true);
			};
			field.onblur = function () {
				if (this.value === '') {
					hideLabel(this.getAttribute('id'), false);
				}
			};
			// Handle clicks to LABEL elements (for Safari).
			labels[i].onclick = function () {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id))) {
					field.focus();
				}
			};
		}
	}
};
function hideLabel (field_id, hide) {
	var field_for;
	var labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute('for');
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
			labels[i].style.display = (hide) ? 'none' : 'inline';
			return true;
		}
	}
}
window.onload = function () {
	setTimeout(initOverLabels, 50);
};



/*
* Create the new window
*/
function openInNewWindow(e) {
	var event;
	if (!e) event = window.event;
	else event = e;
	// Abort if a modifier key is pressed
	if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
		return true;
	}
	else {
		// Change "_blank" to something like "newWindow" to load all links in the same new window
		var newWindow = window.open(this.getAttribute('href'), '_blank');
		if (newWindow) {
			if (newWindow.focus) {
				newWindow.focus();
			}
			return false;
		}
		return true;
	}
}
/*
* Add the openInNewWindow function to the onclick event of links with a class name of "new-window"
*/
function getNewWindowLinks() {
	// Check that the browser is DOM compliant
	if (document.getElementById && document.createElement && document.appendChild) {
		// Change this to the text you want to use to alert the user that a new window will be opened
		// var strNewWindowAlert = " (opens in a new window)";
		// Find all links
		var links = document.getElementsByTagName('a');
		// var objWarningText;
		var link;
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			// Find all links with a class name of "new-window"
			if (/\bnew\-window\b/.test(link.className)) {
				// Create an em element containing the new window warning text and insert it after the link text
				// objWarningText = document.createElement("em");
				// objWarningText.appendChild(document.createTextNode(strNewWindowAlert));
				// link.appendChild(objWarningText);
				link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}
addEvent(window, 'load', getNewWindowLinks);



/*
* Checks all checkboxes
*/
function checkAll(Checkbox, selector) {
	var checks = document.getElementsByName(Checkbox);
	var boxLength = checks.length;
	var totalChecked = 0;
	for ( i=0; i < boxLength; i++ ) {
		if ( checks[i].checked == true ) {
			allChecked = true;
			continue;
		} else {
			allChecked = false;
			break;
		}
	}
	if ( allChecked == false ) {
		for ( i=0; i < boxLength; i++ ) {
			checks[i].checked = true;
		}
		allChecked = true;
		document.getElementById(selector).checked = true;
	} else {
		for ( i=0; i < boxLength; i++ ) {
			checks[i].checked = false;
		}
		allChecked = false;
		document.getElementById(selector).checked = false;
	}
}

function toggleSearchText(element,searchText) {

	if(element.value == "") {
		element.value = searchText;
	} else if(element.value == searchText) {
		element.value = "";
	}
}

function searchForm(defaultText,defaultColor,toggleColor,formId,toggleFieldId) {

	form = document.getElementById(formId);
	toggleField = document.getElementById(toggleFieldId);

	toggleField.value = defaultText;

	foc = "toggleSearchText(this,'" + defaultText + "');";
	blu = "toggleSearchText(this,'" + defaultText + "');";
	submit = 'if(document.getElementById(\'' + toggleFieldId + '\').value == \'' + defaultText + '\' || document.getElementById(\'' + toggleFieldId + '\').value == \'' + defaultText + '\') { document.getElementById(\'' + toggleFieldId + '\').value = \'\';}';

	if(defaultColor != "") {
		foc = foc + ' this.style.color = \'' + defaultColor + '\';';
	}

	if(toggleColor != "") {
		blu = blu + ' this.style.color = \'' + toggleColor + '\';';
	}

	focu = document.createAttribute("onFocus");
	focu.nodeValue = foc;
	bluu = document.createAttribute("onBlur");
	bluu.nodeValue = blu;
	sub = document.createAttribute("onSubmit");
	sub.nodeValue = submit;

	toggleField.setAttributeNode(focu);
	toggleField.setAttributeNode(bluu);
	form.setAttributeNode(sub);
}

function prepareTextElement(checkbox, elementId, mode) {
	element = document.getElementById(elementId);
	if (checkbox.checked && mode == 'disable' ) {
		element.disabled = true;
		element.className = element.className + ' disabled';
	} else if (checkbox.checked && mode == 'enable' ) {
		element.disabled = false;
		element.className = 'default';
	} else if (!checkbox.checked && mode == 'disable' ) {
		element.disabled = false;
		element.className = 'default';
	} else if (!checkbox.checked && mode == 'enable' ) {
		element.disabled = true;
		element.className = element.className + ' disabled';
	}
}

function disableSubmitButton(whichButton) {
	if (document.getElementById) {
		document.getElementById(whichButton).disabled = true;
	} else if (document.all) {
		document.all[whichButton].disabled = true;
	}
}

function locationViaSelect (select) {
	var value = select.options[select.options.selectedIndex].value;
	if (value != "" && value != "0") {
		window.location.href = value;
	}
}

function bookmarkThis(url, title) {
	if (document.all) {
		window.external.AddFavorite(url, title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	}
}


// get correct offsets in ie 6/7
// also works for firefox, opera and netscape
function getPos (id) {
	var result = {top:0, bottom:0, left:0, right:0};
	var base = document.getElementById(id);
	var actual = base.offsetParent;

	result.top = base.offsetTop;
	result.left = base.offsetLeft;

	if(actual != null && actual.nodeName != "") {
		while((actual.nodeName != "BODY" && actual.nodeName != "HTML" && actual != null)) {
			result.top += parseInt(actual.offsetTop);
			result.left += parseInt(actual.offsetLeft);
			actual = actual.offsetParent;
		}
	}

	result.bottom = (base.offsetHeight + result.top);
	result.right = (base.offsetWidth + result.left);

	return result;
}

function showTooltip(elementId) {
	element = document.getElementById(elementId);
	element.style.display = "inline";

}

function hideTooltip(elementId) {
	element = document.getElementById(elementId);
	element.style.display= "none";

}

function goTo(target) {
	location.href = target;
}

///lixlpixel Javascript tooltips http://lixlpixel.org/javascript-tooltips/
// position of the tooltip relative to the mouse in pixel //
var offsetx = -10;
var offsety = -63;


var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);
var ie7 = (window.VBArray && window.XMLHttpRequest);
var ua = navigator.userAgent.toLowerCase();
var isapple = (ua.indexOf('applewebkit') != -1 ? 1 : 0);

function getmouseposition(e) {
		var lixlpixel_tooltip = document.getElementById(eleID);

	ie5me=(document.getElementById && document.all && document.styleSheets)?1:0;
		if(ie5me) {
      offsetx=event.clientX+document.body.scrollLeft;
      offsety=event.clientY+document.body.scrollTop;

		} 
	
	
		var iebody=(document.compatMode &&
		document.compatMode != 'BackCompat') ?
		document.documentElement : document.body;
		
		pagex = (isapple == 1 ? 0:(ie5)?iebody.scrollLeft:window.pageXOffset);
		pagey = (isapple == 1 ? 0:(ie5)?iebody.scrollTop:window.pageYOffset);
		mousex = (ie5)?event.x:(ns6)?clientX = e.clientX:false;
		mousey = (ie5)?event.y:(ns6)?clientY = e.clientY:false;


		if(lixlpixel_tooltip.style.display == "block") {
      if (ie7){
			   lixlpixel_tooltip.style.left = offsetx + pagex - 12 + 'px';
			   lixlpixel_tooltip.style.top =  offsety + pagey - lixlpixel_tooltip.offsetHeight - 25+ 'px';
      }
			else if(ie5me){
			   lixlpixel_tooltip.style.left = (offsetx - 20) + 'px';
			   lixlpixel_tooltip.style.top = (offsety - lixlpixel_tooltip.offsetHeight - 5) + 'px';
			}
      else{
			   lixlpixel_tooltip.style.left = (mousex+pagex+offsetx) + 'px';
			   lixlpixel_tooltip.style.top = (mousey+pagey+offsety) + 'px';
      }
      

		}
}

function tooltipShow(elementId,ac) {
	ele = document.getElementById(elementId);
	ele.style.display = "block";

	eleID = elementId;

	height = ele.offsetHeight;

	offsety = -(height+20);

	if(!document.getElementById(elementId)) return false;
	var lixlpixel_tooltip = document.getElementById(elementId);
	lixlpixel_tooltip.style.display = 'block';
	document.onmousemove = getmouseposition;
}

function tooltipExit(elementId) {
	document.getElementById(elementId).style.display = 'none';
}

function jsHide() {
	hide = document.getElementById("jsHide");

	if(hide != null) {
		hide.style.display = "none";
	}
}

function addTopic(topic) {

	addon = "";
	field = parent.document.getElementById("chooseTopic");

	if(field == null) {
		field = document.getElementById("chooseTopic");;
	}

	if(field.value == "Thema auswählen") {
		field.value = "";
	}

	if(field.value != "") {
		addon = field.value + "; ";
	}

	field.value = addon + topic;

	message = parent.document.getElementById("messagebox_topic_selector");
	message.className = "success";
	message.style.width = "490px";
	message.style.padding = "10px";
	message.style.textAlign = "center";
	message.innerHTML = "<p>Das Themengebiet \"" + topic + "\" wurde hinzugefügt.</p>";

}

addEvent(window,'load',jsHide);

function addAccessLabels() {

	tooltip = document.createElement("div");
	tooltip.className = "tooltip";
	tooltip.style.display = "none";
	tooltip.id = "accessTip";

	ttipText = document.createElement("p");
	ttipText.innerHTML = "Exklusiv für Mitglieder";
	tooltip.appendChild(ttipText);

	ttipImage = document.createElement("img");
	ttipImage.src = "/images/backgrounds/tooltip_corner.gif";
	ttipImage.width = "36";
	ttipImage.height = "22";
	ttipImage.alt = "";
	tooltip.appendChild(ttipImage);

	document.body.appendChild(tooltip);

	elements = getElementsByClassName("access");

	for(i=0; i <= (elements.length-1);i++) {

		element = elements[i];

		element.onmouseover = function() {tooltipShow('accessTip',this);};
		element.onmouseout = function() {tooltipExit('accessTip');};

	}
}
addEvent(window,'load',addAccessLabels);

function showErfacat(id, infoText) {

	ttipLink = document.getElementById(id);

	if(ttipLink == null) {
		return false;
	}


	//ttipLink.style.textDecoration = "underline";
	ttipLink.style.cursor = "pointer";

	tooltip = document.createElement("div");
	tooltip.className = "tooltip";
	tooltip.style.display = "none";
	tooltip.id = 'inner'+id;
	tooltip.style.width = "300px";

	ttipHeadline = document.createElement("h4");
	ttipHeadline.innerHTML = "Info";
	tooltip.appendChild(ttipHeadline);

	ttipText = document.createElement("p");
	ttipText.innerHTML = infoText;
	tooltip.appendChild(ttipText);

	ttipImage = document.createElement("img");
	ttipImage.src = "/images/backgrounds/tooltip_corner.gif";
	ttipImage.width = "36";
	ttipImage.height = "22";
	ttipImage.alt = "";
	tooltip.appendChild(ttipImage);

	document.body.appendChild(tooltip);


	tooltipShow(tooltip.id,this);
	//this.style.textDecoration = "none";

}

function hideErfacat(id) {	
  tooltipExit('inner'+id);
}

function showErfaInfo(id, infoText) {

	ttipLink = document.getElementById(id);

	if(ttipLink == null) {
		return false;
	}


	ttipLink.style.textDecoration = "underline";
	ttipLink.style.cursor = "pointer";

	tooltip = document.createElement("div");
	tooltip.className = "tooltip";
	tooltip.style.display = "none";
	tooltip.id = 'inner'+id;
	tooltip.style.wdith = "300px";

	ttipHeadline = document.createElement("h4");
	ttipHeadline.innerHTML = "Gruppenname";
	tooltip.appendChild(ttipHeadline);

	ttipText = document.createElement("p");
	ttipText.innerHTML = infoText;
	tooltip.appendChild(ttipText);

	ttipImage = document.createElement("img");
	ttipImage.src = "/images/backgrounds/tooltip_corner.gif";
	ttipImage.width = "36";
	ttipImage.height = "22";
	ttipImage.alt = "";
	tooltip.appendChild(ttipImage);

	document.body.appendChild(tooltip);


	tooltipShow(tooltip.id,this); this.style.textDecoration = "none";

}

function hideErfaInfo(id) {	
  tooltipExit('inner'+id);
}

function dbFieldsTip() {

	ttipLink = document.getElementById("dbFieldsTip");

	if(ttipLink == null) {
		return false;
	}


	ttipLink.style.textDecoration = "underline";
	ttipLink.style.cursor = "pointer";

	tooltip = document.createElement("div");
	tooltip.className = "tooltip";
	tooltip.style.display = "none";
	tooltip.id = 'inner'+id;
	tooltip.style.width = "300px";

	ttipHeadline = document.createElement("h4");
	ttipHeadline.innerHTML = "Detailsuche";
	tooltip.appendChild(ttipHeadline);

	ttipText = document.createElement("p");
	ttipText.innerHTML = "Ermöglicht eine Suche gezielt in einzelnen oder kombiniert in mehreren Datenbankfeldern.<br />Zusätzlich können Dokumente nach vordefinierten Themen gesucht werden, hierzu klicken Sie einfach das Wort „Themengebiet“ an und wählen anschließend ein oder mehrere Themen durch anklicken des Begriffes aus, diese werden automatisch in das Suchfeld übertragen.";
	tooltip.appendChild(ttipText);

	ttipImage = document.createElement("img");
	ttipImage.src = "/images/backgrounds/tooltip_corner.gif";
	ttipImage.width = "36";
	ttipImage.height = "22";
	ttipImage.alt = "";
	tooltip.appendChild(ttipImage);

	document.body.appendChild(tooltip);


	ttipLink.onmouseover = function() {tooltipShow('dbFieldsTipDiv',this); this.style.textDecoration = "none";};
	ttipLink.onmouseout = function() {tooltipExit('dbFieldsTipDiv'); this.style.textDecoration = "underline";};

}
addEvent(window,'load',dbFieldsTip);

function uncheck(id) {

	ele = document.getElementById(id);

	if(ele != null) {
		ele.checked = false;
	}
}

function onCheckAll(baseField) {

	form = baseField.form;

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

		if(form.elements[i].type == "checkbox") {
			if(form.elements[i] != baseField) {
				form.elements[i].checked = false;
			}
		}
	}
}

function beitragBerechnen() {
	gehalt = document.beitragsform.gehalt.value;

	if(gehalt <= 2500000) {
		beitrag = 500;
	}
	if(gehalt > 2500000 && gehalt <= 4000000) {
		beitrag = 500 + ((gehalt - 2500000) * 0.01 / 100);
	}
	if(gehalt > 4000000 && gehalt <= 7700000) {
		beitrag = 650 + ((gehalt - 4000000) * 0.0076 / 100);
	}
	if(gehalt > 7700000 && gehalt <= 13000000) {
		beitrag = 931 + ((gehalt - 7700000) * 0.0041 / 100);
	}
	if(gehalt > 13000000 && gehalt <= 20000000) {
		beitrag = 1148 + ((gehalt - 13000000) * 0.0029 / 100);
	}
	if(gehalt > 20000000 && gehalt <= 36000000) {
		beitrag = 1351 + ((gehalt - 20000000) * 0.0024 / 100);
	}
	if(gehalt > 36000000 && gehalt <= 56000000) {
		beitrag = 1735 + ((gehalt - 36000000) * 0.0018 / 100);
	}
	if(gehalt > 56000000 && gehalt <= 77000000) {
		beitrag = 2095 + ((gehalt - 56000000) * 0.0014 / 100);
	}
	if(gehalt > 77000000 && gehalt <= 100000000) {
		beitrag = 2389 + ((gehalt - 77000000) * 0.0012 / 100);
	}
	if(gehalt > 100000000 && gehalt <= 140000000) {
		beitrag = 2665 + ((gehalt - 100000000) * 0.0008 / 100);
	}
	if(gehalt > 140000000 && gehalt <= 510000000) {
		beitrag = 2985 + ((gehalt - 140000000) * 0.0005 / 100);
	}
	if(gehalt > 510000000 && gehalt <= 4100000000) {
		beitrag = 4835 + ((gehalt - 510000000) * 0.00021 / 100);
	}
	if(gehalt > 4100000000) {
		beitrag = 12374 + ((gehalt - 4100000000) * 0.00012 / 100);
	}

	document.beitragsform.beitrag.value = Math.round(beitrag);
}

function openPrintWindow(url) {
	
	printwindow = window.open(url, "printwindow", "width=700,height=500,scrollbars=yes");
	printwindow.focus();
	
}


function checkTopCheckboxAkedemieSearch(checked) {
	if (!checked) jQuery('input[name=region_all]').attr('checked', false);
	else {
		if (jQuery('input[name^=regions]:not(input[name^=regions]:checked)').get().length == 0) {
			jQuery('input[name=region_all]').attr('checked', true);
		}
	}
}

function checkSubCheckboxesAkedemieSearch(checked) {
	if (checked) jQuery('input[name^=regions]').attr('checked', true);
	else jQuery('input[name^=regions]').attr('checked', false);
} 