/*************************************************************************
 * FILE:   emailForm.css                                                 *
 * AUTHOR: Justin Spargur                                                *
 * DATE:   04/11/07                                                      *
 *                                                                       *
 * DESCRIPTION:                                                          *
 * This file contains the Javascript used to provide functionality to    *
 * the WYSIWYG email form used in the UA Spending Reduction application. *
 *                                                                       *
 * FUNCTIONS INCLUDED:                                                   *
 *    - initForm       - Used to initialize global variables and to hide *
 *                       no-Javascript elements and display elements     *
 *                       used by Javascript to create the WYSIWYG form.  *
 *    - makeBold       - Sets the selected text to bold.                 *
 *    - makeItalic     - Sets the selected text to italic.               *
 *    - makeUnderline  - Sets the selected text to underline.            *
 *    - makeLeft       - Sets the selected text to left-align.           *
 *    - makeCenter     - Sets the selected text to center-align.         *
 *    - makeRight      - Sets the selected text to right-align.          *
 *    - moveMessage    - Grabs HTML from the WYSIWYG area and places it  *
 *                       in DOM textarea element.                        *
 *                                                                       *
 * GLOBAL VARIABLES:                                                     *
 *    - theForm     => Used to store the DOM form element. Initialized   *
 *                     in the initForm function.                         *
 *    - messageArea => Used to store the DOM content window document     *
 *                     used as the message text area. Initialized in the *
 *                     initForm function.                                *
 *    - isIE        => Used as a browser check variable.                 *
 *    - browserType => Used to store the User Agent of the browser.      *
 *                                                                       *
 * KNOWN BUGS:                                                           *
 *    - None                                                             *
 *                                                                       *
 * ADDITIONAL NOTES:                                                     *
 *    - As of creation, the only file using this was                     *
 *      "../comment.php".                                                *
 *                                                                       *
 * CHANGES MADE:                                                         *
 *    - Discovered that the WYSIWYG form didn't work in the following    *
 *      browsers:                                                        *
 *         - Safari                                                      *
 *         - Netscape Navigator before version 8.                        *
 *      As such, we had to add more browser checking to ensure that      *
 *      users with those browsers could still submit comments using a    *
 *      simple textarea web form.                                        *
 *      Date changed: 04/20/2007                                         *
 *      Modified by: Justin Spargur                                      *
 *                                                                       *
 *************************************************************************/


// Global variables. 
var theForm;
var messageArea;
var isIE;
var isSafari;
var isOpera;
var isNetscape;
var browserType = window.navigator.userAgent;

/****************************************************
 * METHOD NAME: initForm                            *
 *                                                  *
 * DESCRIPTION: Used to initialize global variables *
 *              and to hide no-Javascript elements  *
 *              and display elements used by        *
 *              Javascript to create the WYSIWYG    *
 *              form. This is also where we do our  *
 *              browser checks.                     *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     None                                *
 ****************************************************/
function initForm()
{
   // Figure out some general browser information.
   if(browserType.indexOf("Opera") != -1 )
      isOpera = true;
   else
      isOpera = false;

   if(browserType.indexOf("MSIE") != -1 && !isOpera)
      isIE = true;
   else
      isIE = false;

   if(browserType.indexOf("Safari") != -1 && !isOpera)
      isSafari = true;
   else
      isSafari = false;

   if(browserType.indexOf("Netscape") != -1 && !isOpera)
   {
      // We only care about Netscape before version 8.
      var tmpbrowserType = browserType;
      var pattern = /.*Netscape\/([0-9]).*/;
      if(tmpbrowserType.replace(pattern, "$1") < 8);
         isNetscape = true;
   }
   else
      isNetscape = false;

   // Don't use WYSIWYG for unsupported browsers.
   if(!isSafari && !isNetscape)
   {
      // Hide no-javascript elements.
      document.getElementById("msgCell").style.display = "none";
      document.getElementById("goBackLink").style.display = "none";

      // Display javascript-only elements.
      if(isIE)
      {
         document.getElementById("WYSIWIGCell").style.display = "block";
         document.getElementById("goBackButton").style.display = "inline";
      }
      else
      {
         document.getElementById("WYSIWIGCell").style.display = "table-row";
         document.getElementById("goBackButton").style.display = "inline";
      }

      // Setup the tab index for the WYSIWYG
      document.getElementById("emailMessage").setAttribute('tabindex', document.getElementById("msg").getAttribute('tabindex'));

      // Initialize the global theForm variable.
      theForm = document.getElementById("emailForm");

      // Initialize the global messageArea variable.
      messageArea = document.getElementById("emailMessage").contentWindow.document;

      // Turn on design mode (activate WYSIWYG).
      messageArea.designMode = 'On'; 

     /***********************************
      * If a message has been preset in *
      * the textarea, move it to the    *
      * WYSIWYG message box.            *
      ***********************************/
      if(document.getElementById("msg").value != "")
         messageArea.body.innerHTML = document.getElementById("msg").value;
   }
   return true;
}

