/*
 *  File:  mailForm2.js
 *
 *  Note:  This script has been deprecated (use mailForm3.js instead)
 *
 *  Send e-mail to selected addresses
 *
 *  Example 1:  mailForm2Test1.html
 *
 *    <!-- a simple mailform with multiple recipients -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm2.js">
 *    </script>
 *    <script type="text/javascript">
 *      recipients[1] = 
 *        new Recipient( "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      recipients[2] = 
 *        new Recipient( "Human Resources", "ohr@bgnet.bgsu.edu" );
 *      recipients[3] = 
 *        new Recipient( "Registration and Records", "registrar@bgnet.bgsu.edu" );
 *      recipients[4] = 
 *        new Recipient( "Tech Support Center", "tsc@bgnet.bgsu.edu" );
 *      recipients[5] = 
 *        new Recipient( "Webmaster", "webmaster@bgnet.bgsu.edu" );
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:openMailForm()">Contact us!</a>
 *
 *  Example 2:  mailForm2Test2.html
 *
 *    <!-- a single recipient renders differently -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm2.js">
 *    </script>
 *    <script type="text/javascript">
 *      recipients[1] = 
 *        new Recipient( "Webmaster", "webmaster@bgnet.bgsu.edu" );
 *    </script>
 *    <!-- Put this form in the <body> of your document -->
 *    <form>
 *      <input type="button" onclick="openMailForm()"
 *             value="Contact the Webmaster!">
 *    </form>
 *
 *  Note:  According to the HTML 4.0 specification, the <form> tag
 *         is not needed in the above example, but a lone <input>
 *         tag will not render in Netscape Navigator 4, for example.
 *
 *  Example 3:  mailForm2Test3.html
 *
 *    <!-- groups are configurable -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm2.js">
 *    </script>
 *    <script type="text/javascript">
 *      recipients[1] =
 *        new Recipient( "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      recipients[2] =
 *        new Recipient( "Human Resources", "ohr@bgnet.bgsu.edu" );
 *      groups[1] = "Prospective Student";
 *      groups[2] = "Parent";
 *      groups[3] = "Guest";
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:openMailForm()">Contact us!</a>
 *
 *
 *  Example 4:  mailForm2Test4.html
 *
 *    <!-- groups may be eliminated altogether -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm2.js">
 *    </script>
 *    <script type="text/javascript">
 *      recipients[1] =
 *        new Recipient( "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      recipients[2] =
 *        new Recipient( "Human Resources", "ohr@bgnet.bgsu.edu" );
 *      displayGroupLine = false;
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:openMailForm()">Contact us!</a>
 *
 *  Example 5: mailForm2Test5.html
 *
 *    <!-- a single group will be hidden from the user -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm2.js">
 *    </script>
 *    <script type="text/javascript">
 *      recipients[1] =
 *        new Recipient( "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      recipients[2] =
 *        new Recipient( "Human Resources", "ohr@bgnet.bgsu.edu" );
 *      groups[1] = "prospective student";
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javasctip:openMailForm()">Contact us!</a>
 *
 *  Example 6: mailFrom2Test6.html
 *
 *    <!-- the sender may be fixed, rather than input -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm2.js">
 *    </script>
 *    <script type="text/javascript">
 *      recipients[1] =
 *        new Recipient( "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      recipients[2] =
 *        new Recipient( "Human Resources", "ohr@bgnet.bgsu.edu" );
 *      // useful in an authenticated environment:
 *      senderEmailAddress = "someone@somewhere";  
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:openMailForm()">Contact us!</a>
 *
 *  Example 7: mailFrom2Test7.html
 *
 *    <!-- the default text in the mailFrom field may be modified -->
 *    <script type="text/javascript"
 *            src="http://www.bgsu.edu/scripts/mailForm2.js">
 *    </script>
 *    <script type="text/javascript">
 *      recipients[1] =
 *        new Recipient( "Admissions Office", "admissions@bgnet.bgsu.edu" );
 *      recipients[2] =
 *        new Recipient( "Human Resources", "ohr@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.
 *      fromLineDefaultText = "Your email here";
 *    </script>
 *    <!-- Put this link in the <body> of your document -->
 *    <a href="javascript:openMailForm()">Contact us!</a>
 *
 */

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

// A Window object:
var mailFormWin = null;

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

// Hard coded sender
var senderEmailAddress = "";

// Directions placed in from text box
var fromLineDefaultText = "Enter your email address"

// A user-specified array of e-mail recipients:
var recipients = new Array();

// A user-specified array of groups:
var groups = new Array();

// Allow group selection?
var displayGroupLine = true;

