// This file contains the code which I need to generate
// a form that can get submitted without attracting
// comment spam. I rely quite heavily on the
// recommendations found at
//
// http://www.baekdal.com/articles/technology/comment-spam/
//
// as well as adding a few of my own twists; e.g., progressively
// modifying the form.

function addMFReply(locID, msg) {
  var par = $(locID + '_replyp');
  var newNode = document.createElement("span");
  newNode.innerHTML = msg;
  par.appendChild(newNode);
}

function maybeSubmitMF(locID, formParams) {
  var form = document.forms[locID + "_mf"];
  if (form.mf_mf.value == "") {
    alert("I'm really going to need an email address here.");
  } else {
    if (form.mf_mf.value.indexOf("@") == -1) {
      alert("Your email address doesn't seem to be kosher.");
    } else if (form.mf_mf.value.indexOf("@", form.mf_mf.value.indexOf("@") + 1) != -1) {
      // More than one @ sign.
      alert("Your email address doesn't seem to be kosher.");
    } else {
      addMFReply(locID, "Sending your email...");
      var timeID = setInterval(
	function () {
	  addMFReply(locID, ".");
	}, 500);      
      // Call the AJAX function.
      webRequest({
	asynchronous: true,
	onSuccess: function (transport) {
	  clearInterval(timeID);
	  try {
	    pair = transport.responseText.evalJSON(true);
	    if (pair[0] === true) {
	      addMFReply(locID, "done. Thanks! I got your email.");
	    } else if (pair[1]) {
	      addMFReply(locID, "oops! I didn't get your email. " + pair[1]);
	    } else {
	      addMFReply(locID, "oops! I didn't get your email, because something mysterious went wrong. Please try again later.");
	    }
	  }
	  catch (e) {
	    addMFReply(locID, "oops! I don't really know whether I got your email, because something mysterious went wrong. Please try again later.");
	  }
	},
	onFailure: function (transport) {
	  clearInterval(timeID);
	  addMFReply(locID, "oops! Something very bad went wrong. Please try again later.");
	},
	parameters: {
	  op: "admin",
	  mf_action: form.mf_action.value,
	  mf_mf: form.mf_mf.value,
	  mf_ta: formParams.mf_ta.value,
	  mf_extra: formParams.mf_extra.value
	}
      });
    }
  }
}

// There's a bug from hell in trying to set the name attribute
// on form elements in IE; it apparently APPEARS to work, but
// actually doesn't. The hack is that you have to pass a 
// node string to createElement. I can't bear this. Fortunately,
// the prototype library takes care of this.

// It gets worse. If you try to add an onsubmit method and try
// to use a submit button that was added after the form was created,
// it won't do anything. And if you pass an object to a callback
// which was created after the form was created, it won't point
// back to the form appropriately. So instead of using onsubmit,
// we need to use onclick on the submit button, and that callback
// can't reference "this", because it doesn't point to the form.
// And then the other new nodes that are added, like mf_ta
// and mf_extra, aren't accessible via the form object.
// So I'm just going to have to build the input object and then
// set its callback.

function expandMF(locID, bodyPrefix) {
  if (!$(locID+"_mf_body")) {

    var attrs = {};
    // Add the text area.
    var newNode = new Element("textarea", {
      id: locID + "_mf_body",
      rows: "10",
      cols: "30",
      style: "border: 1px solid black",
      name: "mf_ta"});
    newNode.value = bodyPrefix || "";
    $(locID+"_internaldiv").appendChild(newNode);
    attrs.mf_ta = newNode;

    // Add the submit button.

    newNode = document.createElement("div");
    var subP = new Element("p");
    newNode.appendChild(subP);
    var newInput = new Element("input", {
      type: "submit",
      name: 'mf_submit'});
    newInput.value = 'Send that baby!';
    subP.appendChild(newInput);
    newNode.appendChild(new Element("p", {id: locID+"_replyp"}));
    $(locID+"_internaldiv").appendChild(newNode);    

    // Add an invisible text area just to be on the safe side.
    var newNode = new Element("textarea", {
      name: "mf_extra"});
    // And we can't use a class as an attribute because it's
    // a reserved word in some Javascripts.
    newNode.addClassName("mf_extra");
    newNode.value = "This content element cannot be altered";
    $(locID+"_internaldiv").appendChild(newNode);
    attrs.mf_extra = newNode;

    // Finally, add the damn callback.
    newInput.onclick = function () {
      maybeSubmitMF(locID, attrs);
      return false;
    };    
  }
}

// It appears that in IE, you can't add a button after the fact and 
// expect it to as a submit button.

function writeMF(action, locID, bodyPrefix) {
  
  var FHTML = "<form name='"+locID+"_mf' action='cgi-bin/donothing.cgi' method='get'>\n" +
  "<p>Email address: <input type='text' name='mf_mf' id='"+locID+"_mf_mf'>\n" +
  "<div id='"+locID+"_internaldiv'>\n" +
  "<input type='hidden' name='mf_action' value='"+action+"'>\n" +
  "</form>";

  var newNode = document.createElement("div");
  newNode.innerHTML = FHTML;
  
  $(locID).appendChild(newNode);

  var formNode = newNode.firstChild;

  $(locID+"_mf_mf").onfocus = function () {
    expandMF(locID, bodyPrefix);
  };
}
