// HEADER START
// This file is part of the wdss system.
// Author: Eduard Gode
// Copyright by the author
// Copying, distribution, inclusion (execution) without explicit permission is denied.
// Permissions granted to <wdss> websites of customers of:
//    Weitblick Internetwerkstatt, Erhard Klein or
//    Weitblick <wdss> Services, Eduard Gode
//
// Coding conventions:
// * All functions, methods and data has to be coded within the global object WDSS
// * All functions, methods and data named with a stating '_' is internal and may be change in future revisions
if(null==window.WDSS)window.WDSS={module:{},requireModule:function(m){if(!this.module[m]){this.module[m]=1;document.write('<script type="text/javascript" src="/js-pool/wdss/'+m+'.js"></script>');}}};
// HEADER END

//WDSS.requireModule('event');

WDSS.module.encemail = '$Revision: 1.8 $';

// see /js-pool/wdss/event.js
if ( null == WDSS.event ) {

  WDSS.event = function () {};


  /* ******* */
  /* onload  */
  /* ******* */

  WDSS.event.onloadHdl = function () {
    while ( WDSS.event.onloadHdl.stack.length ) {
      var handler = WDSS.event.onloadHdl.stack.shift();
      handler();
    }
  };
  WDSS.event.onloadHdl.stack = [];

  WDSS.event.pushOnloadHdl = function ( handler ) {
    if ( null == handler ) return;
    if ( null == window.onload || window.onload != WDSS.onloadHdl ) {
      if ( null != window.onload ) WDSS.event.onloadHdl.stack.push(window.onload);
      window.onload = WDSS.event.onloadHdl;
    }
    WDSS.event.onloadHdl.stack.push(handler);
  };
  window.wdss_onloadHdl = { push: WDSS.event.pushOnloadHdl };


  /* ********* */
  /* onunload  */
  /* ********* */

  WDSS.event.onunloadHdl = function () {
    while ( WDSS.event.onunloadHdl.stack.length ) {
      var handler = WDSS.event.onunloadHdl.stack.shift();
      handler();
    }
  };
  WDSS.event.onunloadHdl.stack = [];

  WDSS.event.pushOnunloadHdl = function ( handler ) {
    if ( null == handler ) return;
    if ( null == window.onunload || window.onunload != WDSS.onunloadHdl ) {
      if ( null != window.onunload ) WDSS.event.onunloadHdl.stack.push(window.onunload);
      window.onunload = WDSS.event.onunloadHdl;
    }
    WDSS.event.onunloadHdl.stack.push(handler);
  };


  /* ********** */
  /* onDomReady */
  /* ********** */

  WDSS.event.pushOnDomReadyHdl = function (handler) {
    if ( document.addEventListener ) { // browser is complient to w3c
      document.addEventListener( 'DOMContentLoaded', handler, false );
    } else { // e.g. IE
      if ( null == WDSS.event.readystateChanged ) {
        WDSS.event.readystateChanged = function () {
          if ( 'interactive' == document.readyState || 'complete' == document.readyState ) {
            for ( var i = 0; i < WDSS.event.onDomReadyHdlList.length; i++ ) {
              WDSS.event.onDomReadyHdlList[i]();
            }
	    WDSS.event.onDomReadyHdlList = [];
          }
        };
        WDSS.event.onDomReadyHdlList = [];
      }
      if ( 'interactive' == document.readyState || 'complete' == document.readyState ) {
        handler();
      } else if ( null != document.onreadystatechange ) {
        alert( 'onreadystatechange is already in use' );
      } else {
        WDSS.event.onDomReadyHdlList.push(handler);
        document.onreadystatechange = WDSS.event.readystateChanged;
      }
    }
  };
}


// decrypt encrypted email adresses
if ( null == WDSS.decryptEMail ) {
  WDSS.decryptEMail = function () {
    var nodes = document.getElementsByTagName('span');
    for ( var i = nodes.length-1; i >= 0; i-- ) {
      var node = nodes[i];
      if ( 'EncMail' == node.className ) {
	var str = node.firstChild.nodeValue;
	//node.parentNode.replaceChild(document.createTextNode(str),node);
        str = WDSS.decryptEMail.decrypt( str );
	str = str.replace( /-/g, '-<wbr />' );
	str = str.replace( /@/g, '@<wbr />' );
	node.className = '';
	node.innerHTML = str;
      }
    }

    nodes = document.getElementsByTagName('a');
    for ( var i = nodes.length-1; i >= 0; i-- ) {
      var node = nodes[i];
      var res;
      if ( res = /(file:\/\/)?\/(cgi-pool\/encemail|js-pool\/wdss\/encemail.html)\?adr=(.*)/.exec(node.href) ) {
	var str = WDSS.decryptEMail.decrypt( res[3] );
	node.href = 'mailto:'+str;
	if ( !node.hasChildNodes() ) {
	  // node.appendChild(document.createTextNode(str));
	  str = str.replace( /-/g, '-<wbr />' );
	  str = str.replace( /\./g, '.<wbr />' );
	  str = str.replace( /@/g, '@<wbr />' );
	  node.innerHTML = str;
	}
      }
    }
  };

  WDSS.decryptEMail.reverse = function (str) {
    if ( null == str ) return str;
    var n = '';
    for ( var i = str.length ; i; ) {
      i--;
      n += str.charAt(i);
    }
    return n;
  };
  WDSS.decryptEMail.decrypt = function (str) {
    var res;
    if ( null != str && (res = /([^\@]*)\@(.*)/.exec(str) ) ) {
      str = WDSS.decryptEMail.reverse(res[1]) + '@' + WDSS.decryptEMail.reverse(res[2]);
    }
    return str;
  };

  //WDSS.event.pushOnDomReadyHdl(WDSS.decryptEMail);
  WDSS.event.pushOnloadHdl(WDSS.decryptEMail);
}

