function comment_handler(post_status, request_uri, id) {
//   alert("Post: " + post_status + " request_uri: " + request_uri);
   // relevant areas: comment_area (div), comment_post_count (span)
   // post_count -> count of total posts
   // post_status = "auth", "login", "post_login", "noauth", "invalid", "post"
   if (post_status == "noauth") {
      post_status = "login";
      first_post_alert();
      document.getElementById("comment_area").innerHTML = "<A HREF='#' onclick='javascript:comment_handler(\"" + post_status + "\", \"" + request_uri + "\", \"" + id + "\"); return(false);'><IMG SRC='/images/css/btn_add_comment.jpg' ALT='Add Comment'></A>";
      return(post_status);
   }
   else if (post_status == "invalid") {
      post_status = "post_login";
      first_post_alert();
      document.AddComment.action = request_uri;
      document.getElementById("comment_area").innerHTML = '<B>Invalid username or password, try again.</B><BR><BR>'
         + 'You may <A HREF="/forum/register.php">create an account</A> or login here:<BR><BR>'
         + '<!-- login form -->'
         + '<table cellpadding="0" cellspacing="3" border="0">'
         + '<tr>'
            + '<td class="smallfont"><label for="navbar_username">User Name</label></td>'
            + '<td><input type="text" class="bginput" style="font-size: 11px" name="vb_login_username" id="navbar_username" size="10" accesskey="u" tabindex="101" value="User Name" onfocus="if (this.value == ' + "'User Name'" + ') this.value = ' + "''" + ';" /></td>'
            + '<td class="smallfont" colspan="2" nowrap="nowrap"><label for="cb_cookieuser_navbar"><input type="checkbox" name="cookieuser" value="1" tabindex="103" id="cb_cookieuser_navbar" accesskey="c" />Remember Me?</label></td>'
         + '</tr>'
         + '<tr>'
            + '<td class="smallfont"><label for="navbar_password">Password</label></td>'

            + '<td><input type="password" class="bginput" style="font-size: 11px" name="vb_login_password" id="navbar_password" size="10" tabindex="102" /></td>'
            + '<td><input type="submit" class="button" value="Log in" tabindex="104" title="Enter your username and password in the boxes provided to login, or click the register button to create a profile for yourself." accesskey="s" /></td>'
         + '</tr>'
         + '</table>'
         + '<input type="hidden" name="s" value="" />'
         + '<input type="hidden" name="do" value="login" />    '
         + '<input type="hidden" name="vb_login_md5password" />'
         + '<input type="hidden" name="vb_login_md5password_utf" />'
         + '<!-- / login form -->';
      return(post_status);
   }
   else if (post_status == "login") {
      post_status = "post_login";
      first_post_alert();
      document.AddComment.action = request_uri;

      document.getElementById("comment_area").innerHTML = REGISTERHTML;
      return(post_status);
   }
   else if (post_status == "post_login") {
      document.AddComment.submit();
   }
   else if (post_status == "auth") {
      post_status = "post";
      first_post_alert();
      document.getElementById("comment_area").innerHTML = ''
         + "<INPUT TYPE=\"hidden\" NAME=\"add_comment_id\" id=\"add_comment_id\" VALUE=\"" + id + "\">"
         + '<textarea name="add_comment_text" cols="60" rows="7" wrap="VIRTUAL" id="add_comment_text"></textarea>'
         + '<P>'
         + "<SPAN id='comment_button'><A HREF='#' onclick='javascript:ajax_comment_handler(); return(false);'><IMG SRC='/images/css/btn_add_comment.jpg' ALT='Add Comment'></A></SPAN>";
      return(post_status);
   }
   else if (post_status == "post") {
      document.AddComment.submit();
   }
   else {
      alert("Invalid post status type!");
   }
   return(post_status);
//   document.getElementById("comment_area").innerHTML = "Blah";
//   document.AddComment.submit();
}

function first_post_alert() {
   if (post_count == 0) {
      document.getElementById("post_count_info").innerHTML = "No comments yet.  Be the first.";
   }
   else {
      comment_text = '<span id="comment_post_count">' + post_count + '</span> comment';
      if (post_count == 1) {
         comment_text += "s";
      }
      comment_text += ' on this item.  <span id="enter_topic_url"><A id="topic_url" HREF="/forum/showthread.php?t=' + topic_id + '">See all posts</A></span></span>';
      document.getElementById("post_count_info").innerHTML = comment_text;
   }
   return false;
}

function ajax_comment_handler() {
   url = "/post_message.php";
   document.getElementById("comment_button").innerHTML = "<IMG SRC='/images/dots.gif' style='margin-top: 10px;'>";
   id = document.getElementById("add_comment_id").value;
   text = document.getElementById("add_comment_text").value;
   if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
   }
   req.open("POST", url, true);
   req.onreadystatechange = ajax_comment_showit;
   req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   req.send("id=" + escape(id) + "&text=" + escape(text));
}

function ajax_comment_showit() {
   if (req.readyState == 4) {
      if (req.status == 200) {
         document.getElementById("comment_button").innerHTML = "<A HREF='#' onclick='javascript:ajax_comment_handler(); return(false);'><IMG SRC='/images/css/btn_add_comment.jpg' ALT='Add Comment'></A>";
         xmlDoc = req.responseXML.documentElement;
         add_comment_text = document.getElementById("add_comment_text").value;
         document.getElementById("add_comment_text").value = "";
         status = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
         post_id = xmlDoc.getElementsByTagName("post_id")[0].childNodes[0].nodeValue;
         post_date = xmlDoc.getElementsByTagName("post_date")[0].childNodes[0].nodeValue;
         post_author = xmlDoc.getElementsByTagName("post_author")[0].childNodes[0].nodeValue;
         topic_id = xmlDoc.getElementsByTagName("topic_id")[0].childNodes[0].nodeValue;
         if (status != 0) {
            alert("Error posting message!  Your session may have timed out (usually after 4 hours).  Please login again and re-post your message.");
         }
         else {
            document.getElementById("insert_post").innerHTML = '<div class="comment" id="' + post_id + '"><span>by <B>' + post_author + '</B> on ' + post_date + '</span><p>' + add_comment_text + '</p><HR></div>' + document.getElementById("insert_post").innerHTML;
            post_count++;
            first_post_alert();
         }
      }
   }
}

