function toggleResponses(id, level)
{
   if (document.getElementById("throbber" + id))
   {
      return;
   }
   
   var responses = document.getElementById("responses" + id);
   var expanded = responses.style.display === "block";
   var loaded = responses.hasChildNodes();
   
   if (expanded)
   {
      hideResponses(id);
      return;
   }
   
   if (loaded)
   {
      showResponses(id);
      return;
   }
   
   var throbber = document.createElement("DIV");
   throbber.className = "throbber";
   throbber.id = "throbber" + id;
   responses.parentNode.insertBefore(throbber, responses);
   
   new AsyncInclude("/shared/async/propComments.jsp?parentID=" + id + "&level=" + level, "responses" + id, function() {doResponsesLoaded(id);});
}

function hideResponses(id)
{
   document.getElementById("responses" + id).style.display = "none";
   document.getElementById("showHide" + id).innerHTML = "Show Responses";
}

function showResponses(id)
{
   document.getElementById("responses" + id).style.display = "block";
   document.getElementById("showHide" + id).innerHTML = "Hide Responses";
}

function doResponsesLoaded(id)
{
   setTimeout(function() {showResponsesLoaded(id);}, 250);
}

function showResponsesLoaded(id)
{   
   var throbber = document.getElementById("throbber" + id);
   throbber.parentNode.removeChild(throbber);
   
   showResponses(id);
}

function viewDroppedProp(id, top)
{
   var view = document.getElementById("viewProp" + id);
   view.parentNode.removeChild(view);
   
   var propDrop = document.getElementById("propDrop" + id);
   if (propDrop)
   {
      propDrop.style.display = "block";
   }
   
   document.getElementById("commentHeader" + id).className = top ? "commentHeader commentHeaderTop" : "commentHeader";
   document.getElementById("propComment" + id).style.display = "block";
   if (top)
   {
      document.getElementById("topComment" + id).style.display = "block";
   }
}

function recordPropVote(id, comment, up)
{
   var propDrop = document.getElementById("propDrop" + (comment ? id : "Main"));
   var iFrame = document.getElementById("recordPropVote");
   
   if (!iFrame)
   {
      iFrame = document.createElement("IFRAME");
      iFrame.id = "recordPropVote";
      iFrame.style.display = "none";
      propDrop.appendChild(iFrame);
   }

   disableOnClick(propDrop);
   propDrop.className = "blurred";
   iFrame.src = "/recordPropVote.jsp?type=" + (comment ? "comment" : "prop") + "&id=" + id + "&vote=" + (up ? "up" : "down");

   var submitWithVote = document.getElementById("submitWithVote");
   
   if (submitWithVote)
   {
      submitWithVote.style.display = "none";
      document.getElementById("staticSubmit").style.display = "block";
   }
}

function recordBrowsePropVote(id, up)
{
   var propDrop = document.getElementById("propDrop" + id);
   var propDown = document.getElementById("propDown" + id);
   var iFrame = document.getElementById("recordPropVote");
   
   if (!iFrame)
   {
      iFrame = document.createElement("IFRAME");
      iFrame.id = "recordPropVote";
      iFrame.style.display = "none";
      propDrop.appendChild(iFrame);
   }

   disableOnClick(propDrop);
   propDrop.className = "blurred";
   propDown.style.display = "none";
   iFrame.src = "/recordPropVote.jsp?type=browse&id=" + id + "&vote=" + (up ? "up" : "down");
}

function disableOnClick(propDrop)
{
   for (var ndx = 0; ndx < propDrop.childNodes.length; ndx++)
   {
      if (propDrop.childNodes[ndx].tagName == "A")
      {
         propDrop.childNodes[ndx].onclick = null;
      }
   }
}

function doPropVoteRecorded(idTag, newScore)
{
   document.getElementById("scoreSign" + idTag).innerHTML = newScore == 0 ? "&plusmn;" : (newScore > 0 ? "+" : "");
   document.getElementById("scoreValue" + idTag).innerHTML = addThousandsSeparators(newScore);
   document.getElementById("scorePlural" + idTag).innerHTML = (newScore == 1 || newScore == -1) ? "" : "s";
}

function addThousandsSeparators(score)
{
   var regExp = new RegExp('(-?[0-9]+)([0-9]{3})');
   
   score = score.toString();

   while (regExp.test(score)) {
      score = score.replace(regExp, '$1,$2');
   }
   
   return score;
}

function showMobileReplyBox(id, level)
{
   var commentLinks = document.getElementById("commentLinks" + id);
   var replyBox = document.getElementById("mobileReplyBox");
   
   if (replyBox)
      replyBox.parentNode.removeChild(replyBox);
   else
      replyBox = createMobileReplyBox();
   
   var width = level <= 1 ? INPUT_WIDTH_1 : INPUT_WIDTH_2;

   // elements array used here because Opera was choking on "replyBox.body" and "replyBox.parentID"   
   if (width)
      replyBox.elements[0].style.width = width + "px";
   replyBox.elements[1].value = id;
   commentLinks.parentNode.insertBefore(replyBox, commentLinks.nextSibling);
   replyBox.elements[0].focus();
}

