var xmlHttp = null;
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch(e) {}
}
if (xmlHttp == null) {
  xmlHttp = new XMLHttpRequest();
}
oktocomment = true;

function processXMLHttp(callname) {
  if (oktocomment) {
    oktocomment = false;
    author = escape(document.getElementById(callname).author.value);
    entry = escape(document.getElementById(callname).entry.value);
    id = document.getElementById(callname).id.value;
    query = "?author" + "=" + author + "&entry=" + entry + "&id=" + id;
    url = "./process.php" + query;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.send(null);
    return (true);
  }
  else {
    return(false);
  }
}

function stateChanged() {
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    if (xmlHttp.responseText=='already') {
      alert ("This comment has already been posted");
    }
    else if (xmlHttp.responseText=='fail') {
      alert ("There was an error adding your comment. You may try again");
    }
    else {
      responseA = xmlHttp.responseText.split('/split/');
      author = unescape(responseA[0]);
      authorA = author.split('+');
      author = authorA.join(' ');
      dates = responseA[1];
      entry = unescape(responseA[2]);
      entryA = entry.split('+');
      entry = entryA.join(' ');
      id = responseA[3];
      //////long
      callname = "comments" + id;
      addcmtlong = document.getElementById(callname).innerHTML;
      addcmtlong = addcmtlong + "<div class='commentblurb'>\n<div class='commentDate'>" + dates + "</div>\n<div class='commentAuthor'>" + author + "</div>\n<div class='commentText'>" + entry + "</div>\n</div>\n";
      document.getElementById(callname).innerHTML=addcmtlong;
      //////short
      callname = "commentshort" + id;
      addcmtshort = document.getElementById(callname).innerHTML;
      addcmtshort = addcmtshort + "<div class='cmtentry'>\n<b>" + author + "</b>: " + entry + "</div>\n";
      document.getElementById(callname).innerHTML=addcmtshort;
    }
    oktocomment = true;
  }
}

function showAdd(commentsfor) {
        callname = "commentsfor" + commentsfor;
        htmlLine = "<div class='commentstuff'><div class='commentdata'><input name='author' type='text' id='author' /><br /><textarea name='entry' id='entry' cols='45' rows='3'></textarea></div>Author:<br />Comment:<div class='cancelsubmit'><div class='cancel' onclick='noAdd(" + commentsfor + ")'>Cancel </div><div class='submit'><input type='text' name='comment' style='display:none' value='1' /><a href='javascript: checkSubmit(" + commentsfor + ")'>Submit Comment</a></div></div></div></div>";
        document.getElementById(callname).innerHTML=htmlLine;
}

function noAdd(commentsfor) {
       callname = "commentsfor" + commentsfor;
       htmlLine = "<div class='toclick' onclick='showAdd(" + commentsfor + ")'>Add a Comment</div></form>";
       document.getElementById(callname).innerHTML=htmlLine;
}

function checkSubmit(formid) {
       callname = "form" + formid;
       author = document.getElementById(callname).author.value;
       entry = document.getElementById(callname).entry.value;
       entrycheck = entry.replace(/ /g, "");
       authorcheck = author.replace(/ /g, "");
       if (entrycheck!='') {
         if (authorcheck=='') {
            agree = confirm("Comment will be posted as Author: 'Anonymous' if you leave the 'Author' field blank");
            if (agree) {
              document.getElementById(callname).author.value='Anonymous';
              //scrollSave(formid);
              if (!processXMLHttp(callname)) {alert ("Please wait before adding another comment");}
              //document.getElementById(callname).submit();
            }
         }
         else {
            //scrollSave(formid);
            if (!processXMLHttp(callname)) {alert ("Please wait before adding another comment");}
            //document.getElementById(callname).submit();
         }
       }
       else {
         alert("Please type something in the 'Comment' field in order to post a comment");
       }
}

/*function scrollSave(scrollinfofor) {
       scallname = "scrollinfofor";
       scallname = scallname.concat(scrollinfofor);
       scrollpos = document.body.scrollTop;
       htmlLine = "<input type='text' id='scrollpos' name='scrollpos' style='display:none' value='";
       htmlLine = htmlLine.concat(scrollpos);
       htmlLine = htmlLine.concat("' />");
       document.getElementById(scallname).innerHTML=htmlLine;
}

function showscrollinfofor(showscrollinfofor) {
       scallname = "showscrollinfofor";
       scallname = scallname.concat(showscrollinfofor);
       scrollpos = document.body.scrollTop;
       htmlLine = "<input type='text' id='scrollpos' name='scrollpos' style='display:none' value='";
       htmlLine = htmlLine.concat(scrollpos);
       htmlLine = htmlLine.concat("' />");
       document.getElementById(scallname).innerHTML=htmlLine;
}*/