/** 
 *	Version $Id: general.js,v 1.11 2005/04/22 09:40:40 cvs Exp $
 */

/* browserdetection */
var uA = navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var is_win = (uA.indexOf("win")!=-1) || (uA.indexOf("16bit")!=-1); 	//os
var is_mac = (uA.indexOf("mac")!=-1);
var is_lynx = uA.indexOf("lynx") != -1; 							//ua
var is_opera = (uA.indexOf("opera") != -1);
var is_opera7up = (is_opera && (is_major >= 7)) || (uA.indexOf("opera 7") != -1);
var is_safari = (uA.indexOf("safari") != -1);
var iePos = appVer.indexOf('msie');								//ie
if (iePos != -1) {
	is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
	is_major = parseInt(is_minor);
}
var is_ie = (iePos!=-1);
var is_ie4down = (is_ie && is_minor < 5);
var is_ie5up = (is_ie && is_minor >= 5);
var is_ie6up = (is_ie && is_minor >= 6);
var nav6Pos = uA.indexOf('netscape6');								//nav
if (nav6Pos !=-1) {
	is_minor = parseFloat(uA.substring(nav6Pos+10))
	is_major = parseInt(is_minor)
}
var is_nav = ((uA.indexOf('mozilla')!=-1) 
				&& (uA.indexOf('spoofer')==-1) && (uA.indexOf('compatible') == -1) 
				&& (uA.indexOf('webtv')==-1) && !is_opera && !is_safari )
var is_nav5up = (is_nav && is_minor >= 5);

/* janus */
function checkVersion() {
	var loc = document.location;
	if (loc.search.indexOf('check=no') != -1 || loc.hostname.indexOf('search.') != -1 || loc.pathname.indexOf('/forms') != -1 || loc.pathname.indexOf('/app') != -1) return;
	//version
	var isHiCurrent = (loc.search.indexOf('tpl=html3') == -1) ? 1 : 0;
	var	hiByScreen = (screen.availWidth >= 1024) ? 1 : 0;
	var hiByUa = ((is_nav && !is_nav5up) || (is_ie && is_mac) || (is_opera && !is_opera7up) || is_ie4down) ? 0 : 1
	if ((isHiCurrent && !hiByScreen) || (isHiCurrent && !hiByUa)) {
		gotoLo();
		return;
	}
	if (!isHiCurrent && hiByScreen && hiByUa) {
		gotoHi();
		return
	}
	return
}
checkVersion();

function gotoHi() {
	currentURL = document.location.href;
	newURL = currentURL;
	if(currentURL.indexOf('&tpl=html3') > -1) { 
		newURL = currentURL.replace(/&tpl=html3/,""); 
	}
	if(currentURL.indexOf('?tpl=html3') > -1) { 
		newURL = currentURL.replace(/tpl=html3/,"");
	}
	doSwitch(newURL);
}
function gotoLo() {
	currentURL = document.location;
	thisPath 	= currentURL.pathname
	thisHash 	= currentURL.hash;
	thisSearch	= currentURL.search;
	newURL = thisPath;
	if(thisSearch != "") {
		newURL += thisSearch;
		newURL += '&tpl=html3';
	} else {
		newURL += '?tpl=html3';
	}
	if(thisHash != "") newURL += thisHash;
	doSwitch(newURL);
}
function doSwitch(newURL) {
	//if ((is_nav || is_mac || is_opera) && !is_opera7up) stop();
	setTimeout("document.location = newURL",1000);
}

/**	JS-FRAMEWORK #####
 * 	for including and registering external scripts
 * 	v1.0 sk, integration ch
 */

/** will be set to 1 if loading complete: */
	DPAG_pageLoaded = 0;

/**	ONLOAD */
	window.onload=globalOnLoad;
	initOnLoadAr = new Array();
	
	// +++++ registering of general onload funcs, add here if needed +++++
	addOnLoad("anchorToggleFaq()");
	addOnLoad("initPrintpreview()");
	
	function globalOnLoad() {
		localInit();
		DPAG_pageLoaded = 1; //complete
	}
	//add User-Defined func
	function addOnLoad(func) {
		initOnLoadAr[initOnLoadAr.length] = func;
	}
	//Load User-Defined stuff
	function localInit() {
		for(var i=0; i < initOnLoadAr.length; i++) {
			eval(initOnLoadAr[i]);
		}
	}
