var i;
var year   = new Array();
var decade = new Array();

for (i = 0;   i <= 9;   i++) { year[i] = "190" + i; }
for (i = 10;  i <= 99;  i++) { year[i] = "19"  + i; }
for (i = 100; i <= 109; i++) { year[i] = "200" + (i - 100); }

for (i = 0;   i <=  9;  i++) { decade[i] =  0; }
for (i = 10;  i <= 19;  i++) { decade[i] =  1; }
for (i = 20;  i <= 29;  i++) { decade[i] =  2; }
for (i = 30;  i <= 39;  i++) { decade[i] =  3; }
for (i = 40;  i <= 49;  i++) { decade[i] =  4; }
for (i = 50;  i <= 59;  i++) { decade[i] =  5; }
for (i = 60;  i <= 69;  i++) { decade[i] =  6; }
for (i = 70;  i <= 79;  i++) { decade[i] =  7; }
for (i = 80;  i <= 89;  i++) { decade[i] =  8; }
for (i = 90;  i <= 99;  i++) { decade[i] =  9; }
for (i = 100; i <= 109; i++) { decade[i] = 10; }

function bdoChangeYears_Homepage(src, target, yearA, decadeA)
{
  var j, t, x;

  // Empty the target dropdown
  for (j = target.options.length; j >= 0; j--)
  {
    target.options[j] = null;
  }
  // Re-populate the target dropdown
  x = document.createElement("option");
  x.setAttribute("value", 0);
  t = document.createTextNode("");
  x.appendChild(t);
  target.appendChild(x);
  for (j = 1; j < yearA.length; j++)
  {
    if (decadeA[j] == src.value)
    {
      x = document.createElement("option");
      x.setAttribute("value", j - (decadeA[j] * 10) + 1);
      t = document.createTextNode(yearA[j]);
      x.appendChild(t);
      target.appendChild(x);
    }
  }
}

function bdoCheckDropdownChange_Homepage(id)
{
	var b, c, j, o, s, ss, t, x, y;

	x = document.getElementById(id);
	if (x.value == "year")
	{
    ss = document.getElementById('st');            // Remove text input
    ss.parentNode.removeChild(ss);

    b = document.getElementById('srchbtn');        // Remove button
    b.parentNode.removeChild(b);

    c = document.createElement("select");          // Create decades dropdown
    c.setAttribute("id",   "sd");
    c.setAttribute("name", "sd");
    c.setAttribute("size", "1");
    for (j = 0; j <= 9; j++)
    {
      o = document.createElement("option");
      o.setAttribute("value", j);
      t = document.createTextNode("19" + j + "0s");
      o.appendChild(t);
      c.appendChild(o);
    }
    o = document.createElement("option");
    o.setAttribute("value", 10);
    t = document.createTextNode("2000s");
    o.appendChild(t);
    c.appendChild(o);
    // c.style.width    = "80px";
    // c.style.position = "relative";
    // c.style.right    = "3px";

    y = document.createElement("select");          // Create years 1900-1909 dropdown
    y.setAttribute("id",   "sy");
    y.setAttribute("name", "sy");
    y.setAttribute("size", "1");
    o = document.createElement("option");
    o.setAttribute("value", 0);
    t = document.createTextNode("");
    o.appendChild(t);
    y.appendChild(o);
    for (j = 1; j <= 10; j++)
    {
      o = document.createElement("option");
      o.setAttribute("value", j);
      t = document.createTextNode("190" + (j - 1));
      o.appendChild(t);
      y.appendChild(o);
    }
    y.style.width = "80px";

    function bdoOnChange(e)
    {
      var z = document.getElementById('searchform');
      bdoChangeYears_Homepage(z.sd, z.sy, year, decade);
    }
    c.onchange = bdoOnChange;

    ss = document.getElementById('sp2');
    ss.appendChild(c);                             // Insert decade dropdown
    ss = document.getElementById('sp3');
    ss.appendChild(y);                             // Insert years 1900-1909 dropdown
    ss = document.getElementById('sp4');
    ss.appendChild(b);                             // Replace button
	}
	else
	{
    c = document.getElementById('sd');
    if ((typeof(c) != 'undefined') && (typeof(c) != 'null') && (c !== undefined) && (c !== null))
    {
      ss = document.getElementById('sy');          // Remove year dropdown
      ss.parentNode.removeChild(ss);
      ss = document.getElementById('sd');          // Remove decade dropdown
      ss.parentNode.removeChild(ss);

      b = document.getElementById('srchbtn');      // Remove button
      b.parentNode.removeChild(b);

      s = document.createElement("input");
      s.setAttribute("id",   "st");
      s.setAttribute("name", "search_text");
      s.setAttribute("type", "text");
      s.setAttribute("size", "30");
      // s.style.width    = "154px";
      // s.style.position = "relative";
      // s.style.left     = "3px";

      ss = document.getElementById('sp2');         // Insert text input
      ss.appendChild(s);
      ss = document.getElementById('sp4');         // Replace button
      ss.appendChild(b);
	  }
	  setTimeout(bdoSearchFocus, 0);
	}
}