function createMobileReplyBox()
{
   var replyBox = document.createElement("FORM");
   replyBox.id = "mobileReplyBox";
   replyBox.className = "formFactory";
   replyBox.method = "POST";
   replyBox.onsubmit = function() {return submitOnce();};
   
   var replyBody = document.createElement("TEXTAREA");
   replyBody.style.height = INPUT_HEIGHT + "px";
   replyBody.name = "body";
   replyBox.appendChild(replyBody);
   
   var replyParentID = document.createElement("INPUT");
   replyParentID.type = "hidden";
   replyParentID.name = "parentID";
   replyBox.appendChild(replyParentID);
   
   var replySubmit = document.getElementById("staticSubmit").cloneNode(true);
   replySubmit.id = "mobileSubmit";
   replySubmit.getElementsByTagName("BUTTON")[0].onclick = function() {return validateBody(replyBody);};
   replySubmit.style.display = "block";
   replyBox.appendChild(replySubmit);
   
   return replyBox;
}

function validateBody(body)
{
   var trimmed = body.value.replace(/(^\s+|\s+$)/g, "").length;
   
   if (trimmed == 0)
   {
      alert("Please enter a comment in the space provided.");
      body.focus();
   }
   
   return trimmed > 0;
}

function toggleCategories(id)
{
   toggleElementVisibility("categories" + id);
   
   var toggle = document.getElementById("toggle" + id);
   
   toggle.className = toggle.className == "expand" ? "collapse" : "expand";
}

function doThumbnailLoaded()
{
   var element = document.getElementById("fileMetaID");
   
   if (!element || element.value == 0)
   {
      document.getElementById("usingThumbnailtrue").setAttribute("disabled", "true");
      document.getElementById("usingThumbnailfalse").setAttribute("checked", "true");
   }
   else
   {
      document.getElementById("usingThumbnailtrue").click();
   }
}

var importing = false;

function importImage()
{
   if (importing)
      return;
   
   var url = document.getElementById("importImageURL").value;
   
   if (!url)
   {
      alert("Please enter the URL of the image you would like to import.");
      return;
   }
   
   if (url.indexOf("://") != -1 && url.indexOf("http://") != 0)
   {
      alert("Image URL's may start with http:// only.");
      return;
   }
   
   if (url.indexOf("http://") != 0)
      url = "http://" + url;

   importing = true;
   
   document.getElementById("importImageError").style.display = "none";
   document.getElementById("importImageThrobber").style.display = "block";
   
   new AsyncInclude("/shared/async/fetchThumbnail.jsp?url=" + encodeURIComponent(url), "thumbnail", doImportedThumbnailLoaded);
}

function doImportedThumbnailLoaded()
{
   var element = document.getElementById("fileMetaID");
   
   if (!element || element.value == 0)
   {
      document.getElementById("importImageError").style.display = "block";
   }
   else
   {
      document.getElementById("importImage").style.display = "none";
      document.getElementById("thumbnail").style.display = "block";
      document.getElementById("thumbnailOption").style.display = "block";
      document.getElementById("changeThumbnail").style.display = "block";
   }

   document.getElementById("importImageThrobber").style.display = "none";

   var hidden = document.createElement("INPUT");
   hidden.type = "hidden";
   hidden.name = "usedImportImage";
   hidden.value = "true";
   document.propForm.appendChild(hidden);

   importing = false;
}

function showImportForm()
{
   var element = document.getElementById("fileMetaID");
   
   if (element)
      element.value = -1;
   
   document.getElementById("importImageURL").value = "";
   document.getElementById("importImage").style.display = "block";
   document.getElementById("thumbnail").style.display = "none";
   document.getElementById("thumbnailOption").style.display = "none";
   document.getElementById("changeThumbnail").style.display = "none";
}

function showIDOrSpan(type, internal)
{
   document.getElementById("locationLabel").innerHTML = internal ? "Prop ID or URL:" : "Prop URL:";
   document.getElementById("idOrMember").style.display = type == "Member" ? "inline" : "none";
   document.getElementById("idOrSolution").style.display = type == "Solution" ? "inline" : "none";
   document.getElementById("idOrComment").style.display = type == "Comment" ? "inline" : "none";
   
   document.getElementById("typeLabel").innerHTML = type;
}

function showLinkURL(show)
{
   document.getElementById("propURLText").style.display = show == true ? "block" : "none";
   document.getElementById("idOrURL").style.display = show == true ? "block" : "none";
}

function previousStep()
{
   var hidden = document.createElement("INPUT");
   hidden.type = "hidden";
   hidden.name = "edit";
   hidden.value = "true";
   document.propForm.appendChild(hidden);
   document.propForm.submit();
}

function initializeImport(propID, importableProps)
{
   for (var ndx = 0; ndx < importableProps.length; ndx++)
   {
      var anchor = document.createElement("A");
      anchor.href = "javascript:" + (importableProps[ndx].imported ? "remove" : "add") + "Prop(" + propID + ", " + importableProps[ndx].id + ");";
      anchor.className = importableProps[ndx].imported ? "removeProp" : "addProp";
      anchor.appendChild(document.createTextNode(importableProps[ndx].imported ? "Remove from Profile" : "Add to Profile"));
      
      var span = document.createElement("SPAN");
      span.id = "importLink" + importableProps[ndx].id;
      span.appendChild(anchor);
      
      var replyLink = document.getElementById("replyLink" + importableProps[ndx].id);
      replyLink.parentNode.insertBefore(span, replyLink.nextSibling);
   }
}

function addProp(propID, commentID)
{
   new AsyncInclude("/shared/async/importProp.jsp?propID=" + propID + "&commentID=" + commentID + "&add", "importLink" + commentID);
   
   var omniture = setUpOmnitureVariable();
   omniture.events = "event58";
   postToOmniture(omniture);
}

function removeProp(propID, commentID)
{
   new AsyncInclude("/shared/async/importProp.jsp?propID=" + propID + "&commentID=" + commentID + "&remove", "importLink" + commentID);
}