/**	ONUNLOAD */
	window.onunload=globalOnUnLoad;
	initOnUnLoadAr = new Array();
	
	// +++++ registering of general onunload funcs, add here if needed +++++
	addOnUnLoad("popupclose()");

	function globalOnUnLoad() {
		localUnload();
	}
	//add User-Defined func
	function addOnUnLoad(func) {
		initOnUnLoadAr[initOnUnLoadAr.length] = func;
	}
	//Load User-Defined stuff
	function localUnload() {
		for(IOULAi=0;IOULAi<initOnUnLoadAr.length;IOULAi++) {
			eval(initOnUnLoadAr[IOULAi]);
		}
	}
// end JS-FRAMEWORK #####

/* partnerbox should always be below 600px from top */
addOnLoad("watchPartnerbox()");
function watchPartnerbox() {
	box = document.getElementById("partnerbox");
	if (!box) return;
	if (getWinH() < 600) box.style.top = "600px";
	else box.style.top = "auto";
	setTimeout("watchPartnerbox()",500);
}
//menulogo should not be above the menu
addOnLoad("watchMenulogo()");
function watchMenulogo(minTop) {
	var logo = document.getElementById("menulogo");
	if (!logo) return;
	var menu = document.getElementById("menu");
	if (menu) {
		minTop = 83 + menu.offsetHeight + 42
		if (getWinH() < minTop) logo.style.top = minTop + "px";
		else logo.style.top = "auto";
		setTimeout("watchMenulogo(" + minTop + ")",1000);
	}
}
//window
function getWinW() { 
	if (is_ie6up) return getInnerSize().width;
	return (is_ie) ? document.body.offsetWidth : window.innerWidth;
	}
function getWinH() { 
	if (is_ie6up) return getInnerSize().height;
	return (is_ie) ? document.body.offsetHeight : window.innerHeight;
	}
	//ie6:
function getInnerSize() {
   var el = window.document.compatMode == "CSS1Compat" ?
               window.document.documentElement : window.top.document.body;
   return {
      width:   el.clientWidth,
      height:  el.clientHeight
   };
}

/**	disables all links in printpreview except the printbutton itself */
function initPrintpreview() {
	if (location.search.indexOf('print=true') == -1 && location.hash.indexOf('print=true') == -1) return;
	if (document.getElementsByTagName) {
		var atag = document.getElementsByTagName('a');
		for (i=0; i < atag.length; i++) {
			if (atag[i].id != 'printfooter' && atag[i].id != 'printheaderbutton') { 
				atag[i].removeAttribute("href","false");
				atag[i].onmouseover = '';
				atag[i].onmouseout = '';
			}
		}
	}
}
/**	if anchorlink on a question in FAQ open this question v1.0 02-09-19 ch */
function anchorToggleFaq() {
	if (window.location.hash.indexOf('question') == -1) return;
	else {
		var anker = window.location.hash;
		var questionid = anker.substring( 1, anker.length);
			//find onclick call of question and call it
			var questionEle = document.getElementById(questionid);
			var innerEle = questionEle.getElementsByTagName('a');
			var innerOnclick = innerEle[0].getAttribute('onclick');
			if (is_ie) innerOnclick();	//ie gives anonymous function
			else { 						//n6.x gives string
				if (innerOnclick.indexOf('return') > -1) innerOnclick = innerOnclick.replace('return', '')
				eval(innerOnclick);
			}
	}
}
/** opens or closes a question v1.0 ch 02-08-13 */
function toggleFAQ(faq) {
	var e = document.getElementById(faq);
	if (e.style.display == '') //test in ie, if not set -> set it!
		e.style.display = "none";
	if (e.style.display == "none") 
		e.style.display = "block";
	else 
		e.style.display = "none";
	return false;
}

