function ShowNotes(id,text,songs) {
  var note = document.getElementById("Note"+id)
  var ctrl = document.getElementById("NoteCtrl"+id)
  if (note.innerHTML == "") {
    if (songs.length > 0) {
      if (text.length > 0) {
        var tag, multiline = false, foundAtEnd = false;
        var tags = "<br/> <\/p> <\/ol> <\/ul> <\/li>".split(" ");
        for (tag in tags) {
          tag = tags[tag];
          var pos = text.lastIndexOf(tag);
          if (pos >= 0) multiline = true;
          if (pos == text.length - tag.length) foundAtEnd = true;
          //alert(tag+": pos="+pos+", tlen="+text.length+", tag="+tag+
          //      "; mult="+multiline+", atend="+foundAtEnd);
          if (foundAtEnd) break;
        }
        if (multiline) {
          if (!foundAtEnd) text += "<br/>";
        } else
          text += "<br/>";
      }
      plural = songs.indexOf(',') >= 0 ? 's' : '';
      text += "<b>Bhajan" + plural + ":<\/b> " + songs;
    }
    note.innerHTML = text;
    ctrl.innerHTML = "&laquo;";
  } else {
    note.innerHTML = "";
    ctrl.innerHTML = "&raquo;";
  }
  return false;
}
function ValidateOpts() {
  var text, num;
  if ((text = document.Options.from.value) != "") {
    // if the text is a single number larger than 1000
    num = parseInt(text);
    if (num + "" == text && num > 1000) {
      // assume it's a year, and make it a valid date format
      document.Options.from.value = "1/1/" + text;
    }
  }
  if ((text = document.Options.to.value) != "") {
    // if the text is a single number larger than 1000
    num = parseInt(text);
    if (num + "" == text && num > 1000) {
      // assume it's a year, and make it a valid date format
      document.Options.to.value = "12/31/" + text;
    }
  }
  if ((text = document.Options.n.value) != "") {
    num = parseInt(text);
    if (num + "" == text) {
      SetPageSize(num);
    }
    else {
      alert("Invalid number of tapes per page: " + text);
      return false;
    }
  }
  return true;
}
var NumberTapesPerPage = 52;
var PageOptsCoords = { top:0, bottom:0, left:0, right:0 };
var PageOptsClicks = 0;
function PageOptsHandler(e) {
  if (document.all) e = window.event;
  if (++PageOptsClicks <= 1) return;   // ignore the initial click
  if (!inRegion(e.clientX,e.clientY,PageOptsCoords)) {
    document.onclick = null;
    HideElement('PageOpts');
  }
}
function ShowPageOpts(ctrl) {
  var opts = document.getElementById('PageOpts');
  if (opts.style.visibility != 'visible') {
    ShowElement(opts,ctrl,-35,20);
    PageOptsCoords = GetCoords(opts);
    // enable event handler
    PageOptsClicks = 0;
    document.onclick = PageOptsHandler;
  } else {
    // disable event handler
    document.onclick = null;
    HideElement(opts);
  }
  return true;
}
function CustomPageOpts() {
  var elem = document.getElementById('PageOptsList');

  elem.innerHTML =
'<form id=PageOptsForm name=PageOptsForm onSubmit="ValidatePageOpts(); return false;">'+
'  <input type=text name=n value="'+NumberTapesPerPage+'" maxlength="5" class=npp /> tapes'+
'</form>';

  document.PageOptsForm.n.focus();
  document.PageOptsForm.n.select();

  return false;
}
function ValidatePageOpts() {
  var text, num = 0;
  if ((text = document.PageOptsForm.n.value) != "") {
    num = parseInt(text);
    if (num + "" != text) {
      alert("Invalid number of tapes per page: " + text);
      return false;
    }
    if (num != NumberTapesPerPage) {
        GetPage(num);
    }
  }
  HideElement('PageOpts');
  return num != NumberTapesPerPage;
}
function GetPageSize() {
  var value = GetCookie("npp");
  if (!value) value = NumberTapesPerPage;
  document.Options.n.value = value;
}
function SetPageSize(value) {
  SetCookie("npp", value, "");
}
function DefaultPageSize(value) {
  SetCookie("npp", value, "");
}
function InitPage(num) {
  var labels = 'Tapes Results PrevNextTop PrevNextBot SearchBox SearchText Footnotes OptionsTable OptionsRange';
  LoadBackground(labels);
  NumberTapesPerPage = num;
  if (document.Options) {
    GetPageSize();
    document.Search.SearchText.focus();
  }
  return true;
}
