function getPosition(element)
/* der Aufruf dieser Funktion ermittelt die absoluten Koordinaten
   des Objekts element */
{
  var elem=element,tagname="",x=0,y=0;

/* solange elem ein Objekt ist und die Eigenschaft offsetTop enthaelt
   wird diese Schleife fuer das Element und all seine Offset-Eltern ausgefuehrt */
  while ((typeof(elem)=="object")&&(typeof(elem.tagName)!="undefined"))
  {
    y+=elem.offsetTop;     /* Offset des jeweiligen Elements addieren */
    x+=elem.offsetLeft;    /* Offset des jeweiligen Elements addieren */
    tagname=elem.tagName.toUpperCase(); /* tag-Name ermitteln, Grossbuchstaben */

/* wenn beim Body-tag angekommen elem fuer Abbruch auf 0 setzen */
    if (tagname=="BODY")
      elem=0;

/* wenn elem ein Objekt ist und offsetParent enthaelt
   Offset-Elternelement ermitteln */
    if (typeof(elem)=="object")
      if (typeof(elem.offsetParent)=="object")
        elem=elem.offsetParent;
  }

/* Objekt mit x und y zurueckgeben */
  position=new Object();
  position.x=x;
  position.y=y;
  return position;
}

function getSize() {
	var myWidth = 0, myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function getScrollY() {
	var scrOfX = 0, scrOfY = 0;

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return scrOfY;
}


var AjaxWindow = {
  openFlyingLayer: function(title,content,width,showMainCloseButton) {
    if(this.counter==0) {
      $('disable_layer').style.display = 'block';
  	  $('disable_layer').style.zIndex = 1;
      new Effect.Opacity('disable_layer', { from: 0.0, to: 0.7, duration: 0.3 });
    } else {
      $('disable_layer').style.zIndex++;
    }

    this.counter++;
    var fCloseButton = '<a href="javascript:AjaxWindow.closeFlyingLayer()" title="Schließen" class="fCloseButton">&nbsp;</a>';
    var fLayer = document.createElement('div')
    var fId = document.createAttribute('id');
    fId.nodeValue = 'flyingLayer_'+this.counter;
    fLayer.setAttributeNode(fId);

    $('htmlbody').appendChild(fLayer);

    if(width) {
        var screensize = getSize();
        $('flyingLayer_'+this.counter).style.left = ((screensize[0]/2)-(width/2)+(this.counter*10))+'px';
        $('flyingLayer_'+this.counter).style.width = width+'px';
    }
    $('disable_layer').style.zIndex++;
    $('flyingLayer_'+this.counter).style.zIndex=$('disable_layer').style.zIndex|0+1;
    $('flyingLayer_'+this.counter).style.position = 'fixed';/*'absolute';*/
    $('flyingLayer_'+this.counter).className = 'flyingLayer';
    $('flyingLayer_'+this.counter).style.top = '50px';
    fTop = 50+(this.counter*10);
    $('flyingLayer_'+this.counter).style.top = fTop+'px';

    $('flyingLayer_'+this.counter).innerHTML='<div class="fTitleRight">'+fCloseButton+'</div>';
    $('flyingLayer_'+this.counter).innerHTML+='<h1 class="fTitle">'+title+'</h1>';
    if(showMainCloseButton) {
      var fContent = '<div class="fcontent">'+content+'<form id="editform" style="text-align: center"><input type="button" onmouseout="this.className=\'but_green\'" onmouseover="this.className=\'but_green_hover\'" class="but_green" value="Schließen" name="submit" onclick="AjaxWindow.closeFlyingLayer()"></form><br /></div>';
    } else {
      var fContent = '<div class="fcontent">'+content+'</div>';
    }
    $('flyingLayer_'+this.counter).innerHTML+=fContent;
    fDrag = new Draggable('flyingLayer_'+this.counter, { handle:'fTitle' });
  },

  closeFlyingLayer: function(c) {
    fDrag.destroy();
    var fLayer = $('flyingLayer_'+this.counter);
    $('htmlbody').removeChild(fLayer);
	//new Effect.Opacity('disable_layer', { from: 0.7, to: 0.0, duration: 0.3 });
    if(this.counter>1) {
          $('disable_layer').style.zIndex=$('disable_layer').style.zIndex-2;
    } else {
	    $('disable_layer').style.display = 'none';
	}
    this.counter--;

  },

  enterPressed: function(e) {
    var charCode;

    if(e && e.which){
        charCode = e.which;
    } else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    if(charCode == 13) {
        return true;
    } else {
        return false;
    }
  },

  openLayerContact: function() {
      new Ajax.Request(
        window.location.protocol+'//'+window.location.host+'/ajax_guest_control.php?action=getForm&form=contact&referer='+encodeURIComponent(location.href),
        {
            onSuccess:function(r)
            {
               var fc=unescape(r.responseText);
               AjaxWindow.openFlyingLayer('Kontakt',fc,580);
            },
            onFailure:function()
            {
                alert('Fehler');
                return false;
            }
        }
        );
  },
  openLayerPicture: function(title,pictureurl,width) {
      new Ajax.Request(
        window.location.protocol+'//'+window.location.host+'/ajax_guest_control.php?action=getForm&form=picture&pictureurl='+encodeURIComponent(pictureurl),
        {
            onSuccess:function(r)
            {
               var fc=unescape(r.responseText);
               AjaxWindow.openFlyingLayer(title,fc,width);
            },
            onFailure:function()
            {
                alert('Fehler');
                return false;
            }
        }
        );
  }
}
AjaxWindow.counter = 0;

function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
}