// Write the <option> elements into the mail form window:
function writeRecipientOptions() {
  var name, email;
  recipients[0] = new Recipient( "Choose a recipient:", "" );
  for ( var i = 0; i < recipients.length; i++ ) {
    name = recipients[i].name;
    email = recipients[i].email;
    mailFormWin.document.write( '              ' );
    mailFormWin.document.write( '<option value="' + email + '">' );
    mailFormWin.document.write( name );
    mailFormWin.document.writeln( '</option>' );
  }
}

// Write "To:" selection component into the mail form window:
function writeMailToLine() {
  var name, email;

  // Use a list for 2+ recipients, plain text for 1, display alert for 0  
  if (recipients.length > 2) {
    mailFormWin.document.writeln( '            <select name="mailTo" size="1" tabindex="1">' );
    writeRecipientOptions();
    mailFormWin.document.writeln( '            </select>' );    
  } else if (recipients.length > 0) {
    name = recipients[1].name;
    email = recipients[1].email;
    mailFormWin.document.writeln( '            <input type="hidden" name="mailTo" value="' + email + '">' + name );
  } else {
    mailFormWin.document.writeln( '            <input type="hidden" name="mailTo" value="">NO ADDRESSES PROVIDED' );
  }
}

// Set groups array to defaults:
function setGroupsToDefaults() {
  groups[1] = "BGSU Student";
  groups[2] = "BGSU Faculty";
  groups[3] = "BGSU Staff";
  groups[4] = "BGSU Alumni";
  groups[5] = "Prospective Student";
  groups[6] = "Parent";
  groups[7] = "Guest";
}

// Write group options into mail form:
function writeGroupOptions() {
  for (var i = 1; i < groups.length; ++i) {
    mailFormWin.document.writeln( '                 <option>' + groups[i] + '</option>' );
  }
}

// Write group selection line:
function writeGroupLine() {
  if (displayGroupLine == true) {
    if (groups[1] == null) {
      setGroupsToDefaults();
    }
    if (groups.length > 2 ) {
      mailFormWin.document.writeln( '            <select name="group" size="1">' );
      mailFormWin.document.writeln( '              <option selected value="">Identify yourself:</option>' );
      writeGroupOptions();
      mailFormWin.document.writeln( '            </select>' );
    } else {
      mailFormWin.document.writeln( '            <input type="hidden" name="group" value="' + groups[1] + '">' );
    }
  } else {
    if (groups[1] != null && groups.length < 2) {
      mailFormWin.document.writeln( '            <input type="hidden" name="group" value="' + groups[1] + '">' );
    } else {
      mailFormWin.document.writeln( '            <input type="hidden" name="group" value="">' );
    }
  }
}

// Write from line:
function writeFromLine() {
  if (senderEmailAddress == "") {
    mailFormWin.document.writeln( '          <td><input type="text" name="mailFrom" value = "' + fromLineDefaultText 
        + '" size="24" tabindex="2"></td>' );
  } else {
    mailFormWin.document.writeln( '          <td><input type="hidden" name="mailFrom" value="' + senderEmailAddress 
        + '">' + senderEmailAddress + '</td>' );
  }
}

