var g_commentDiv;
var g_serverRequest;
var g_topic;

addEvent(window, "load", initializeCommentsDiv, false);

// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture)
{
  if(elm.addEventListener)
  {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  }
  else if(elm.attachEvent)
  {
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  }
  else
  {
    //alert("Handler could not be removed");
  }
}

// Make request to cgi script to retrieve comments.
function initializeCommentsDiv()
{
  // First we need to spruce up the comment div a bit.
  g_commentDiv = document.getElementById('dynacomments');
  g_commentDiv.style.clear = "both";
  var para = document.createElement('p');
  para.appendChild(document.createTextNode("Loading comments..."));
  para.style.color = "gray";
  para.style.fontSize = "0.8em";
  g_commentDiv.appendChild(para);

  //return;

  // Now fetch and load any existing comments.
  loadXMLDoc(location.protocol + '//' + location.host + '/dynacomments.cgi', 'topic=' + g_topic);
}

function setDynacommentsTopic(topic)
{
  g_topic = topic;
}


function trim(string)
{
  if(string)
  {
    return string.replace(/^\s+|\s+$/g, "");
  }
  else
  {
    return "";
  }
}

function resetStyles()
{
//   var thing = document.getElementById("c__name");
//   thing.style.borderColor = "#554";
//   thing.style.borderWidth = "1px";

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

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

  thing = document.getElementById("c__commentlabel");
  thing.style.color = "black";
}

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

  var label = document.getElementById(idToShow + "label");
  label.style.color = "red";

  var errBox = label.childNodes[1];
  errBox.style.display = "inline";
  errBox.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("c__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 g_serverRequest shows "loaded"
  if(g_serverRequest.readyState == 4)
  {
    var status = document.getElementById('c__submit_status');
    if(status)
    {
      status.style.visibility = 'hidden';
    }

    // only if "OK"
   if(g_serverRequest.status == 200)
   {
     g_commentDiv.innerHTML = g_serverRequest.responseText;
   }
   else
   {
     g_commentDiv.innerHTML = '<p style="font-size:0.8em;color:red;line-height:1.1em;">Sorry, comments are not available at this time.  Please try again later ("' + g_serverRequest.status + ':' + g_serverRequest.statusText + '")</p>';
   }
  }
}

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

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

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

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

  }

  return false;
}