/** calls the printversion of the page */
function printpopup(url) {
    if (url == null) { url = location.href; }
	if (url.indexOf('?') > -1) { 
		return popup(url + '&amp;print=true', 588, 500, -1, -1, false, 'toolbar=1,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1');
	}
	else {
		return popup(url + '?print=true', 588, 500, -1, -1, false, 'toolbar=1,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1');
	}
}

/** escapes alt and title parameter values or href value because xsl does not escape these */
function escapeZoomURL(url) {
	if (url.indexOf('title=') > -1 || url.indexOf('alt=') > -1) {
		var part = url.split('&');
		url = '';
		for (i=0; i < part.length; i++) {
			//first is href + first param!
			var param = part[i].split('=');
			url = url + param[0] + '=' + escape(param[1]);
			if (i < part.length) url += '&';
		}
	}
	if (url.indexOf('disclaimer.jsp') > -1) {
		var part = url.split('href=');
		url = part[0] + 'href=' + escape(part[1]);
	}
	return url;
}

/**	popup functs */
var winpop;
var img;
var w;
function popup(url, winBreite, winHoehe, posLinks, posTop, linkfield, options) {	
	var screenBreite = (screen.availWidth) ? screen.availWidth : 800;
	var screenHoehe = (screen.availHeight) ? screen.availHeight : 600;
	if ((winBreite != null) && (winBreite <= 100)) winBreite = 100;
	if ((winBreite == null) || (winBreite < 100) || (winBreite > screenBreite)) winBreite = screenBreite - 100;
	if ((winHoehe != null) && (winHoehe <= 100)) winHoehe = 100;
	if ((winHoehe == null) || (winHoehe < 100) || (winHoehe > screenHoehe-120)) winHoehe = screenHoehe - 120;
	if ( (posLinks == null) || (posLinks < 0) || (screenBreite < posLinks + winBreite) ) posLinks = (screenBreite - winBreite) / 2; // mitte des bildschirms
	if ( (posTop == null) || (posTop < 0) || (screenHoehe < posTop + winHoehe) ) posTop = (screenHoehe - winHoehe) / 2 - 20; // mitte des bildschirms
	if (posTop < 0) posTop = 0;
	if (winpop && (winpop.closed != true)) winpop.close();
	if (!options) options = 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1';
	winpop = window.open(escapeZoomURL(url),'w',options + ',width=' +winBreite+ ',height=' +winHoehe+ ',left=' +posLinks+ ',top=' +posTop);
	if (linkfield) winpop.w = linkfield;
	winpop.focus();
	return false;
}
//window.onunload = popupclose; //s.o.
function popupclose() {
	if (winpop && (winpop.closed == false)) winpop.close();
}


//alt?
function popup2(url,winBreite,winHoehe, posLinks, posTop,bild,linkname) 
{	
	if ((bild == null || bild == '') && (url == '/popup.html')) { alert("kein Bild ausgewählt"); }
	else {
	if (winpop && (winpop.closed != true)) winpop.close();
	winpop = window.open(url,'w','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=0,width='+winBreite+',height='+winHoehe+',left='+posLinks+',top='+posTop);
	winpop.focus();
	winpop.img = bild;
	winpop.w = bild;
	winpop.linkname = linkname;
	return false;
	}
}


// editor functions

function format_text(s,c) {
  br ='';
  var str = document.selection.createRange().text;
  document.forms[0].elements(c).focus();
  var sel = document.selection.createRange();
  if (s=='item') br = "\n";
  if (s == 'br') sel.text = "<" + s + "/>";
  else if (s == 'nobr') sel.text = "&#160;";
  else if (s == 'list') sel.text = "\n<" + s + ">\n</" + s + ">";
  else if (str != '') sel.text = br + "<" + s + ">" + str + "</" + s + ">";
  else alert('keine Auswahl!');
  return;
}

function requestLink(ele) {
	popup('/pinc?cmd=linkSelect',600,450,-1,-1,ele);
	}

