/**
 * Created by cgarcia on 08/05/2019.
 */

function datalogger2json_cableRangerToJSON(loggerFolder, stringXml) {
  var parser = new DOMParser();
  xmlDoc = parser.parseFromString(stringXml, 'text/xml');

  var jsonObj = {};
  var channels = [];
  var measurements = {};

  switch (loggerFolder) {
    case 'OPM':
      jsonObj.header = {
        type: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('type'),
        date: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('date'),
        time: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('time'),
        datalogger_name: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('name'),
        threshold_power: xmlDoc.getElementsByTagName('threshold')[0].innerHTML,
        pass:
          parseFloat(xmlDoc.getElementsByTagName('opt_power')[0].innerHTML) > parseFloat(xmlDoc.getElementsByTagName('threshold_value')[0].innerHTML)
            ? 'OK'
            : 'X',
        rf_power:
          parseFloat(xmlDoc.getElementsByTagName('rf_power')[0].innerHTML) > 80
            ? xmlDoc.getElementsByTagName('rf_power')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('rf_power')[0].getAttribute('units')
            : '---',
        opt_power: xmlDoc.getElementsByTagName('opt_power')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('opt_power')[0].getAttribute('units'),
        wavelength: xmlDoc.getElementsByTagName('wavelength')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('wavelength')[0].getAttribute('units'),
        rel_power: xmlDoc.getElementsByTagName('rel_pwr')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('rel_pwr')[0].getAttribute('units'),
        reference: xmlDoc.getElementsByTagName('ref')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('ref')[0].getAttribute('units'),
      };
      jsonObj.channelsets = [];
      break;
    case 'SCAN_RF':
    case 'SCAN_OPT':
      jsonObj.header = {
        type: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('type'),
        date: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('date'),
        time: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('time'),
        datalogger_name: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('name'),
        threshold_power:
          xmlDoc.getElementsByTagName('threshold_power')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('threshold_power')[0].getAttribute('units'),
        rf_power: xmlDoc.getElementsByTagName('rf_power')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('rf_power')[0].getAttribute('units'),
        opt_power: xmlDoc.getElementsByTagName('opt_power')[0].innerHTML + ' ' + xmlDoc.getElementsByTagName('opt_power')[0].getAttribute('units'),
      };
      jsonObj.channelsets = [];
      channels = [];

      //Remove #text element from the DOM
      for (var i = 0; i < xmlDoc.getElementsByTagName('TABLE')[0].childNodes.length; i++) {
        if (xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].nodeName == '#text') {
          xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].remove();
        }
      }

      for (var i = 0; i < xmlDoc.getElementsByTagName('TABLE')[0].childNodes.length; i++) {
        measurements.measures = {};
        for (var j = 0; j < xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes.length; j++) {
          switch (xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes[j].nodeName) {
            case 'power':
              measurements.measures.power = xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes[j].innerHTML;
              break;
            case 'c_n':
              measurements.measures.cn = xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes[j].innerHTML;
              break;
            case 'mer':
              measurements.measures.mer = xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes[j].innerHTML;
              break;
            case 'postBER':
              measurements.measures.postber = xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes[j].textContent;
              break;
            case 'preBER':
              measurements.measures.preber = xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes[j].textContent;
              break;
            case 'v_a':
              measurements.measures.va = xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].childNodes[j].innerHTML;
              break;
          }
        }

        //Doing a clone of measurements object to avoid the change of his values by reference
        var measProfile = JSON.parse(JSON.stringify(measurements));

        channels.push({
          name: xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].getAttribute('name'),
          freq: xmlDoc.getElementsByTagName('TABLE')[0].childNodes[i].getAttribute('frequency'),
          measurements: measProfile,
        });
      }
      jsonObj.channelsets.push({
        channels: channels,
      });
      break;
    case 'REGISTERING':
      jsonObj.header = {
        type: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('type'),
        date: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('date'),
        time: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('time'),
        datalogger_name: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('name'),
      };
      jsonObj.channelsets = [];
      channels = [];

      //Remove #text element from the DOM
      for (var i = 0; i < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes.length; i++) {
        if (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].nodeName == '#text') {
          xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].remove();
        }
      }

      for (var i = 0; i < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes.length; i++) {
        switch (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].nodeName) {
          case 'REGISTERING_DOWN':
            channels = [];
            //Remove #text element from the DOM
            for (var j = 0; j < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes.length; j++) {
              if (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].nodeName == '#text') {
                xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].remove();
              }
            }

            for (var j = 0; j < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes.length; j++) {
              var channelName = '';
              var channelFreq = '';

              measurements.measures = {};
              for (var k = 0; k < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
                if (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].nodeName == '#text') {
                  xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].remove();
                }
              }

              for (var k = 0; k < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
                switch (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].nodeName) {
                  case 'power':
                    measurements.measures.power = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'channel':
                    channelName = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'frequency':
                    channelFreq = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'mer':
                    measurements.measures.mer = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'mod':
                    measurements.measures.mod = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                }
              }

              //Doing a clone of measurements object to avoid the change of his values by reference
              var measProfile = JSON.parse(JSON.stringify(measurements));

              channels.push({
                name: channelName,
                freq: channelFreq,
                measurements: measProfile,
              });
            }

            jsonObj.channelsets.push({
              registeringDown: {
                channels: channels,
              },
            });

            break;

          case 'REGISTERING_UP':
            channels = [];
            //Remove #text element from the DOM
            for (var j = 0; j < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes.length; j++) {
              if (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].nodeName == '#text') {
                xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].remove();
              }
            }

            for (var j = 0; j < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes.length; j++) {
              var channelName = '';
              var channelFreq = '';

              measurements.measures = {};
              for (var k = 0; k < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
                if (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].nodeName == '#text') {
                  xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].remove();
                }
              }

              for (var k = 0; k < xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
                switch (xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].nodeName) {
                  case 'power':
                    measurements.measures.power = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'uc':
                    channelName = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'frequency_bw':
                    channelFreq = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'att':
                    measurements.measures.att = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                  case 'sr':
                    measurements.measures.sr = xmlDoc.getElementsByTagName('REGISTERING')[0].childNodes[i].childNodes[j].childNodes[k].innerHTML;
                    break;
                }
              }

              //Doing a clone of measurements object to avoid the change of his values by reference
              var measProfile = JSON.parse(JSON.stringify(measurements));

              channels.push({
                name: channelName,
                freq: channelFreq,
                measurements: measProfile,
              });
            }

            jsonObj.channelsets.push({
              registeringUp: {
                channels: channels,
              },
            });
            break;
        }
      }

      break;
  }

  return jsonObj;
}