function checkForm()
{
    var errors = 0;
    if($('vorname').value=="") {
        $('vorname').className+= ' error';
        errors++;
    }
    if($('name').value=="") {
        $('name').className+= ' error';
        errors++;
    }
    if(($('kontaktzeit').value=="nuremail" && validate($('email').value)==false) || ($('email').value!="" && validate($('email').value)==false)) {
        $('email').className+= ' error';
        errors++;
    }
    if($('email').value=="" && $('telefon').value=="") {
        $('email').className+= ' error';
        $('telefon').className+= ' error';
        errors++;
    }
    if($('rueckruf').value=="1" && $('telefon').value=="") {
        $('telefon').className+= ' error';
        errors++;
    }
    if($('nachricht').value=="") {
        $('nachricht').className = 'error';
        errors++;
    }

    if(errors>0) {
        alert('Eingabefehler:\nPrüfen Sie bitte Ihre Eingaben in den markierten Feldern!');
        return false;
    } else {
        return true;
    }
}

function sendContactForm()
{
    var inputfields = $('emailform').getElementsByTagName('input');
    for(i=0;i<inputfields.length;i++) {
        if(inputfields[i].type!="button" && inputfields[i].type!="checkbox") inputfields[i].className = 'authorinput';
    }
    $('nachricht').className = '';
    if(checkForm()) {
      Effect.BlindUp('formcontainer');
      $('sendstatus').style.display='block';
      setTimeout(function(){
          var strPost = '';
          if($('kid').value!='') strPost+='&kid='+$('kid').value;
          if($('kursanfrage').checked) strPost+='&kursanfrage=1';
          if($('rueckruf').checked) strPost+='&rueckruf=1';
          if($('infomaterial').checked) strPost+='&infomaterial=1';

          new Ajax.Request(
            url+'ajax_guest_control.php?action=sendContactEmail&id='+$('aid').value,
            {
                postBody: 'anrede='+$('anrede').value+
                  '&vorname='+$('vorname').value+
                  '&name='+$('name').value+
                  '&email='+$('email').value+
                  '&telefon='+$('telefon').value+
                  '&kontaktzeit='+$('kontaktzeit').value+
                  strPost+
                  '&nachricht='+$('nachricht').value,
                onSuccess:function(r)
                {
                   var fc=unescape(r.responseText);
                   AjaxWindow.openFlyingLayer('Kontakt',fc,260);
                },
                onFailure:function()
                {
                    return false;
                }
            }
            );
        },2000);

    }
}

function getZitat(range)
{
    new Ajax.Request(
            url+'ajax_guest_control.php?action=getZitat&id='+zitat_id+'&range='+range,
            {
                onSuccess:function(r)
                {
                    //var zdiv = document.getElementById('zitat');
                    var root = r.responseXML.getElementsByTagName('container');
                    var text = root[0].getElementsByTagName('data');
                    if (text.length > 0) {
                    //zdiv.innerHTML='';
                        zitat_id = unescape(text[0].childNodes[0].firstChild.nodeValue);
                        $('zitat_title').innerHTML = unescape(text[0].childNodes[1].firstChild.nodeValue);
                        $('zitat_text').innerHTML = unescape(text[0].childNodes[2].firstChild.nodeValue);
                        $('zitat_author').innerHTML = unescape(text[0].childNodes[3].firstChild.nodeValue);
                    }
                },
                onFailure:function()
                {
                    return false;
                }
            }
            );
}

function getEvent(range)
{
    new Ajax.Request(
            url+'ajax_guest_control.php?action=getEvent&id='+event_id+'&range='+range+'&event_page='+event_page,
            {
                onSuccess:function(r)
                {
                    var root = r.responseXML.getElementsByTagName('container');
                    var text = root[0].getElementsByTagName('data');
                    if (text.length > 0) {
                        event_id = unescape(text[0].childNodes[0].firstChild.nodeValue);
                        $('news_ueber').title = unescape(text[0].childNodes[1].firstChild.nodeValue);
                        $('news_ueber').innerHTML = unescape(text[0].childNodes[2].firstChild.nodeValue);
                        $('news_image_tag').src = unescape(text[0].childNodes[3].firstChild.nodeValue);
                        $('news_image_tag').alt = 'Bild ' + unescape(text[0].childNodes[1].firstChild.nodeValue);
                        $('news_teaser').innerHTML = unescape(text[0].childNodes[4].firstChild.nodeValue);
                        $('news_link').href = unescape(text[0].childNodes[5].firstChild.nodeValue);
                        event_page = unescape(text[0].childNodes[6].firstChild.nodeValue);
                    }
                },
                onFailure:function()
                {
                    return false;
                }
            }
            );
}

/*
var heading = null
var start_opa = 0.2;
function moveText(milliseconds) {
    window.setInterval("changePosition()", milliseconds)
}
function changePosition() {
    var to_x = Math.random()*400+140;
    var to_y = Math.random()*20+240;
    if(start_opa == 0.2) {
        start_opa = 0.7;
        from_opa = 0.2;
        to_opa = 0.7;
    } else {
        start_opa = 0.2;
        from_opa = 0.7;
        to_opa = 0.2;
    }
    new Effect.Move($('moveme'), { x: to_x, y: to_y, mode: 'absolute', duration: 5 });
    new Effect.Opacity('moveme', { from: from_opa, to: to_opa, duration: 5 });
}
*/


function utf8_encode(text) {
    return unescape(encodeURIComponent(text));
}

function utf8_decode(text) {
    return decodeURIComponent(escape(text));
}