var str = '';
function insert_link(c) {
	str = document.selection.createRange().text;
	document.forms[0].elements(c).focus();
	if (str == '') {
		alert('keine Auswahl!');
	}
	else {
		document.forms[0].elements['linkdummy'].value = '';
		document.forms[0].elements['linkdummytarget'].value = '';
		requestLink('linkdummy');
	}
	return;
}
//called from popup!
function do_insert_link() {
	var newVal = document.forms[0].elements['linkdummy'].value;
	var newTarget  = document.forms[0].elements['linkdummytarget'].value;
	var sel = document.selection.createRange();
	if (newTarget.length > 0) sel.text = '<link href="' + newVal + '" target="' + newTarget + '">' + str + '</link>';
	else sel.text = '<link href="' + newVal + '">' + str + '</link>';  	
}

function insert_image(c) {
	document.forms[0].text.focus();
		var my_link = prompt("URL angeben:","#");
		if ((my_link != null) && (my_link != '#')) {
			var sel = document.selection.createRange();
			sel.text = '<image align="' + c + '">' + my_link + '</image>';  	
		}
		else { 
			alert('keine Url angegeben!');
			insert_link()
		}
		return;
}



//video functions


function getLayerRef(name) 

{
	if (document.layers) {
		return false;
	}
	
	else if (document.getElementById) return document.getElementById(name);
	else if (document.all) return document.all[name];
}
 
function writeToLayer(nest, name, content) {
	getLayerRef(name).innerHTML = content;

}
 
function prepareVideo (VideoLocation, id) {
 if(VideoLocation != 'auswahl') { 
  if (VideoLocation.indexOf('.zip') > 1) {
   document.location = VideoLocation;
  } else {
  	VideoArray = VideoLocation.split('|');
	showVideo(VideoArray[0], id, VideoArray[1], VideoArray[2]);
  }
 }
}

function showVideo(url, divId, breit, hoch) {
 var html = '';
 html += '<table border="0" cellpadding="0" cellspacing="0" width="100%">';
 html += '<tr>';
 html += ' <td>';
 html += '  <OBJECT ID="'+divId+'" name="'+divId+'" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" HEIGHT="'+ hoch +'" WIDTH="'+ breit +'">';
 html += '   <PARAM NAME="controls" VALUE="ImageWindow">';
 html += '   <PARAM NAME="console" VALUE="Clip'+divId+'">';
 html += '   <PARAM NAME="autostart" VALUE="false">';
 html += '   <PARAM NAME="src" VALUE="'+url+'">';
 html += '   <EMBED SRC="'+url+'" type="audio/x-pn-realaudio-plugin" CONSOLE="'+divId+'" CONTROLS="ImageWindow" HEIGHT="'+ hoch +'" WIDTH="'+ breit +'" AUTOSTART="false"></embed>';
 html += '  </OBJECT>';
 html += ' </td>';
 html += '</tr>';
 html += '<tr>';
 html += ' <td>';
 html += '  <OBJECT ID="'+divId+'" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" HEIGHT="30" width="'+ breit +'">';
 html += '   <PARAM NAME="controls" VALUE="ControlPanel">';
 html += '   <PARAM NAME="console" VALUE="Clip'+divId+'">';
 html += '   <PARAM NAME="autostart" VALUE="true">';
 html += '   <EMBED type="audio/x-pn-realaudio-plugin" CONSOLE="'+divId+'" CONTROLS="ControlPanel" HEIGHT="30" width="'+ breit +'" AUTOSTART="true"></embed>';
 html += '  </OBJECT>';
 html += ' </td>';
 html += '</tr>';
 html += '</table>';
 writeToLayer('maincontent', divId, html);
}

select_popups = new Array();
function handleSelectPopups(thisForm,nodeID) {
	thisSelectIndex = eval("thisForm.elements['href'].selectedIndex");
	
	popupOpened = false;
	for(i=0; i < select_popups.length; i++) {
		if(select_popups[i].formid == nodeID && select_popups[i].id == thisSelectIndex) {
			popup(select_popups[i].href,select_popups[i].width,select_popups[i].height);
			popupOpened = true;
			break;
		}
	}
	
	if(popupOpened) {
		return false;
	} else {
		return true;
	}
}

