var errors=0; // Javascript-Fehler unterdruecken (0) oder anzeigen (1)
onerror=stopError;
function stopError(){if(!errors)return true;}

var loginBar = 1;

function isIE6() {
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var brVer = navigator.userAgent;
	  	var brVerId = brVer.indexOf('MSIE 6');
	   	if( brVerId < 0 ) {return 0;} 
	   	return 1;
	} else {
	  return 0;
	}
}

function hasCookies() {
	var expires = new Date();
	var period = expires.getTime() + (60 * 1000);
	expires.setTime(period);
	document.cookie = "Cookie_test=active; expires=" + expires.toGMTString();

	var test = document.cookie;
	if ( test.search('Cookie_test=active') >= 0 ) {
		return true;
	} else {
		return false;
	}
}

/* only IE6 */
window.onresize = setSize;
function setSize(mode) {
	if( isIE6() == 0 ) { return; }
	mode = parseInt(mode);

	/* special mode 4 for KIS integration */
	var padding = 2;
	var headerHeight = (mode == 4) ? 0:40;
	var headerLeftWidth = 300;
	var menueHeight = (mode == 4) ? 25:46;
	var footerHeight = 18;
	var loginBarWidth = (mode == 4) ? 0:147;
	if (!loginBar) { loginBarWidth = (mode == 4) ? 0:14; }
	var spaceMenuLoginBar = (mode == 4) ? 0:8;
	var sideBarWidth = 30;
	var leftSideBar = document.getElementById('sidebarLeft') ? 1:0;
	var rightSideBar = document.getElementById('sidebarRight') ? 1:0;
	
	var helpContentHeight = 370;
	var helpContentWidth = 392;

	var fullHeight, fullWidth;
	fullWidth = document.body.offsetWidth ? document.body.offsetWidth : window.innerWidth;
	fullHeight = document.body.offsetHeight ? document.body.offsetHeight : window.innerHeight;
	
	var newHeight, newWidth, newCenterWidth;
	
	if( mode != 3 ) {
		newHeight = fullHeight - padding - headerHeight - menueHeight - footerHeight - padding;
		newWidth = fullWidth - padding - loginBarWidth - spaceMenuLoginBar;
		document.getElementById('headerCenter').style.width = fullWidth - headerLeftWidth - headerLeftWidth + 'px';
		document.getElementById('subMenu').style.width = newWidth - 5 + 'px';
		document.getElementById('footer').style.width = newWidth - 5 + 'px';
		document.getElementById('content').style.width = newWidth - 3 + 'px';
	
		document.getElementById('content').style.height = newHeight + 2 + 'px';
		document.getElementById('contentCenter').style.height = newHeight + 'px';
		document.getElementById('helpContent').style.height = helpContentHeight + 'px';
		document.getElementById('helpContent').style.width = helpContentWidth + 'px';
		
		newCenterWidth = newWidth - 6;
		if( leftSideBar ) {
			document.getElementById('sidebarLeft').style.height = newHeight - 10 + 'px';
			document.getElementById('sidebarLeftContent').style.height = newHeight - 10 + 'px';
			newCenterWidth -= sideBarWidth;
		}
		if( rightSideBar ) {
			document.getElementById('sidebarRight').style.height = newHeight - 10 + 'px';
			document.getElementById('sidebarRightContent').style.height = newHeight - 10 + 'px';
			newCenterWidth -= sideBarWidth;
		}
		document.getElementById('contentCenter').style.width = newCenterWidth + 'px';
	
		if( document.getElementById('loginBarLeft') ) {		
			newHeight = fullHeight - padding - headerHeight - padding;
			document.getElementById('loginBarLeft').style.height = newHeight + 'px';
			newHeight = newHeight - 2;
			document.getElementById('loginBarRight').style.height = newHeight + 'px';
		}
	} else {
		try {
			newHeight = fullHeight;
			newWidth = fullWidth;
			
			document.getElementById('contentCenter').style.height = newHeight + 'px';
			
			if( leftSideBar ) {
				document.getElementById('sidebarLeft').style.height = newHeight - 10 + 'px';
				document.getElementById('sidebarLeftContent').style.height = newHeight - 10 + 'px';
				newWidth -= sideBarWidth;
			}
			if( rightSideBar ) {
				document.getElementById('sidebarRight').style.height = newHeight - 10 + 'px';
				document.getElementById('sidebarRightContent').style.height = newHeight - 10 + 'px';
				newWidth -= sideBarWidth;
			}
			document.getElementById('contentCenter').style.width = newWidth + 'px';
		} catch(e) {
			return;
		}
	}
}

