// Wird in jeder Seite im Body-Onload aufgerufen.
// Falls es in der Seite eine JS-Funktion "init()" gibt, wird diese aufgerufen
function checkForInit() {
	if (window.init && typeof(window.init) == "function") {
		init();
	}	
}

// Beschriftung steht in Suchbox - bei onFocus ausblenden
function clearSearchbox(field) {
	if (!field || (field.value != "Mitarbeiter-Name" && field.value != "Suchbegriff")) { return; }
	field.oldCaption = field.value;
	field.value = "";
}

// Beschriftung steht in Suchbox - bei onBlur einblenden
function resetSearchbox(field) {
	if (!field || !field.oldCaption || field.value != "") { return; }
	field.value = field.oldCaption;
}
// delete multiple entries (array)
function dme(a1) {
	var a2 = new Array();
	var a3 = new Array();
	for (var i = 0; i < a1.length; i++) {
 		if (typeof(a2[a1[i]]) == "undefined") {
			a2[a1[i]] = true;
			a3[a3.length] = a1[i];
		}
	}
	var a4 = new Array();
	for (var i = 0; i < a3.length; i++) {
		a4[a4.length] = a3[i];
	}
	return a4;
}  

// auf Klick einer Option den Weiterpfeil in-/aktiv schalten
function checkInput(formName) { 
	document.getElementById('stress_next').innerHTML = '<a href="#"  onmouseover="showGlossary(\'Um auf die n&auml;chste Seite zu gelangen, ist es erforderlich, alle Punkte auszuf&uuml;llen.\', event)" onmouseout="hideGlossary()"><img src="fileadmin/templates/img/spacer.gif" id="stress_weiter_hidden" /></a>';
	if ( checkReady(formName) ) {
		document.getElementById('stress_next').innerHTML = '<input type="image" src="fileadmin/templates/img/spacer.gif" id="stress_weiter_input" alt="Weiter" class="image" onmouseover="showRO(this);" onmouseout="hideRO(this);"  />';
	//	document.getElementById('stress_hinweis').innerHTML = '&nbsp;';
	}
}
// check, ob alle form inputs mit den namen tx_nutri_stress.. ausgewaehlt
function checkReady(formName) { 
	var questions = new Array();
	for (var i=0; i < document.stresstest.length; i++) { // alle formnamen einlesen
		questions.push(document.stresstest.elements[i].name);
	}	
	questions = dme(questions); // doppelte formnamen loeschen
	for (var i = 0; i < questions.length; i++) {// alle formnamen durchgehen
		if (questions[i].indexOf(formName) > -1 ) { // check, ob formelement relevant
			var temp_ready = false;
			for (var j = 0; j < document.getElementsByName(questions[i]).length; j++) { // alle formelemente eines namens durchgehen
				if (document.getElementsByName(questions[i])[j].checked) { temp_ready = true; }
			}
			if (!temp_ready) {
				return false;
			}
		}
	}
	return true;
}
function showGlossary(text, mouse) {
	// Glossar-Bubble befüllen	
	if (!document.getElementById('glossary_bubble') || !document.getElementById('glossary_bubble').getElementsByTagName) { return; }
	tmpContentDiv = document.getElementById('glossary_content');
	if (!tmpContentDiv) { return; }
	tmpContentDiv.innerHTML = text;
	// Blase positionieren
	if (!mouse) mouse = window.event;
	document.getElementById('glossary_bubble').style.left = mouse.clientX + document.body.scrollLeft + document.documentElement.scrollLeft + 10 + "px";
	document.getElementById('glossary_bubble').style.top = mouse.clientY + document.body.scrollTop + document.documentElement.scrollTop - 100 + "px";
	if(BrowserDetect.browser == "Safari" && BrowserDetect.OS == "Mac") {
		document.getElementById('glossary_bubble').style.top = mouse.clientY +  "px";
	} else if (BrowserDetect.browser == "Opera") {
		document.getElementById('glossary_bubble').style.top = mouse.clientY + window.pageYOffset + "px";
	}
}
function hideGlossary() {
	tmpBubble = document.getElementById('glossary_bubble');
	if (!tmpBubble) { return; }
	tmpBubble.style.top = "-1000em";
	tmpBubble.style.left = "-1000em";
}

function showRO(obj) {
	if(obj.currentStyle) { // nur fuer ie6
	 	obj.style.backgroundPosition = "bottom";
	}
}
function hideRO(obj) {
	if(obj.currentStyle) { // nur fuer ie6
	 	obj.style.backgroundPosition = "top";
	}
}
// Browser Detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
