var ns4 = (document.layers) ? 1 : 0; // Netscape 4
var op = (window.opera) ? 1 : 0; // Opera
var ie = (document.all && !op) ? 1 : 0; // MS Internet Explorer
var moz = (document.getElementById && !ie) ? 1 : 0; // Mozilla/Gecko Browser
var ie7 = (navigator.userAgent.toLowerCase().indexOf("msie 7")>-1) ? 1:0; // MS Internet Explorer

//onerror = ErrorFunction;
function ErrorFunction(msg, url, line) {
  var jserrmsgValue = "";
  if($("jserrmsg")) {
    jserrmsgValue = $("jserrmsg").innerHTML;

    if( jserrmsgValue!=null ) {
      jserrmsgValue = jserrmsgValue +'\n'+ msg;
    } else {
      jserrmsgValue = msg;
    }
    $("jserrmsg").setHTML(jserrmsgValue);
  }
  return true;
}

function isNumber(number) {
  if (number != "" && number.length > 0) {
    for (i = 0;i < number.length; i++) {
      if(number.charAt(i) < "0" || number.charAt(i) > "9") {
        return false;
      }
    }
  } else {
    return false;
  }

  return true;
}

function isDate(dateStr) {
  var datePat = /^(\d{1,2})(\/|.)(\d{1,2})(\/|.)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is the format ok?

  if (matchArray == null) {
    return false;
  }

  month = matchArray[3];
  day   = matchArray[1];
  year  = matchArray[5];

  if (month < 1 || month > 12) { // check month range
    return false;
  }

  if (day < 1 || day > 31) {
    return false;
  }

  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
    return false;
  }

  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
      return false;
    }
  }
  return true; // date is valid
}

function olderThan18(gebtag, gebmonat, gebjahr) {
  try {
    dateStr = gebtag + "." + gebmonat + "." + gebjahr;
    if( isDate(dateStr) ) {
      var stichtag = new Date();
      if( stichtag.getYear() > 2000) {
        stichtag.setYear( (stichtag.getYear()-18) );
      } else {
        stichtag.setYear( (stichtag.getYear()+1900-18) );
      }
      var geburtstag = new Date();
      geburtstag.setMonth( (gebmonat-1) );
      geburtstag.setYear( gebjahr );
      geburtstag.setDate( gebtag );
      if( geburtstag <= stichtag ) {
        return true;
      } else {
        return false;
      }
    }
  }catch(e) {}
  return false;
}

var ajformid    = "";
var ajcontentid = "";
var ajaction = "";
var errBackgroundColor      = '#FFCCCB';
var defaultBackgroundColor  = '#FFFFFF';

function prepareAjIds(suffix, action) {
  ajformid    = "ajform" + suffix;
  ajcontentid = "ajcontent" + suffix;

  if(action != null) {
    ajaction = action;
  } else {
    ajaction = "";
  }
}

function showAjaxFormular() {
  var yheight = $("base").offsetHeight;
  var xwidth = document.body.clientWidth;

  $("outgreyLayer").setStyle('height',yheight+'px');
  $("outgreyLayer").setStyle('width',xwidth+'px');
  $("outgreyLayer").setStyle('visibility','visible');

  if($(ajformid)) {
    $(ajformid).setStyle('visibility','visible');
  }
}

function closeAjaxFormular() {
  if( $("outgreyLayer") ) {
    $("outgreyLayer").setStyle('visibility','hidden');
  }
  if( $(ajcontentid) ) {
    $(ajformid).setStyle('visibility','hidden');
    $(ajcontentid).setHTML('');
  } else if( $(ajformid) ) {
    $(ajformid).setStyle('visibility','hidden');
    $(ajformid).setHTML('');
  }
  if( $('errMsgJs') ) {
    $('errMsgJs').setStyle('padding','0px');
    $('errMsgJs').setStyle('visibility','hidden');
  }
}

