var _entriesAry = _entries.split(";");
var _hashedEntryShown = 0;
for (var i = 0;  i < _entriesAry.length;  i++) {
 var id = _entriesAry[i];
 actASAP("submButt"+id, prepCommentButtons, id);
 actASAP("newComment"+id,
  function(funcVar) {
   getEl("newComment"+funcVar).onsubmit = function() {
    return checkFormFields(funcVar);
   };
  }, id);
}

function prepCommentButtons(id) {
 getEl("name"+id).onclick = getEl("name"+id).onkeypress = getEl("email"+id).onclick =
 getEl("email"+id).onkeypress = getEl("city"+id).onclick =
 getEl("city"+id).onkeypress = getEl("comment"+id).onclick =
 getEl("comment"+id).onkeypress = function() { checkIfSubmittable(id, 0); };
 // all activity in form triggers this now create the preview button, seen only if
 // Javascript is enabled
 var prevButton = document.createElement("input");
 prevButton.type = "submit";
 prevButton.id = "prevButt"+id;
 prevButton.name = "prevButt"+id;
 prevButton.value = "Preview";
 prevButton.onclick = function() { return previewSubmit(id); };
 getEl("newCommentText"+id).insertBefore(prevButton, getEl("submButt"+id));
 getEl("prevButt"+id).style.color = getEl("submButt"+id).style.color = "#999999";  
}

function makePostSafe(str) {
 var newStr = str.replace(/&/g, "%26");
 newStr = newStr.replace(/=/g, "%3D");
 return newStr;
}

function previewSubmit(id) {
 var commentOkay = checkFormFields(id);
 var commentPreview = getEl("comment-preview"+id);
 if (commentOkay) { // prepare to build the preview via AJAJ
  var queryStr = "name="+makePostSafe(getEl("name"+id).value)+
   "&email="+makePostSafe(getEl("email"+id).value)+
   "&url="+makePostSafe(getEl("url"+id).value)+
   "&comment="+makePostSafe(getEl("comment"+id).value)+ "&id="+id;
  var getCleanComment = "http://blog.toddstadler.com/js/commentClean.php";
  // ** change for switch to blog.toddstadler.com to BLOG2 ** 
  GoAJAJ(AJAJObj, queryStr, getCleanComment, "POST", function() { buildPreview(id); });
 } else if (commentPreview) {
  commentPreview.parentNode.removeChild(commentPreview);
 }
 return false;
}

function buildPreview(id) {
 var commentPreview = getEl("comment-preview"+id);
 if (!commentPreview) {
  var commentPreview = document.createElement("div");
  commentPreview.className="comment preview";
  commentPreview.id="comment-preview"+id;
 }
 var nameHtml = (!AJAJObj.results.url) ? AJAJObj.results.nam :
  "<a href=\"" + AJAJObj.results.url + "\">" + AJAJObj.results.nam + "</a>";
 var prev = "<p class=\"commentInfo\"><span class=\"commentNum\">" +
  AJAJObj.results.num + "</span><span>" + AJAJObj.results.dat + "</span><span>" +
  AJAJObj.results.tim + "</span><span class=\"noCSS\">:</span></p>" +
  "<div class=\"commentContent\"><p class=\"commentBy\">" + nameHtml +
  " <span>may eventually reply:</span></p><p class=\"commentText\">" +
  AJAJObj.results.com + "<br /><br/><span id=\"previewNote\">" +
  "This is a preview. Your comment hasn't been added yet.</span>" +
  "<span><input type=\"submit\" value=\"Add comment\"" +
  "onclick=\"getEl('newComment"+id+"').submit()\" />" +
  "<input type=\"submit\" id=\"commentCancel\" value=\"Cancel preview\"" +
  "onclick=\"cancelPreview("+id+")\" /></span>" +
  "</p></div><br class=\"clear\" />";
 commentPreview.innerHTML = prev;
 getEl("newComment"+id).parentNode.insertBefore(commentPreview, getEl("newComment"+id));
 window.location.hash = "comment-preview"+id;
}

function cancelPreview(id) {
 var commentPreview = getEl("comment-preview"+id);
 commentPreview.parentNode.removeChild(commentPreview);
}

