

function set_del_opt( del_op )
{
  var inp1 = document.getElementById("inp_pick_up"),
      inp2 = document.getElementById("inp_deliver"),
      tr, mode;

  if (inp1 && inp2)
  {
    if (del_op == "pu")
    {
      inp1.checked = true;
      inp2.checked = false;
      mode = 1;
    }
    else if (del_op == "del")
    {
      inp1.checked = false;
      inp2.checked = true;
      mode = 2;
    }
    else
    {
      inp1.checked = false;
      inp2.checked = false;
      mode = 0;
    }

    var disp1, disp2, disp3;
    if (mode == 1)  // pick up
    {
      disp1 = "";
      disp2 = "";
      disp3 = "none";
    }
    else if (mode == 2)  // deliver
    {
      disp1 = "";
      disp2 = "none";
      disp3 = "";
    }
    else // none selected
    {
      disp1 = "none";
      disp2 = "none";
      disp3 = "none";
    }

    tr = document.getElementById("tr_name");
    if (tr)  tr.style.display = disp1;
    tr = document.getElementById("tr_addr1");
    if (tr)  tr.style.display = disp3;
    tr = document.getElementById("tr_addr2");
    if (tr)  tr.style.display = disp3;
    tr = document.getElementById("tr_addr3");
    if (tr)  tr.style.display = disp3;
    tr = document.getElementById("tr_postcode");
    if (tr)  tr.style.display = disp3;
    tr = document.getElementById("tr_telephone");
    if (tr)  tr.style.display = disp1;
    tr = document.getElementById("tr_email");
    if (tr)  tr.style.display = disp1;
    tr = document.getElementById("tr_call_me");
    if (tr)  tr.style.display = disp2;
    tr = document.getElementById("tr_comments");
    if (tr)  tr.style.display = disp1;
    tr = document.getElementById("tr_info");
    if (tr)  tr.style.display = disp1;
    tr = document.getElementById("tr_proceed");
    if (tr)  tr.style.display = disp1;

    if (mode != 0)
    {
      var inp = document.getElementById("inp_name");
      if (inp)
      {
        inp.focus();
        inp.select();
      }
    }
  } // if (inp1 && inp2)
}


var purchase_http_req = false;
var purchase_del_type = 0;  // 1 = pick up; 2 = pick up - call when ready; 3 = deliver

function purchase_proceed()
{
  var post_data, inp_data, del_opt = "";
  var inp = document.getElementById("inp_pick_up");

  if (inp && inp.checked)
  {
    del_opt = "pu";
  }
  else
  {
    inp = document.getElementById("inp_deliver");
    if (inp && inp.checked)
    {
      del_opt = "del";
    }
    else
    {
      alert("Could not determine the delivery option.");
      return;
    }
  }
  post_data = "del_opt=" + del_opt;

  inp = document.getElementById("inp_name");
  if (inp)
  {
    inp_data = inp.value;
    if (inp_data == "")
    {
      alert("You must enter your name.");
      inp.focus();
      return;
    }
    post_data += "&name=" + encodeURIComponent(inp_data);
  }
  else // if (inp)
  {
    alert("Could not find the name input.");
    return;
  }

  inp = document.getElementById("inp_telephone");
  if (inp)
  {
    inp_data = inp.value;
    if (inp_data == "")
    {
      alert("You must enter your telephone number.");
      inp.focus();
      return;
    }
    post_data += "&telephone=" + encodeURIComponent(inp_data);
  }
  else // if (inp)
  {
    alert("Could not find the telephone number input.");
    return;
  }

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

  if (del_opt == "pu")
  {
    inp = document.getElementById("chk_call_me");
    if (inp)
    {
      if (inp.checked)
      {
        post_data += "&call_me=on";
        purchase_del_type = 2;
      }
      else
      {
        purchase_del_type = 1;
      }
    }
    else // if (inp)
    {
      alert("Could not find the call me input.");
      return;
    }
  }
  else if (del_opt == "del")
  {
    purchase_del_type = 3;

    inp = document.getElementById("inp_addr1");
    if (inp)
    {
      inp_data = inp.value
      if (inp_data == "")
      {
        alert("You must enter your address.");
        inp.focus();
        return;
      }
      post_data += "&addr1=" + encodeURIComponent(inp_data);
    }
    else // if (inp)
    {
      alert("Could not find the first address line input.");
      return;
    }

    inp = document.getElementById("inp_addr2");
    if (inp)
    {
      inp_data = inp.value
      if (inp_data != "")
      {
        post_data += "&addr2=" + encodeURIComponent(inp_data);
      }
    }
    else // if (inp)
    {
      alert("Could not find the second address line input.");
      return;
    }

    inp = document.getElementById("inp_addr3");
    if (inp)
    {
      inp_data = inp.value
      if (inp_data != "")
      {
        post_data += "&addr3=" + encodeURIComponent(inp_data);
      }
    }
    else // if (inp)
    {
      alert("Could not find the third address line input.");
      return;
    }

    inp = document.getElementById("inp_postcode");
    if (inp)
    {
      inp_data = inp.value
      if (inp_data == "")
      {
        alert("You must enter your postcode.");
        inp.focus();
        return;
      }
      post_data += "&postcode=" + encodeURIComponent(inp_data);
    }
    else // if (inp)
    {
      alert("Could not find the postcode input.");
      return;
    }
  } // else if (del_opt == "del")

  inp = document.getElementById("ta_comments");
  if (inp)
  {
    comments = inp.value;
    if (comments != "")
    {
      post_data += "&comments=" + encodeURIComponent(comments);
    }
  }
  else
  {
    alert("Could not find the comments input.");
    return;
  }

  purchase_http_req = createXMLHttpRequest();

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

    purchase_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";

    process_set_heading("Submitting Online Order");

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

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

} // purchase_proceed()