function datalogger2json_headerToJSON(stringXmlHeader) {
  var parser = new DOMParser();
  xmlDoc = parser.parseFromString(stringXmlHeader, 'text/xml');

  var jsonObj = {};

  jsonObj.header = {
    equipment_name: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('equipment'),
    sn: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('sn'),
    date: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('date'),
    time: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('time'),
    datalogger_name: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('name'),
    company: xmlDoc.getElementsByTagName('DATALOGGER')[0].getAttribute('trademark'),
    channelsets: [],
    atenuationTest: [],
  };

  //Remove #text element from the DOM
  for (var i = 0; i < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes.length; i++) {
    if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].nodeName == '#text') {
      xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].remove();
    }
  }

  for (var i = 0; i < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes.length; i++) {
    switch (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].nodeName) {
      case 'CHANNEL-SETS':
        for (var j = 0; j < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].nodeName == 'CHANNEL-SET') {
            if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('band') == 'SATELLITE') {
              jsonObj.header.channelsets.push({
                band: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('band'),
                date: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('date'),
                equipment: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('equipment'),
                name: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('name'),
                version: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('version'),
                lnb_band: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[1].childNodes[1].nodeName,
                high_osc: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[1].childNodes[1].getAttribute('high'),
                low_osc: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[1].childNodes[1].getAttribute('low'),
              });
            } else {
              jsonObj.header.channelsets.push({
                band: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('band'),
                date: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('date'),
                equipment: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('equipment'),
                name: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('name'),
                version: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('version'),
              });
            }
          }
        }

        break;
      case 'ATTENUATION-TESTS':
        var pilots = [];

        //Remove #text element from the DOM
        for (var j = 0; j < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].nodeName == '#text') {
            xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].remove();
          }
        }

        for (var j = 0; j < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes.length; j++) {
          //Remove #text element from the DOM
          for (var k = 0; k < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
            if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].nodeName == '#text') {
              xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].remove();
            }
          }

          for (var k = 0; k < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
            if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].nodeName == 'PILOT') {
              pilots.push({
                id: j + 1,
                freqUnits: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].getAttribute('Freq_Units'),
                measUnits: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].getAttribute('Meas_Units'),
                refUnits: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].getAttribute('Ref_Units'),
                reference: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].getAttribute('Reference'),
                frequency: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].childNodes[k].getAttribute('frequency'),
              });
            }
          }

          jsonObj.header.atenuationTest.push({
            name: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('name'),
            lnb: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('LNB'),
            band: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('band'),
            maxAttenuation: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('Max_Attenuation'),
            date: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('date'),
            time: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('time'),
            type: xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('type'),
            pilots: pilots,
          });
        }

        break;
      case 'OPTIONS':
        jsonObj.header.options = {};

        //Remove #text element from the DOM
        for (var j = 0; j < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].nodeName == '#text') {
            xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].remove();
          }
        }

        for (var j = 0; j < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes.length; j++) {
          switch (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].nodeName) {
            case 'CAPTURE-PSI':
              jsonObj.header.options.capturePsi = xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('value');
              break;
            case 'PAUSE-CHSETS':
              jsonObj.header.options.pauseChSets = xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('value');
              break;
            case 'FSM':
              jsonObj.header.options.fsm = xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('value');
              break;
          }
        }
        break;
      case 'TEST':
        jsonObj.header.testpoints = [];
        //Remove #text element from the DOM
        for (var j = 0; j < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].nodeName == '#text') {
            xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].remove();
          }
        }

        for (var j = 0; j < xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].nodeName == 'xi:include') {
            jsonObj.header.testpoints.push({
              name: xmlDoc
                .getElementsByTagName('DATALOGGER')[0]
                .childNodes[i].childNodes[j].getAttribute('href')
                .substring(0, xmlDoc.getElementsByTagName('DATALOGGER')[0].childNodes[i].childNodes[j].getAttribute('href').length - 4),
            });
          }
        }

        break;
    }
  }

  return jsonObj;
}

