////////////////////////////////////////////////////////////////////////////////
// <file
//   repository_path  = "$Source: /pub/cvsprojects/nucleo/web/home/meta/js/add_mailtos.js,v $"
//   revision         = "$Revision: 1.2 $"
//   date             = "$Date: 2005/02/28 03:40:42 $"
//   tag              = "$Name:  $"
//   template_version = "cp_template.0.12"
// >
//   <license>
//     We have not chosen a copyright/license at the time of this release.
//   </license>
// </file>
////////////////////////////////////////////////////////////////////////////////

//##############################################################################
// <routine name="add_mailtos()">
//
//   <description>
//     <abstract>
//       A web page that contains e-mail addresses can use this javascript
//       routine as part of an anti-spam technique.
//       For example, the home template adds it to pages at our site.
//       The anti-spam technique is as follows.
//       To support browsers that do not run javascript, replace the `@'
//       character in all e-mail addresses in the page with an (X)HTML `img'
//       element whose `src' attribute points to an image of an `@'.
//       To support browsers that do run javascript, set the `img' element's
//       `class' attribute to "EmailAtImg".
//       Finally, invoke add_mailtos() from the `onload' attribute of the
//       `body' element.
//       In browsers that run javascript, this routine will convert every
//       e-mail address into a mailto link (in an (X)HTML anchor element) with
//       a real `@' character rather than the image.
//       Nevertheless, regardless of whether a browser runs javascript, the
//       source (X)HTML itself will not contain valid e-mail addresses, so many
//       spam bots will not recognize them.
//     </abstract>
//     <keywords>
//       anti-spam, spam bot, javascript, e-mail addresses, @, img, image,
//       XHTML, HTML, home template
//     </keywords>
//   </description>
// 
//   <authors>
//     <current>
//       <author userid="jdenny" start_date="2005/01/28" />
//     </current>
//     <previous>
//     </previous>
//   </authors>
// 
//   <compatibility>
//     Browser: IE version 6.0.2900.2180.xpsp_sp2_rtm.040803-2158;
//     Browser: Mozilla 1.7.3;
//     Browser: Firefox version 1.0;
//     Browser: Opera 7.54, build 3869;
//   </compatibility>
//
//   <usage>
// 
//     <calling_sequence>
//       add_mailtos();
//     </calling_sequence>
// 
//     <param name="html" kind="inout,dom" doc="EmailAtImg,mailtos" />
// 
//     <doc kind="pre" id="EmailAtImg">
//       Every `img' element in the (X)HTML source whose `class' attribute has
//       the value "EmailAtImg" appears in place of the `@' character in an
//       e-mail address.
//       Each such e-mail address is delimited by whitespace or (X)HTML tags
//       in order to distinguish it from surrounding text.
//     </doc>
// 
//     <doc kind="post" id="mailtos">
//       Each e-mail address specified by doc id="EmailAtImg" was replaced with
//       an (X)HTML `a' (anchor) tag.
//       The content of each such anchor tag is the original e-mail address but
//       with the `img' element replaced by an actual `@' character.
//       The `href' attribute's value is identical to the content except that
//       it is prefixed by `mailto:'.
//     </doc>
// 
//     <doc kind="example" id="body_onload">
//       body onload="add_mailtos();"
//     </doc>
// 
//   </usage>
//
// </routine>
//##############################################################################

function add_mailtos() {
  var a_imgs = document.getElementsByTagName( 'img' );
  for ( var i_img_index = 0; i_img_index < a_imgs.length; ++i_img_index ) {
    var r_img = a_imgs[ i_img_index ];
    var s_class = null;
    if ( r_img.getAttribute( 'class' ) ) {
      s_class = r_img.getAttribute( 'class' );
    }
    else if ( r_img.getAttribute( 'className' ) ) {
      s_class = r_img.getAttribute( 'className' );
    }
    if ( s_class && s_class == 'EmailAtImg' ) {
      var s_email = '';
      if ( r_img.previousSibling && r_img.previousSibling.nodeType == 3 ) {
        var a_prefixes = r_img.previousSibling.nodeValue.split( /\s/ );
        s_email += a_prefixes[ a_prefixes.length - 1 ];
        a_prefixes[ a_prefixes.length - 1 ] = null;
        r_img.parentNode.replaceChild(
          document.createTextNode( a_prefixes.join(' ') ),
          r_img.previousSibling
        );
      }
      s_email += '@';
      if ( r_img.nextSibling && r_img.nextSibling.nodeType == 3 ) {
        var a_prefixes = r_img.nextSibling.nodeValue.split( /\s/ );
        s_email += a_prefixes[ 0 ];
        a_prefixes[ 0 ] = null;
        r_img.parentNode.replaceChild(
          document.createTextNode( a_prefixes.join(' ') ),
          r_img.nextSibling
        );
      }
      var r_anchor = document.createElement( 'a' );
      r_anchor.setAttribute( 'href', 'mailto:' + s_email );
      r_anchor.appendChild( document.createTextNode(s_email) );
      r_img.parentNode.replaceChild( r_anchor, r_img );
      --i_img_index;
    }
  }
}
