const notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
const noteOffsets = [-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2];
const octaves = [0, 1, 2, 3, 4, 5, 6];
const octaveOffsets = [-4, -3, -2, -1, 0, 1, 2];

const unitWeightsDAddarioPL = [.00001085, .00001418, .00001601, .00001794, .00001999, .00002215, .00002442, .00002680, .00002930, .00003190, .00003744, .00004037, .00004342, .00004984, .00005671, .00006402, .00007177, .00007997, .00008861, .00010722, .00012760, .00014975];
const gaugesDAddarioPL = [".007", ".008", ".0085", ".009", ".0095", ".010", ".0105", ".011", ".0115", ".012", ".013", ".0135", ".014", ".015", ".016", ".017", ".018", ".019", ".020", ".022", ".024", ".026"];
const unitWeightsDAddarioNW = [.00005524, .00006215, .00006947, .00007495, .00008293, .00009184, .00010857, .00012671, .00014666, .00017236, .00019347, .00021590, .00023964, .00026471, .00027932, .00032279, .00035182, .00038216, .00041382, .00043014, .00048109, .00053838, .00057598, .00064191, .00066542, .00070697, .00074984, .00079889, .00084614, .00089304, .00094124, .00098869, .00115011];
const gaugesDAddarioNW = [".017", ".018", ".019", ".020", ".021", ".022", ".024", ".026", ".028", ".030", ".032", ".034", ".036", ".038", ".039", ".042", ".044", ".046", ".048", ".049", ".052", ".054", ".056", ".059", ".060", ".062", ".064", ".066", ".068", ".070", ".072", ".074", ".080"];

function writeGaugeSelect(span) {
  var markup = "<select class=\"threequarterwidth\" name=\"unitWeight\" size=\"1\"><optgroup label=\"D\'Addario Plain Steel\">";
  for (var a = 0; a < gaugesDAddarioPL.length; a++) {
    markup += "<option value=\"" + unitWeightsDAddarioPL[a] + "\">" + gaugesDAddarioPL[a] + "p</option>";
    }
  markup += "</optgroup><optgroup label=\"D\'Addario Nickel Wound\">";
  for (var a = 0; a < gaugesDAddarioNW.length; a++) {
    markup += "<option value=\"" + unitWeightsDAddarioNW[a] + "\">" + gaugesDAddarioNW[a] + "w</option>";
    }
  markup += "</optgroup></select>";
  document.getElementById(span).innerHTML = markup;
  }

function writeToneSelect(span) {
  var markup = "<select class=\"threequarterwidth monospace\" name=\"tone\" size=\"1\">";
  for (var a = toneArray.length - 1; a >= 0; a--) {
    var marked = false;
    var selected = false;
    if (toneArray[a].name == "E4" ||
        toneArray[a].name == "B3" ||
        toneArray[a].name == "G3" ||
        toneArray[a].name == "D3" ||
        toneArray[a].name == "A2" ||
        toneArray[a].name == "E2") {
      marked = true;
      }
    if (marked) {
      markup += "<option class=\"marked monospace\"";
      }
    else {
      markup += "<option class=\"monospace\"";
      }
    if (toneArray[a].name == "E2") {
      markup += " selected=\"selected\"";
      }
    markup += " value=\"" + a + "\">" + toneArray[a].monoformName + " | " + toneArray[a].frequency + " Hz</option>";
    }
  markup += "</select>";
  document.getElementById(span).innerHTML = markup;
  }

function tone(name, distance) {
  this.name = name;
  this.monoformName = (this.name.length == 2) ? "&nbsp;" + this.name : this.name;
  this.frequency = calculateFrequency(distance).toPrecision(4);
  }

function writeToneArray(notes, noteOffsets, octaves, octaveOffsets) {
  var toneArray = [];
  for (var a = 0; a < notes.length; a++) {
    for (var b = 0; b < octaves.length; b++) {
      toneArray[octaves[b] * notes.length + a] =
        new tone(notes[a] + octaves[b].toString(),
                 octaveOffsets[b] * notes.length + noteOffsets[a]);
      }
    }
  return(toneArray);
  }

const toneArray = writeToneArray(notes, noteOffsets, octaves, octaveOffsets);