// JavaScript Document

var GREY = "#C0C0C0";
var Namelist, Booklist, Chaplist;
var DisabledLink = "<font style=\"color:" + GREY + ";\">-------- </font><img src=\"" + StyleDir + "/downloadIconDis.gif\" class=\"dlimage\" />";
function writeChapterChooser(book, lang, sizeMB) {
  document.write("<a name=\"audio-installers-chapters\"></a><br>");
  document.write("<table class=\"dltable\" cellpadding=\"4\" frame=\"border\" rules=\"all\"><caption class=\"dlcaption\">" + Title + "</caption><thead class=\"dlhead\"><tr><th>" + C1 + "</th><th>" + C2 + "</th></tr></thead>");
  document.write("<tbody><tr><td>");
  document.write("<table><tr><td>" + Language + ":</td><td style=\"width:200px\"><select id=\"name\" onchange=\"updateBook();\" style=\"width:100%;\"><option value=\"undef\" selected=\"selected\">-- " + Select + " --</option>");
  for (var x=0; x<files.names.length; x++) {
    document.write("<option value=\"" + files.names[x] + "\">" + locale[files.names[x]] + "</option>");
  }
  document.write("</select></td></tr><tr><td>" + Book + ":</td><td><select id=\"book\" onchange=\"updateChapter();\" disabled=\"disabled\" style=\"width:100%;\"><option value=\"undef\" selected=\"selected\">-- " + Select + " --</option>");
  for (var x=0; x<books.length; x++) {
    document.write("<option value=\"" + books[x] + "\">" + locale["b" + books[x]] + "</option>");
  }
  document.write("</select></td></tr><tr><td>" + Chapter + ":</td><td><select id=\"chapter\" onchange=\"updateLink();\" disabled=\"disabled\" style=\"width:100%;\"><option value=\"undef\" selected=\"selected\">-- " + Select + " --</option>");
  for (var chap=1; chap<=150; chap++) {
    document.write("<option value=\"" + chap + "\">" + chap + "</option>");
  }
  document.write("</select></td></tr></table>");
  document.write("</td><td id=\"chaplink\" class=\"dlcol\">" + DisabledLink + "</td></tr></tbody></table><br>");
}

function updateBook() {
  Namelist = document.getElementById("name");
  Booklist = document.getElementById("book");
  Chaplist = document.getElementById("chapter");
  
  document.getElementById("chaplink").innerHTML=DisabledLink;
  Booklist.selectedIndex=0;
  Chaplist.selectedIndex=0;
  var elem = Booklist.firstChild.nextSibling;
  var data = files[Namelist.value + "_books"];
  while(data && elem) {
    for (var x=0; x<data.length; x++) {
      if (data[x]==elem.value) {
        elem.removeAttribute("disabled");
        elem.style.color="#000000"; // IE cludge!!
        break;
      }
    }
    if (x==data.length) {
      elem.setAttribute("disabled", "disabled");
      elem.style.color=GREY; // IE cludge!!
    }
    elem = elem.nextSibling;
  }

  if (Namelist.value!="undef") Booklist.removeAttribute("disabled");
  else Booklist.setAttribute("disabled", "disabled");
  Chaplist.setAttribute("disabled", "disabled");
}

function updateChapter() {
  // IE cludge!!
  var data = files[Namelist.value + "_books"];
  for (var x=0; x<data.length; x++) {if (data[x]==Booklist.value) break;}
  if (x==data.length) {
    updateBook();
    return;
  }
  
  document.getElementById("chaplink").innerHTML=DisabledLink;
  Chaplist.selectedIndex=0;
  var elem = Chaplist.firstChild.nextSibling;
  var data = files[Namelist.value + "_chapters_" + Booklist.value];
  while(data && elem) {
    for (var x=0; x<data.length; x++) {
      if (data[x].split(":")[0]==elem.value) {
        elem.removeAttribute("disabled");
        elem.style.color="#000000"; // IE cludge!!
        break;
      }
    }
    if (x==data.length) {
      elem.setAttribute("disabled", "disabled");
      elem.style.color=GREY; // IE cludge!!
    }
    elem = elem.nextSibling;
  }

  if (Booklist.value!="undef") Chaplist.removeAttribute("disabled");
  else Chaplist.setAttribute("disabled", "disabled");
}

function updateLink() {
  // IE cludge!! (and also gets size)
  var data = files[Namelist.value + "_chapters_" + Booklist.value];
  var size ="~1.5";
  for (var x=0; x<data.length; x++) {
    if (data[x].split(":")[0]==Chaplist.value) {
      size = data[x].split(":")[1];
      break;
    }
  }
  if (x==data.length) {
    updateChapter();
    return;
  }
  
  var link = document.getElementById("chaplink");
  if (Namelist.value=="undef" || Booklist.value=="undef" || Chaplist.value=="undef") {
    link.innerHTML=DisabledLink;
    return;
  }
  
  var chapter = padChapter(Booklist.value, Chaplist.value, Namelist.value);
  var href;
  if (Namelist.value == "tk") href = HBDir + "/audio/" + Namelist.value + "/" + Namelist.value + "-" + Booklist.value + "-" + chapter + ".mp3";
  else href = HBDir + "/audio/" + Namelist.value + "/" + Booklist.value + "/" + Namelist.value + "-" + Booklist.value + "-" + chapter + ".mp3";
  link.innerHTML = "<a class=\"dllink\" href=\"" + href + "\">" + size + " MB <img src=\"" + StyleDir + "/downloadIcon.gif\" class=\"dlimage\" /></a>";
}

function padChapter(bk, ch, name) {
  var chs = "";
  if (bk=="Ps" && ch<100) chs +="0";
  if (ch<10) chs += "0";
  chs += String(ch);
  //if (name!="tk" && bk!="Ps") chs = "0" + chs;
  return chs;
}
