/* -*- JavaScript -*-
# The leading '#'s are just for consistency with other text formats
#
# rdf:
# dc:title Comment-form expansion script
# dc:date 2004-03-19
# cvs:date $Date$
# dc:creator http://norman.walsh.name/knows/who#norman-walsh
# dc:description JavaScript for expanding the content form inline.
*/

function inlineComment() {
    var olddiv = document.getElementById("newcomment");

    var div = document.createElement("div");
    div.setAttribute("id", "newcomment");

    var page = document.location.toString().substring(7);
    var pos = page.indexOf("/");
    page = page.substring(pos);
    pos = page.indexOf("."); // just in case there's an extension
    if (pos >= 0) {
	page = page.substring(0,pos);
    }

    var form = document.createElement("form");
    form.setAttribute("action", "/cgi-bin/talkback");
    form.setAttribute("method", "post");

    form.appendChild(formField("sawform", "1"));
    form.appendChild(formField("page", page));
    form.appendChild(formField("okcomment", ""));
    form.appendChild(formField("inline", "1"));

    var table = document.createElement("table");
    table.setAttribute("class", "commentform");
    table.setAttribute("summary", "Form design");
    table.setAttribute("border", "0");

    var tbody = document.createElement("tbody");
    table.appendChild(tbody);

    var label = document.createTextNode("Name:");
    tbody.appendChild(formRow(label, "name", "40", "80"));

    label = document.createElement("span");
    label.appendChild(document.createTextNode("Email"));
    var sup = document.createElement("sup");
    sup.appendChild(document.createTextNode("*"));
    label.appendChild(sup);
    label.appendChild(document.createTextNode(":"));
    tbody.appendChild(formRow(label, "email", "40", "80"));

    var tr = document.createElement("tr");
    var td = document.createElement("td");
    td.appendChild(document.createTextNode("\u00A0"));
    tr.appendChild(td);

    td = document.createElement("td");
    sup = document.createElement("sup");
    sup.appendChild(document.createTextNode("*"));
    td.appendChild(sup);
    td.appendChild(document.createTextNode("Please provide your real email address; it will not be displayed as part of the comment."));
    tr.appendChild(td);
    tbody.appendChild(tr);

    label = document.createTextNode("Homepage:");
    tbody.appendChild(formRow(label, "homepage", "62", "128"));

    tr = document.createElement("tr");
    td = document.createElement("td");
    td.appendChild(document.createTextNode("Comment"));
    sup = document.createElement("sup");
    sup.appendChild(document.createTextNode("**"));
    td.appendChild(sup);
    td.appendChild(document.createTextNode(":"));
    tr.appendChild(td);

    td = document.createElement("td");
    var textarea = document.createElement("textarea");
    textarea.setAttribute("name", "comment");
    textarea.setAttribute("rows", "12");
    textarea.setAttribute("cols", "55");
    td.appendChild(textarea);
    tr.appendChild(td);
    tbody.appendChild(tr);

    tr = document.createElement("tr");
    td = document.createElement("td");
    td.appendChild(document.createTextNode("\u00A0"));
    tr.appendChild(td);

    td = document.createElement("td");
    sup = document.createElement("sup");
    sup.appendChild(document.createTextNode("**"));
    td.appendChild(sup);
    td.appendChild(document.createTextNode("The following markup may be used in the body of the comment: a, abbr, b, br, code, em, i, p, pre, strong,  and var. You can also use character entities. Any other markup will be discarded, including all attributes (except "));
    var code = document.createElement("code");
    code.appendChild(document.createTextNode("href"));
    td.appendChild(code);
    td.appendChild(document.createTextNode(" on "));
    code = document.createElement("code");
    code.appendChild(document.createTextNode("a"));
    td.appendChild(code);
    td.appendChild(document.createTextNode("). Your tag soup will be sanitized with..."));
    var a = document.createElement("a");
    a.setAttribute("href","http://mercury.ccil.org/%7Ecowan/XML/tagsoup/");
    a.appendChild(document.createTextNode("TagSoup"));
    td.appendChild(a);
    tr.appendChild(td);
    tbody.appendChild(tr);
    form.appendChild(table);

    var p = document.createElement("p");
    var input = document.createElement("input");
    input.setAttribute("value", "Preview comment");
    input.setAttribute("type", "submit");
    p.appendChild(input);
    form.appendChild(p);

    div.appendChild(form);

    var parent = olddiv.parentNode;
    parent.insertBefore(div, olddiv);
    parent.removeChild(olddiv);
}

function formRow(label, name, size, maxlength) {
    cookieInfo=readCookie("remember-me");

    var tr = document.createElement("tr");
    var td = document.createElement("td");
    td.appendChild(label);
    tr.appendChild(td);

    td = document.createElement("td");
    var input = document.createElement("input");
    input.setAttribute("name", name);
    input.setAttribute("size", size);
    input.setAttribute("maxlength", maxlength);
    input.setAttribute("value", cookieValue(cookieInfo,name));
    input.setAttribute("type", "text");
    td.appendChild(input);
    tr.appendChild(td);

    return tr;
}

function formField(name, value) {
    var field = document.createElement("input");
    field.setAttribute("name", name);
    field.setAttribute("value", value);
    field.setAttribute("type", "hidden");
    return field
}

function cookieValue(data, name) {
    data = "&" + data + "&";
    var key = "&" + name;
    var pos = data.indexOf(key);
    if (pos >= 0) {
	data = data.substring(pos+key.length+1);
	pos = data.indexOf("&");
	data = data.substring(0,pos);
	return unescape(data);
    }

    return "";
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";

  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

