text: function changeFrame1(doSize) {
    var sel = document.getElementById("search1Select");
    var url = sel.options[sel.selectedIndex].value;
    var tf = document.getElementById("tFrame");
    if (tf.src != url) { tf.src = url; }
    if (doSize)
    {
      tf.style.display = "block";
      document.getElementById("bFrame").style.display = "none";
    }
  }
  
  function changeFrame2(doSize) {
    var sel = document.getElementById("search2Select");
    var url = sel.options[sel.selectedIndex].value;
    var bf = document.getElementById("bFrame");
    if (bf.src != url) { bf.src = url; }
    if (doSize)
    {
      bf.style.display = "block";
      document.getElementById("tFrame").style.display = "none";
    }
  }
  
  function flipFrame()
  {
    var tf = document.getElementById("tFrame");
    var bf = document.getElementById("bFrame");
    if (tf.style.display == "none")
    {
      tf.style.display = "block";
      bf.style.display = "none";
    }
    else
    {
      tf.style.display = "none";
      bf.style.display = "block";
    }
  }
  
  function popRenewalDate()
  {
    var renNum = document.getElementById('renewalField');
    var renDate = document.getElementById('getDate');
    var id  = renNum.value;
    renDate.value = "Searching...";
    var req = new XMLHttpRequest();
    req.onreadystatechange = function()
    {
      if (req.readyState == 4)
      {
        var icren = document.getElementById('ICREN');
        var pdusren = document.getElementById('PDUSREN');
        var icrenButton, pdusrenButton;
        if (icren)
        {
          icrenButton = document.getElementById("r" + icren.title);
        }
        if (icren)
        {
          pdusrenButton = document.getElementById("r" + pdusren.title);
        }
        if (req.status == 200)
        {
          var rights = document.getElementsByName("rights");
          var sel = GetCheckedValue(rights);
          var und = document.getElementById('UNDNFI').title;
          renDate.value = req.responseText;
          // PDUS if renewal is on or before the current year minus 69
          // So 1951 and earlier in 2020
          var cutoff = new String(new Date().getFullYear() - 69).slice(-2);
          var pdus = req.responseText.endsWith(cutoff);
          if (und && sel == und) { return; }
          if (pdus)
          {
            if (pdusrenButton) { pdusrenButton.checked = "checked"; }
          }
          else
          {
            if (icrenButton) { icrenButton.checked = "checked"; }
          }
        }
        else
        {
          renDate.value = "";
          if (icrenButton) { icrenButton.checked = ""; }
          if (pdusrenButton) { pdusrenButton.checked = ""; }
        }
      }
    };
    req.open("GET", gCGI + "getRenewalDate?id=" + id, true);
    req.send(null);
  }
  
  function PredictRights(id, year, pub, crown)
  {
    var img = document.getElementById("predictionLoader");
    var req = null;
    var rights = document.getElementsByName("rights");
    var sel = GetCheckedValue(rights);
    // There is a hidden div that tells us which button id is und/nfi
    var und = document.getElementById('UNDNFI').title;
    if (und && sel == und) { return; }
    if (img) { img.style.display = 'block'; }
    var req = new XMLHttpRequest();
    req.onreadystatechange = function()
    {
      if (req.readyState == 4 && req.status == 200)
      {
        if (img) { img.style.display = 'none'; }
        sel = GetCheckedValue(rights);
        // Bail out if und/nfi is selected.
        // May be redundant with above, but better here to avoid race condition.
        if (und && sel == und) { return; }
        if (!und || sel != und)
        {
          if (sel)
          {
            var button = document.getElementById("r" + sel);
            button.checked = "";
          }
          if (req.responseText)
          {
            var button = document.getElementById("r" + req.responseText);
            if (button) { button.checked = "checked"; }
          }
        }
      }
    };
    var url = gCGI + "predictRights?id=" + id + ";year=" + year +
              ";ispub=" + pub + ";crown=" + crown;
    //Debug(url, true);
    req.open("GET", url, true);
    req.send(null);
  }
  
  function PredictDate(id)
  {
    var rf = document.getElementById("renewalField2");
    var year = rf.value;
    var span = document.getElementById("renewalSpan2");
    if (year.length == 0)
    {
      span.innerHTML = '';
      return;
    }
    //var isPub = (document.getElementById('renewalFieldCheckbox2').checked)? 1:0;
    //var cat = SelectedCategory();
    //var isCrown = (cat == 'Crown Copyright')? 1:0;
    var actualPubField = document.getElementById('actualPubDateField');
    var actualPub;
    if (actualPubField)
    {
      actualPub = actualPubField.value;
    }
    span.innerHTML = "Calculating...";
    var req = new XMLHttpRequest();
    req.onreadystatechange = function()
    {
      if (req.readyState == 4)
      {
        if (req.status == 200)
        {
          var txt = req.responseText;
          span.innerHTML = txt;
        }
        else
        {
          span.innerHTML = '';
        }
      }
    };
    var url = gCGI + "predictRights?sys=crmsworld;doyear=1;id=" +
              id + ";year=" + year;
    if (actualPub) url += ";pub=" + actualPub;
    req.open("GET", url, true);
    req.send(null);
  }
  
  function ToggleADD()
  {
    var lab = document.getElementById("renewalFieldLabel");
    var cb = document.getElementById("renewalFieldCheckbox");
    var field = document.getElementById("renewalField");
    var actualPubRow = document.getElementById('actualPubRow');
    lab.innerHTML = (cb.checked)? ((actualPubRow)? "Actual&nbsp;Pub&nbsp;Date":"Publication&nbsp;Date:"):"Author&nbsp;Death&nbsp;Date:";
    field.title = (cb.checked)? "Publication Date":"Author Death Date";
    if (actualPubRow)
    {
      actualPubRow.style.display = (cb.checked)? "none":"table-row";
      var actualPubField = document.getElementById('actualPubDateField');
      if (actualPubField && cb.checked && actualPubField.value)
      {
        // not sure about this; it is destructive
        field.value = actualPubField.value;
        actualPubField.value = "";
      }
    }
  }
  
  function SelectedCategory()
  {
    var cat = document.getElementById("catMenu");
    return cat.options[cat.selectedIndex].value;
  }
  
  // return the value of the radio button that is checked
  // return an empty string if none are checked, or
  // there are no radio buttons
  function GetCheckedValue(radioObj)
  {
    if (!radioObj) { return null; }
    var radioLength = radioObj.length;
    if (radioLength == undefined)
    {
      if (radioObj.checked) { return radioObj.value; }
      else { return null; }
    }
    var i;
    for (i = 0; i < radioLength; i++)
    {
      if (radioObj[i].checked) { return radioObj[i].value; }
    }
    return null;
  }
  
  function zoom(selector,name)
  {
    var val = selector.options[selector.selectedIndex].value;
    setCookie(name,val,365);
    document.location.search = document.location.search;
    document.getElementById('tFrame').contentWindow.location.reload(true);
    //document.getElementById('tFrame').src = document.getElementById('tFrame').src;
  }
  
  function PullPubDate()
  {
    var date = document.getElementById('pubDateSpan').innerHTML;
    var re = /^\s*(-?\d+)(--?\d+)?\s*$/;
    var a = re.exec(date);
    if (a.length > 1) { date = a[1]; }
    else { date = ''; }
    document.getElementById('renewalField').value=date;
  }
  
  function Debug(msg, append)
  {
    var el = document.getElementById('debugArea');
    if (el)
    {
      if (append)
      {
        msg = el.innerHTML + "<br/>\n" + msg;
      }
      el.innerHTML = msg;
    }
  }
  