function toggleLoginBar() {
	loginBar = 1 - loginBar;
	
	var paddingRight = 2;
	var sideBarWidth = 30;
	var loginBarRightWidth = 133;
	var loginBarLeftWidth = 13;
	var spaceMenuLoginBar = 8;
	var leftSideBar = document.getElementById('sidebarLeft') ? 1:0;
	var rightSideBar = document.getElementById('sidebarRight') ? 1:0;
	
	var newRight; 	
	var fullWidth, newWidth, newCenterWidth;
	fullWidth = document.body.offsetWidth ? document.body.offsetWidth : window.innerWidth;
	
	if( loginBar ) {
		document.getElementById("loginBarLeft").style.right = loginBarRightWidth + paddingRight + 'px'; 		
		document.getElementById("loginBarRight").style.display = "inline";
		document.getElementById("loginBarLeftIcon").src = loginBarIcons[1];
		
		newRight = loginBarRightWidth + paddingRight + loginBarLeftWidth + spaceMenuLoginBar;
		document.getElementById("mainMenuRight").style.right = newRight + 'px';
		document.getElementById("subMenu").style.right = newRight + 'px';
		document.getElementById("content").style.right = newRight + 'px';
		document.getElementById("footer").style.right = newRight + 'px';
		document.getElementById("help").style.right = newRight + 2 + 'px';

		/* only IE6 */
		if( isIE6() != 0 ) {
			newWidth = fullWidth - paddingRight - loginBarRightWidth - loginBarLeftWidth - spaceMenuLoginBar;
			document.getElementById("subMenu").style.width = newWidth - 6 + 'px';
			document.getElementById("content").style.width = newWidth - 4 + 'px';
			newCenterWidth = newWidth - 8;
			if( leftSideBar ) { newCenterWidth -= sideBarWidth; }
			if( rightSideBar ) { newCenterWidth -= sideBarWidth; }		
			document.getElementById("contentCenter").style.width = newCenterWidth + 'px';
			document.getElementById("footer").style.width = newWidth - 6 + 'px';
		}
	} else {
		document.getElementById("loginBarRight").style.display = "none";	
		document.getElementById("loginBarLeft").style.right = paddingRight + 'px';  	
		document.getElementById("loginBarLeftIcon").src = loginBarIcons[0];
		
		newRight = paddingRight + loginBarLeftWidth + spaceMenuLoginBar;
		document.getElementById("mainMenuRight").style.right = newRight + 'px';
		document.getElementById("subMenu").style.right = newRight + 'px';
		document.getElementById("content").style.right = newRight + 'px';
		document.getElementById("footer").style.right = newRight + 'px';
		document.getElementById("help").style.right = newRight + 2 + 'px';

		/* only IE6 */
		if( isIE6() != 0 ) {
			newWidth = fullWidth - paddingRight - loginBarLeftWidth - spaceMenuLoginBar;
			document.getElementById("subMenu").style.width = newWidth - 6 + 'px';
			document.getElementById("content").style.width = newWidth - 4 + 'px';
			newCenterWidth = newWidth - 8;
			if( leftSideBar ) { newCenterWidth -= sideBarWidth; }
			if( rightSideBar ) { newCenterWidth -= sideBarWidth; }		
			document.getElementById("contentCenter").style.width = newCenterWidth + 'px';
			document.getElementById("footer").style.width = newWidth - 6 + 'px';
		}
	}

	var zeit = new Date();
	xmlhttp = window.XMLHttpRequest? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open("POST", "/common/cgi-bin/ajax_toggleloginbar.py/ajax_toggleloginbar");
	var body = "zeit=" + zeit.getTime();
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", body.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(body);
}

/* ######################### extra functions ##################################### */
function help(){
  el=document.getElementById('help');
  if( isIE6() ) { el.style.top = "108px"; }
  el.style.display=(el.style.display=='block')?'none':'block';
}
function hideHelp(){
  document.getElementById('help').style.display='none';
}

