/*
 * honestJourney.js
 *
 */

/**
 * Add a smiley to the opening window's form.
 **/
function addSmiley (code)
{
  if (document.postMessage != null)
    document.postMessage.body.value += ' ' + code + ' ';
  else if (opener != null && opener.document.postMessage != null)
    opener.document.postMessage.body.value += ' ' + code + ' ';
  return false;
}

function popupSmileyHelp ()
{
  var win =
    window.open ("/help/emoticonPopup.html", "smileys",
                 "width=300,height=300,resizable=yes,scrollbars=yes,status=yes,titlebar=yes");
  win.focus ();
  return false;
}

/**
 * Do some Javascript validation.
 **/
function validatePostMessage ()
{
  var form = document.postMessage;
  if (form != null && form.elements != null)
    {
      var name = form.elements ["name"];
      var subject = form.elements ["subject"];
      var body = form.elements ["body"];

      clearHighlight (name);
      clearHighlight (subject);
      clearHighlight (body);

      var nameMissing = false;
      if (name != null && name.value == "") nameMissing = true;

      var subjectMissing = false;
      if (subject != null && subject.value == "") subjectMissing = true;

      var bodyMissing = false;
      if (body != null && body.value == "") bodyMissing = true;

      if (nameMissing) name.focus ();
      else if (subjectMissing) subject.focus ();
      else if (bodyMissing) body.focus ();

      var msg = "";
      if (nameMissing)
        {
          highlight (name);
          msg += "You have forgotten to specify your name.\n";
        }
      if (subjectMissing)
        {
          highlight (subject)
          msg += "You have forgotten to specify a subject for your topic.\n";
        }
      if (bodyMissing)
        {
          highlight (body);
          msg += "You have forgotten to specify a body for your message.\n";
        }

      if (nameMissing || subjectMissing || bodyMissing)
        {
          alert (msg);
          return false;
        }
      else
        return true;
    };

  return true;
}

function clearHighlight (input)
{
  if (input != null && input.style != null)
    input.style.backgroundColor = "white";
}

function highlight (input)
{
  if (input != null && input.style != null)
    input.style.backgroundColor = "yellow";
}
