/*
 *  File:  mailForm.js
 *  
 *  Send e-mail to selected addresses
 *
 *  Usage:
 *  
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *
 *  Insert the <script> element into the <body> of your document.
 *
 *  Constructor:
 *    MailForm()                         Create mail form window
 *
 *  Methods:
 *    addRecipient( name, email )        Add single recipient
 *    addRecipients( name1, email1,      Add multiple recipients
 *                   name2, email2, ... )              
 *    addGroup( group )                  Add single group
 *    addGroups( group1, group2, ... )   Add multiple groups
 *    close()                            Close mail form window
 *    open()                             Open mail form window
 *    setGroupLineDisplayed( boolean )   Turn group line on/off
 *    setMailFromAddress( email )        Set hard-coded "From" address
 *    setMailFromDefaultText( text )     Set "From" default text
 *
 *  Note:  A "boolean" value is either true or false
 *
 *  Example 1:  mailFormTest1.html
 *
 *    <!-- a simple mailform with multiple recipients -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      var mailForm = new MailForm();
 *      mailForm.addRecipients(
 *        "Admissions Office", "admissions@bgnet.bgsu.edu",
 *        "Human Resources", "ohr@bgnet.bgsu.edu",
 *        "Registration and Records", "registrar@bgnet.bgsu.edu",
 *        "Tech Support Center", "tsc@bgnet.bgsu.edu",
 *        "Webmaster", "webmaster@bgnet.bgsu.edu" );           
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:mailForm.open()">Contact us!</a>
 *
 *  Example 2:  mailFormTest2.html
 *
 *    <!-- a single recipient renders differently -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      var mailForm = new MailForm();
 *      mailForm.addRecipient( 
 *        "Webmaster", "webmaster@bgnet.bgsu.edu" );           
 *    </script>
 *    <!-- Put this form in the <body> of your document -->
 *    <form>
 *      <input type="button" value="Contact the Webmaster!" 
 *             onclick="mailForm.open()">
 *    </form>
 *
 *  Note:  According to the HTML 4.0 specification, the <form> tag is 
 *         not needed in the above code, but a lone <input> tag will 
 *         not render in Netscape Navigator 4 without it.
 *
 *  Example 3:  mailFormTest3.html
 *
 *    <!-- groups are configurable -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      var mailForm = new MailForm();
 *      mailForm.addRecipient(
 *        "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      mailForm.addRecipient(
 *        "Human Resources", "ohr@bgnet.bgsu.edu" );
 *      mailForm.addRecipient(
 *        "Registration and Records", "registrar@bgnet.bgsu.edu" );
 *      mailForm.addRecipient(
 *        "Tech Support Center", "tsc@bgnet.bgsu.edu" );
 *      mailForm.addRecipient(
 *        "Webmaster", "webmaster@bgnet.bgsu.edu" );
 *      mailForm.addGroup( "Prospective Student" );
 *      mailForm.addGroups( "Parent", "Guest" );
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:mailForm.open()">Contact us!</a>
 *
 *  Example 4:  mailFormTest4.html
 *
 *    <!-- groups may be eliminated altogether -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      var mailForm = new MailForm();
 *      mailForm.addRecipient(
 *        "Tech Support Center", "tsc@bgnet.bgsu.edu" );           
 *      mailForm.setGroupLineDisplayed( false );
 *    </script>
 *    <!-- Put this form in the <body> of your document -->
 *    <form>
 *      <input type="button" value="Contact the TSC!" 
 *             onclick="mailForm.open()">
 *    </form>
 *
 *  Example 5:  mailFormTest5.html
 *
 *    <!-- a single group will be hidden from the user -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      var mailForm = new MailForm();
 *      mailForm.addRecipients(
 *        "Admissions Office", "admissions@bgnet.bgsu.edu",
 *        "Human Resources", "ohr@bgnet.bgsu.edu",
 *        "Registration and Records", "registrar@bgnet.bgsu.edu",
 *        "Tech Support Center", "tsc@bgnet.bgsu.edu",
 *        "Webmaster", "webmaster@bgnet.bgsu.edu" );           
 *      // a single group will not be shown on the form:
 *      mailForm.addGroup( "student" );
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:mailForm.open()">Contact us!</a>
 *
 *  Example 6:  mailFormTest6.html
 *
 *    <!-- the sender may be fixed, rather than input -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      var mailForm = new MailForm();
 *      mailForm.addRecipient(
 *        "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      // sender could be used in an authenticated environment:
 *      mailForm.setMailFromAddress( "someone@somewhere.com" );
 *    </script>
 *    <!-- Put this form in the <body> of your document -->
 *    <form>
 *      <input type="button" value="Contact Admissions!" 
 *             onclick="mailForm.open()">
 *    </form>
 *
 *  Example 7:  mailFormTest7.html
 *
 *    <!-- the default text in the mailFrom field may be modified -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm.js">
 *    </script>
 *    <script type="text/javascript">
 *      var mailForm = new MailForm();
 *      mailForm.addRecipients(
 *        "Admissions Office", "admissions@bgnet.bgsu.edu",
 *        "Human Resources", "ohr@bgnet.bgsu.edu",
 *        "Registration and Records", "registrar@bgnet.bgsu.edu",
 *        "Tech Support Center", "tsc@bgnet.bgsu.edu",
 *        "Webmaster", "webmaster@bgnet.bgsu.edu" );           
 *      // Use with care.  For example, the default text should not
 *      // take the form of an email address (someone@somewhere)
 *      // since this might confuse the user.
 *      mailForm.setMailFromDefaultText( "Your email here" );  
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:mailForm.open()">Contact us!</a>
 *
 */