function changeLang(newLang) {
	var newLangKey = "";
	if( newLang.length == 4 ) {
		newLangKey = newLang;
	} else {
		newLangKey = document.submenuValues.lang_key.value;
	}

	var curHost = window.location.hostname;
	var newHost = curHost;
	var ptIndexFirst = curHost.indexOf(".");
	var ptIndexLast  = curHost.lastIndexOf(".");
	
	var firstPart = "";
	var lastPart = "";
	if( ptIndexFirst != ptIndexLast ) {
		firstPart = curHost.substring(0, ptIndexFirst);
		lastPart  = curHost.substring(ptIndexFirst);
	} else {
		lastPart = curHost;
	}
	
	var findEn = firstPart.indexOf("en");
	
	// switch to english
	if( findEn < 0 && newLangKey == "0001" ) {
		if( firstPart == "" ) {
			newHost = "en." + lastPart;
		} else {
			if( firstPart == "test" ) {
				newHost = "en" + firstPart + lastPart;
			} else {
				newHost = "en" + lastPart;
			}
		}
	}
	
	// switch to german
	if( findEn >= 0 && newLangKey == "0000" ) {
		firstPart = firstPart.substring(2);
		if( firstPart == "" ) {
			newHost = "www" + lastPart;
		} else {
			newHost = firstPart + lastPart;
		} 
	}
		
	if( newHost != curHost ) {
		window.location.hostname = newHost;
	}	
}