function initReset()
{
   // Figure out some general browser information.
   if(browserType.indexOf("Opera") != -1 )
      isOpera = true;
   else
      isOpera = false;

   if(browserType.indexOf("MSIE") != -1 && !isOpera)
      isIE = true;
   else
      isIE = false;

   if(browserType.indexOf("Safari") != -1 && !isOpera)
      isSafari = true;
   else
      isSafari = false;

   if(browserType.indexOf("Netscape") != -1 && !isOpera)
   {
      // We only care about Netscape before version 8.
      var tmpbrowserType = browserType;
      var pattern = /.*Netscape\/([0-9]).*/;
      if(tmpbrowserType.replace(pattern, "$1") < 8);
         isNetscape = true;
   }
   else
      isNetscape = false;

   // Don't use WYSIWYG for unsupported browsers.
   if(!isSafari && !isNetscape)
   {
      // Hide no-javascript elements.
      document.getElementById("goBackLink").style.display = "none";

      // Display javascript-only elements.
      if(isIE)
      {
         //document.getElementById("WYSIWIGCell").style.display = "block";
         document.getElementById("goBackButton").style.display = "inline";
      }
      else
      {
         //document.getElementById("WYSIWIGCell").style.display = "table-row";
         document.getElementById("goBackButton").style.display = "inline";
      }

      // Setup the tab index for the WYSIWYG
      //document.getElementById("emailMessage").setAttribute('tabindex', document.getElementById("msg").getAttribute('tabindex'));

      // Initialize the global theForm variable.
      //theForm = document.getElementById("emailForm");

      // Initialize the global messageArea variable.
      //messageArea = document.getElementById("emailMessage").contentWindow.document;

      // Turn on design mode (activate WYSIWYG).
      //messageArea.designMode = 'On'; 

     /***********************************
      * If a message has been preset in *
      * the textarea, move it to the    *
      * WYSIWYG message box.            *
      ***********************************/
      //if(document.getElementById("msg").value != "")
      //   messageArea.body.innerHTML = document.getElementById("msg").value;
   }
   return true;
}


/****************************************************
 * METHOD NAME: makeBold                            *
 *                                                  *
 * DESCRIPTION: Sets the selected text to bold.     *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     None                                *
 ****************************************************/
function makeBold()
{
   messageArea.execCommand('bold', false, null);
}

/****************************************************
 * METHOD NAME: makeItalic                          *
 *                                                  *
 * DESCRIPTION: Sets the selected text to italic.   *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     None                                *
 ****************************************************/
function makeItalic()
{
   messageArea.execCommand('italic', false, null);
}

/****************************************************
 * METHOD NAME: makeUnderline                       *
 *                                                  *
 * DESCRIPTION: Sets the selected text to           *
 *              underline.                          *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     None                                *
 ****************************************************/
function makeUnderline()
{
   messageArea.execCommand('underline', false, null);
}

/****************************************************
 * METHOD NAME: makeLeft                            *
 *                                                  *
 * DESCRIPTION: Sets the selected text to left-     *
 *              align.                              *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     None                                *
 ****************************************************/
function makeLeft()
{
   messageArea.execCommand('justifyLeft', false, null);
}

/****************************************************
 * METHOD NAME: makeCenter                          *
 *                                                  *
 * DESCRIPTION: Sets the selected text to center-   *
 *              align.                              *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     None                                *
 ****************************************************/
function makeCenter()
{
   messageArea.execCommand('justifyCenter', false, null);
}

/****************************************************
 * METHOD NAME: makeRight                           *
 *                                                  *
 * DESCRIPTION: Sets the selected text to right-    *
 *              align.                              *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     None                                *
 ****************************************************/
function makeRight()
{
   messageArea.execCommand('justifyRight', false, null);
}

/****************************************************
 * METHOD NAME: moveMessage                         *
 *                                                  *
 * DESCRIPTION: Grabs HTML from the WYSIWYG area    *
 *              and places it in DOM textarea.      *
 *                                                  *
 * PARAMETERS:  None                                *
 *                                                  *
 * RETURNS:     Boolean - True on success, false    *
 *                        otherwise.                *
 ****************************************************/
function moveMessage()
{
  /**************************************
   * If a message has been entered and  *
   * it's not just a single line break  *
   * then we'll move it to the DOM      *
   * textarea. Otherwise, we highlight  *
   * the message field and alert the    *
   * user that they need to enter a     *
   * message.                           *
   **************************************/
   if(messageArea.body.innerHTML != "" && messageArea.body.innerHTML != "<br>\n")
   {
      document.getElementById("msg").value = messageArea.body.innerHTML;
      return true;
   }
   else
   {
      document.getElementById("emailMessage").style.border = "2px solid red";
      alert("Please enter a message to be sent.");
      return false;
   }
}
