var thediv;
var req;
var topic;

window.addEventListener("load", loadComments, false);
function loadComments()
{
  thediv = document.getElementById('xmlhttprequest');
  topic = thediv.getAttribute('title');
  loadXMLDoc(location.protocol + '//' + location.host + '/fc_comments.cgi', 'topic=' + topic);
}


function trim(string)
{
  if(string)
  {
    return string.replace(/^\s+|\s+$/g, "");
  }
  else
  {
    return "";
  }
}
function resetStyles()
{
  var thing = document.getElementById("formerrs");
  thing.style.display = "none";

  var thing = document.getElementById("name");
  thing.style.borderColor = "#554";
  thing.style.borderWidth = "1px";

  thing = document.getElementById("namelabel");
  thing.style.color = "#777";

  thing = document.getElementById("comment");
  thing.style.borderColor = "#554";
  thing.style.borderWidth = "1px";

  thing = document.getElementById("commentlabel");
  thing.style.color = "#777";
}
function showError(idToShow)
{
  var thing = document.getElementById("formerrs");
  thing.style.display = "inline";

  var thing = document.getElementById(idToShow);
  thing.style.borderColor = "red";
  thing.style.borderWidth = "3px";

  var label = document.getElementById(idToShow + "label");
  label.style.color = "red";
}
function stripSpaces()
{
  document.comment_form.name.value = trim(document.comment_form.name.value);
  document.comment_form.url.value = trim(document.comment_form.url.value);
  document.comment_form.comment.value = trim(document.comment_form.comment.value);
}
function confirmSubmit()
{
  resetStyles();
  stripSpaces();

  var ok = true;
  if(document.comment_form.name.value == ''){showError("name");ok = false;}
  if(document.comment_form.comment.value == ''){showError("comment");ok = false;}
  return ok;
}

// Note: It is essential that the data returned from the server be sent with a Content-Type set to text/xml.
// Content that is sent as text/plain or text/html is not accepted by the instance of the request object.


function processReqChange()
{
  // only if req shows "loaded"
  if(req.readyState == 4)
  {
    var status = document.getElementById('comment_submit_status');
    if(status)
    {
      status.style.visibility = 'hidden';
    }

    // only if "OK"
//    if(req.status == 200)
//    {
//      thediv.innerHTML = req.responseText;
//    }
//    else
//    {
//      thediv.innerHTML = 'Problem: ' + req.statusText;
//    }
    // Our cgi app will send us whatever we should display, error or no error.
    thediv.innerHTML = req.responseText;
  }
}

function loadXMLDoc(url, postString)
{
  var async = true;

  // Mozilla/IE branching.
  if (window.XMLHttpRequest)
  {
    req = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }

  req.onreadystatechange = processReqChange;
  req.open("POST", url, async);
  req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  req.send(postString);
}

function submitComment()
{
  if(confirmSubmit())
  {
    document.getElementById('comment_submit_status').style.visibility = 'visible';
    loadXMLDoc(location.protocol + '//' + location.host + '/fc_comments.cgi',
               'name=' + document.comment_form.name.value + '&comment=' + document.comment_form.comment.value + '&url=' + document.comment_form.url.value + '&topic=' + topic);
  }

  return false;
}

