////////////////////////////////////////////////////////////////////////////////
// <file
//   repository_path  = "$Source: /pub/cvsprojects/nucleo/web/home/meta/js/mark_external_links.js,v $"
//   revision         = "$Revision: 1.4 $"
//   date             = "$Date: 2007/08/27 15:11:28 $"
//   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="mark_external_links()">
//
//   <description>
//     <abstract>
//       A web page can invoke this javascript routine to visually mark all
//       contained links pointing to external web sites.
//       For example, the home template adds a call on this routine to the
//       `onload' attribute of the `body' element of pages at our site.
//       The mark forces the web user to recognize that webnucleo developers do
//       not deserve credit for the content of the linked page and are not
//       responsible for problems or poor style in the linked page.
//     </abstract>
//     <keywords>
//       external links, offsite links, javascript, XHTML, HTML, home template,
//       CSS, title, tooltip
//     </keywords>
//   </description>
// 
//   <authors>
//     <current>
//       <author userid="mbradle" start_date="2007/08/08" />
//     </current>
//     <previous>
//       <author userid="jdenny" start_date="2005/09/27" />
//     </previous>
//   </authors>
// 
//   <compatibility>
//     Browser: Firefox version 1.0.7;
//     Browser: IE version 6.0.2900.2180.xpsp_sp2_gdr.050301-1519;
//     Browser: Opera 8.5, build 7700;
//   </compatibility>
//
//   <usage>
// 
//     <calling_sequence>
//       mark_external_links();
//     </calling_sequence>
// 
//     <param name="html" kind="inout,dom" doc="link_criteria,visual_mark" />
// 
//     <doc kind="pre" id="link_criteria">
//       Within the (X)HTML source, every link that should be marked as an
//       external link meets the following criteria:
//       <item id="anchor">
//         The link is an (X)HTML anchor, `a', element.
//       </item>
//       <item id="href">
//         The link's target URL is the anchor element's `href' attribute's
//         value minus any whitespace.
//       </item>
//       <item id="offsite">
//         The link's target URL starts with `http://' followed by something
//         other than `nucleo.ces.clemson.edu/' or `webnucleo.org/'
//         and is not exactly `http://nucleo.ces.clemson.edu' or
//         `http://www.webnucleo.org' or `http://webnucleo.org'.  Also,
//         if a target URL starts with `ftp://', it is considered an
//         external link.
//       </item>
//       <item id="mailing_lists">
//         The link's target is not one of our mailing lists.
//       </item>
//       <item id="global_links">
//         The anchor element's CSS class is not `glink', which is the class
//         for all global links at the top of pages using our home template.
//       </item>
//       <item id="validators">
//         The link is not one of our standard W3C validator links.
//       </item>
//     </doc>
// 
//     <doc kind="post" id="visual_mark">
//       For each link that is determined to be an external link, the anchor
//       element's CSS class is set to `ExternalLink', and the anchor element's
//       `title' attribute is set to `Offsite Resource'.
//     </doc>
// 
//     <doc kind="example" id="body_onload">
//       body onload="mark_external_links();"
//     </doc>
// 
//   </usage>
//
// </routine>
//##############################################################################

function mark_external_links() {
  var a_anchors = document.getElementsByTagName( 'a' );
  for (
    var i_anchor_index = 0; i_anchor_index < a_anchors.length; ++i_anchor_index
  ) {
    var r_anchor = a_anchors[ i_anchor_index ];
    var s_href = r_anchor.getAttribute( 'href' );
    if ( s_href ) {
      s_href = s_href.replace( /\s/g, '' );
      var s_class = r_anchor.getAttribute( 'class' );
      if ( !s_class ) {
        s_class = r_anchor.getAttribute( 'className' );
      }
      if (
        s_href.match( /^http:\/\// )
        && !s_href.match( /^http:\/\/nucleo.ces.clemson.edu$/ )
        && !s_href.match( /^http:\/\/nucleo.ces.clemson.edu\// )
        && !s_href.match( /^http:\/\/www.webnucleo.org$/ )
        && !s_href.match( /^http:\/\/www.webnucleo.org\// )
        && !s_href.match(
          /^http:\/\/www.ces.clemson.edu\/mailman\/listinfo\/webnucleo/
        )
        && !s_href.match(
          /^http:\/\/www.ces.clemson.edu\/pipermail\/webnucleo/
        )
        && !s_href.match(
          /^http:\/\/www.ces.clemson.edu\/mailman\/private\/webnucleo/
        )
        && !( s_class && s_class == 'glink' )
        && !s_href.match( /^http:\/\/validator.w3.org\/check\?uri=referer/ )
        && !s_href.match(
          /^http:\/\/jigsaw.w3.org\/css-validator\/validator\?uri/
        )
      ) {
        r_anchor.setAttribute( 'title', 'Offsite Resource' );
        r_anchor.setAttribute( 'class', 'ExternalLink' );
        r_anchor.setAttribute( 'className', 'ExternalLink' );
      }
      if (
        s_href.match( /^ftp:\/\// )
      ) {
        r_anchor.setAttribute( 'title', 'Offsite Resource' );
        r_anchor.setAttribute( 'class', 'ExternalLink' );
        r_anchor.setAttribute( 'className', 'ExternalLink' );
      }
    }
  }
}