function bdoSearchFocus()
{
	var x;
  x = document.getElementById('st');
  if ((typeof(x) != 'undefined') && (typeof(x) != 'null') && (x !== undefined) && (x !== null))
  {
    x.focus();
    x.value = x.value;
  }
}


// BDO: The functions below here are used by search_display.php


function bdoChangeYears(yearA, decadeA)
{
  var j, k, t, x, src, target;

  src    = document.getElementById('sd');
  target = document.getElementById('sy');

  // Empty the target dropdown
  for (j = target.options.length; j >= 0; j--)
  {
    target.options[j] = null;
  }
  // Re-populate the target dropdown
  x = document.createElement("option");
  x.setAttribute("value", 0);
  t = document.createTextNode(" ");
  x.appendChild(t);
  target.appendChild(x);
  for (j = 1; j < yearA.length; j++)
  {
    if (decadeA[j] == src.value)
    {
      x = document.createElement("option");
      x.setAttribute("value", j - (decadeA[j] * 10) + 1);
      t = document.createTextNode(yearA[j]);
      x.appendChild(t);
      target.appendChild(x);
    }
  }
  target.style.width = "80px";

  j = document.getElementById('sdx');
  j.value = src.value;
  k = document.getElementById('syx');
  k.value = 0;
}

function bdoSelectYear(src)
{
  var x = document.getElementById('syx');
  x.value = src.value;
}

function bdoCheckDropdownChange(id)
{
  var b, c, j, o, s, ss, t, x, y;

  x = document.getElementById(id);
  if (x.value == "year")
  {
    ss = document.getElementById('st');            // Remove text input
    ss.parentNode.removeChild(ss);

    b = document.getElementById('srchbtn');        // Remove button
    b.parentNode.removeChild(b);

    c = document.createElement("select");          // Create decades dropdown
    c.setAttribute("id",   "sd");
    c.setAttribute("size", "1");
    for (j = 0; j <= 9; j++)
    {
      o = document.createElement("option");
      o.setAttribute("value", j);
      t = document.createTextNode("19" + j + "0s");
      o.appendChild(t);
      c.appendChild(o);
    }
    o = document.createElement("option");
    o.setAttribute("value", 10);
    t = document.createTextNode("2000s");
    o.appendChild(t);
    c.appendChild(o);
    // c.style.width    = "80px";
    // c.style.position = "relative";
    // c.style.right    = "6px";

    y = document.createElement("select");          // Create years 1900-1909 dropdown
    y.setAttribute("id",   "sy");
    y.setAttribute("size", "1");
    o = document.createElement("option");
    o.setAttribute("value", 0);
    t = document.createTextNode(" ");
    o.appendChild(t);
    y.appendChild(o);
    for (j = 1; j <= 10; j++)
    {
      o = document.createElement("option");
      o.setAttribute("value", j);
      t = document.createTextNode("190" + (j - 1));
      o.appendChild(t);
      y.appendChild(o);
    }
    y.style.width = "80px";

    function bdoOnDecadeChange(e)
    {
      // var z1 = document.getElementById('sd');   // was searchform
      // var z2 = document.getElementById('sy');
      bdoChangeYears(year, decade);                // was z.sd and z.sy
    }
    c.onchange = bdoOnDecadeChange;

    function bdoOnYearChange(e)
    {
      var z = document.getElementById('sy');
      bdoSelectYear(z);
    }
    y.onchange = bdoOnYearChange;

    ss = document.getElementById('sp2');
    ss.appendChild(c);                             // Insert decade dropdown
    ss = document.getElementById('sp3');
    ss.appendChild(y);                             // Insert years 1900-1909 dropdown
    ss = document.getElementById('sp4');
    ss.appendChild(b);                             // Replace button
  }
  else
  {
    c = document.getElementById('sd');
    if ((typeof(c) != 'undefined') && (typeof(c) != null) && (c != undefined) && (c != null))
    {
      ss = document.getElementById('sy');          // Remove year dropdown
      ss.parentNode.removeChild(ss);
      ss = document.getElementById('sd');          // Remove decade dropdown
      ss.parentNode.removeChild(ss);

      b = document.getElementById('srchbtn');      // Remove button
      b.parentNode.removeChild(b);

      s = document.createElement("input");
      s.setAttribute("id",   "st");
      s.setAttribute("name", "search_text");
      s.setAttribute("type", "text");
      s.setAttribute("size", "30");
      // s.style.width    = "154px";
      // s.style.position = "relative";
      // s.style.left     = "3px";

      ss = document.getElementById('sp2');         // Insert text input
      ss.appendChild(s);
      ss = document.getElementById('sp4');         // Replace button
      ss.appendChild(b);
    }
    //  setTimeout(bdoSearchFocus, 0);
  }
}