function purchase_http_req_readychange()
{
  if (purchase_http_req)
  {
    var status_msg = "", err_msg = "", op_errors = false;

    switch (purchase_http_req.readyState)
    {
    case 4:
      var http_status = purchase_http_req.status;
      if ( http_status == 200 )
      {
        var xml_doc = purchase_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 order_number, order_md5, order_msg;
              var order_info = xml_doc.getElementsByTagName("online-order").item(0);
              if (order_info)
              {
                order_number = order_info.getAttribute("order_number");
                order_md5 = order_info.getAttribute("md5");
                order_msg = order_info.firstChild.data;
              }
              cart_empty_on_page();
              purchase_submitted_setup(purchase_del_type, order_number, order_msg, order_md5);
            }
            else
            {
              op_errors = op_status.getElementsByTagName("op-err-msg");
            }
          }
          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
      }
      purchase_http_req = false;
      var 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);
    }
    else if (op_errors)
    {
      if (status_msg == "")  status_msg = "An error occured preventing the online order from being submitted. The error message was:";
      process_set_err_list(op_errors);
    }

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

  } // if (purchase_http_req)
} // purchase_http_req_readychange()


function purchase_submitted_setup(del_type, order_number, order_msg, order_md5)
{
  var i, el;

  for (i=1; i<=3; i++)
  {
    el = document.getElementById("div_submitted_" + i);
    if (el)
    {
      el.style.display = (i == del_type) ? "" : "none";
    }
  }

  el = document.getElementById("b_order_number");
  if (el)
  {
    while (el.firstChild)  el.removeChild(el.firstChild);
    el.appendChild(document.createTextNode(order_number));
  }

  el = document.getElementById("a_view_order");
  if (el)
  {
    el.setAttribute("href", "get_online_order.php?order_number=" + encodeURIComponent(order_number) +
                            "&md5=" + encodeURIComponent(order_md5)
                   );
  }

  // watch out!! buggy crap IE fucks up badly if we try to display the order message
  el = document.getElementById("pre_order_message");
  if (el && !window.ActiveXObject)
  {
    while (el.firstChild)  el.removeChild(el.firstChild);
    el.appendChild(document.createTextNode(order_msg));
  }

  el = document.getElementById("inp_pick_up");
  if (el)  el.checked = false;
  el = document.getElementById("inp_deliver");
  if (el)  el.checked = false;
  el = document.getElementById("tr_name");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_telephone");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_email");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_name");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_addr1");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_addr2");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_addr3");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_postcode");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_call_me");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_comments");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_info");
  if (el)  el.style.display = "none";
  el = document.getElementById("tr_proceed");
  if (el)  el.style.display = "none";

  el = document.getElementById("table_process");
  if (el)  el.style.display = "none";
  el = document.getElementById("div_cart");
  if (el)  el.style.display = "none";
  el = document.getElementById("table_submitted");
  if (el)  el.style.display = "";

} // purchase_submitted_setup()
