
// golbal XMLHttpRequest object used when sending the form data
var mail_friend_http_req = false;


// function to show the Tell a Friend entry form and hide the link and status displays
function show_mail_friend()
{
  var tr1 = document.getElementById("mail_friend_link"),
      tr2 = document.getElementById("mail_friend_entry"),
      tr3 = document.getElementById("mail_friend_status");
  if (tr1 && tr2)
  {
    tr1.style.display = "none";
    tr2.style.display = "";
    tr3.style.display = "none";
  }
  else
  {
    alert("Could not find the Tell a Friend entry form.");
  }
}

// function to hide the Tell a Friend form and return to the link display
function hide_mail_friend()
{
  var tr1 = document.getElementById("mail_friend_link"),
      tr2 = document.getElementById("mail_friend_entry"),
      tr3 = document.getElementById("mail_friend_status");
  if (tr1 && tr2 && tr3)
  {
    tr1.style.display = "";
    tr2.style.display = "none";
    tr3.style.display = "none";
  }
  else
  {
    alert("Could not find the Tell a Friend entry form.");
  }
}

// function to send the form data to the server
//  - checks that the required input has been entered
//  - create and initialize the XMLHttpRequest to handle the server request
//  - build the POST data from the form content and send the request
function send_mail_friend()
{
  var inp_name, inp_email, inp_your_name, inp,
      friend_name, friend_email, message, your_name, page_type;

  inp_name = document.getElementById("mail_friend_name");
  if (!inp_name)
  {
    alert("Couldn't read your friend's name from the form.");
    return;
  }
  friend_name = inp_name.value;

  inp_email = document.getElementById("mail_friend_email");
  if (!inp_email)
  {
    alert("Couldn't read your friend's email address from the form.");
    return;
  }
  friend_email = inp_email.value;

  inp = document.getElementById("mail_friend_message");
  if (!inp)
  {
    alert("Couldn't read your message from the form.");
    return;
  }
  message = inp.value;

  inp_your_name = document.getElementById("mail_friend_your_name");
  if (!inp_your_name)
  {
    alert("Couldn't read your name from the form.");
    return;
  }
  your_name = inp_your_name.value;

  inp = document.getElementById("mail_friend_page_type");
  page_type = (inp) ? inp.value : "";
/*
  if (friend_name == "")
  {
    alert("You must enter your friend's name.");
    inp_name.focus();
  }
*/
  if (friend_email == "")
  {
    alert("You must enter your friend's email address.");
    inp_email.focus();
    return;
  }
/*
  if (your_name == "")
  {
    alert("You must enter your name.");
    inp_your_name.focus();
  }
*/
  // create the XMLHttpRequest to handle the server request
  mail_friend_http_req = createXMLHttpRequest();
  if (mail_friend_http_req)
  {
    // initialize the XMLHttpRequest
    mail_friend_http_req.onreadystatechange = mail_friend_readychange;
    mail_friend_http_req.open("POST", "mail_friend.php", true);
    mail_friend_http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    // build the POST data
    var post_data = "href=" + encodeURIComponent(location.href)
                  + "&friend_name=" + encodeURIComponent(friend_name)
                  + "&friend_email=" + encodeURIComponent(friend_email)
                  + "&sender_name=" + encodeURIComponent(your_name);
    if (message != "")  post_data += "&message=" + encodeURIComponent(message);
    if (page_type != "")  post_data += "&page_type=" + encodeURIComponent(page_type);

    mail_friend_http_req.send(post_data);  // submit the XMLHttpRequest
    mail_friend_readychange();

    // show the process status display
    var tr = document.getElementById("mail_friend_status_button");
    if (tr)  tr.style.display = "none";
    tr = document.getElementById("mail_friend_status_err");
    if (tr)  tr.style.display = "none";
    var tr1 = document.getElementById("mail_friend_link"),
        tr2 = document.getElementById("mail_friend_entry"),
        tr3 = document.getElementById("mail_friend_status");
    if (tr1 && tr2 && tr3)
    {
      tr1.style.display = "none";
      tr2.style.display = "none";
      tr3.style.display = "";
    }
  }
  else // if (mail_friend_http_req)
  {
    // browser does not support XMLHttpRequest
    alert("Unable to create XMLHttpRequest. You may need to update your browser.");
  }
}

function mail_friend_readychange()
{
  if (mail_friend_http_req)
  {
    var status_msg = "", err_msg = "", ready_state = mail_friend_http_req.readyState;

    if ( ready_state == 4 )
    {
      var http_status = mail_friend_http_req.status;
      if ( http_status == 200 )
      {
        var xml_doc = mail_friend_http_req.responseXML;
        if ( xml_doc )
        {
          var op_status = xml_doc.getElementsByTagName("op-status").item(0);
          if ( op_status )
          {
            var err_code = op_status.getAttribute("error");

            if ( err_code == "0" )
            {
              status_msg = "An email message has been mailed to your friend. Thank you for spreading the word about the Local Geek and our great products.";
            }
            else
            {
              err_msg = op_status.firstChild.data;
            }
          }
          else // if ( op_status )
          {
            err_msg = "Could not get the op-status element from the response XML document.";
          }
        }
        else // if ( xml_doc )
        {
          err_msg = "Could not get the response XML document.";
        }
      }
      else // if ( http_status == 200 )
      {
        err_msg = "HTTP error #" + http_status;  // http error
      }
      mail_friend_http_req = false;
      var tr = document.getElementById("mail_friend_status_button");
      if (tr)  tr.style.display = "";
    }
    else if ( ready_state < 2 )
      status_msg = "Contacting Server...";
    else if ( ready_state == 2 )
      status_msg = "Server processing request...";
    else if ( ready_state == 3 )
      status_msg = "Receiving response from server...";

    if (err_msg != "")
    {
      mail_friend_set_error_msg(err_msg);
    }
    else
    {
      if (status_msg == "")  status_msg = "Status unknown!";
      mail_friend_set_status_msg(status_msg);
    }

  } // if (mail_friend_http_req)
}

function mail_friend_set_status_msg( msg )
{
  var td = document.getElementById("mail_friend_status_msg");
  if (td)
  {
    var new_node = document.createTextNode(msg);
    while (td.firstChild)  td.removeChild(td.firstChild);
    td.appendChild(new_node);
  }
}

function mail_friend_set_error_msg( msg )
{
  mail_friend_set_status_msg("An error occured preventing the message being sent to your friend. The error message was:");
  var td = document.getElementById("mail_friend_status_err");
  if (td)
  {
    var new_node = document.createTextNode(msg);
    while (td.firstChild)  td.removeChild(td.firstChild);
    td.appendChild(new_node);

    var tr = document.getElementById("mail_friend_status_err");
    if (tr)  tr.style.display = "";
  }
}