function datalogger2json_testPointToJSON(stringXmlTestPoint) {
  var parser = new DOMParser();
  xmlDoc = parser.parseFromString(stringXmlTestPoint, 'text/xml');

  var jsonObj = {
    name: xmlDoc.getElementsByTagName('POINT')[0].getAttribute('name'),
    date: xmlDoc.getElementsByTagName('POINT')[0].getAttribute('date'),
    time: xmlDoc.getElementsByTagName('POINT')[0].getAttribute('time'),
    channelsets: [],
    atenuationTest: [],
  };

  //Remove #text element from the DOM
  for (var i = 0; i < xmlDoc.getElementsByTagName('POINT')[0].childNodes.length; i++) {
    if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].nodeName == '#text') {
      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].remove();
    }
  }

  for (var i = 0; i < xmlDoc.getElementsByTagName('POINT')[0].childNodes.length; i++) {
    switch (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].nodeName) {
      case 'GPS':
        jsonObj.gps = {
          latitude: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('latitude'),
          longitude: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('longitude'),
          altitude: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('altitude'),
          locked: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('locked'),
        };

        break;
      case 'CHANNEL-SET':
        let channels = [];
        let measurements;
        //Remove #text element from the DOM
        for (var j = 0; j < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].nodeName == '#text') {
            xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].remove();
          }
        }

        for (var j = 0; j < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].nodeName == 'CHANNEL') {
            //Remove #text element from the DOM
            for (var k = 0; k < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
              if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].nodeName == '#text') {
                xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].remove();
              }
            }

            for (var k = 0; k < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes.length; k++) {
              if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].nodeName == 'MEASUREMENTS') {
                measurements = {};

                //Remove #text element from the DOM
                for (var l = 0; l < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes.length; l++) {
                  if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].nodeName == '#text') {
                    xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].remove();
                  }
                }

                for (var l = 0; l < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes.length; l++) {
                  measurements.signalType = xmlDoc
                    .getElementsByTagName('POINT')[0]
                    .childNodes[i].childNodes[j].childNodes[k].childNodes[l].nodeName.toLowerCase();

                  //Remove #text element from the DOM
                  for (
                    var m = 0;
                    m < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes.length;
                    m++
                  ) {
                    if (
                      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].nodeName ==
                      '#text'
                    ) {
                      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].remove();
                    }
                  }

                  for (
                    var m = 0;
                    m < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes.length;
                    m++
                  ) {
                    measurements.parameters = {
                      symbolrate: '',
                      bandwith: '',
                    };
                    if (
                      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].nodeName ==
                      'PARAMETERS'
                    ) {
                      for (
                        var n = 0;
                        n <
                        xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes
                          .length;
                        n++
                      ) {
                        switch (
                          xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n]
                            .nodeName
                        ) {
                          case 'SYMBOLRATE':
                            measurements.parameters.symbolrate = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'BANDWIDTH':
                            measurements.parameters.bandwith = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                        }
                      }
                    }

                    if (
                      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].nodeName ==
                      'MEASURES'
                    ) {
                      measurements.measures = {};
                      for (
                        var n = 0;
                        n <
                        xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes
                          .length;
                        n++
                      ) {
                        switch (
                          xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n]
                            .nodeName
                        ) {
                          case 'POWER':
                            measurements.measures.power = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'LEVEL':
                            measurements.measures.power = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'CN':
                            measurements.measures.cn = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'OFFSET':
                            measurements.measures.offset = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'MER':
                            measurements.measures.mer = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'CBER':
                            measurements.measures.cber = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'LBER':
                            measurements.measures.lvber = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'VBER':
                            measurements.measures.lvber = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                          case 'LM':
                            measurements.measures.lm = xmlDoc
                              .getElementsByTagName('POINT')[0]
                              .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value');
                            break;
                        }
                      }
                    }

                    if (
                      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].nodeName ==
                      'PROFILE'
                    ) {
                      //Remove #text element from the DOM
                      for (
                        var n = 0;
                        n <
                        xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes
                          .length;
                        n++
                      ) {
                        if (
                          xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n]
                            .nodeName == '#text'
                        ) {
                          xmlDoc
                            .getElementsByTagName('POINT')[0]
                            .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].remove();
                        }
                      }

                      for (
                        var n = 0;
                        n <
                        xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes
                          .length;
                        n++
                      ) {
                        if (
                          xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n]
                            .nodeName == 'PLP'
                        ) {
                          for (
                            var o = 0;
                            o <
                            xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[
                              n
                            ].childNodes.length;
                            o++
                          ) {
                            if (
                              xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m]
                                .childNodes[n].childNodes[o].nodeName == 'MEASURES'
                            ) {
                              for (
                                var p = 0;
                                p <
                                xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m]
                                  .childNodes[n].childNodes[o].childNodes.length;
                                p++
                              ) {
                                switch (
                                  xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m]
                                    .childNodes[n].childNodes[o].childNodes[p].nodeName
                                ) {
                                  case 'MER':
                                    measurements.measures.mer = xmlDoc
                                      .getElementsByTagName('POINT')[0]
                                      .childNodes[
                                        i
                                      ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].childNodes[p].getAttribute('value');
                                    break;
                                  case 'CBER':
                                    measurements.measures.cber = xmlDoc
                                      .getElementsByTagName('POINT')[0]
                                      .childNodes[
                                        i
                                      ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].childNodes[p].getAttribute('value');
                                    break;
                                  case 'LBER':
                                    measurements.measures.lvber = xmlDoc
                                      .getElementsByTagName('POINT')[0]
                                      .childNodes[
                                        i
                                      ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].childNodes[p].getAttribute('value');
                                    break;
                                  case 'LM':
                                    measurements.measures.lm = xmlDoc
                                      .getElementsByTagName('POINT')[0]
                                      .childNodes[
                                        i
                                      ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].childNodes[p].getAttribute('value');
                                    break;
                                }
                              }
                            }

                            if (
                              xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m]
                                .childNodes[n].childNodes[o].nodeName == 'STATUS'
                            ) {
                              measurements.status = xmlDoc
                                .getElementsByTagName('POINT')[0]
                                .childNodes[
                                  i
                                ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].getAttribute('locked');
                            }
                          }

                          //Doing a clone of measurements object to avoid the change of his values by reference
                          var measProfile = JSON.parse(JSON.stringify(measurements));

                          channels.push({
                            name:
                              xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('name') +
                              ' | ' +
                              xmlDoc
                                .getElementsByTagName('POINT')[0]
                                .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].getAttribute('value') +
                              ' | PLP ' +
                              xmlDoc
                                .getElementsByTagName('POINT')[0]
                                .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].getAttribute('value'),
                            freq: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('frequency'),
                            units: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('units'),
                            date: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('date'),
                            time: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('time'),
                            measurements: measProfile,
                          });
                        }
                      }
                    }

                    if (
                      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].nodeName ==
                      'LAYER'
                    ) {
                      //Remove #text element from the DOM
                      for (
                        var n = 0;
                        n <
                        xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes
                          .length;
                        n++
                      ) {
                        if (
                          xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n]
                            .nodeName == '#text'
                        ) {
                          xmlDoc
                            .getElementsByTagName('POINT')[0]
                            .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].remove();
                        }
                      }

                      for (
                        var n = 0;
                        n <
                        xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes
                          .length;
                        n++
                      ) {
                        if (
                          xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n]
                            .nodeName == 'MEASURES'
                        ) {
                          for (
                            var o = 0;
                            o <
                            xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[
                              n
                            ].childNodes.length;
                            o++
                          ) {
                            if (
                              xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m]
                                .childNodes[n].childNodes[o].nodeName == '#text'
                            ) {
                              xmlDoc
                                .getElementsByTagName('POINT')[0]
                                .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].remove();
                            }
                          }

                          for (
                            var o = 0;
                            o <
                            xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[
                              n
                            ].childNodes.length;
                            o++
                          ) {
                            switch (
                              xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m]
                                .childNodes[n].childNodes[o].nodeName
                            ) {
                              case 'MER':
                                measurements.measures.mer = xmlDoc
                                  .getElementsByTagName('POINT')[0]
                                  .childNodes[
                                    i
                                  ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].getAttribute('value');
                                break;
                              case 'CBER':
                                measurements.measures.cber = xmlDoc
                                  .getElementsByTagName('POINT')[0]
                                  .childNodes[
                                    i
                                  ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].getAttribute('value');
                                break;
                              case 'VBER':
                                measurements.measures.lvber = xmlDoc
                                  .getElementsByTagName('POINT')[0]
                                  .childNodes[
                                    i
                                  ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].getAttribute('value');
                                break;
                              case 'LM':
                                measurements.measures.lm = xmlDoc
                                  .getElementsByTagName('POINT')[0]
                                  .childNodes[
                                    i
                                  ].childNodes[j].childNodes[k].childNodes[l].childNodes[m].childNodes[n].childNodes[o].getAttribute('value');
                                break;
                            }
                          }

                          //Doing a clone of measurements object to avoid the change of his values by reference
                          const measLayer = JSON.parse(JSON.stringify(measurements));

                          channels.push({
                            name:
                              xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('name') +
                              ' | ' +
                              'LAYER ' +
                              xmlDoc
                                .getElementsByTagName('POINT')[0]
                                .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].getAttribute('value'),
                            freq: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('frequency'),
                            units: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('units'),
                            date: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('date'),
                            time: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('time'),
                            measurements: measLayer,
                          });
                        }
                      }
                    }

                    if (
                      xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].nodeName ==
                      'STATUS'
                    ) {
                      measurements.status = xmlDoc
                        .getElementsByTagName('POINT')[0]
                        .childNodes[i].childNodes[j].childNodes[k].childNodes[l].childNodes[m].getAttribute('locked');
                    }
                  }
                }
              }
            }

            if (measurements.signalType != 'dvb-t2' && measurements.signalType != 'isdb-t') {
              if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('band') == 'SATELLITE') {
                channels.push({
                  name: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('name'),
                  freq: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('downlink'),
                  sat_band: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('sat_band'),
                  polarization:
                    xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('polarization') != null
                      ? xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('polarization')
                      : '',
                  units: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('units'),
                  date: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('date'),
                  time: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('time'),
                  measurements: measurements,
                });
              } else {
                channels.push({
                  name: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('name'),
                  freq: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('frequency'),
                  units: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('units'),
                  date: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('date'),
                  time: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('time'),
                  measurements: measurements,
                });
              }
            }
          }
        }

        jsonObj.channelsets.push({
          band: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('band'),
          date: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('date'),
          equipment: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('equipment'),
          name: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('name'),
          time: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('time'),
          version: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('version'),
          channels: channels,
        });

        break;

      case 'ATTENUATION-TEST':
        var pilots = [];

        //Remove #text element from the DOM
        for (var j = 0; j < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].nodeName == '#text') {
            xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].remove();
          }
        }

        for (var j = 0; j < xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes.length; j++) {
          if (xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].nodeName == 'PILOT') {
            pilots.push({
              id: j + 1,
              freqUnits: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('Freq_Units'),
              measUnits: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('Meas_Units'),
              refUnits: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('Ref_Units'),
              measure: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('Measure'),
              reference: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('Reference'),
              frequency: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].childNodes[j].getAttribute('frequency'),
            });
          }
        }

        jsonObj.atenuationTest.push({
          name: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('name'),
          band: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('band'),
          maxAttenuation: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('Max_Attenuation'),
          date: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('date'),
          time: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('time'),
          type: xmlDoc.getElementsByTagName('POINT')[0].childNodes[i].getAttribute('type'),
          pilots: pilots,
        });

        break;
      default:
        break;
    }
  }

  return jsonObj;
}