// This script uses a PopUpWindow object:
if ( window.PopUpWindow == null ) {
  document.writeln( '<script type="text/javascript"' );
  document.writeln( '        src="http://www.bgsu.edu/scripts/popUpWindow.js">' );
  document.writeln( '<\/script>' );
}

// This script uses isEmail() from validation.js:
if ( window.isEmail == null ) {
  document.writeln( '<script type="text/javascript"' );
  document.writeln( '        src="http://intranet.bgsu.edu/scripts/validation.js">' );
  document.writeln( '<\/script>' );
}


// Recipient object constructor:
function Recipient( name, email ) {
  this.name = name;
  this.email = email;
}


// MailForm object constructor:
function MailForm() {
  this.recipients = new Array();
  this.groups = new Array();
  this.mailFromAddress = "";
  this.groupLineDisplayed = true;
  this.mailFromDefaultText = "Enter your email address"; 
  this.mailFormWin = null;
}


// Necessary for older browsers (NN3) to create prototype property:
new MailForm();

// PRIVATE methods:

// PRIVATE: Add default groups:
function MailForm_private_setGroupsToDefaults() {
  this.addGroups(
    'BGSU Student',
    'BGSU Faculty',
    'BGSU Staff',
    'BGSU Alumni',
    'Prospective Student',
    'Parent',
    'Guest' );
}
MailForm.prototype._setGroupsToDefaults 
  = MailForm_private_setGroupsToDefaults;


// PRIVATE: Write group options into mail form:
function MailForm_private_writeGroupOptions() {
  for (var i = 0; i < this.groups.length; ++i) 
    this.mailFormWin.document.writeln( '                 <option>' 
      + this.groups[i] + '</option>' );
}
MailForm.prototype._writeGroupOptions 
  = MailForm_private_writeGroupOptions;


// PRIVATE: Write group selection line:
function MailForm_private_writeGroupLine() {
  var doc = this.mailFormWin.document;
    
  if ( this.groupLineDisplayed == true ) {
    if ( this.groups.length == 0 ) 
      this._setGroupsToDefaults();
    if ( this.groups.length > 1 ) {
      // this._writeLines( doc,
      doc.writeln( '            <select name="group" size="1">' );
      doc.writeln( '              <option selected value="">Identify yourself:</option>' );
      this._writeGroupOptions();
      doc.writeln( '            </select>' );
    } else 
      doc.writeln( '            <input type="hidden" name="group" value="' + this.groups[0] + '">' );
  } else { 
    // We still allow a single undisplayed group in case 
    // the user thought the line had to be made invisible 
    // for the single group case
    if ( this.groups[0] != null && this.groups.length == 0 ) 
      doc.writeln( '            <input type="hidden" name="group" value="' + this.groups[0] + '">' );
    else 
      doc.writeln( '            <input type="hidden" name="group" value="">' );
  }
}
MailForm.prototype._writeGroupLine = MailForm_private_writeGroupLine;