function checkIfSubmittable(id, returnSomething) {
 var validName = getEl("name"+id).value;
 var validEmail = getEl("email"+id).value.isValidEmail();
 var validText = getEl("comment"+id).value;
 var validSum = (getEl("city"+id).value == "11");
 var maybeMe = ((getEl("name"+id).value=="tODD" || // yeah, I know, not really safe
  getEl("name"+id).value=="Todd" ||
  getEl("url"+id).value.indexOf("toddstadler.com")>=0 ||
  getEl("url"+id).value.indexOf("cockahoop.com")>=0) &&
  getCookie("cockahoopUsername")!="tstadler");
 if (maybeMe) { showLogin(id); }
 var isSubmittable = (validName && validEmail && validText && validSum && !maybeMe);
 getEl("prevButt"+id).style.color = getEl("submButt"+id).style.color =
  (isSubmittable) ? "#000000" : "#999999"; // color buttons give hint on submissability
 if (returnSomething) return isSubmittable;
}

function showLogin(id) {
 getEl("login"+id).style.display = "block";
 getEl("prevButt"+id).style.display = "none";
 getEl("submButt"+id).style.display = "none";
 getEl("newComment"+id).onsubmit = function() { return false; };
 getEl("loginCancel"+id).onclick = function() { return hideLogin(id); };
 getEl("loginButton"+id).onclick = function() {
  var queryStr = "username=" + getEl("username"+id).value + "&password=" +
   getEl("password"+id).value;
  var loginUrl = "http://blog.toddstadler.com/js/login.php";
  // ** change to blog.toddstadler.com for switch to BLOG2 **
  GoAJAJ(AJAJObj, queryStr, loginUrl, "POST", function() {
   return loginResponse(id);
  });
 };
}

function loginResponse(id) {
 if (AJAJObj.results.loginStatus) { hideLogin(id); }  
}

function hideLogin(id) {
 getEl("login"+id).style.display = "none";
 getEl("prevButt"+id).style.display = "inline";
 getEl("submButt"+id).style.display = "inline";
 getEl("newComment"+id).onsubmit = function() { return checkFormFields(id); };
 return false;
}

function formFieldResponse(field,condition,errorStr,fieldDesc) {
 if (!condition) {
  getEl(field).style.backgroundColor = "#FFCCCC";
  getEl(field).style.border = "2px solid #996666";
  if (!errorStr) { getEl(field).focus(); }
  return "\n* "+fieldDesc;
 } else {
  getEl(field).style.backgroundColor = "#FFFFFF";
  getEl(field).style.border = "1px solid #BBBBBB";
  return "";
 }
}

function checkFormFields(id) {
 var errorStr = "";
 errorStr += formFieldResponse("name"+id, getEl("name"+id).value, errorStr, "a name");
 errorStr += formFieldResponse("email"+id, getEl("email"+id).value.isValidEmail(),
  errorStr, "a valid email address");
 errorStr += formFieldResponse("city"+id, (getEl("city"+id).value=="11"), errorStr,
  "the answer to 3+8");
 errorStr += formFieldResponse("comment"+id, getEl("comment"+id).value, errorStr,
  "some sort of comment");
 if (!checkIfSubmittable(id, 1)) {
  alert("Sorry, but your comment wasn't submitted because you didn't give:"+errorStr);
  return false;
 } else {
  return true;
 }
}

function showEntry(el) {
 var parentBox = el.parentNode.parentNode;
 classRemove(parentBox, "entryFront");
 classAdd(parentBox, "entryFrontFull")
 return false;
}

function collapseEntry(entryID) {
 var proceedWithCollapsing = 1;
 if (document.location.hash) {
  var showCommID = document.location.hash.replace(/\D/g, ""); // #comment-1542 -> 1542
  var entryEl = getEl("comment-"+showCommID);
  if (entryEl && !_hashedEntryShown) { // if comment exists (and not yet shown entry), start at that node, work up to entryFront level
   while (entryEl.className != "entryFront")  entryEl = entryEl.parentNode;
   classRemove(entryEl, "entryFront"); 
   classAdd(entryEl, "entryFrontFull");
   proceedWithCollapsing = 0;
   _hashedEntryShown = 1;
  }
 }
 if (proceedWithCollapsing) {
  var contentEl = document.getElementById("entry"+entryID);
  var pctShown = Math.round(contentEl.offsetHeight*100/contentEl.scrollHeight);
  document.write("<span class='expShown'><span class='expShownPct'>"+ pctShown);
  document.write("%</span> of this entry is shown</span>");
 }
}