////////////////////////////////////////////////////////////////////////////////
// Open Source: JavaScript Library: Safe Mail
// Author(s):   FakeTP: faker@faketp.com, http://www.faketp.com/safemail/
//              Michael Stilson Jr. - Western Michigan University
//
// Created:     20040123
// Modifications:
// +===============================================================+
//  200XXXXX   <function_name> INSERTED|DELETED|UPDATED
//  ---------------------------------------------------------------
//  <description>
// +===============================================================+
//
// The original code acquired from FakeTP:
//
// function safemail(name,domain,display) {
//     displayed=(typeof(display)=="undefined") ? name+"@"+domain : display
//     document.write('<a href=mailto:'+name+'@'+domain+'>'+displayed+'</a>');
// }
//
// Example of usage:
// 
// 1) You must include this file into HTML file. Within the head of your 
//    page, place the following line of code if you are on the WMU WWW server:
// <head>
// <script src="/javascript/os/safemail.js" type="text/javascript"></script>
// </head>
// OR, if you are on another server:
// <head>
// <script src="http://www.wmich.edu/javascript/os/safemail.js" type="text/javascript"></script>
// </head>
// 
// 2) Add links to your page in a manner similar to the following example:
// <script type="text/javascript">
//     safemail("username","domain.com","Link Text");
// </script>
//
// NOTE: Replace "username" with the part of the email address that is before 
// the at sign (@), replace "domain" with the part of the email address that is
// after the at sign and you may optionally replace "anchor" with what you want 
// the link to say. If "anchor" is not specified, then the full email address 
// will become the text written in the anchor tag.
//

// Global Variables
var gBLANK="";
var gHTML_A_CLOSE="</a>";
var gHTML_GREATER="&gt;";
var gHTML_LESSER="&lt;";
var gHTML_A_HREF0="<a href=\"mailto:";
var gHTML_A_HREF1="\">";
var gSTR_AT="@";
var gSTR_GREATER=">";
var gSTR_LESSER="<";
var gSTR_SUBJECT="?subject=";
var gSTR_SPACE=" ";
var gSTR_UNDEF="undefined";
var gTYPE_BOOLEAN="boolean";
var gTYPE_STRING="string";

////////////////////////////////////////////////////////////////////
// Function: safemail(string,string,string,string,boolean)
// Purpose:  To provide a way to have email links in web pages that 
// cannot be parsed and acquired by current (as of 01/2004) methods 
// used by "rouge" www bots.
//
// Args In:  username, domain, subject, anchor (optional) and markup (optional)
// Pre:      username: a string that defines a valid email address username. 
//           domain:   a string that defines a valid email address domain
//           anchor:   a (n optional) string that defines the anchor text
//           markup:   a (n optional) boolean that flags a markup
//
// Args Out: none
// Post:     An HTML anchor is created from the passed args and written to the 
//           document.
//
function safemail(username,domain,subject,anchor,markup) {
	if(arguments.length<3) return 0;
	// Verify the first three arguments are strings
	if(arguments.length==3) {
		for(var i=0;i<3;i++) {
			if((typeof arguments[i])!=gTYPE_STRING) return 0;
		}
	}
	var isAnchor=true;
	var isMarkup=false;
	// Determine the optional arguments passed
	if(arguments.length>3) {
		for(var i=3;i<arguments.length;i++) {
			var argType=typeof arguments[i];
			if(argType==gTYPE_BOOLEAN) {
				// This argument is "markup"
				if(i==3) {
					// There is no "anchor" argument
					anchor=null;
				}
				isMarkup=true;
			}
		}
	}
	// If "anchor" has not been defined, then set it to the "href"
	if((typeof(anchor)==gSTR_UNDEF)||(anchor==null)) {
		var anchor=username+gSTR_AT+domain;
		isAnchor=false;
	}
	// Assemble the email address pieces
	var href=username+gSTR_AT+domain;
	// Build the anchor string
	if(isAnchor) {
		// Build a link in the following format:
		// <a href="mailto:anchor <username@domain>">anchor</a>
		var safeAnchor=
		gHTML_A_HREF0+anchor+gSTR_SPACE+gSTR_LESSER+href+gSTR_GREATER+gSTR_SUBJECT+subject+gHTML_A_HREF1+anchor+gHTML_A_CLOSE;
	} else {
		// Build a link in the following format:
		// <a href="mailto:username@domain">username@domain</a>
		var safeAnchor=
		gHTML_A_HREF0+href+gSTR_SUBJECT+subject+gHTML_A_HREF1+anchor+gHTML_A_CLOSE;
	}
	// If "markup", add the standard WMU email boundaries
	// <Email Link>
	if(isMarkup) safeAnchor=gHTML_LESSER+safeAnchor+gHTML_GREATER;
	// Write the anchor string to the "document"
	document.write(safeAnchor);
return 1;
}

// 
// Copyright 2004 - Western Michigan University - All rights reserved
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
////////////////////////////////////////////////////////////////////////////////
