/* ********************************************************************
 * Evertz Optical Power Budget Calculator
 *
 ******************************************************************* */

attachOnload(function() {
  var txrx_launch = document.getElementById('txrx_launch');
  var txrx_launch_description = document.getElementById('txrx_launch_description');
  var txrx_receive = document.getElementById('txrx_receive');
  var txrx_receive_description = document.getElementById('txrx_receive_description');

  var total_maxloss = document.getElementById('total_maxloss');

  var fiberloss_length = document.getElementById('fiberloss_length');
  var fiberloss_wavelength = document.getElementById('fiberloss_wavelength');
  var fiberloss_attenuation = document.getElementById('fiberloss_attenuation');
  var fiberloss_fiber = document.getElementById('fiberloss_fiber');

  var connloss_connectors = document.getElementById('connloss_connectors');
  var conloss_conloss = document.getElementById('conloss_conloss');

  var passive_devices = document.getElementById('passive_devices');
  var passive_totals = document.getElementById('passive_totals');

  var safety_margin = document.getElementById('safety_margin');

  var total_systemloss = document.getElementById('total_systemloss');
  var total_reserve = document.getElementById('total_reserve');


  var txrx_descriptions = {
    '-10': "7707RGBT13(-A2)(-EO), 7707RGBT(-A2)-GF",
    '-9':  "7707ET13M-4-W Only",
    '-7':  "Standard 1310nm FP",
    '-6':  "1310nm FP OC48 modules",
    '-4':  "240xLT13-WP-B50, 240xLT13-WP, 240xLT13-B50, 240xLT13",
    '-3':  "7707VT-8-HS XFP Transmitter",
    '-1':  "Typical 1310nm FP Tx / 1550nm DFB Rx WDM (13M and -W versions)",
    '0':   "1270nm to 1610nm DFB CWDM",
    '2':   "Typical L-Band & IF-Band transmit power (IFTA, LT and LTA modules)",
    '7':   "D200 to D400 DFB DWDM",
    '8':   "7707CATVTA13-110-8 Only",
    '10':  "",
    '11':  "7707CATVTA13-110-11 Only"
  };


  /* ******************************************************************
   * Run through all inputs and calculate totals
   *
   */
  function calculateTotals() {
    txrx_launch_description.firstChild.nodeValue = txrx_descriptions[txrx_launch.value] || "\xa0";

    // Transmission / Reception
    total_maxloss.value = txrx_launch.value - txrx_receive.value;
    total_maxloss.className = (Number(total_maxloss.value) < 0) ? "warning" : "disabled";

    // Fiber Loss
    fiberloss_attenuation.value = fiberloss_wavelength.value;
    fiberloss_fiber.value = numForm(fiberloss_length.value * fiberloss_attenuation.value);

    // Patch Point Connector Loss
    conloss_conloss.value = numForm(connloss_connectors.value * 0.5);

    // Passive Device Attenuation
    var devices = passive_devices.getElementsByTagName('select');
    var totals = passive_totals.getElementsByTagName('input');
    for (var x = 0, pall = 0; x < devices.length; x++)
      pall += Number(totals[x].value = devices[x].value);

    // Safety Margin

    // Totals
    total_systemloss.value = numForm(Number(fiberloss_fiber.value) + Number(conloss_conloss.value) + pall + Number(safety_margin.value));
    total_reserve.value = numForm(Number(total_maxloss.value) - Number(total_systemloss.value));
    total_reserve.className = (Number(total_reserve.value) < 3) ? "warning" : "disabled";
  }


  /* ******************************************************************
   * Add a new passive device row
   *
   */
  function addPassiveDevice() {
    if (passive_devices.getElementsByTagName('div').length < 16) {
      var newTotal = passive_totals.getElementsByTagName('div')[0].cloneNode(true);
      var newDevice = passive_devices.getElementsByTagName('div')[0].cloneNode(true);

      var img = newDevice.getElementsByTagName('img')[0];
      img.onclick = removePassiveDevice;
      img.title = "Remove this device";
      img.alt = "Delete";
      img.src = "img/icon.remove.png";

      var select = newDevice.getElementsByTagName('select')[0];
      select.selectedIndex = 0;
      select.onchange = calculateTotals;

      passive_totals.appendChild(newTotal);
      passive_devices.appendChild(newDevice);

      calculateTotals();
    }
  }


  /* ******************************************************************
   * Remove an existing passive device row
   *
   */
  function removePassiveDevice() {
    passive_totals.removeChild(passive_totals.lastChild);
    this.parentNode.parentNode.removeChild(this.parentNode);

    calculateTotals();
  }


  /* ******************************************************************
   * Reset all values to defaults
   *
   */
  function clearAll() {
    var devices = passive_devices.getElementsByTagName('img');
    for (var x = devices.length - 1; x >= 1; x--) devices[x].onclick();

    document.getElementById('opbc_form').reset();

    calculateTotals();
  }


  /* ******************************************************************
   * Maximize the link length, leaving all other values as-is
   *
   */
  function linkMax() {
    while (total_reserve.value > 3) {
      fiberloss_length.value++;
      calculateTotals();
    }
    while (total_reserve.value < 3 && fiberloss_length.value > 0) {
      fiberloss_length.value--;
      calculateTotals();
    }
    if (total_reserve.value < 3)
      alert('System loss is too great');
  }


  /* ******************************************************************
   * Get a number from a value, limited to one decimal place
   *
   */
  function numForm(value) {
    return parseInt(Number(value) * 10) / 10;
  }



  // ***** Initialize document events *********************************
  txrx_launch.onchange = txrx_receive.onchange = fiberloss_wavelength.onchange = passive_devices.getElementsByTagName('select')[0].onchange = calculateTotals;
  passive_devices.getElementsByTagName('img')[0].onclick = addPassiveDevice;
  document.getElementById('total_linkmax').onclick = linkMax;
  document.getElementById('total_clearall').onclick = clearAll;

  function showPop(trans) {
    document.getElementById('hdif_transmit').style.display = (trans) ? "block" : "none";
    document.getElementById('hdif_receive').style.display = (trans) ? "none" : "block";

    howdoifind.show();
  }
  document.getElementById('txrx_launch_link').onclick = function() { showPop(true); };
  document.getElementById('txrx_receive_link').onclick = function() { showPop(false); };

  fiberloss_length.onkeypress = connloss_connectors.onkeypress = safety_margin.onkeypress = function (e) {
    e = e || event;
    var keyCode = e.charCode || e.keyCode;
    switch (keyCode) {
      case 37: case 38: case 39: case 40:
      case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
      case 46: case 8: return true;
    }
    return false;
  };
  fiberloss_length.onkeyup = connloss_connectors.onkeyup = safety_margin.onkeyup = calculateTotals;
  fiberloss_length.onblur = connloss_connectors.onblur = safety_margin.onblur = function() { if (!this.value) this.value = 0; };

  calculateTotals();


  var howdoifind = new Popup('howdoifind', true, true);
});
