/* Support script */

(function() {

function gebid(id)  /* utility function */ {
  return document.getElementById(id);
}

function initFAQ(showall) {
  
  var links = gebid("support").getElementsByTagName("a");
  
  for (var i=0; i<links.length; i++) {
    if (!showall && /#support$/.test(links[i].href)) /* Links back to the top hide the current FAQ */ {
      links[i].onclick = function() {
        var faqDiv = this.parentNode;
        
        while (faqDiv.tagName && faqDiv.tagName.toUpperCase() != "DIV") {
          faqDiv = faqDiv.parentNode;
        }
        
        faqDiv.className = "hidden";
      };
      continue;
    }
    
    if (!showall && links[i].className == "faqlink") /* Set link to show the FAQ */ {
      links[i].onclick = function() {
        var faqId = this.href.replace(/^.*#/, "");
        gebid(faqId).className = "";
      };
    }
  }
}

function hideAnswers(showall) {
  if (showall) {
    return;
  }
  /* Hide the answers to the FAQs */
  
  var supportDivs = gebid("support").getElementsByTagName("div");
  
  for (var i=2;i < supportDivs.length; i++) /* Skip the crumbs & the FAQs themselves */ {
    if ("#" + supportDivs[i].id != document.location.hash) /* Keep the FAQ visible if it was bookmarked */ {
      supportDivs[i].className = "hidden";
    }
  }
}

function addShowHideAll(showall) /* Add a show all/hide all button */ {
  var showHideAll = document.createElement("a");
  showHideAll.id = "showall";
  
  if (showall) {
    showHideAll.href = document.location.href.replace(/\?showall/,"");
    showHideAll.innerHTML = "Hide";  
  } else {
    showHideAll.href = document.location.href.replace(/^([^#]*)/, "$1?showall");
    showHideAll.innerHTML = "Show";
  }
  
  showHideAll.innerHTML += " all <acronym title='frequently asked questions'>FAQs</acronym>.";
  
  document.getElementById("faq").appendChild(showHideAll);
}

window.onload = function() {
  /* The showall flag gets set to true if the page was loaded in showall (i.e. screenreader) mode, with the "?showall" search string appended to the URL */
  
  var showall = (document.location.search == "?showall")?true:false;

  initFAQ(showall);
  hideAnswers(showall);
  addShowHideAll(showall);
};

})();