// PRIVATE: Write "From" line:
function MailForm_private_writeFromLine() {
  var email = "", doc = this.mailFormWin.document;
  
  if (this.mailFromAddress == "") {
    doc.writeln( '          <td><input type="text" name="mailFrom" value = "' 
      + this.mailFromDefaultText + '" size="24" tabindex="2"></td>' );
  } else {
    doc.write( '          <td><input type="hidden" name="mailFrom" value="' );
    
    // Verify email address
    if ( isEmail( this.mailFromAddress ) == "" )        
      doc.write( '">INVALID EMAIL ADDRESS' );
    else
      doc.write( this.mailFromAddress + '">' + this.mailFromAddress );
    doc.writeln( '</td>' );
  }
}
MailForm.prototype._writeFromLine = MailForm_private_writeFromLine;


// PRIVATE: Write the <option> elements into the mail form window:
function MailForm_private_writeRecipientOptions() {
  var name, email, doc = this.mailFormWin.document;
  
  doc.writeln( '              <option selected value = "">Choose a recipient</option>' );
  for ( var i = 0; i < this.recipients.length; ++i ) {
    name = this.recipients[i].name;
    email = this.recipients[i].email;
    doc.writeln( '              <option value="' + email + '">' 
      + name + '</option>' );
  }
}
MailForm.prototype._writeRecipientOptions
  = MailForm_private_writeRecipientOptions;


// PRIVATE: Write "To:" selection component into the mail form window:
function MailForm_private_writeMailToLine() {
  var name = "", email = "";
  var doc = this.mailFormWin.document;
  if ( this.recipients.length > 1 ) {
    doc.writeln( '            <select name="mailTo" size="1" tabindex="1">' );
    this._writeRecipientOptions();
    doc.writeln( '            </select>' );    
  } else if ( this.recipients.length > 0 ) {
    name = this.recipients[0].name;
    email = this.recipients[0].email;
    doc.writeln( '            <input type="hidden" name="mailTo" value="' + email + '">' + name );
  } else 
    doc.writeln( '            <input type="hidden" name="mailTo" value="">NO ADDRESSES PROVIDED' );
}
MailForm.prototype._writeMailToLine 
  = MailForm_private_writeMailToLine;

// PUBLIC methods:

// Set hardcoded "From" address:
function MailForm_setMailFromAddress( address ) {
  this.mailFromAddress = address;
}
MailForm.prototype.setMailFromAddress = MailForm_setMailFromAddress;


// Set "From" line default text:
function MailForm_setMailFromDefaultText( text ) {
  this.mailFromDefaultText = text;
}
MailForm.prototype.setMailFromDefaultText 
  = MailForm_setMailFromDefaultText;


// Add multiple recipients:
function MailForm_addRecipients() {
  var args = MailForm_addRecipients.arguments;

  // is args.length even?
  if ( args.length & 0x1 )
    alert( 'MailForm.addRecipients:\n\n'
      + 'Number of arguments is not even.\n'
      + 'There must be an even number of arguments in '
      + 'name, email address pairs.\n\n' );
    
  for ( var i = 0; i < args.length; i += 2 ) {
    if ( args[i+1] != null  &&  isEmail( args[i+1] ) == "" )
      alert( 'MailForm.addRecipients:\n\n'
        + '"' + args[i+1] + '" is not a valid email address.\n' );

    this.recipients[this.recipients.length] 
      = new Recipient( args[i], args[i+1] );
  }
}
MailForm.prototype.addRecipients = MailForm_addRecipients;


// Add a single recipient:
MailForm.prototype.addRecipient = MailForm_addRecipients;


// Add multiple groups:
function MailForm_addGroups() {
  var args = MailForm_addGroups.arguments;

  for ( var i = 0; i < args.length; ++i ) 
    this.groups[this.groups.length] = args[i];
}
MailForm.prototype.addGroups = MailForm_addGroups;


// Add a single group:
MailForm.prototype.addGroup = MailForm_addGroups;