function showOutgreyLayer() {
  if( $("outgreyLayer") ) {
    var yheight = document.body.clientHeight;
    var xwidth  = document.body.clientWidth;
    var iHeight = 0;

    if( window.innerHeight!=null ) {
      iHeight = window.innerHeight;
    }

    if( iHeight>yheight ) {
      yheight = iHeight;
    }

    if( navigator.appName=='Netscape' ) {
      yheight = yheight - 147;
    } else {
      yheight = yheight - 157;
    }
    var ajcontainerheight = 0;

    if( ajcontainer!=null && ajcontainer!='' && $(ajcontainer) ) {
      ajcontainerheight = $(ajcontainer).offsetHeight;

      if( ajcontainerheight!=null && ajcontainerheight>(yheight-36-50) ) {
        yheight = ajcontainerheight + 36 + 50;
      }
    }

    $("outgreyLayer").setStyle('height',yheight+'px');
    $("outgreyLayer").setStyle('width',xwidth+'px');
    
    if(ie && !ie7) {
      $("outgreyLayer").setStyle('margin-top','5px');
    }
    
    //$("outgreyLayer").style.visibility="visible";
    var fxOutgreyLayer = new Fx.Style('outgreyLayer', 'opacity', {duration: 500});
    if (!slideshow) {
      fxOutgreyLayer.set(0);
      fxOutgreyLayer.start(0.7);
    } else {
      fxOutgreyLayer.set(0.7);
    }
    if ($('ie6selectBoxBugIframe')) {
      $('ie6selectBoxBugIframe').setStyle('height',ajcontainerheight+'px');
    }
  }
}

function resizeOutgreyLayer() {
  setTimeout('timeResizeOutgreyLayer()', 600)
}

function timeResizeOutgreyLayer() {
  if($('outgreyLayer')) {
    if( $('outgreyLayer').getStyle('visibility') == 'visible') {
      var yheight = document.body.clientHeight;
      var xwidth  = document.body.clientWidth;
      var iHeight = 0;
      if( window.innerHeight!=null ) {
        iHeight = window.innerHeight;
      }
      if( iHeight>yheight ) {
        yheight = iHeight;
      }
      if( navigator.appName=='Netscape' ) {
        yheight = yheight - 147;
      } else {
        yheight = yheight - 157;
      }
      var ajcontainerheight = 0;
      if( ajcontainer!=null && ajcontainer!='' && $(ajcontainer) ) {
        ajcontainerheight = $(ajcontainer).offsetHeight;

        if( ajcontainerheight!=null && ajcontainerheight>(yheight-36-50) ) {
          yheight = ajcontainerheight + 36 + 50;
        }
      }

      $("outgreyLayer").setStyle('height',yheight+'px');
      $("outgreyLayer").setStyle('width',xwidth+'px');
    }
  }
}

function closeOutgreyLayer(param) {
  if( $("outgreyLayer") ) {
    //$("outgreyLayer").style.visibility="hidden";
    var fxOutgreyLayer = new Fx.Style('outgreyLayer', 'opacity', {duration: 800});
    if (param != 'poster') {
      fxOutgreyLayer.set(0.7);
      fxOutgreyLayer.start(0);
      var timeout = 800;
    } else {
      fxOutgreyLayer.set(0);
      var timeout = 0;
    }
    if ($('ie6selectBoxBugIframe')) {
      $('ie6selectBoxBugIframe').setStyle('height','1px');
    }
    setTimeout(function(){$("outgreyLayer").setStyle('height','1px');}, timeout);
  }
}

function clearItem(item, value) {
  if( item.value==value ) {
    item.value = '';
  }
}

function fillItem(item, value) {
  if( item.value=='' ) {
    item.value = value;
  }
}

function EMail(s) { // http://www.drweb.de/javascript/email_check.shtml
  var a = false;
  var res = false;
  if(typeof(RegExp) == 'function') {
    var b = new RegExp('abc');
    if(b.test('abc') == true) {a = true;}
  }

  if(a == true) {
    reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+'(\\@)([a-zA-Z0-9\\-\\.]+)'+'(\\.)([a-zA-Z]{2,4})$');
    res = (reg.test(s));
  } else {
    res = (s.search('@') >= 1 && s.lastIndexOf('.') > s.search('@') && s.lastIndexOf('.') >= s.length-5);
  }
  return(res);
}

function decodeHTML(text) {
  while( text.indexOf("&amp;") !=-1 ) {
    text = text.replace("&amp;", "&");
  }
  return text;
}

var lastTimeInSeconds = 0;
var minTimeDifference = 4;
function mintime(lastExecutionTime, mindiff) {
  var now     = new Date();
  var seconds = now.getSeconds();
  if( lastExecutionTime!=null ) {
    var diff = 0;
    if( seconds > lastExecutionTime ) {
      diff = Math.abs(seconds - lastExecutionTime);
    } else {
      diff = Math.abs(seconds + 60 - lastExecutionTime );
    }
    if( diff < mindiff ) {
      return false;
    }
  }
  lastTimeInSeconds = seconds;
  return true;
}

function getRadiobuttonValue(radioObject) {
  for(var i=0; i<radioObject.length; i++) {
    if( radioObject[i].checked ) {
      return radioObject[i].value;
    }
  }
  return null;
}