function setURL() {
	if( hasCookies() == false ) {
		alert(myLang.mainCookies); // "Sie müssen Cookies erlauben um sich einzuloggen."
		return false;
	}
	
	var zeit = new Date();
	var lang_key = document.submenuValues.lang_key.value;
	
	if( document.login_data.login_store.checked == true ) {
		var login_store = 1;
	} else {
		var login_store = 0;
	} 

	document.getElementById('login_form').current_url.value=document.location.href;
	xmlhttp = window.XMLHttpRequest? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.onreadystatechange = finishFunctionLogin;
	xmlhttp.open("POST", "/common/cgi-bin/ajax_checklogin.py/ajax_checklogin");
	var body = "password=" + encodeURIComponent( document.login_data.password.value )
		+ "&login=" + encodeURIComponent( document.login_data.username.value )
		+ "&lang_key=" + lang_key
		+ "&login_store=" + login_store
		+ "&zeit=" + zeit.getTime();
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", body.length);
  xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(body);
	return false;
}
function finishFunctionLogin() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
    var error = parseInt(xmlhttp.responseText);
		if( error == 0 ) {
			document.getElementById('login_wrong').style.display = "none";
			document.getElementById('password_wrong').style.display = "none";

			myURL = document.location.href;
			registerPos = myURL.search(/register/);			
			activationPos = myURL.search(/activation/);

			langPos0000 = myURL.search(/\/de\//);
			langPos0001 = myURL.search(/\/en\//);
			if( registerPos >= 0 || activationPos >= 0 || langPos0000 >= 0 || langPos0001 >= 0 ) {
				if( registerPos >= 0 || activationPos >= 0 ) {
					document.location.href = "/"; 
				} else {
					myURL = myURL.replace(/\/de\//, "/");
					myURL = myURL.replace(/\/en\//, "/");
					document.location.href = myURL;
				}
			} else {
				document.location.reload();
			}
		}
		if( error == 1 || error == 3) {
			document.getElementById('login_wrong').style.display = "block";
		} else {
			document.getElementById('login_wrong').style.display = "none";
		}
		if( error == 2 || error == 3) {
			document.getElementById('password_wrong').style.display = "block";
		} else {
			document.getElementById('password_wrong').style.display = "none";
		}
	}
}
function changeApp(){
	var myURL = document.submenuValues.application.value;
	document.location.href = myURL;
}
function writeMainEmail(emailType) {
	var zeit = new Date();
	var entryURL = "/common/cgi-bin/emailing.py/emailing?"
			+ "email_type=" + emailType
			+ "&zeit=" + zeit.getTime();
	
	suchFenster = window.open( entryURL, "mainEmail",
		"width=600, height=485, top=100, left=100, menubar=no, locationbar=no, status=no, resizable=yes" );
	suchFenster.focus();
}

/* ############################### sidebars ####################################### */
var menue = false;
var timer;
var tabNode = '';
var tabSide = '';
var oldTop, oldMid, oldBottom;

function resetTabs(){
	for(var i = 1; i < 6; i++) {
		if( document.getElementById('sidebarLeftTab'+i+'Content') ) {
			document.getElementById('sidebarLeftTab'+i+'Content').style.display = "none";
		}
		if( document.getElementById('sidebarRightTab'+i+'Content') ) {
			document.getElementById('sidebarRightTab'+i+'Content').style.display = "none";
		}
	}
	if( !(tabNode=='') ) {
		tabImgSide = (tabSide=='Right') ? '-right':'';
		document.getElementById(tabNode+'Top').src = '/common/images/tab' + tabImgSide + '_top.gif';
		document.getElementById(tabNode+'Middle').style.background = 'url(/common/images/tab' + tabImgSide + '_mid.gif)';
		document.getElementById(tabNode+'Bottom').src = '/common/images/tab' + tabImgSide + '_bottom.gif';
	}
}
function toggleTab(side,node){
	window.clearTimeout(timer);
	if( !(side=='') && (side==tabSide) && !(node=='') && (node==tabNode) ) {
		tabImgSide = (tabSide=='Right')? '-right':'';
		document.getElementById(tabNode + 'Top').src = '/common/images/tab' + tabImgSide + '_top.gif';
		document.getElementById(tabNode + 'Middle').style.background = 'url(/common/images/tab' + tabImgSide + '_mid.gif)';
		document.getElementById(tabNode + 'Bottom').src = '/common/images/tab' + tabImgSide + '_bottom.gif';
		oldTop = '/common/images/tab' + tabImgSide + '_top.gif';
		oldMid = 'url(/common/images/tab' + tabImgSide + '_mid.gif)';
		oldBottom = '/common/images/tab' + tabImgSide + '_bottom.gif';
		hideMenues();
		return; 
	} 
	hideMenues();
	showMenue(side);
	leaveTab(side, node);
	imgSide = (side=='Right')? '-right':'';
	if( !(tabNode=='') ) {
		tabImgSide = (tabSide=='Right')? '-right':'';
		document.getElementById(tabNode + 'Top').src = '/common/images/tab' + tabImgSide + '_top.gif';
		document.getElementById(tabNode + 'Middle').style.background = 'url(/common/images/tab' + tabImgSide + '_mid.gif)';
		document.getElementById(tabNode + 'Bottom').src = '/common/images/tab' + tabImgSide + '_bottom.gif';
	}
	document.getElementById(node + 'Top').src = '/common/images/tab'+imgSide+'_top_active.gif';
	document.getElementById(node + 'Middle').style.background = 'url(/common/images/tab'+imgSide+'_mid_active.gif)';
	document.getElementById(node + 'Bottom').src = '/common/images/tab'+imgSide+'_bottom_active.gif';
	oldTop = document.getElementById(node + 'Top').src;
	oldMid = document.getElementById(node + 'Middle').style.background;
	oldBottom = document.getElementById(node + 'Bottom').src;
	document.getElementById(node+'Content').style.display = 'block';
}
function leaveTab(side, node) {
	tabNode = node;
	tabSide = side;
}
function tabHover(side,node){
	imgSide = (side=='Right') ? '-right':'';
	oldTop = document.getElementById(node + 'Top').src;
	oldMid = document.getElementById(node + 'Middle').style.background;
	oldBottom = document.getElementById(node + 'Bottom').src;
	document.getElementById(node + 'Top').src = '/common/images/tab' + imgSide + '_top_hover.gif';
	document.getElementById(node + 'Middle').style.background = 'url(/common/images/tab' + imgSide + '_mid_hover.gif)';
	document.getElementById(node + 'Bottom').src = '/common/images/tab' + imgSide + '_bottom_hover.gif';
}
function tabLeave(side,node){
	document.getElementById(node + 'Top').src = oldTop;
	document.getElementById(node + 'Middle').style.background = oldMid;
	document.getElementById(node + 'Bottom').src = oldBottom;
}
function hideMenues(){
	if( document.getElementById('sidebarLeft') ) {
		document.getElementById('sidebarLeftContent').style.display = 'none';
		document.getElementById('sidebarLeftTabs').style.left = '0px';
		document.getElementById('sidebarLeft').style.zIndex = 0;
	}
	if( document.getElementById('sidebarRight') ) {
		document.getElementById('sidebarRightContent').style.display = 'none';
		document.getElementById('sidebarRightTabs').style.right = '0px';
		document.getElementById('sidebarRight').style.zIndex = 0;
	}
	window.clearTimeout(timer);
	resetTabs();
	menue = false;
	tabNode = "";
	tabSide = "";	
}
function showMenue(side){
	document.getElementById('sidebar' + side).style.zIndex = 2;
	if( side == "Left" ) {
		document.getElementById('sidebar' + side + 'Tabs').style.left = 341 + 'px';
	} else {
		document.getElementById('sidebar' + side + 'Tabs').style.right = 341 + 'px';
	}
	document.getElementById('sidebar' + side + 'Content').style.display = "block";	
	menue=true;
}
function out(){
	timer=window.setTimeout('hideMenues()',2000);
}
function over(){
	window.clearTimeout(timer);
}

/* ############################### shortcuts.js ################################### */
var shortcut=false;
var parsed=false;
var accessKeyLinks=new Array();
window.document.onkeyup=function(){shortcut=false;}
window.document.onkeydown=function(event){
  var charCode=getKeyCode(event);
  if(charCode==18){
    shortcut=true;
  }else{
    switch(charCode){
      case 48:
        if(shortcut)toggleLoginBar();
        break;
      default: checkAccessKey(charCode);
    }
    shortcut=false;
  }
}
function getKeyCode(event){
  event=event||window.event;
  return event.keyCode;
}
function underlineAccessKeys(){
  var elements=new Array('a','label');
  var element='';
  var curr='';
  var old='';
  var pattern='<span class=\"accesskey\">$1<\/span>';
  if(document.getElementsByTagName){
    for(var i=0;i<=elements.length;i++){
      element=document.getElementsByTagName(elements[i]);
      for(var k=0;k<element.length;k++){
        if(element[k].getAttribute('accesskey')!=null&&element[k].getAttribute('accesskey')!=''&&element[k].getAttribute('accesskey').length==1){
          curr=eval('/('+element[k].getAttribute('accesskey')+')/i');
          if(element[k].firstChild.nextSibling==null){
            old=element[k].firstChild.data;
            if(old)element[k].innerHTML=old.replace(curr,pattern);
          }
        }
      }
    }
  }
}
function parseAccessLinks(){
  var links=document.getElementsByTagName('a');
  var c=0;
  var accessKey,accessLink;
  if(links){
    for(var i=0;i<links.length;i++){
      accessKey=links[i].getAttribute('accesskey');
      accessLink=links[i].getAttribute('href');
      if(accessKey){
        accessKeyLinks[c]=new Array(2);
        accessKeyLinks[c][0]=accessKey;
        accessKeyLinks[c][1]=accessLink;
        c++;
      }
    }
    parsed=true;
  }
  underlineAccessKeys();
}
function checkAccessKey(code){
  if(parsed&&shortcut&&!(navigator.appName=="Netscape")){
    var index=getAccessKey(code);
    if(index!=-1)document.location.href=accessKeyLinks[index][1];
  }
}
function getAccessKey(code){
  var index=-1;
  for(var i=0;i<accessKeyLinks.length;i++){
    if(String.fromCharCode(code)==accessKeyLinks[i][0].toUpperCase())index=i;
  }
  return index;
}

/* ############################### Box Class ####################################### */
function boxToggleContent(boxID) {
	var status = document.getElementById("boxContentDisplayed_"+boxID).style.display;
	
	if( status == "none" ) {
		document.getElementById("boxContentDisplayed_"+boxID).style.display = "block";
		document.getElementById("boxContentHidden_"+boxID).style.display = "none";
		document.getElementById("boxContentImage_"+boxID).src = "/common/images/down_label.gif";
	} else {
		document.getElementById("boxContentDisplayed_"+boxID).style.display = "none";
		document.getElementById("boxContentHidden_"+boxID).style.display = "block";
		document.getElementById("boxContentImage_"+boxID).src = "/common/images/up_label.gif";
	}
}

function boxShowMore(boxID) {
	document.getElementById("boxMoreHidden_"+boxID).style.display = "none";
	document.getElementById("boxMoreDisplayed_"+boxID).style.display = "block";
}

function boxSwitchPage(boxID, number) {
	nextPage = 1;
	curPage = 1;
	countPage = parseInt(document.getElementById("boxCountPage_"+boxID).value)+1;
	
	for( i = 1; i < countPage; i++ ) {
		if( document.getElementById("boxPage"+i+"Link_"+boxID).style.fontWeight == "bold" ) {
			curPage = i;
		}
	}
	
	if( number == 'up' ) {
		nextPage = curPage + 1;
		if( nextPage >= countPage ) { nextPage = curPage; }
	}
	if( number == 'down' ) {
		nextPage = curPage - 1;
		if( nextPage == 0 ) { nextPage = 1; }
	}
	if( !isNaN(number) ) {
		nextPage = parseInt(number);
	}

	for( i = 1; i < countPage; i++ ) {
		if( i == nextPage ) {
			document.getElementById("boxPage"+i+"_"+boxID).style.display = "block";
			document.getElementById("boxPage"+i+"Link_"+boxID).style.fontWeight = "bold";
		} else {
			document.getElementById("boxPage"+i+"_"+boxID).style.display = "none";
			document.getElementById("boxPage"+i+"Link_"+boxID).style.fontWeight = "normal";
		}
	}
}

