
var contact_http_req = false;


function send_contact_email()
{
  var post_data = "", inp, inp_data;

  inp = document.getElementById("inp_email_addr");
  if (inp)
  {
    inp_data = inp.value;
    if (inp_data != "")
    {
      if (post_data != "")  post_data += "&";
      post_data += "addr=" + encodeURIComponent(inp_data);
    }
  }
  else // if (inp) -- "inp_email_addr"
  {
    alert("Could not find email address input.");
    return;
  }

  inp = document.getElementById("inp_email_subject");
  if (inp)
  {
    inp_data = inp.value;
    if (inp_data != "")
    {
      if (post_data != "")  post_data += "&";
      post_data += "subject=" + encodeURIComponent(inp_data);
    }
  }
  else // if (inp) -- "inp_email_subject"
  {
    alert("Could not find subject input.");
    return;
  }

  inp = document.getElementById("ta_email_message");
  if (inp)
  {
    inp_data = inp.value;
    while (inp_data.indexOf(" ") == 0) { inp_data = inp_data.substr( 1, inp_data.length - 1 ); }
    while (inp_data.lastIndexOf(" ") == inp_data.length - 1) { inp_data = inp_data.substr( 0, inp_data.length - 1 ); }
    if (inp_data != "")
    {
      if (post_data != "")  post_data += "&";
      post_data += "message=" + encodeURIComponent(inp_data);
    }
    else // if (inp_data != "")
    {
      alert("No message has been entered!");
      inp.focus();
      return;
    }
  }
  else // if (inp) -- "ta_email_message"
  {
    alert("Could not find message input.");
    return;
  }

  contact_http_req = createXMLHttpRequest();

  if (contact_http_req)
  {
    // initialize the XMLHttpRequest
    contact_http_req.onreadystatechange = contact_http_req_readychange;
    contact_http_req.open("POST", "send_email.php", true);
    contact_http_req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );

    contact_http_req.send(post_data);

    var tr = document.getElementById("tr_process_err");
    if (tr)  tr.style.display = "none";
    tr = document.getElementById("tr_process_button");
    if (tr)  tr.style.display = "none";

    var element = document.getElementById("table_email");
    if (element)  element.style.display = "none";
    element = document.getElementById("table_process");
    if (element)  element.style.display = "";

  }
  else // if (contact_http_req)
  {
    alert("Unable to create XMLHttpRequest. You may need to update your browser.");
  }

} // send_contact_email()


function contact_http_req_readychange()
{
  if (contact_http_req)
  {
    var tr, status_msg = "", err_msg = "", op_errors = false;

    switch (contact_http_req.readyState)
    {
    case 4:
      var http_status = contact_http_req.status;
      if ( http_status == 200 )
      {
        var xml_doc = contact_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" )
            {
              var inp;
              inp = document.getElementById("inp_email_addr");
              if (inp)  inp.value = "";
              inp = document.getElementById("inp_email_subject");
              if (inp)  inp.value = "";
              inp = document.getElementById("ta_email_message");
              if (inp)  inp.value = "";
              status_msg = "Your email has been sent successfully. Thank you for your interest.";
            }
            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
      }
      contact_http_req = false;
      tr = document.getElementById("tr_process_button");
      if (tr)  tr.style.display = "";

      break;

    case 3:
      status_msg = "Receiving response from server...";
      break;

    case 2:
      status_msg = "Server processing request...";
      break;

    case 1:
      status_msg = "Contacting Server...";
      break;

    } // switch ()

    if (err_msg != "")
    {
      if (status_msg == "")  status_msg = "An error occured preventing the online order from being submitted. The error message was:";
      process_set_err(err_msg);
    }

    if (status_msg == "")  status_msg = "Status unknown!";
    process_set_status(status_msg);

  } // if (contact_http_req)
} // contact_http_req_readychange()


function contact_process_continue()
{
  var element = document.getElementById("table_process");
  if (element)  element.style.display = "none";
  element = document.getElementById("table_email");
  if (element)  element.style.display = "";
}