// Turn group line on or off:
function MailForm_setGroupLineDisplayed( state ) {
  this.groupLineDisplayed = state;
}
MailForm.prototype.setGroupLineDisplayed 
  = MailForm_setGroupLineDisplayed;

  
// Display MailForm window:
function MailForm_open() {
  var doc = null;
  
  this.mailFormWin = new PopUpWindow();

  this.mailFormWin.setWidth( 475 );
  this.mailFormWin.setHeight( 375 );
  this.mailFormWin.setScrollbars( true );
  this.mailFormWin.setResizable( true );
  this.mailFormWin.setStatus( true );
  this.mailFormWin.open();
 
  doc = this.mailFormWin.document;

  doc.open();
  doc.writeln( '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' );
  doc.writeln( '' );
  doc.writeln( '<html>' );
  doc.writeln( '' );
  doc.writeln( '  <head>' );
  doc.writeln( '    <title>BGSU Mail Form</title>' );
  doc.writeln( '    <script type="text/javascript">' );
  doc.writeln( '// Returns true if all form elements pass their respective test:' );
  doc.writeln( 'function checkForm( formObj ) {' );
  doc.writeln( '' );
  doc.writeln( '  // Local variables:' );
  doc.writeln( '  var element, type, name, oldName;' );
  doc.writeln( '  ' );
  doc.writeln( '  // Check all input elements:' );
  doc.writeln( '  var n = formObj.elements.length;' );
  doc.writeln( '  for ( var i = 0; i < n; i++ ) {' );
  doc.writeln( '    element = formObj.elements[i];' );
  doc.writeln( '    type = element.type;' );
  doc.writeln( '    // Ignore buttons and hidden fields:' );
  doc.writeln( '    if ( type == "text" ) {' );
  doc.writeln( '      if ( !checkText( element ) ) return false;' );
  doc.writeln( '    } else if ( type == "textarea" ) {' );
  doc.writeln( '      if ( !checkTextarea( element ) ) return false;' );
  doc.writeln( '    } else if ( type == "select-one" ) {' );
  doc.writeln( '      if ( !checkSelectOne( element ) ) return false;' );
  doc.writeln( '    }' );
  doc.writeln( '  }' );
  doc.writeln( '  ' );
  doc.writeln( '  // All tests were successful:' );
  doc.writeln( '  return true;' );
  doc.writeln( '}' );
  doc.writeln( '' );
  doc.writeln( '// Default function assignments:' );
  doc.writeln( 'var checkText = checkTextGeneric;' );
  doc.writeln( 'var checkTextarea = checkTextGeneric;' );
  doc.writeln( '// Returns true if a generic text element is not empty:' );
  doc.writeln( 'function checkTextGeneric( textObj ) {' );
  doc.writeln( '  if ( isEmpty( textObj ) ) {' );
  doc.writeln( '    window.alert( "Enter " + textObj.name + "!" );' );
  doc.writeln( '    textObj.value = "";' );
  doc.writeln( '    textObj.focus();' );
  doc.writeln( '    return false;' );
  doc.writeln( '  }' );
  doc.writeln( '  return true;' );
  doc.writeln( '}' );
  doc.writeln( '// A function to check select elements with attribute SIZE="1".' );
  doc.writeln( '// Returns true if the user makes a selection other than options[0]:' );
  doc.writeln( 'function checkSelectSpecial( selectObj ) {' );
  doc.writeln( '  if ( selectObj.selectedIndex == 0 ) {' );
  doc.writeln( '    window.alert( "Please select " + selectObj.name + "!" );' );
  doc.writeln( '    selectObj.focus();' );
  doc.writeln( '    return false;' );
  doc.writeln( '  }' );
  doc.writeln( '  return true;' );
  doc.writeln( '}' );
  doc.writeln( '' );
  doc.writeln( '// Returns true if the value of the text element is null or whitespace:' );
  doc.writeln( 'function isEmpty( textObj ) {' );
  doc.writeln( '  var regexp = /^\s*$/;' );
  doc.writeln( '  return regexp.test( textObj.value );' );
  doc.writeln( '}' );
  doc.writeln( '' );
  doc.writeln( '// For backwards compatibility with a previous version of this script:' );
  doc.writeln( '// (Do not use!  Will be removed in a future version!)' );
  doc.writeln( 'var checkSelect = checkSelectSpecial;' );
  doc.writeln( '      // Override default behavior of checkSelectOne():' );
  doc.writeln( '      checkSelectOne = checkSelectSpecial;' );
  doc.writeln( '      function initialize() {' );
  doc.writeln( '        self.focus();' );
  doc.writeln( '        var form = self.document.mailForm;' );
  doc.writeln( '        var selectObj = form.mailTo;' );
  doc.writeln( '        // if there are no options, must be <= 1 recipients ' );
  doc.writeln( '        if ( selectObj.options == null ) {' );
  doc.writeln( '          // move focus to subject if mailFrom hardcoded' );
  doc.writeln( '          if ( form.mailFrom.type == "hidden" ) {' );
  //doc.writeln( '            alert( "mailFrom is hidden" );' );
  doc.writeln( '            form.mailSubject.focus();' );
  doc.writeln( '          } else {' );
  //doc.writeln( '            alert( "mailFrom is not hidden" );' );
  doc.writeln( '            form.mailFrom.select();' );
  doc.writeln( '          }' );
  doc.writeln( '        } else {' );
  doc.writeln( '          selectObj.focus();' );
  doc.writeln( '        }' );
  doc.writeln( '      }' );
  doc.writeln( '      function closeThisWindow() {' );
  doc.writeln( '        self.close();' );
  doc.writeln( '      }' );
  doc.writeln( '    </script>' );
  doc.writeln( '  </head>' );
  doc.writeln( '  ' );
  doc.writeln( '  <body onload="initialize()">' );
  doc.writeln( '    <form name="mailForm" method="post" onsubmit="return checkForm( this );"' );
  doc.writeln( '          action="http://webapps.bgsu.edu/cgi-bin/bgsuform/submit3">' );
  doc.writeln( '      <table>' );
  doc.writeln( '        <caption><big><strong>BGSU Mail Form</strong></big></caption>' );
  doc.writeln( '        <tr>' );
  doc.writeln( '          <td align="right">To:</td>' );
  doc.writeln( '          <td>' );
  this._writeMailToLine();
  doc.writeln( '          </td>' );
  doc.writeln( '          <td align="center">' );
  doc.writeln( '            <input type="submit" value="Send">' );
  doc.writeln( '          </td>' );
  doc.writeln( '        </tr>' );
  doc.writeln( '        <tr>' );
  doc.writeln( '          <td align="right">From:</td>' );
  this._writeFromLine();
  doc.writeln( '          <td align="center">' );
  doc.writeln( '            <input type="reset" value="Cancel" onclick="closeThisWindow()">' );
  doc.writeln( '          </td>' );
  doc.writeln( '        </tr>' );
  doc.writeln( '        <tr>' );
  doc.writeln( '          <td align="right">Subject:</td>' );
  doc.writeln( '          <td><input type="text" name="mailSubject" size="24" tabindex="3"></td>' );
  doc.writeln( '          <td align="center">' );
  this._writeGroupLine();
  doc.writeln( '          </td>' );
  doc.writeln( '        </tr>' );
  doc.writeln( '        <tr>' );
  doc.writeln( '          <td align="center" colspan="3">' );
  doc.writeln( '            <textarea rows="10" cols="50" name="message" tabindex="4"></textarea>' );
  doc.writeln( '          </td>' );
  doc.writeln( '        </tr>' );
  doc.writeln( '        <tr>' );
  doc.writeln( '          <td align="center" colspan="3">' );
  doc.writeln( '            <small>' );
  doc.writeln( '              Bowling Green State University,' );
  doc.writeln( '              Bowling Green, OH 43403 USA<br>' );
  doc.writeln( '              Switchboard: 1-419-372-2531' );
  doc.writeln( '            </small>' );
  doc.writeln( '          </td>' );
  doc.writeln( '        </tr>' );
  doc.writeln( '      </table>' );
  doc.writeln( '    </form>' );
  doc.writeln( '  </body>' );
  doc.writeln( '  ' );
  doc.writeln( '</html>' );
  doc.close();
}
MailForm.prototype.open = MailForm_open;


// Close MailForm window:
function MailForm_close() {
  this.mailFormWin.close();
}
MailForm.prototype.close = MailForm_close;
