/* Copyright (c) 2006-2007 Ido Kanner under the Modified MIT license
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software (the "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge,  publish, distribute,
 * sub-license, and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

var contact_DEBUG   = false;
var contact_address = location.href.replace(/^(http:\/\/.*?\/).*?$/,"$1");

function contact_printDebug(message)
{
  if (contact_DEBUG)
  {
    Log.add ("Contact: " + message);
    Log.displayLog(); 
  } // if (contact_DEBUG)
} // function contact_printDebug(message

function placeContact()
{
  contact_printDebug("Set contact_address to: " + contact_address);
  contact_printDebug("About to create AJAX to generate the form");
  var contact              = new ikajax(contact_address);
  contact.page             = "contact.rhtml";
  contact.addParam("a", "f");
  contact.method           = ajax_async;
  contact_printDebug("Creating callback function: onContentChanged");
  var nick    = arguments[0] ? arguments[0] : "";
  var email   = arguments[1] ? arguments[1] : "";
  var subject = arguments[2] ? arguments[2] : "";
  var body    = arguments[3] ? arguments[3] : "";

  contact.onContentChanged = function () 
  {
    if (contact.returnContentText != "")
    {
      contact_printDebug("About to write the content of the form: ");
      document.getElementById("contactForm").innerHTML = contact.returnContentText;
      contact_printDebug("Getting the form: \n"         +
                         "nick: ["    + nick    + "]\n" +
                         "email: ["   + email   + "]\n" +
                         "subject: [" + subject + "]\n" +
                         "body: ["    + body    + "]");
      if (nick != null && nick != "")
        document.getElementById("nick").value    = decodeURIComponent(nick);
      if (email != null && email != "")
        document.getElementById("email").value   = decodeURIComponent(email);
      if (subject != null && subject != "")
        document.getElementById("subject").value = decodeURIComponent(subject);
      if (body != null && body != "")
        document.getElementById("body").value    = decodeURIComponent(body);
      contact_printDebug("Wrote the content of the form.");
    }
    else
    {
      contact_printDebug("We do not have the form content yet.");
    }
  };
  
  contact_printDebug("Going to start a communication: ");
  contact.openConnection();
  contact_printDebug("Done.");
}

function sendForm()
{
  function getField(name)
  {
    return document.getElementById(name).value;
  }

  function validKey(name)
  {
    var field = document.getElementById(name);
    if (field.value == "" || field.value.match(/^[\s\n\r].*?$/g))
    {
      contact_printDebug("The field \"" + name + "\" is empty.");
      alert('This field "' + name + '" can not be empty.');
      field.focus();
      return false;
    }
    return true;
  }

  if (! validKey("email") || ! validKey("subject") || ! validKey("body") || ! validKey("turingTest"))
  {
    return false;
  }

  contact_printDebug("Set contact_address to: " + contact_address);
  contact_printDebug("About to create AJAX to send the email");
  var contact              = new ikajax(contact_address);
  contact.page             = "contact.rhtml";
  contact_printDebug("Addign parameters: ");
  contact.addParam("a", "e");
  contact.addParam("nick", getField("nick"));
  contact.addParam("fromEmail", getField("email"));
  contact.addParam("subject", getField("subject"));
  contact.addParam("body", getField("body"));
  contact.addParam("questionAnswer", getField("turingTest"));
  contact.addParam("questionID", getField("questionID"));
  contact.method           = ajax_async;
  contact.head             = ajax_HEADER_post;
  contact_printDebug("About to declear a callback function");
  contact.onContentChanged = function () 
  {
    if (contact.returnContentText != "")
    {
      contact_printDebug("About to know if the message sending was successful: ");
      if (contact.returnContentText.match(/true/))
      {
        contact_printDebug("We have sent the form successfully.");
        document.getElementById("contactPrompts").innerHTML = 
            '<span class="contactDone">Thank you for contacting me.<br/>\n' +
            "I'll answer you as soon as possible.<br/><br/>\n" +
            "Ido</span>";
      }
      else
      {
        var nick    = getField("nick");
        var email   = getField("email");
        var subject = getField("subject");
        var body    = getField("body");
        contact_printDebug("Refreshing the form:\n"       + 
                           "nick: ["    + nick    + "]\n" +
                           "email: ["   + email   + "]\n" +
                           "subject: [" + subject + "]\n" +
                           "body: ["    + body    + "]");

        alert("Sorry, but something went wrong when trying to send your email. \n" +
              "Please see if all of the fields are filled in correctly.\n\n" +
              "Ido");
        placeContact(nick, email, subject, body);
      }
      contact_printDebug("Now we know: " + contact.returnContentText);
    }
    else
    {
      contact_printDebug("We do not know the result yet... ");
    }
  };
  document.getElementById("buttonSep").innerHTML = "&lt;------&lt;" +
        '<img src="/img/throbber.gif" alt="Sending your email ..." title="Sending your email ..." class="throbber" />' +
        "&gt;------&gt;";
  contact_printDebug("Going to start a communication: ");
  contact.openConnection();

  contact_printDebug("Done.");

  return false;
}