function forgotPassword() {
  var optionalParameters = '';
  var logonId = document.LogonForm.logonId.value;
  if( logonId!=null && logonId!='' ) {
    optionalParameters += '&logonId=' + encodeURIComponent(logonId);
  }
  openAjaxLayer('Logoff', '', 'state=forgetpassword'+optionalParameters);
}

function MM_controlShockwave(objStr,x,cmdName,frameNum) {
  f=getFlashMovieObject(objStr);
  if(f!=null && f!=undefined) {
    if(cmdName=="Play") {
      eval('f.'+cmdName+'();');
    } else if(cmdName!="Play" && cmdName!="GotoFrame") {
      eval('f.'+cmdName);
    }
  }
}

function getFlashMovieObject(movieName) {
  if(window.document[movieName]) {
    return window.document[movieName];
  }
  if(navigator.appName.indexOf("Microsoft Internet")==-1) {
    if(document.embeds && document.embeds[movieName]) {
      return document.embeds[movieName];
    }
  } else {
    return $(movieName);
  }
}

function set_cookie ( name, value, exp_y, exp_m, exp_d ) {
  var cookie_string = name + "=" + escape ( value );
  if ( exp_y ) {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }
  document.cookie = cookie_string;
}

function delete_cookie ( cookie_name ) {
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function flashCookieCheck(name, id) {
  if (get_cookie('flashStatus'+name)) {
    MM_controlShockwave(id,'','GotoFrame', '1');
  }
  else {
    set_cookie('flashStatus'+name, 'ran');
  }
}

function fadeInDiv (linkID, fadeInDiv) {
  var fxFadeInDiv=new Fx.Style(fadeInDiv, 'opacity', {duration: 500});
  fxFadeInDiv.set(0);
  $(linkID).addEvent('mouseenter', function() {
    fxFadeInDiv.start(1);
  });
  $(linkID).addEvent('mouseleave', function() {
    fxFadeInDiv.start(0);
  });
}

function toggleFadeDiv (linkID, fadeInDivId) {
  var fxFadeInDiv=new Fx.Style(fadeInDivId, 'opacity', {duration: 500});
  fxFadeInDiv.set(0);
  $(linkID).addEvent('click', function() {
    if (fadeInDivId.opacity == 0) {
      fxFadeInDiv.start(1);
    }
    else {
      fxFadeInDiv.start(0);
    }
  });
}
var coin = 1;
function fadeInImg (div2fade) {
  if ($(div2fade) && coin == 1){
    var fxDiv = new Fx.Style(div2fade, 'opacity', {duration: 300});
    fxDiv.set(0);
    fxDiv.start(1);
    coin = 0;
  }
}

function fadeOutImg (div2fade) {
  if($(div2fade)) {
    var fxDiv = new Fx.Style(div2fade, 'opacity', {duration: 300});
    fxDiv.start(0);
    coin = 1;
  }
}

function showForShure() {
  $('leftNavContainer').setStyle('visibility', 'visible');
}

var Ticker = new Class({
  setOptions: function(options) {
    this.options = Object.extend({
      speed: 1000,
      delay: 5000,
      direction: 'vertical',
      onComplete: Class.empty,
      onStart: Class.empty
    }, options || {});
  },
  initialize: function(el,options){
    this.setOptions(options);
    this.el = $(el);
    this.items = this.el.getElements('li');
    var w = 0;
    var h = 0;
    if(this.options.direction.toLowerCase()=='horizontal') {
      h = this.el.getSize().size.y;
      this.items.each(function(li,index) {
        w += li.getSize().size.x;
      });
    } else {
      w = this.el.getSize().size.x;
      this.items.each(function(li,index) {
        h += li.getSize().size.y;
      });
    }
    this.el.setStyles({
      position: 'absolute',
      top: 0,
      left: 0,
      width: w,
      height: h
    });
    this.fx = new Fx.Styles(this.el,{duration:this.options.speed,onComplete:function() {
      var i = (this.current==0)?this.items.length:this.current;
      this.items[i-1].injectInside(this.el);
      this.el.setStyles({
        left:0,
        top:0
      });
    }.bind(this)});
    this.current = 0;
    this.next();
  },
  next: function() {
    this.current++;
    if (this.current >= this.items.length) this.current = 0;
    var pos = this.items[this.current];
    this.fx.start({
      top: -pos.offsetTop,
      left: -pos.offsetLeft
    });
    this.next.bind(this).delay(this.options.delay+this.options.speed);
  }
});