// Open the mail form window:
function openMailForm() {
  mailFormWin = new PopUpWindow();
  mailFormWin.setWidth( 465 );
  mailFormWin.setHeight( 375 );
  mailFormWin.setScrollbars( true );
  mailFormWin.setResizable( true );
  mailFormWin.open();
  mailFormWin.document.open();
  mailFormWin.document.writeln( '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' );
  mailFormWin.document.writeln( '' );
  mailFormWin.document.writeln( '<html>' );
  mailFormWin.document.writeln( '' );
  mailFormWin.document.writeln( '  <head>' );
  mailFormWin.document.writeln( '    <title>BGSU Mail Form</title>' );
  mailFormWin.document.writeln( '    <script type="text/javascript"' );
  mailFormWin.document.writeln( '            src="http://www.bgsu.edu/scripts/checkForm.js">' );
  mailFormWin.document.writeln( '    </script>' );
  mailFormWin.document.writeln( '    <script type="text/javascript">' );
  mailFormWin.document.writeln( '      // Override default behavior of checkSelectOne():' );
  mailFormWin.document.writeln( '      var checkSelectOne = checkSelectSpecial;' );
  mailFormWin.document.writeln( '    </script>' );
  mailFormWin.document.writeln( '    <script type="text/javascript">' );
  mailFormWin.document.writeln( '      function initialize() {' );
  mailFormWin.document.writeln( '        self.focus();' );
  mailFormWin.document.writeln( '        var form = self.document.mailForm;' );
  mailFormWin.document.writeln( '        var selectObj = form.mailTo;' );
  mailFormWin.document.writeln( '        // if there are no options, must be <= 1 recipients ' );
  mailFormWin.document.writeln( '        if (selectObj.options == null) {' );
  mailFormWin.document.writeln( '          // move focus to subject if mailFrom hardcoded' );
  mailFormWin.document.writeln( '          if (form.mailFrom != null) {' );
  mailFormWin.document.writeln( '            form.mailFrom.focus();' );
  mailFormWin.document.writeln( '          } else {' );
  mailFormWin.document.writeln( '            form.mailSubject.focus();' );
  mailFormWin.document.writeln( '          }' );
  mailFormWin.document.writeln( '        } else {' );
  mailFormWin.document.writeln( '          selectObj.options[0].selected = true;' );
  mailFormWin.document.writeln( '          selectObj.focus();' );
  mailFormWin.document.writeln( '        }' );
  mailFormWin.document.writeln( '      }' );
  mailFormWin.document.writeln( '      function closeThisWindow() {' );
  mailFormWin.document.writeln( '        self.close();' );
  mailFormWin.document.writeln( '      }' );
  mailFormWin.document.writeln( '    </script>' );
  mailFormWin.document.writeln( '  </head>' );
  mailFormWin.document.writeln( '  ' );
  mailFormWin.document.writeln( '  <body onload="initialize()">' );
  mailFormWin.document.writeln( '    <form name="mailForm" method="post" onsubmit="return checkForm( this )"' );
  mailFormWin.document.writeln( '          action="http://webapps.bgsu.edu/cgi-bin/bgsuform/submit3">' );
  mailFormWin.document.writeln( '      <table>' );
  mailFormWin.document.writeln( '        <caption><big><strong>BGSU Mail Form</strong></big></caption>' );
  mailFormWin.document.writeln( '        <tr>' );
  mailFormWin.document.writeln( '          <td align="right">To:</td>' );
  mailFormWin.document.writeln( '          <td>' );
  writeMailToLine();
  mailFormWin.document.writeln( '          </td>' );
  mailFormWin.document.writeln( '          <td  align="center">' );
  mailFormWin.document.writeln( '            <input type="submit" value="Send">' );
  mailFormWin.document.writeln( '          </td>' );
  mailFormWin.document.writeln( '        </tr>' );
  mailFormWin.document.writeln( '        <tr>' );
  mailFormWin.document.writeln( '          <td align="right">From:</td>' );
  writeFromLine();
  //mailFormWin.document.writeln( '          <td><input type="text" name="mailFrom" size="24" tabindex="2"></td>' );
  mailFormWin.document.writeln( '          <td  align="center">' );
  mailFormWin.document.writeln( '            <input type="reset" value="Cancel" onclick="closeThisWindow()">' );
  mailFormWin.document.writeln( '          </td>' );
  mailFormWin.document.writeln( '        </tr>' );
  mailFormWin.document.writeln( '        <tr>' );
  mailFormWin.document.writeln( '          <td align="right">Subject:</td>' );
  mailFormWin.document.writeln( '          <td><input type="text" name="mailSubject" size="24" tabindex="3"></td>' );
  mailFormWin.document.writeln( '          <td align="center">' );
  writeGroupLine();
  mailFormWin.document.writeln( '          </td>' );
  mailFormWin.document.writeln( '        </tr>' );
  mailFormWin.document.writeln( '        <tr>' );
  mailFormWin.document.writeln( '          <td align="center" colspan="3">' );
  mailFormWin.document.writeln( '            <textarea rows="10" cols="50" name="message" tabindex="4"></textarea>' );
  mailFormWin.document.writeln( '          </td>' );
  mailFormWin.document.writeln( '        </tr>' );
  mailFormWin.document.writeln( '        <tr>' );
  mailFormWin.document.writeln( '          <td align="center" colspan="3">' );
  mailFormWin.document.writeln( '            <small>' );
  mailFormWin.document.writeln( '              Bowling Green State University,' );
  mailFormWin.document.writeln( '              Bowling Green, OH 43403 USA<br>' );
  mailFormWin.document.writeln( '              Switchboard: 1-419-372-2531' );
  mailFormWin.document.writeln( '            </small>' );
  mailFormWin.document.writeln( '          </td>' );
  mailFormWin.document.writeln( '        </tr>' );
  mailFormWin.document.writeln( '      </table>' );
  mailFormWin.document.writeln( '    </form>' );
  mailFormWin.document.writeln( '  </body>' );
  mailFormWin.document.writeln( '  ' );
  mailFormWin.document.writeln( '</html>' );
  mailFormWin.document.close();
}

// Close the mail form window:
function closeMailForm() {
  mailFormWin.close();
}