var ScreenRes = screen.width;
var loadingImage = verzeichniss+'images/'+skin+'/xajax_loading_big.gif';		
var closeButton = verzeichniss+'images/'+skin+'/close.gif';

function $(id) {
	return document.getElementById(id);
}
function setzteCookie(name,value,days) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+days);
	document.cookie = name + '=' + value + ';expires='+exdate.toGMTString();
}
function getCookie( name ) {
      var start = document.cookie.indexOf( name + "=" );
      var len = start + name.length + 1;
      if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
      return null;
      }
      if ( start == -1 ) return null;
      var end = document.cookie.indexOf( ';', len );
      if ( end == -1 ) end = document.cookie.length;
   return unescape( document.cookie.substring( len, end ) );
}
function changeLang(lang) {
	setzteCookie('language',lang,'365');
	if(lang == 'de')
	 {
		w = window.open('http://www.mgs-del.de');
	 }
	else
	 {
		w = window.open('http://translate.google.de/translate?u=http%3A%2F%2Fwww.mgs-del.de&amp;hl=de&amp;ie=UTF-8&amp;sl=de&amp;tl='+lang);
	 }
	(typeof w == "undefined") ? alert('You may disable your popupblocker do enable this content') : '';
	return false;
}
function help(w) {
	window.open(verzeichniss+'include/Help.html?show='+w);
}
function checkemail(email) {
	var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	return  (filter.test(email)) ? true : false;
}
function check_form(id,a) {
	for(i = 0; i < a.length; i++) {
		if(a[i] == "email") {
			if(checkemail(document.forms[id].elements[a[i]].value) == false) {
				alert('Die Email ist ungültig!');return false;
			}
		}
		else if(document.forms[id].elements[a[i]].value == "") {
			 alert(a[i] + " ist nicht richtig ausgefüllt worden!"); return false;
		}
	}
	return true;//document.forms[id].submit();
}
function fade (element, from, to) {
/*
	if (typeof show == 'undefined') {
		show = true;
	}
*/
	if (typeof element.style.opacity != 'undefined') {
		element.style.opacity = from;
	}
	else if (typeof element.style.filter != 'undefined') {
		element.style.filter = 'progidXImageTransform.Microsoft.Alpha(opacity=' + (from * 100) +')';
	}
	if (from > to) {
		from -= 0.1;
		from = Math.round(from * 100) / 100;
		if (from >= to) {
			setTimeout(function () { fade(element, from, to, from); },25);
		}
	}
	else {
		from += 0.1;
		from = Math.round(from * 100) / 100;
		if (from <= to) {
			setTimeout(function () { fade(element, from, to, from); },25);
		}
	}
	if(from == 0) { // && show == true) {
		//setTimeout(function(){element.style.display = 'none'},200);
		element.style.display = 'none';
	}
}
function showObj(obj,display) {
	if ($(obj).style.display == 'none' || $(obj).style.display == '') {
	   $(obj).style.display = display;
	} else {
	   $(obj).style.display = "none";
	}
}
function selectAll(f) {
	var z=f['bid[]'];
	for(var i=0;i<z.length;i++){
		z[i].checked = true;
	}
}
function unselectAll(f) {
	var z=f['bid[]'];
	for(var i=0;i<z.length;i++){
		z[i].checked = false;
	}
}
function mail(user, domain) {
	window.location = 'mailto:'+user+'@'+domain;
}
function zitat(wohin,name,was) {
	$(wohin).value += "[quote=" + name + "]" + was + "[/quote]";
	$(wohin).focus();
}
function insert(aTag, eTag,input) {
//  var input = document.forms['formular'].elements['eingabe'];
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightbox(); }
}
function listenKey () {	document.onkeypress = getKey; }
function showLightbox(objLink)
{
	var objOverlay = $('overlay');
	var objLightbox = $('lightbox');
	var objCaption = $('lightboxCaption');
	var objImage = $('lightboxImage');
	var objLoadingImage = $('loadingImage');
	var objLightboxDetails = $('lightboxDetails');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	imgPreload = new Image();

	imgPreload.onload=function(){
		objImage.src = objLink.href;
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
		objLightboxDetails.style.width = imgPreload.width + 'px';
		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			//objCaption.style.width = imgPreload.width + 'px';
			objCaption.innerHTML = objLink.getAttribute('title');
		} else {
			objCaption.style.display = 'none';
		}
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		} 
		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }
		objLightbox.style.display = 'block';
		fade(objLightbox,0,1);
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		listenKey();
		return false;
	}
	imgPreload.src = objLink.href;
}
function hideLightbox()
{
	objOverlay = $('overlay');
	objLightbox = $('lightbox');
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	document.onkeypress = '';
}
function initLightbox()
{	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	//objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var imgPreloader = new Image();
	imgPreloader.onload=function(){
		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '150';
		objLoadingImageLink.appendChild(objLoadingImage);
		imgPreloader.onload=function(){};
		return false;
	}
	imgPreloader.src = loadingImage;
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);
	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title','Click to close');
	objLink.onclick = function () {hideLightbox(); return false;}
	objLightbox.appendChild(objLink);
	var imgPreloadCloseButton = new Image();
	imgPreloadCloseButton.onload=function(){
		var objCloseButton = document.createElement("img");
		objCloseButton.src = closeButton;
		objCloseButton.setAttribute('id','closeButton');
		objCloseButton.style.position = 'absolute';
		objCloseButton.style.zIndex = '200';
		objLink.appendChild(objCloseButton);
		return false;
	}
	imgPreloadCloseButton.src = closeButton;
	var objImage = document.createElement("img");
	objImage.setAttribute('id','lightboxImage');
	objLink.appendChild(objImage);
	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetails');
	objLightbox.appendChild(objLightboxDetails);
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','lightboxCaption');
	objCaption.style.display = 'none';
	objLightboxDetails.appendChild(objCaption);
}
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}
addLoadEvent(initLightbox);
function showOverlayBox(id,w,h) {
	var objOverlay = $('overlay');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - h) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - w) / 2);
	$('loadingImage').style.display = 'none';
	objOverlay.style.display = 'block';
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	if($(id))
	{
		$(id).style.display = 'block';
	}
	else
	{
		_box = document.createElement('div');
		_box.id = id;
		_box.className = 'overlaybox';
		_box.style.position = 'absolute';
		//_box.style.textAlign = 'center';
		_box.style.width = w+'px';	
		_box.style.zIndex = '100';	
		_box.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		_box.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
		_box.innerHTML = '<img src="'+verzeichniss+'images/'+skin+'/xajax_loading.gif" alt="Loading" />';
	       $('overlay').appendChild(_box);
	}
}
function hideOverlayBox(id)
{
	$('overlay').style.display = 'none';
	$(id).style.display = 'none';
	document.onkeypress = '';
}