/**
 * Expands the selected navigation sub-menu.
 */
function expand(id) {
  lists = document.getElementById("navi").getElementsByTagName("UL");
  if (lists != null) {
    for(i=0; i<lists.length; i++) {
      if(lists.item(i).getAttribute("id") == id) {
        lists.item(i).style.display = lists.item(i).style.display == "block" ? "none" : "block";
      } else {
        lists.item(i).style.display = "none";
      }
    }
  }
}











/**
 * Inserts generic initial and terminal BBCode tags on the selection.
 */
function BBCodeBorder(code){
  var bb = document.getElementById("BBCode");
  var start = bb.selectionStart;
  var end = bb.selectionEnd;

  bb.value = bb.value.substring(0, start) + "[" + code + "]" + bb.value.substring(start, end)
    + "[/" + code + "]" + bb.value.substring(end, bb.value.length);
}

/**
 * Inserts the appropriate hyperlink BBCode tags on the selection.
 */
function BBCodeHyper(){
  var bb = document.getElementById("BBCode");
  var start = bb.selectionStart;
  var end = bb.selectionEnd;

  var url = prompt("Enter the page url.", "http://");
  if(url == "http://" || url == ""){
    alert("Please enter a valid URL");
    return;
  }else if(url == null) return;

  var title = start == end ? prompt("Enter the link title.") : bb.value.substring(start, end);
  if(title == null || title == ""){
    bb.value = bb.value.substring(0, start) + "[url]" + url + "[/url]"
      + bb.value.substring(end, bb.value.length);
  }else{
  //MODIFY THIS FOR TWO URL BBCODE TAGS
    bb.value = bb.value.substring(0, start) + "[url=" + url + "]" + title + "[/url]"
      + bb.value.substring(end, bb.value.length);
  }
}

/**
 * Inserts the appropriate image BBCode tags on the selection.
 */
function BBCodeImage(){
  var bb = document.getElementById("BBCode");
  var start = bb.selectionStart;
  var end = bb.selectionEnd;

  var url = prompt("Enter the image location.", "http://");
  if(url == "http://" || url == ""){
    alert("Please enter a valid URL");
    return;
  }else if(url == null) return;
  bb.value = bb.value.substring(0, start) + "[img]" + url + "[/img]" + bb.value.substring(end, bb.value.length);
}

/**
 * Determines whether a text contains invalid characters. Characters in the English ALPHABET are ALWAYS allowed.
 */
function validate(text, allow){
  allow += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for(i=0; i<text.length; i++) if(allow.indexOf(text.charAt(i)) < 0) return false;
  return true;
}