var timer = null;
var CustID;
var TxPresent = 0;
var DTM3200Present = 0;
var PresetNumber = 0;
var B120Present = 0;
var LicenseMask;
var LicDec;
var license_des;
var license_biss;
var license_enc;
var license_dec;
var license_ip;

var logo  = 'logo.jpg'

var decoder_mode = ["H.265 HEVC", "H.264 AVC", "MPEG-2"];
var vid_format = ["480i59", "576i50", "480p59", "576p50", "720p50", "720p59", "720p60", "1080i50", "1080i59", "1080i60", "1080p23", "1080p24", "1080p25", "1080p29", "1080p30", "1080psf23", "1080psf24", "1080psf25", "1080psf29", "1080psf30", "1080p50", "1080p59", "1080p60", "2160p23", "2160p24", "2160p25", "2160p29", "2160p30", "2160p50", "2160p59", "2160p60"];
var video_delay_mode = ["Normal", "Low", "Ultra Low"];
var quad_sync_mode = ["Off", "HD-SDI 1080i", "HD-SDI 1080psf", "HD-SDI 1080p", "3G-SDI 1080p", "HD-SDI 720p"];
var sample_format = ["2SI", "SQD"];
var off_on = ["Off", "On"];
var no_yes = ["No", "Yes"];
var audio_dec = ["Off", "LPCM (16-bit)", "LPCM (20-bit)", "LPCM (24-bit)", "AAC-LC", "MPEG-1 L1", "MPEG-2 L2"];
var aud_ref = ["+18dBu", "+24dBu"];
var video_fail_mode = ["Freeze", "Blue"];
var service_select = ["Manual", "Auto"];
var genlock_mode = ["Off", "External", "Internal"];
var mode_select = ["Encoder", "Decoder"];
var tx_select = ["No", "D1550"];
var oled_type = ["SSD1307 (96x16)", "SSD1306 (96x16)"];
var stream_interface_select = ["Port 1", "Port 2", "IP Module"];
var stream_type_select = ["Unicast", "Multicast"];
var stream_rate_mode = ["PCR Estimate", "Fixed Buffer", "Stream Estimate", "Disable Buffer"];

var chroma_depth = ["8-bit", "10-bit"];
var chroma_format = ["4:2:0", "4:2:2"];
var data_mode = ["Off", "8-bit No Parity, 1 Stop"];
var data_baud = ["1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200"];
var error_ok = ["ERROR", "OK"];
var nolock_lock = ["NO LOCK", "LOCK"];

var preset_sel = ["Unit", "Config"]
var preset_val = ["Unit", "Decoder"]

function ethDHCPChange(port)
{
	var dhcp = getValue("DHCPModeSelect"+port);
	if(dhcp != 0)
	{
		document.getElementById("IPAddr"+port).style.display = "none"
		document.getElementById("IPGateway"+port).style.display = "none"
		document.getElementById("IPMask"+port).style.display = "none"
		document.getElementById("eth"+port+"_ipaddr").style.display = "inline-block"
		document.getElementById("eth"+port+"_gwaddr").style.display = "inline-block"
		document.getElementById("eth"+port+"_mask").style.display = "inline-block"	
	}
	else
	{
		document.getElementById("IPAddr"+port).style.display = "inline-block"
		document.getElementById("IPGateway"+port).style.display = "inline-block"
		document.getElementById("IPMask"+port).style.display = "inline-block"
		document.getElementById("eth"+port+"_ipaddr").style.display = "none"
		document.getElementById("eth"+port+"_gwaddr").style.display = "none"
		document.getElementById("eth"+port+"_mask").style.display = "none"		
	}
}

function dtmDHCPChange()
{
	var dhcp = getValue("DtmDHCPModeSelect");
	if(dhcp != 0)
	{
		document.getElementById("DtmIPAddress").style.display = "none"
		document.getElementById("DtmIPGateway").style.display = "none"
		document.getElementById("DtmIPMask").style.display = "none"
		document.getElementById("dtm_ipaddr").style.display = "inline-block"
		document.getElementById("dtm_gwaddr").style.display = "inline-block"
		document.getElementById("dtm_mask").style.display = "inline-block"
	}
	else
	{
		document.getElementById("DtmIPAddress").style.display = "inline-block"
		document.getElementById("DtmIPGateway").style.display = "inline-block"
		document.getElementById("DtmIPMask").style.display = "inline-block"
		document.getElementById("dtm_ipaddr").style.display = "none"
		document.getElementById("dtm_gwaddr").style.display = "none"
		document.getElementById("dtm_mask").style.display = "none"
	}
}

function ipInterfaceChange()
{
	if((DTM3200Present == 0) && (license_ip == 0))
    {
		document.getElementById("ipstream").style.display = "none";
    }
    else
    {
		document.getElementById("ipstream").style.display = "inline-block";
    }	
	
    if(DTM3200Present == 0)
	{
		stream_interface_select = ["Port 1", "Port 2"];
		var tmpIPStreamInterface = getValue("IPStreamInterface");
		removeOptions("IPStreamInterface");
		selectOptions("IPStreamInterface",stream_interface_select);
		setSelectValue("IPStreamInterface",tmpIPStreamInterface);	  
		document.getElementById("Ethernet_Stream").style.display = "none";
		document.getElementById("IP_Hardware").style.display = "none";
	}
	else
	{
		stream_interface_select = ["Port 1", "Port 2", "IP Module"];
		var tmpIPStreamInterface = getValue("IPStreamInterface");
		removeOptions("IPStreamInterface");
		selectOptions("IPStreamInterface",stream_interface_select);
		setSelectValue("IPStreamInterface",tmpIPStreamInterface);
		document.getElementById("Ethernet_Stream").style.display = "inline-block";
		document.getElementById("IP_Hardware").style.display = "inline-block";
	}
	
	var IPStreamInterface = getValue("IPStreamInterface");
	if(IPStreamInterface != 2) // not DTM-3200
	{
		document.getElementById("IPStreamDelayRow").style.display = "none";
		document.getElementById("IPStreamRateModeRow").style.display = "none";
		document.getElementById("IPFECEnableRow").style.display = "none";
		document.getElementById("DTM_Diagnostics").style.display = "none";
		document.getElementById("B110_Diagnostics").style.display = "inline-block";
	}
	else
	{
		document.getElementById("IPStreamDelayRow").style.display = "inline-block";
		document.getElementById("IPStreamRateModeRow").style.display = "inline-block";
		document.getElementById("IPFECEnableRow").style.display = "inline-block";
		document.getElementById("DTM_Diagnostics").style.display = "inline-block";
		document.getElementById("B110_Diagnostics").style.display = "none";
	}
	
	var IPStreamMode = getValue("IPStreamMode");
	if(IPStreamMode == 0) // unicast
	{
		document.getElementById("IPStreamAddressRow").style.display = "none";	
	}
	else
	{
		document.getElementById("IPStreamAddressRow").style.display = "inline-block";	
	}
}

function showEngineering()
{
	if(document.getElementById('Unit').style.display == "inline-block")
		document.getElementById('EngineeringOptions').style.display = "inline-block"
}

function startupDec()
{
  openTabh(event, 'Status');
  selectOptions("ServiceSelSelect",service_select)
  selectOptions("DESModeSelect",off_on)
  selectOptions("BISSModeSelect",off_on)
  selectOptions("AncillaryModeSelect",off_on);
  selectOptions("DecodeModeSelect",decoder_mode)
  selectOptions("VideoFormatSelect",vid_format)
  selectOptions("LowDelayModeSelect",video_delay_mode)
  selectOptions("SampleFormatSelect",sample_format)
  selectOptions("QuadSyncSelect",quad_sync_mode)
  selectOptions("HDDuplicateSelect",off_on)
  selectOptions("FailModeSelect",video_fail_mode)
  selectOptions("GenlockModeSelect",genlock_mode)
  selectOptions("AudRefLev0",aud_ref)
  selectOptions("AudRefLev1",aud_ref)
  selectOptions("AudioSource0Select",off_on)
  //selectOptions("AudioSource1Select",off_on)
  //selectOptions("AudioSource2Select",off_on)
  //selectOptions("AudioSource3Select",off_on)
  //selectOptions("AudioSource4Select",off_on)
  //selectOptions("AudioSource5Select",off_on)
  //selectOptions("AudioSource6Select",off_on)
  //selectOptions("AudioSource7Select",off_on)
  selectOptions("IPStreamEnable",off_on)
  selectOptions("IPStreamInterface",stream_interface_select)
  selectOptions("IPStreamMode",stream_type_select)
  selectOptions("IPStreamRateMode",stream_rate_mode)
  selectOptions("IPFECEnable",off_on)
  selectOptions("DHCPModeSelect0",off_on)
  selectOptions("DHCPModeSelect1",off_on)
  selectOptions("DtmDHCPModeSelect",off_on)
  selectOptions("IPBridgeSelect",off_on)
  selectOptions("UnitModeSelect",mode_select)
  selectOptions("TxPresentSelect",tx_select);
  selectOptions("B120PresentSelect",no_yes);
  selectOptions("DTM3200PresentSelect",no_yes);
  selectOptions("OLEDTypeSelect",oled_type);
  //selectOptions(AudioDecoder0Select,audio_dec)
  //selectOptions(AudioDecoder1Select,audio_dec)
  //selectOptions(AudioDecoder2Select,audio_dec)
  //selectOptions(AudioDecoder3Select,audio_dec)
  getStatus();
  getUnit();
  getDecode();
  getStatus();
  getPreset()
  document.getElementById("status").click();
  selectOptionsValue("filePre",preset_sel,preset_val);
  var down = document.getElementById('PresetSelect')
  down.addEventListener("change",downloadPreset);
}

function selectOptions(param,variable)
{
  var e = document.getElementById(param);
  for(index in variable) {
    e.options[e.options.length] = new Option(variable[index], index);
  }
}

function selectOptionsValue(param,variable,value)
{
  var e = document.getElementById(param);
  for(index in variable) {
    e.options[e.options.length] = new Option(variable[index], value[index]);
  }
}

function updateLevelBar(e,a,d,b,c,m)
{
  var elem = document.getElementById("LevelBar" + b + c);
  if (e == 0)
  {
    elem.style.backgroundColor = 'white'
    elem.style.width = (0) + '%';
  }
  else if (d != 0)
  {
    elem.style.backgroundColor = '#335578'
    elem.style.width = (100) + '%';
  }
  else if (a >=90)
  {
    elem.style.backgroundColor = 'red'
    elem.style.width = ((a/90)*100) + '%';
  }
  else if ((a >=78) && (a < 90))
  {
    elem.style.backgroundColor = 'orange'
    elem.style.width = ((a/90)*100) + '%';
  }
  else
  {
    elem.style.backgroundColor = 'green'
    elem.style.width = ((a/90)*100) + '%';
  }
  
  var label = (b+1)+": ";
  if(e == 0)
  {
	 label += "Disabled";
  }
  else if(m == 1 || m == 5 || m == 6)
  {
	  if(d == 0)
		 label += "LPCM";
	  else if(d & (1<<1))
		 label += "AC-3";
	  else if(d & (3<<4))
		 label += "MPEG-1";
	  else if(d & (13<<6))
		 label += "MPEG-2";
	  else if(d & (1<<27))
		 label += "SMPTE KLV";
	  else if(d & (1<<28))
		 label += "Dolby E";
	  else
		 label += "Non-PCM";
  }
  else if(m == 2)
  {
	 label += "AAC-LC";
  }
  else if(m == 3)
  {
	 label += "MPEG-1";
  }
  else if(m == 4)
  {
	 label += "MPEG-2";
  }
  else
  {
	 label += "Unknown";
  }
  document.getElementById("meter_"+(b+1)).innerHTML = label;
}

function refresh()
{
  getDecode()
}

function refresh_unit()
{
  getUnit()
}

function license_hide(param)
{
	if((license_biss == 0) && (license_des == 0))
  {
		document.getElementById("Scrambling").style.display = "none"
  }
	else
  {
    if(param == "des")
    {
      if(license_des == 0)
      {
        document.getElementById("hide_des").style.display = "none"
      }
      else
      {
        document.getElementById("hide_des").style.display = "inline-block"
      }
    }
  	else if(param == "biss")
    {
      if((B120Present == 0) || (license_biss == 0))
      {
        document.getElementById("hide_biss").style.display = "none"
      }
      else
      {
        document.getElementById("hide_biss").style.display = "inline-block"
      }
    }
  }
}

function hide_tab(param)
{
  if(param == "ipstream")
  {
	ipInterfaceChange();
  }
}

function setSelectValue(param,new_select)
{
  var e = document.getElementById (param);
  e.selectedIndex = new_select;
}

function setNumber(param, new_num)
{
  var e = document.getElementById(param);
  e.value = new_num;
}

function getValue(param)
{
  var e = document.getElementById (param);
  var strUser = e.options[e.selectedIndex].value;
  return strUser;
}

function getNumber(param)
{
  var e = document.getElementById(param).value;
  return e;
}

function getText(param)
{
  var e = document.getElementById(param);
  var strUser = e.options[e.selectedIndex].text;
  return strUser;
}

function isHexKey(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode
  if ((charCode < 65 || charCode > 70) && (charCode < 48 || charCode > 57) && (charCode < 97 || charCode > 102) && (charCode != 8))
  {
    return false;
  }
  return true;
}

function isOKKey(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode
  if ((charCode < 65 || charCode > 90) && (charCode < 48 || charCode > 57) && (charCode != 95) && (charCode < 97 || charCode > 122) && (charCode != 8))
  {
    return false;
  }
  else
  {
    return true;
  }
}

function processIPvalue()
{
  var check_ip = 1;
  
  check_ip &= ValidateIPaddress("IPAddr0");
  check_ip &= ValidateIPaddress("IPGateway0");
  check_ip &= ValidateIPaddress("IPMask0");
  
  check_ip &= ValidateIPaddress("IPAddr1");
  check_ip &= ValidateIPaddress("IPGateway1");
  check_ip &= ValidateIPaddress("IPMask1");
  
  if(DTM3200Present == 1)
  {
	  check_ip &= ValidateIPaddress("DtmIPAddress");
	  check_ip &= ValidateIPaddress("DtmIPGateway");
	  check_ip &= ValidateIPaddress("DtmIPMask");
  }
  
  if(check_ip == 1)
  {
    setUnit();
  }
}

function ValidateIPaddress(field)
{
  var ipaddr = document.getElementById(field).value;
  var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
  if(ipaddr.match(ipformat))
  {
    return 1;
  }
  else
  {
    alert("Invalid IP address");
    document.getElementById(field).value = '';
    return 0;
  }
}

function addrFieldCheck(event)
{
  var char = event.which || event.keyCode;
  if (char != 8 && char != 46 && (char  < 0x30 || char  > 0x39) && char != 9 && char != 37 && char != 39)
  {
    event.returnValue = false;
    if(event.preventDefault)
    {
      event.preventDefault();
    }
  }
}

function timer_function()
{
  refreshPreview();
  getStatus();
  getDecode();
  getUnit();
  /*license_hide("des")
  license_hide("biss")
  hide_tab("ipstream")*/
}

function timer_status_only()
{
  getStatus();
}

function openTab(evt, tabName)
{
  var i, tabcontent, tablinks;

  getDecode();
  document.getElementById("apply_config").style.display = "inline-block";

  // hide all tabs contents
  tabcontent = document.getElementsByClassName("cfg_tabcontent");
  for (i = 0; i < tabcontent.length; i++)
  {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++)
  {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(tabName).style.display = "inline-block";
  evt.currentTarget.className += " active";
}

function openTabh(evt, tabName)
{
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontenth");
  for (i = 0; i < tabcontent.length; i++)
  {
    tabcontent[i].style.display = "none";
  }
  clearInterval(timer);
  
  if (tabName == 'Status')
  {
    getStatus();
    document.getElementById("cfg_buttons").style.display = "none";
    document.getElementById("apply_config").style.display = "none";
    document.getElementById("apply_unit").style.display = "none";
    document.getElementById("load").style.display = "none";
    document.getElementById("restore_def").style.display = "none";
    document.getElementById("reload_preset").style.display = "none";
    document.getElementById("load_unit").style.display = "none";
    document.getElementById("reboot_unit").style.display = "none";
    document.getElementById("reset_codec").style.display = "none";
    timer = setInterval("timer_function();",1000);
  }
  else if (tabName == 'Config')
  {
    getDecode();
    document.getElementById("cfg_buttons").style.display = "inline-block";
    document.getElementById("apply_config").style.display = "inline-block";
    document.getElementById("apply_unit").style.display = "none";
    document.getElementById("load").style.display = "inline-block";
    document.getElementById("load_unit").style.display = "none";
    document.getElementById("reload_preset").style.display = "none";
    document.getElementById("restore_def").style.display = "none";
    document.getElementById("reboot_unit").style.display = "none";
    document.getElementById("reset_codec").style.display = "inline-block";
    document.getElementById("video").click();
    timer = setInterval("timer_status_only();",1000);
  }
  else if (tabName == 'Unit')
  {
    getUnit();
    document.getElementById("cfg_buttons").style.display = "none";
    document.getElementById("apply_config").style.display = "none";
    document.getElementById("apply_unit").style.display = "inline-block";
    document.getElementById("load").style.display = "none";
    document.getElementById("reload_preset").style.display = "none";
    document.getElementById("restore_def").style.display = "inline-block";
    document.getElementById("load_unit").style.display = "inline-block";
    document.getElementById("reboot_unit").style.display = "inline-block";
    document.getElementById("reset_codec").style.display = "none";
    timer = setInterval("timer_status_only();",1000);
  }
  else if (tabName == 'Upgrade')
  {
    document.getElementById("cfg_buttons").style.display = "none";
    document.getElementById("apply_config").style.display = "none";
    document.getElementById("apply_unit").style.display = "none";
    document.getElementById("load").style.display = "none";
    document.getElementById("reload_preset").style.display = "none";
    document.getElementById("restore_def").style.display = "none";
    document.getElementById("load_unit").style.display = "none";
    document.getElementById("reboot_unit").style.display = "none";
    document.getElementById("reset_codec").style.display = "none";
  }
  else if (tabName == 'Preset')
  {
    document.getElementById("cfg_buttons").style.display = "none";
    document.getElementById("apply_config").style.display = "none";
    document.getElementById("apply_unit").style.display = "none";
    document.getElementById("load").style.display = "none";
    document.getElementById("reload_preset").style.display = "inline-block";
    document.getElementById("restore_def").style.display = "inline-block";
    document.getElementById("load_unit").style.display = "none";
    document.getElementById("reboot_unit").style.display = "none";
    document.getElementById("reset_codec").style.display = "none";
  }
  else if (tabName == 'Info')
  {
    document.getElementById("cfg_buttons").style.display = "none";
    document.getElementById("apply_config").style.display = "none";
    document.getElementById("apply_unit").style.display = "none";
    document.getElementById("load").style.display = "none";
    document.getElementById("reload_preset").style.display = "none";
    document.getElementById("restore_def").style.display = "none";
    document.getElementById("load_unit").style.display = "none";
    document.getElementById("reboot_unit").style.display = "none";
    document.getElementById("reset_codec").style.display = "none";
    timer = setInterval("timer_status_only();",1000);
  }
  // Disable all tabs "active" status (default colour)
  tablinks = document.getElementsByClassName("tablinksh");
  for (i = 0; i < tablinks.length; i++)
  {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(tabName).style.display = "inline-block";
  evt.currentTarget.className += " active";
}

function headerChange(title, UnitName, TxPresent)
{  
  var a = document.getElementById('title');
  var b = document.getElementById('logo');
  
  if(title == undefined || title == "")
  {
      title = "Sapphire HEVC 4K";
  }
  if(TxPresent != 0)
	  title += " TX";
  else
	  title += " Decoder";
  
  if(a.innerHTML != "<h1>" + title + "</h1>")
  {
    a.innerHTML = "<h1>" + title + "</h1>";
  }
  
  document.getElementById('titleUp').value = title;
  document.getElementById('titleLic').value = title;
  document.getElementById('headUp').value = UnitName;
  document.getElementById('headLic').value = UnitName;
  document.getElementById('unit_name').innerText = UnitName;
  
  img_tag = '<img src="'+ logo + '">';
  if(b.innerHTML != img_tag)
  {
    b.innerHTML = img_tag;
  }
}

function removeOptions(selectbox)
{
  var select = document.getElementById(selectbox);
  var i;
  for(i = select.options.length - 1 ; i >= 0 ; i--)
  {
    select.remove(i);
  }
}

function save_json()
{
  var check_ip = 1;
  
  check_ip &= ValidateIPaddress("IPStreamAddress");
  
  if(check_ip == 1)
  {
    setDecode();
  }
}

function refreshPreview()
{
  var img = document.getElementById('vid_bmp');
  img.src = img.src.split("?")[0] + "?" + new Date().getTime();
}

function getStatus()
{
  var get_json = new XMLHttpRequest();
  get_json.addEventListener("load", (e) =>
  {
    var json_load = JSON.parse(e.target.responseText);
    var ASILock = json_load.ASILock;
    var ASIBitrate = (json_load.ASIBitrate)/1000;
    var ServiceOk =  json_load.ServiceOk;
    var NumServices =  json_load.NumServices;
    var ServiceId =  json_load.ServiceId;
    var ServiceName =  json_load.ServiceName;
    var VidDecodeOk = json_load.VidDecodeOk;
    var VideoFormat = json_load.VideoFormat;
    var VideoMode = json_load.VideoMode;
    var BitDepth = json_load.BitDepth;
    var ChromaFormat = json_load.ChromaFmt;
    var SDIFormat = json_load.SDIFormat;
    var FullRange = json_load.FullRange;
    var Colour = json_load.Colour;
    var Transfer = json_load.Transfer;
    var Matrix = json_load.Matrix;
    var VideoPid1 =  json_load.VideoPid1;
    var AudioPid1 =  json_load.AudioPid1;
    var AudioPid2 =  json_load.AudioPid2;
    var AudioPid3 =  json_load.AudioPid3;
    var AudioPid4 =  json_load.AudioPid4;
    var AudioPid5 =  json_load.AudioPid5;
    var AudioPid6 =  json_load.AudioPid6;
    var AudioPid7 =  json_load.AudioPid7;
    var AudioPid8 =  json_load.AudioPid8;
    var DataPid1 =  json_load.DataPid1;
    var DataPid2 =  json_load.DataPid2;
    var PMTPid =  json_load.PMTPid;
    var PCRPid1 =  json_load.PCRPid1;
    var GenlockStatus =  json_load.GenlockStatus;
    var GenlockFormat =  json_load.GenlockFormat;
    var GenlockDelay =  json_load.GenlockDelay1;
    var genlock_mode = getValue("GenlockModeSelect");
    var SampleFormat = getText("SampleFormatSelect");
    var FPGATemp =  json_load.FPGATemp;
    var PSUTemp =  json_load.PSUTemp;
    var InVoltage =  json_load.InVoltage;
    var SoftwareVersion =  json_load.SoftwareVersion;
    var SRTVersion =  json_load.SRTVersion;
    var SerialNumber =  json_load.SerialNumber;

    CustID = json_load.CustID;
    DTM3200Present = json_load.DTM3200Present;
    B120Present = json_load.B120Present;
    LicenseMask = json_load.LicenseMask;
    LicDec = parseInt(LicenseMask, 16)
    license_des = (LicDec&(1<<12))>>12
    license_biss = (LicDec&(1<<14))>>14
    license_enc = LicDec&(1<<0)
    license_dec = (LicDec&(1<<1))>>1
    license_ip = (LicDec&(1<<16))>>16

    if(CustID == "00000000" || CustID == "0000ffff" || CustID == "00001234" || CustID == "00004E98")
    {
      document.getElementById("Support").style.display = "inline-block"
    }

    if((license_biss == 0) && (license_des == 0))
    {
      document.getElementById("Scrambling").style.display = "none"
    }
    else if(license_des == 0)
    {
      document.getElementById("hide_des").style.display = "none"
    }
    else if((B120Present == 0) || (license_biss == 0))
    {
      document.getElementById("hide_biss").style.display = "none"
    }
	ipInterfaceChange();

	var enableLevelBar0 = ((AudioPid1 == 0) || (AudioPid1 == 8191))?0:1;
	var enableLevelBar1 = ((AudioPid2 == 0) || (AudioPid2 == 8191))?0:1;
	var enableLevelBar2 = ((AudioPid3 == 0) || (AudioPid3 == 8191))?0:1;
	var enableLevelBar3 = ((AudioPid4 == 0) || (AudioPid4 == 8191))?0:1;
	var enableLevelBar4 = ((AudioPid5 == 0) || (AudioPid5 == 8191))?0:1;
	var enableLevelBar5 = ((AudioPid6 == 0) || (AudioPid6 == 8191))?0:1;
	var enableLevelBar6 = ((AudioPid7 == 0) || (AudioPid7 == 8191))?0:1;
	var enableLevelBar7 = ((AudioPid8 == 0) || (AudioPid8 == 8191))?0:1;
	updateLevelBar(enableLevelBar0,json_load.AudLevL0,json_load.AudData0,0,0,json_load.AudioMode1);
	updateLevelBar(enableLevelBar0,json_load.AudLevR0,json_load.AudData0,0,1,json_load.AudioMode1);
	updateLevelBar(enableLevelBar1,json_load.AudLevL1,json_load.AudData1,1,0,json_load.AudioMode2);
	updateLevelBar(enableLevelBar1,json_load.AudLevR1,json_load.AudData1,1,1,json_load.AudioMode2);
	updateLevelBar(enableLevelBar2,json_load.AudLevL2,json_load.AudData2,2,0,json_load.AudioMode3);
	updateLevelBar(enableLevelBar2,json_load.AudLevR2,json_load.AudData2,2,1,json_load.AudioMode3);
	updateLevelBar(enableLevelBar3,json_load.AudLevL3,json_load.AudData3,3,0,json_load.AudioMode4);
	updateLevelBar(enableLevelBar3,json_load.AudLevR3,json_load.AudData3,3,1,json_load.AudioMode4);
	updateLevelBar(enableLevelBar4,json_load.AudLevL4,json_load.AudData4,4,0,json_load.AudioMode5);
	updateLevelBar(enableLevelBar4,json_load.AudLevR4,json_load.AudData4,4,1,json_load.AudioMode5);
	updateLevelBar(enableLevelBar5,json_load.AudLevL5,json_load.AudData5,5,0,json_load.AudioMode6);
	updateLevelBar(enableLevelBar5,json_load.AudLevR5,json_load.AudData5,5,1,json_load.AudioMode6);
	updateLevelBar(enableLevelBar6,json_load.AudLevL6,json_load.AudData6,6,0,json_load.AudioMode7);
	updateLevelBar(enableLevelBar6,json_load.AudLevR6,json_load.AudData6,6,1,json_load.AudioMode7);
	updateLevelBar(enableLevelBar7,json_load.AudLevL7,json_load.AudData7,7,0,json_load.AudioMode8);
	updateLevelBar(enableLevelBar7,json_load.AudLevR7,json_load.AudData7,7,1,json_load.AudioMode8);

	if(getValue("IPStreamEnable") == 0)
	{
		document.getElementById("ip_sts").innerHTML = "DISABLED";
		document.getElementById("ip_sts").classList.remove("low");
		document.getElementById("ip_sts").classList.remove("med");
		document.getElementById("ip_sts").classList.remove("high");
	}
	else if(getValue("IPStreamInterface") != 2) // not DTM-3200
	{
		var ipRxStatus = json_load.IPReceiver;
		document.getElementById("ip_sts").innerHTML = (((ipRxStatus&0x0006)==0)?"UDP":"RTP");
		var errorLevel = "low";
		if((ipRxStatus&0x0001)==0)
			errorLevel = "high";
		
		document.getElementById("ip_sts").classList.remove("low");
		document.getElementById("ip_sts").classList.remove("med");
		document.getElementById("ip_sts").classList.remove("high");
		document.getElementById("ip_sts").classList.add(errorLevel);
		
		ASILock = ((ipRxStatus&0x0001)==0)?'0':'1';
	}
	else
	{
		var dtmStatus = json_load.DtmRxStatus;
		if((dtmStatus&0x03) == 0)
		{
			document.getElementById("ip_sts").innerHTML = ((json_load.DtmRxProtocol==0)?"UDP":"RTP") + ((getValue("IPFECEnable")==0)?"":"+FEC");
			var errorLevel = "low";
			if(((dtmStatus&0x48) != 0) || ((getValue("IPFECEnable")!=0) && (json_load.DtmRxFECCols==0) && (json_load.DtmRxFECRows==0)))
				errorLevel = "med";
			if((dtmStatus&0xb4) != 0)
				errorLevel = "high";
			
			document.getElementById("ip_sts").classList.remove("low");
			document.getElementById("ip_sts").classList.remove("med");
			document.getElementById("ip_sts").classList.remove("high");
			document.getElementById("ip_sts").classList.add(errorLevel);
		}
		else
		{
			document.getElementById("ip_sts").innerHTML = (((dtmStatus&0x03) == 1)?"DISABLED":"ERROR");
			document.getElementById("ip_sts").classList.remove("low");
			document.getElementById("ip_sts").classList.remove("med");
			document.getElementById("ip_sts").classList.add("high");
		}
	}
	
    if(ASILock == '1') // may be overwriten by IP lock status
    {
      document.getElementById("asi_lock").innerHTML = ASIBitrate.toFixed(3)+" Mb/s";
      document.getElementById("asi_lock").classList.add("low");
      document.getElementById("asi_lock").classList.remove("med");
      document.getElementById("asi_lock").classList.remove("high");
    }
    else
    {
      document.getElementById("asi_lock").innerHTML = "NO LOCK";
      document.getElementById("asi_lock").classList.remove("low");
      document.getElementById("asi_lock").classList.remove("med");
      document.getElementById("asi_lock").classList.add("high");
    }

    if(VidDecodeOk == '1')
    {
      document.getElementById("vid_sts_dec").innerHTML = "OK";
      document.getElementById("vid_sts_dec").classList.add("low");
      document.getElementById("vid_sts_dec").classList.remove("med");
      document.getElementById("vid_sts_dec").classList.remove("high");
      document.getElementById("vid_sts_fmt").innerHTML = vid_format[VideoFormat];
      if(VideoFormat > 22)
      {
        var quad_sync = getValue("QuadSyncSelect");
        if(quad_sync == 0)
        {
          document.getElementById("vid_sts_sdi").innerHTML = vid_format[SDIFormat];
        }
        else
        {
          document.getElementById("vid_sts_sdi").innerHTML = vid_format[SDIFormat] + " (Quad)";
        }
      }
      else
      {
        var hd_duplication = getValue("HDDuplicateSelect");
        if(hd_duplication == 0)
        {
          document.getElementById("vid_sts_sdi").innerHTML = vid_format[SDIFormat];
        }
        else
        {
          document.getElementById("vid_sts_sdi").innerHTML = vid_format[SDIFormat] + " (4x)";
        }
      }
      document.getElementById("vid_sts_sample").innerHTML = SampleFormat;
      document.getElementById("vid_sts_depth").innerHTML = chroma_depth[BitDepth];
      document.getElementById("vid_sts_chroma").innerHTML = chroma_format[ChromaFormat];
      document.getElementById("vid_sts_mode").innerHTML = decoder_mode[VideoMode];

      switch(FullRange) {
        case 1: document.getElementById("vid_sts_range").innerHTML = "Full"; break;
        default: document.getElementById("vid_sts_range").innerHTML = "Narrow"; break;
      }
      switch(Colour) {
        case 2: document.getElementById("vid_sts_colour").innerHTML = "Unspecified"; break;
        case 1: document.getElementById("vid_sts_colour").innerHTML = "Rec.709"; break;
        case 5: document.getElementById("vid_sts_colour").innerHTML = "Rec.601 625"; break;
        case 6: document.getElementById("vid_sts_colour").innerHTML = "Rec.601 525"; break;
        case 9: document.getElementById("vid_sts_colour").innerHTML = "Rec.2020"; break;
        default: document.getElementById("vid_sts_colour").innerHTML = Colour; break;
      }
      switch(Transfer) {
        case 2: document.getElementById("vid_sts_transfer").innerHTML = "Unspecified"; break;
        case 1: document.getElementById("vid_sts_transfer").innerHTML = "Rec.709 SDR"; break;
        case 6: document.getElementById("vid_sts_transfer").innerHTML = "Rec.601 SDR"; break;
        case 14: document.getElementById("vid_sts_transfer").innerHTML = "Rec.2020 SDR"; break;
        case 15: document.getElementById("vid_sts_transfer").innerHTML = "Rec.2020 SDR"; break;
        case 16: document.getElementById("vid_sts_transfer").innerHTML = "Rec.2100 PQ"; break;
        case 18: document.getElementById("vid_sts_transfer").innerHTML = "Rec.2100 HLG"; break;
        default: document.getElementById("vid_sts_transfer").innerHTML = Transfer; break;
      }
      switch(Matrix) {
        case 2: document.getElementById("vid_sts_matrix").innerHTML = "Unspecified"; break;
        case 1: document.getElementById("vid_sts_matrix").innerHTML = "Rec.709"; break;
        case 5: document.getElementById("vid_sts_matrix").innerHTML = "Rec.601 625"; break;
        case 6: document.getElementById("vid_sts_matrix").innerHTML = "Rec.601 525"; break;
        case 9: document.getElementById("vid_sts_matrix").innerHTML = ((Transfer==16)||(Transfer==18))?"Rec.2100 YCbCr":"Rec.2020 NCL"; break;
        case 10: document.getElementById("vid_sts_matrix").innerHTML = "Rec.2020 CL"; break;
        case 14: document.getElementById("vid_sts_matrix").innerHTML = "Rec.2100 ICtCp"; break;
        default: document.getElementById("vid_sts_matrix").innerHTML = Matrix; break;
      }
    }
    else
    {
      document.getElementById("vid_sts_dec").innerHTML = "ERROR";
      document.getElementById("vid_sts_dec").classList.remove("low");
      document.getElementById("vid_sts_dec").classList.remove("med");
      document.getElementById("vid_sts_dec").classList.add("high");
      document.getElementById("vid_sts_fmt").innerHTML = "-";
      document.getElementById("vid_sts_sdi").innerHTML = "-";
      document.getElementById("vid_sts_sample").innerHTML = "-";
      document.getElementById("vid_sts_depth").innerHTML = "-";
      document.getElementById("vid_sts_chroma").innerHTML = "-";
      document.getElementById("vid_sts_mode").innerHTML = "-";
      document.getElementById("vid_sts_range").innerHTML = "-";
      document.getElementById("vid_sts_colour").innerHTML = "-";
      document.getElementById("vid_sts_transfer").innerHTML = "-";
      document.getElementById("vid_sts_matrix").innerHTML = "-";
    }

    if(ServiceOk == '1')
    {
      document.getElementById("vid_sts_ser_ok").innerHTML = "OK";
      document.getElementById("vid_sts_ser_ok").classList.add("low");
      document.getElementById("vid_sts_ser_ok").classList.remove("med");
      document.getElementById("vid_sts_ser_ok").classList.remove("high");
      document.getElementById("vid_sts_ser_name").innerHTML = ServiceName;
      document.getElementById("vid_sts_ser_id").innerHTML = ServiceId;
      if((PMTPid == 0) || (PMTPid == 8191))
      {
        document.getElementById("vid_sts_pmt_pid").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_pmt_pid").innerHTML = PMTPid;
      }

      if((VideoPid1 == 0) || (VideoPid1 == 8191))
      {
        document.getElementById("vid_sts_v_pid").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_v_pid").innerHTML = VideoPid1;
      }

      if((PCRPid1 == 0) || (PCRPid1 == 8191))
      {
        document.getElementById("vid_sts_pcr_pid").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_pcr_pid").innerHTML = PCRPid1;
      }

      if((AudioPid1 == 0) || (AudioPid1 == 8191))
      {
        document.getElementById("vid_sts_a_pid1").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid1").innerHTML = AudioPid1;
      }

      if((AudioPid2 == 0) || (AudioPid2 == 8191))
      {
        document.getElementById("vid_sts_a_pid2").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid2").innerHTML = AudioPid2;
      }

      if((AudioPid3 == 0) || (AudioPid3 == 8191))
      {
        document.getElementById("vid_sts_a_pid3").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid3").innerHTML = AudioPid3;
      }

      if((AudioPid4 == 0) || (AudioPid4 == 8191))
      {
        document.getElementById("vid_sts_a_pid4").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid4").innerHTML = AudioPid4;
      }

      if((AudioPid5 == 0) || (AudioPid5 == 8191))
      {
        document.getElementById("vid_sts_a_pid5").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid5").innerHTML = AudioPid5;
      }

      if((AudioPid6 == 0) || (AudioPid6 == 8191))
      {
        document.getElementById("vid_sts_a_pid6").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid6").innerHTML = AudioPid6;
      }

      if((AudioPid7 == 0) || (AudioPid7 == 8191))
      {
        document.getElementById("vid_sts_a_pid7").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid7").innerHTML = AudioPid7;
      }

      if((AudioPid8 == 0) || (AudioPid8 == 8191))
      {
        document.getElementById("vid_sts_a_pid8").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_a_pid8").innerHTML = AudioPid8;
      }

      if((DataPid1 == 0) || (DataPid1 == 8191))
      {
        document.getElementById("vid_sts_d_pid1").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_d_pid1").innerHTML = DataPid1;
      }

      if((DataPid2 == 0) || (DataPid2 == 8191))
      {
        document.getElementById("vid_sts_d_pid2").innerHTML = "-";
      }
      else
      {
        document.getElementById("vid_sts_d_pid2").innerHTML = DataPid2;
      }
    }
    else
    {
      document.getElementById("vid_sts_ser_ok").innerHTML = "ERROR";
      document.getElementById("vid_sts_ser_ok").classList.remove("low");
      document.getElementById("vid_sts_ser_ok").classList.remove("med");
      document.getElementById("vid_sts_ser_ok").classList.add("high");
      document.getElementById("vid_sts_ser_name").innerHTML = "-";
      document.getElementById("vid_sts_ser_id").innerHTML = "-";
      document.getElementById("vid_sts_pmt_pid").innerHTML = "-";
      document.getElementById("vid_sts_v_pid").innerHTML = "-";
      document.getElementById("vid_sts_pcr_pid").innerHTML = "-";
      document.getElementById("vid_sts_a_pid1").innerHTML = "-";
      document.getElementById("vid_sts_a_pid2").innerHTML = "-";
      document.getElementById("vid_sts_a_pid3").innerHTML = "-";
      document.getElementById("vid_sts_a_pid4").innerHTML = "-";
      document.getElementById("vid_sts_a_pid5").innerHTML = "-";
      document.getElementById("vid_sts_a_pid6").innerHTML = "-";
      document.getElementById("vid_sts_a_pid7").innerHTML = "-";
      document.getElementById("vid_sts_a_pid8").innerHTML = "-";
      document.getElementById("vid_sts_d_pid1").innerHTML = "-";
      document.getElementById("vid_sts_d_pid2").innerHTML = "-";
    }
    document.getElementById("unit_sts_temp1").innerHTML = FPGATemp+" &deg;C";
    if(FPGATemp > 85)
    {
      document.getElementById("unit_sts_temp1").classList.remove("low");
      document.getElementById("unit_sts_temp1").classList.remove("med");
      document.getElementById("unit_sts_temp1").classList.add("high");
    }
    else if(FPGATemp > 75)
    {
      document.getElementById("unit_sts_temp1").classList.remove("low");
      document.getElementById("unit_sts_temp1").classList.add("med");
      document.getElementById("unit_sts_temp1").classList.remove("high");
    }    
    else
    {
      document.getElementById("unit_sts_temp1").classList.add("low");
      document.getElementById("unit_sts_temp1").classList.remove("med");
      document.getElementById("unit_sts_temp1").classList.remove("high");
    }

    document.getElementById("unit_sts_temp2").innerHTML = PSUTemp+" &deg;C";
    if(PSUTemp > 125)
    {
      document.getElementById("unit_sts_temp2").classList.remove("low");
      document.getElementById("unit_sts_temp2").classList.remove("med");
      document.getElementById("unit_sts_temp2").classList.add("high");
    }
    else if(PSUTemp > 85)
    {
      document.getElementById("unit_sts_temp2").classList.remove("low");
      document.getElementById("unit_sts_temp2").classList.add("med");
      document.getElementById("unit_sts_temp2").classList.remove("high");
    }
    else
    {
      document.getElementById("unit_sts_temp2").classList.add("low");
      document.getElementById("unit_sts_temp2").classList.remove("med");
      document.getElementById("unit_sts_temp2").classList.remove("high");
    }

    document.getElementById("unit_sts_volt_in").innerHTML = InVoltage+" V";
    if(InVoltage > 17.5 && InVoltage < 18)
    {
      document.getElementById("unit_sts_volt_in").classList.remove("low");
      document.getElementById("unit_sts_volt_in").classList.add("med");
      document.getElementById("unit_sts_volt_in").classList.remove("high");
    }
    else if(InVoltage < 9.5 && InVoltage > 9)
    {
      document.getElementById("unit_sts_volt_in").classList.remove("low");
      document.getElementById("unit_sts_volt_in").classList.add("med");
      document.getElementById("unit_sts_volt_in").classList.remove("high");
    }
    else if(InVoltage > 17.9 || InVoltage < 9.1)
    {
      document.getElementById("unit_sts_volt_in").classList.remove("low");
      document.getElementById("unit_sts_volt_in").classList.remove("med");
      document.getElementById("unit_sts_volt_in").classList.add("high");
    }
    else
    {
      document.getElementById("unit_sts_volt_in").classList.add("low");
      document.getElementById("unit_sts_volt_in").classList.remove("med");
      document.getElementById("unit_sts_volt_in").classList.remove("high");
    }

    if(genlock_mode == 1)
    {
      if(GenlockStatus == 0)
      {
        document.getElementById("genlock_sts").classList.remove("low");
        document.getElementById("genlock_sts").classList.remove("med");
        document.getElementById("genlock_sts").classList.add("high");
        document.getElementById("genlock_sts").innerHTML = "NO LOCK";
      }
      else if(GenlockStatus < 2)
      {
        document.getElementById("genlock_sts").innerHTML = "LOCKING..";
        document.getElementById("genlock_sts").classList.remove("low");
        document.getElementById("genlock_sts").classList.add("med");
        document.getElementById("genlock_sts").classList.remove("high");
      }
      else if(GenlockStatus < 6)
      {
        document.getElementById("genlock_sts").innerHTML = "LOCKING....";
        document.getElementById("genlock_sts").classList.remove("low");
        document.getElementById("genlock_sts").classList.add("med");
        document.getElementById("genlock_sts").classList.remove("high");
      }
      else
      {
        document.getElementById("genlock_sts").innerHTML = "LOCKED";
        document.getElementById("genlock_sts").classList.add("low");
        document.getElementById("genlock_sts").classList.remove("med");
        document.getElementById("genlock_sts").classList.remove("high");
      }
    }
    else if(genlock_mode == 2)
    {
      document.getElementById("genlock_sts").innerHTML = "INTERNAL";
      document.getElementById("genlock_sts").classList.remove("low");
      document.getElementById("genlock_sts").classList.add("med");
      document.getElementById("genlock_sts").classList.remove("high");
    }
    else
    {
      document.getElementById("genlock_sts").innerHTML = "DISABLED";
      document.getElementById("genlock_sts").classList.remove("low");
      document.getElementById("genlock_sts").classList.add("med");
      document.getElementById("genlock_sts").classList.remove("high");
    }
	
	if((GenlockFormat & 0x200) == 0x200)
	{
	  var GenlockFormatString = "????";
	  switch(GenlockFormat & 0x0f0) {
		  case 0x000: GenlockFormatString = "1080"; break;
		  case 0x030: GenlockFormatString = "1080"; break;
		  case 0x010: GenlockFormatString = "720"; break;
		  case 0x090: GenlockFormatString = "625"; break;
		  case 0x080: GenlockFormatString = "525"; break;
	  }
	  
	  var GenlockFrameRate = 0;
	  switch(GenlockFormat & 0x10f) {
		  case 0x102: GenlockFrameRate = 23; break;
		  case 0x103: GenlockFrameRate = 24; break;
		  case 0x105: GenlockFrameRate = 25; break;
		  case 0x106: GenlockFrameRate = 29; break;
		  case 0x107: GenlockFrameRate = 30; break;
		  case 0x104: GenlockFrameRate = 47; break;
		  case 0x108: GenlockFrameRate = 48; break;
		  case 0x109: GenlockFrameRate = 50; break;
		  case 0x10a: GenlockFrameRate = 59; break;
		  case 0x10b: GenlockFrameRate = 60; break;
		  case 0x002: GenlockFrameRate = 47; break;
		  case 0x003: GenlockFrameRate = 48; break;
		  case 0x005: GenlockFrameRate = 50; break;
		  case 0x006: GenlockFrameRate = 59; break;
		  case 0x007: GenlockFrameRate = 60; break;
		  case 0x004: GenlockFrameRate = 95; break;
		  case 0x008: GenlockFrameRate = 96; break;
		  case 0x009: GenlockFrameRate = 100; break;
		  case 0x00a: GenlockFrameRate = 119; break;
		  case 0x00b: GenlockFrameRate = 120; break;
	  }
	  
	  if((GenlockFormat & 0x100) == 0x000)
		  GenlockFormatString += "i";
	  else
		  GenlockFormatString += "p";
	  
	  if(GenlockFrameRate == 0)
		  GenlockFormatString += "??";
	  else
		  GenlockFormatString += GenlockFrameRate.toString();
	
      document.getElementById("genlock_fmt").innerHTML = GenlockFormatString;
	}
	else
	{
      document.getElementById("genlock_fmt").innerHTML = "-";
	}
	
    if(genlock_mode != 0)
    {
      document.getElementById("genlock_delay").innerHTML = GenlockDelay + " us";
	}
	else
	{
      document.getElementById("genlock_delay").innerHTML = "-";
	}

    document.getElementById("software_ver").innerHTML = SoftwareVersion;
    document.getElementById("serial_num").innerHTML = SerialNumber;
    document.getElementById("lic_msk").innerHTML = LicenseMask;
    document.getElementById('srt_ver').innerHTML = SRTVersion;

	if(getValue("IPBridgeSelect") != 0)
	{
		document.getElementById("eth0_ipaddr").innerHTML = json_load.Br0IPaddrActive;
		document.getElementById("eth0_gwaddr").innerHTML = json_load.Br0GWaddrActive;
		document.getElementById("eth0_mask").innerHTML = json_load.Br0MaskActive;
	}
	else
	{
		document.getElementById("eth0_ipaddr").innerHTML = json_load.Eth0IPaddrActive;
		document.getElementById("eth0_gwaddr").innerHTML = json_load.Eth0GWaddrActive;
		document.getElementById("eth0_mask").innerHTML = json_load.Eth0MaskActive;		
	}
    document.getElementById("eth0_macaddr").innerHTML = json_load.Eth0MACaddr;
    document.getElementById("eth0_state").innerHTML = "Link State " + json_load.Eth0LinkState;
    document.getElementById("eth0_rx_count").innerHTML = "RX: " + json_load.Eth0RxCount;
    document.getElementById("eth0_tx_count").innerHTML = "TX: " + json_load.Eth0TxCount;

    document.getElementById("eth1_ipaddr").innerHTML = json_load.Eth1IPaddrActive;
    document.getElementById("eth1_gwaddr").innerHTML = json_load.Eth1GWaddrActive;
    document.getElementById("eth1_mask").innerHTML = json_load.Eth1MaskActive;
    document.getElementById("eth1_macaddr").innerHTML = json_load.Eth1MACaddr;
    document.getElementById("eth1_state").innerHTML = "Link State " + json_load.Eth1LinkState;
    document.getElementById("eth1_rx_count").innerHTML = "RX: " + json_load.Eth1RxCount;
    document.getElementById("eth1_tx_count").innerHTML = "TX: " + json_load.Eth1TxCount;

	document.getElementById("b110_bitrate").innerHTML = ASIBitrate.toFixed(3) + " Mb/s";
	  
	if(DTM3200Present == 1)
    {
      document.getElementById("ip_hardware_ver").innerHTML = "v" + (json_load.DtmSWVersion/10).toFixed(1);
	  
      document.getElementById("dtm_ipaddr").innerHTML = json_load.DtmIPaddrActive;
      document.getElementById("dtm_gwaddr").innerHTML = json_load.DtmGWaddrActive;
      document.getElementById("dtm_mask").innerHTML = json_load.DtmMaskActive;
      document.getElementById("dtm_macaddr").innerHTML = json_load.DtmMACaddr;
      document.getElementById("dtm_state").innerHTML = "Link State " + json_load.DtmLinkState;
      document.getElementById("dtm_rx_count").innerHTML = "RX: " + ((json_load.DtmLinkState=="")?"-":json_load.DtmRxCount);
      document.getElementById("dtm_tx_count").innerHTML = "TX: " + ((json_load.DtmLinkState=="")?"-":json_load.DtmTxCount);
	  
      document.getElementById("dtm_bitrate").innerHTML = (json_load.DtmRxBitrate/1000000).toFixed(3) + " Mb/s";
      document.getElementById("dtm_fec_cols").innerHTML = json_load.DtmRxFECCols;
      document.getElementById("dtm_fec_rows").innerHTML = json_load.DtmRxFECRows;
      document.getElementById("dtm_fec_delay").innerHTML = json_load.DtmRxFECDelay + " ms";
      document.getElementById("dtm_fec_before").innerHTML = json_load.DtmRxFECBefore;
      document.getElementById("dtm_fec_after").innerHTML = json_load.DtmRxFECAfter;
      document.getElementById("dtm_jitter_tolerance").innerHTML = json_load.DtmRxJitterTolerance + " ms";
      document.getElementById("dtm_jitter_errors").innerHTML = json_load.DtmRxJitterErrors;
      document.getElementById("dtm_lock_errors").innerHTML = json_load.DtmRxLockErrors;
      document.getElementById("dtm_delay_factor").innerHTML = (json_load.DtmRxDelayFactor/1000).toFixed(0) + " ms";
	}
	
    var lic_bits = parseInt(LicenseMask,16);
    if((lic_bits&0x00000001)==0) document.getElementById("lic_encode").style.color = "#ddd";
    if((lic_bits&0x00000002)==0) document.getElementById("lic_decode").style.color = "#ddd";
    if((lic_bits&0x00000004)==0) document.getElementById("lic_hevc").style.color = "#ddd";
    if((lic_bits&0x00000008)==0) document.getElementById("lic_h264").style.color = "#ddd";
    if((lic_bits&0x00000010)==0) document.getElementById("lic_mpeg2").style.color = "#ddd";
    if((lic_bits&0x00000020)==0) document.getElementById("lic_ull").style.color = "#ddd";
    if((lic_bits&0x00000040)==0) document.getElementById("lic_uhd").style.color = "#ddd";
    if((lic_bits&0x00000080)==0) document.getElementById("lic_422").style.color = "#ddd";
    if((lic_bits&0x00000100)==0) document.getElementById("lic_10bit").style.color = "#ddd";
    if((lic_bits&0x00000200)==0) document.getElementById("lic_quadsync").style.color = "#ddd";
    if((lic_bits&0x00000400)==0) document.getElementById("lic_psf").style.color = "#ddd";
    if((lic_bits&0x00000800)==0) document.getElementById("lic_dolby").style.color = "#ddd";
    if((lic_bits&0x00001000)==0) document.getElementById("lic_des").style.color = "#ddd";
    if((lic_bits&0x00004000)==0) document.getElementById("lic_biss").style.color = "#ddd";
    if((lic_bits&0x00008000)==0) document.getElementById("lic_ipstream").style.color = "#ddd";
    if((lic_bits&0x00010000)==0) document.getElementById("lic_ipdecode").style.color = "#ddd";
    if((lic_bits&0x00020000)==0) document.getElementById("lic_layer1").style.color = "#ddd";
  }
);
get_json.open("GET", "/sts/UnitStatus.json", true);
get_json.setRequestHeader('Cache-Control', 'no-cache')
get_json.setRequestHeader('Pragma', 'no-cache')
get_json.setRequestHeader('Expires', '0')
get_json.send();
}

function getUnit()
{
  var get_json = new XMLHttpRequest();
  get_json.addEventListener ("load", (e) =>
  {
    var json_load = JSON.parse(e.target.responseText);
    PresetNumber = json_load.PresetNumber;
    B120Present = json_load.B120Present;
    DTM3200Present = json_load.DTM3200Present;
    var UnitMode = json_load.UnitMode;
    var UnitName = json_load.UnitName;
    var DHCPMode0 = json_load.DHCPMode0;
    var IPAddress0 = json_load.IPAddress0;
    var IPGateway0 = json_load.IPGateway0;
    var IPMask0 = json_load.IPMask0;
    var DHCPMode1 = json_load.DHCPMode1;
    var IPAddress1 = json_load.IPAddress1;
    var IPGateway1 = json_load.IPGateway1;
    var IPMask1 = json_load.IPMask1;
    var IPBridgingMode = json_load.IPBridgingMode;
    var ProductName = json_load.ProductName;
    var TxPresent = json_load.TxPresent;
    var B120Present = json_load.B120Present;
    var DTM3200Present = json_load.DTM3200Present;
    var OLEDType = json_load.OLEDType;

    headerChange(ProductName,UnitName,TxPresent);

    setSelectValue("UnitModeSelect",UnitMode);
    setNumber("UnitName",UnitName);
	
    setSelectValue("DHCPModeSelect0",DHCPMode0);
    setNumber("IPAddr0",IPAddress0);
    setNumber("IPGateway0",IPGateway0);
    setNumber("IPMask0",IPMask0);
	ethDHCPChange(0);
	
    setSelectValue("DHCPModeSelect1",DHCPMode1);
    setNumber("IPAddr1",IPAddress1);
    setNumber("IPGateway1",IPGateway1);
    setNumber("IPMask1",IPMask1);
	ethDHCPChange(1);
	
    setSelectValue("IPBridgeSelect",IPBridgingMode);

    var DtmDHCPMode = json_load.DtmDHCPMode;
    var DtmIPAddress = json_load.DtmIPAddress;
    var DtmIPGateway = json_load.DtmIPGateway;
    var DtmIPMask = json_load.DtmIPMask;

    setSelectValue("DtmDHCPModeSelect",DtmDHCPMode);
    setNumber("DtmIPAddress",DtmIPAddress);
    setNumber("DtmIPGateway",DtmIPGateway);
    setNumber("DtmIPMask",DtmIPMask);
	dtmDHCPChange();
	
    setSelectValue("TxPresentSelect",TxPresent);
    setSelectValue("B120PresentSelect",B120Present);
    setSelectValue("DTM3200PresentSelect",DTM3200Present);
    setSelectValue("OLEDTypeSelect",OLEDType);
	
    setNumber("CustIDVal",parseInt(CustID,16));
  }
);
get_json.open("GET", "/config/UnitParamsList.json");
get_json.setRequestHeader('Cache-Control', 'no-cache')
get_json.setRequestHeader('Pragma', 'no-cache')
get_json.setRequestHeader('Expires', '0')
get_json.send();
}

function getDecode()
{
  var get_json = new XMLHttpRequest();
  get_json.addEventListener ("load", (e) =>
    {
      var json_load = JSON.parse(e.target.responseText);
      var DecoderMode	= json_load.DecoderMode;
      //var DecoderSource = json_load.DecoderSource;
      //var VideoSource	= json_load.VideoSource;
      var VideoFormat	= json_load.VideoFormat;
      //var ChromaFormat = json_load.ChromaFormat;
      //var BitDepth = json_load.BitDepth;
      var LowDelayMode = json_load.LowDelayMode;
      var SampleFormat = json_load.SampleFormat;
      var VideoPid = json_load.VideoPid;
      var AudioPid0 = json_load.AudioPid0;
      var AudioPid1 = json_load.AudioPid1;
      var AudioPid2 = json_load.AudioPid2;
      var AudioPid3 = json_load.AudioPid3;
      var AudioPid4 = json_load.AudioPid4;
      var AudioPid5 = json_load.AudioPid5;
      var AudioPid6 = json_load.AudioPid6;
      var AudioPid7 = json_load.AudioPid7;
      var PCRPid = json_load.PCRPid;
      var PMTPid = json_load.PMTPid;
      //var SITPid = json_load.SITPid;
      var DataPid = json_load.DataPid;
      var AncillaryPid = json_load.AncillaryPid;
      //var IPPid = json_load.IPPid;
      var ProgramNumber = json_load.ProgramNumber;
      var DefServiceName = json_load.DefServiceName;
      //var AudioDecoder0 = json_load.AudioDecoder0;
      var AudioSource0 = json_load.AudioSource0;
      //var AudioMode0 = json_load.AudioMode0;
      // var AudioGainLeft0 = json_load.AudioGainLeft0;
      // var AudioGainRight0 = json_load.AudioGainRight0;
      var AudioPTSOffset0 = json_load.AudioPTSOffset0;
      //var AudioDecoder1 = json_load.AudioDecoder1;
      //var AudioSource1 = json_load.AudioSource1;
      //var AudioMode1 = json_load.AudioMode1;
      // var AudioGainLeft1 = json_load.AudioGainLeft1;
      // var AudioGainRight1 = json_load.AudioGainRight1;
      var AudioPTSOffset1 = json_load.AudioPTSOffset1;
      //var AudioDecoder2 = json_load.AudioDecoder2;
      //var AudioSource2 = json_load.AudioSource2;
      //var AudioMode2 = json_load.AudioMode2;
      var AudioPTSOffset2 = json_load.AudioPTSOffset2;
      //var AudioDecoder3 = json_load.AudioDecoder3;
      //var AudioSource3 = json_load.AudioSource3;
      //var AudioMode3 = json_load.AudioMode3;
      var AudioPTSOffset3 = json_load.AudioPTSOffset3;
      //var AudioDecoder4 = json_load.AudioDecoder4;
      //var AudioSource4 = json_load.AudioSource4;
      //var AudioMode4 = json_load.AudioMode4;
      var AudioPTSOffset4 = json_load.AudioPTSOffset4;
      //var AudioDecoder4 = json_load.AudioDecoder4;
      //var AudioSource4 = json_load.AudioSource4;
      //var AudioMode4 = json_load.AudioMode4;
      var AudioPTSOffset4 = json_load.AudioPTSOffset4;
      //var AudioDecoder5 = json_load.AudioDecoder5;
      //var AudioSource5 = json_load.AudioSource5;
      //var AudioMode5 = json_load.AudioMode5;
      var AudioPTSOffset5 = json_load.AudioPTSOffset5;
      //var AudioDecoder6 = json_load.AudioDecoder6;
      //var AudioSource6 = json_load.AudioSource6;
      //var AudioMode6 = json_load.AudioMode6;
      var AudioPTSOffset6 = json_load.AudioPTSOffset6;
      //var AudioDecoder7 = json_load.AudioDecoder7;
      //var AudioSource7 = json_load.AudioSource7;
      //var AudioMode7 = json_load.AudioMode7;
      var AudioPTSOffset7 = json_load.AudioPTSOffset7;
      //var DataMode1 = json_load.DataMode1;
      //var DataFormat = json_load.DataFormat;
      //var DataType = json_load.DataType;
      var IPStreamEnable = json_load.IPStreamEnable;
      var IPStreamInterface = json_load.IPStreamInterface-1;
      var IPStreamMode = json_load.IPStreamMode;
      var IPStreamAddress = json_load.IPStreamAddress;
      var IPStreamPort = json_load.IPStreamPort;
      var IPStreamDelay = json_load.IPStreamDelay;
      var IPStreamRateMode = json_load.IPStreamRateMode;
      var IPFECEnable = json_load.IPFECEnable;
      var BufferDelay = json_load.BufferDelay;
      //var ManualMode = json_load.ManualMode;
      //var ErrorMask = json_load.ErrorMask;
      var AudioRefLevel0 = json_load.AudioRefLevel0;
      var AudioRefLevel1 = json_load.AudioRefLevel1;
      var VideoConcealLev = json_load.VideoConcealLev;
      //var VideoConcealRef = json_load.VideoConcealRef;
      var VideoFailMode1 = json_load.VideoFailMode1;
      //var VideoFailMode2 = json_load.VideoFailMode2;
      var ServiceSelect = json_load.ServiceSelect;
      var GenlockMode = json_load.GenlockMode;
      var GenlockLineOffset = json_load.GenlockLineOffset;
      var GenlockPixelOffset = json_load.GenlockPixelOffset;
      //var AudioDecoderCtrl = json_load.AudioDecoderCtrl;
      var QuadSync = json_load.SynergyMode;
      var DESMode = json_load.DESMode;
      var DESKey = json_load.DESKey;
      var AncillaryMode = json_load.AncillaryMode;
      var HdDuplication = json_load.HdDuplication;
      var BISSMode = json_load.BISSMode;
      var BISSKey = json_load.BISSKey;

      setSelectValue("DecodeModeSelect",DecoderMode);
      setSelectValue("VideoFormatSelect",VideoFormat);
      setSelectValue("LowDelayModeSelect",LowDelayMode);
      setSelectValue("SampleFormatSelect",SampleFormat);
      setNumber("VideoPidVal",VideoPid);
      setNumber("AudioPid0Val",AudioPid0);
      setNumber("AudioPid1Val",AudioPid1);
      setNumber("AudioPid2Val",AudioPid2);
      setNumber("AudioPid3Val",AudioPid3);
      setNumber("AudioPid4Val",AudioPid4);
      setNumber("AudioPid5Val",AudioPid5);
      setNumber("AudioPid6Val",AudioPid6);
      setNumber("AudioPid7Val",AudioPid7);
      setNumber("PCRPidVal",PCRPid);
      setNumber("PMTPidVal",PMTPid);
      setNumber("DataPidVal",DataPid);
      setNumber("AncillaryPidVal",AncillaryPid);
      setNumber("ProgramNumberVal",ProgramNumber);
      setNumber("ServiceNameVal",DefServiceName);
      // setNumber("AudioGainL0Val",AudioGainLeft0);
      // setNumber("AudioGainR0Val",AudioGainRight0);
      // setNumber("AudioGainL1Val",AudioGainLeft1);
      // setNumber("AudioGainR1Val",AudioGainRight1);
      setSelectValue("AudioSource0Select",AudioSource0);
      //setSelectValue("AudioSource1Select",AudioSource1);
      //setSelectValue("AudioSource2Select",AudioSource2);
      //setSelectValue("AudioSource3Select",AudioSource3);
      //setSelectValue("AudioSource4Select",AudioSource4);
      //setSelectValue("AudioSource5Select",AudioSource5);
      //setSelectValue("AudioSource6Select",AudioSource6);
      //setSelectValue("AudioSource7Select",AudioSource7);
      setNumber("AudioPTSOffset0Val",AudioPTSOffset0);
      setNumber("AudioPTSOffset1Val",AudioPTSOffset1);
      setNumber("AudioPTSOffset2Val",AudioPTSOffset2);
      setNumber("AudioPTSOffset3Val",AudioPTSOffset3);
      setNumber("AudioPTSOffset4Val",AudioPTSOffset4);
      setNumber("AudioPTSOffset5Val",AudioPTSOffset5);
      setNumber("AudioPTSOffset6Val",AudioPTSOffset6);
      setNumber("AudioPTSOffset7Val",AudioPTSOffset7);
      setSelectValue("IPStreamEnable",IPStreamEnable);
      setSelectValue("IPStreamInterface",IPStreamInterface);
      setSelectValue("IPStreamMode",IPStreamMode);
      setNumber("IPStreamAddress",IPStreamAddress);
      setNumber("IPStreamPort",IPStreamPort);
      setNumber("IPStreamDelay",IPStreamDelay);
      setSelectValue("IPStreamRateMode",IPStreamRateMode);
      setSelectValue("IPFECEnable",IPFECEnable);
      setNumber("BufferDelay",BufferDelay)
      setSelectValue("AudRefLev0",AudioRefLevel0)
      setSelectValue("AudRefLev1",AudioRefLevel1)
      setNumber("ErrConcealmVal",VideoConcealLev)
      setSelectValue("FailModeSelect",VideoFailMode1)
      setSelectValue("ServiceSelSelect",ServiceSelect);
      setSelectValue("GenlockModeSelect",GenlockMode);
      setNumber("GenlockLineOffsetVal",GenlockLineOffset);
      setNumber("GenlockPixelOffsetVal",GenlockPixelOffset);
      setSelectValue("QuadSyncSelect",QuadSync);
      setSelectValue("DESModeSelect",DESMode);
      setNumber("DESKeyVal",DESKey);
      setSelectValue("AncillaryModeSelect",AncillaryMode);
      setSelectValue("HDDuplicateSelect",HdDuplication);
      setSelectValue("BISSModeSelect",BISSMode);
      setNumber("BISSKeyVal",BISSKey);
	  ipInterfaceChange();
    }
  );
  get_json.open("GET", "/config/DecoderParamsList.json", true);
  get_json.setRequestHeader('Cache-Control', 'no-cache')
  get_json.setRequestHeader('Pragma', 'no-cache')
  get_json.setRequestHeader('Expires', '0')
  get_json.send();
}

function setUnit()
{
  var json_send;
  var set_json = new XMLHttpRequest();
  var url = "/cgi-bin/set_unit_val.cgi";
  var str_val = {
    UnitMode: parseInt(getValue("UnitModeSelect")),
    UnitName: getNumber("UnitName"),
    DHCPMode0: parseInt(getValue("DHCPModeSelect0")),
    IPAddress0: getNumber("IPAddr0"),
    IPGateway0: getNumber("IPGateway0"),
    IPMask0: getNumber("IPMask0"),
    DHCPMode1: parseInt(getValue("DHCPModeSelect1")),
    IPAddress1: getNumber("IPAddr1"),
    IPGateway1: getNumber("IPGateway1"),
    IPMask1: getNumber("IPMask1"),
    IPBridgingMode: parseInt(getValue("IPBridgeSelect")),
    DtmDHCPMode: parseInt(getValue("DtmDHCPModeSelect")),
    DtmIPAddress: getNumber("DtmIPAddress"),
    DtmIPGateway: getNumber("DtmIPGateway"),
    DtmIPMask: getNumber("DtmIPMask"),
    TxPresent: parseInt(getValue("TxPresentSelect")),
    B120Present: parseInt(getValue("B120PresentSelect")),
    DTM3200Present: parseInt(getValue("DTM3200PresentSelect")),
    OLEDType: parseInt(getValue("OLEDTypeSelect"))
  };
  json_send = JSON.stringify(str_val);
  set_json.addEventListener ("load", (e) =>
  {
    var reply = e.target.responseText;
    alert("Unit Changes Applied Successfully" + "\n" + reply.trim());
  });
  set_json.open("POST", url, true);
  set_json.setRequestHeader("Content-type", "application/json");
  set_json.send(json_send);
}

function setDecode()
{
  var json_send;
  var set_json = new XMLHttpRequest();
  var url = "/cgi-bin/set_dec_val.cgi";
  des_key_val = document.getElementById('DESKeyVal').value.toUpperCase();
  biss_key_val = document.getElementById('BISSKeyVal').value.toUpperCase();
  document.getElementById('DESKeyVal').value = des_key_val;
  document.getElementById('BISSKeyVal').value = biss_key_val;

  var str_val = {
    VideoFormat: parseInt(getValue("VideoFormatSelect")),
    SampleFormat: parseInt(getValue("SampleFormatSelect")),
    LowDelayMode: parseInt(getValue("LowDelayModeSelect")),
    DecoderMode: parseInt(getValue("DecodeModeSelect")),
    SynergyMode: parseInt(getValue("QuadSyncSelect")),
    VideoConcealLev: parseInt(getNumber("ErrConcealmVal")),
    VideoFailMode1: parseInt(getValue("FailModeSelect")),
    HdDuplication: parseInt(getValue("HDDuplicateSelect")),
    // AudioGainLeft0: parseInt(getNumber("AudioGainL0Val")),
    // AudioGainRight0: parseInt(getNumber("AudioGainR0Val")),
    // AudioGainLeft1: parseInt(getNumber("AudioGainL1Val")),
    // AudioGainRight1: parseInt(getNumber("AudioGainR1Val")),
    AudioRefLevel0: parseInt(getValue("AudRefLev0")),
    AudioRefLevel1: parseInt(getValue("AudRefLev1")),
    ServiceSelect: parseInt(getValue("ServiceSelSelect")),
    ProgramNumber: parseInt(getNumber("ProgramNumberVal")),
    DefServiceName: getNumber("ServiceNameVal"),
    VideoPid: parseInt(getNumber("VideoPidVal")),
    AudioPid0: parseInt(getNumber("AudioPid0Val")),
    AudioPid1: parseInt(getNumber("AudioPid1Val")),
    AudioPid2: parseInt(getNumber("AudioPid2Val")),
    AudioPid3: parseInt(getNumber("AudioPid3Val")),
    AudioPid4: parseInt(getNumber("AudioPid4Val")),
    AudioPid5: parseInt(getNumber("AudioPid5Val")),
    AudioPid6: parseInt(getNumber("AudioPid6Val")),
    AudioPid7: parseInt(getNumber("AudioPid7Val")),
    AncillaryPid: parseInt(getNumber("AncillaryPidVal")),
    DataPid: parseInt(getNumber("DataPidVal")),
    PCRPid: parseInt(getNumber("PCRPidVal")),
    PMTPid: parseInt(getNumber("PMTPidVal")),
    AudioSource0: parseInt(getValue("AudioSource0Select")),
    //AudioSource1: parseInt(getValue("AudioSource1Select")),
    //AudioSource2: parseInt(getValue("AudioSource2Select")),
    //AudioSource3: parseInt(getValue("AudioSource3Select")),
    //AudioSource4: parseInt(getValue("AudioSource4Select")),
    //AudioSource5: parseInt(getValue("AudioSource5Select")),
    //AudioSource6: parseInt(getValue("AudioSource6Select")),
    //AudioSource7: parseInt(getValue("AudioSource7Select")),
    AudioPTSOffset0: parseInt(getNumber("AudioPTSOffset0Val")),
    AudioPTSOffset1: parseInt(getNumber("AudioPTSOffset1Val")),
    AudioPTSOffset2: parseInt(getNumber("AudioPTSOffset2Val")),
    AudioPTSOffset3: parseInt(getNumber("AudioPTSOffset3Val")),
    AudioPTSOffset4: parseInt(getNumber("AudioPTSOffset4Val")),
    AudioPTSOffset5: parseInt(getNumber("AudioPTSOffset5Val")),
    AudioPTSOffset6: parseInt(getNumber("AudioPTSOffset6Val")),
    AudioPTSOffset7: parseInt(getNumber("AudioPTSOffset7Val")),
    GenlockMode: parseInt(getValue("GenlockModeSelect")),
    GenlockLineOffset: parseInt(getNumber("GenlockLineOffsetVal")),
    GenlockPixelOffset: parseInt(getNumber("GenlockPixelOffsetVal")),
    DESMode: parseInt(getValue("DESModeSelect")),
    DESKey: des_key_val,
    BISSMode: parseInt(getValue("BISSModeSelect")),
    BISSKey: biss_key_val,
    AncillaryMode: parseInt(getValue("AncillaryModeSelect")),
    IPFECEnable: parseInt(getValue("IPFECEnable")),
    IPStreamEnable: parseInt(getValue("IPStreamEnable")),
    IPStreamInterface: parseInt(getValue("IPStreamInterface"))+1,
    IPStreamMode: parseInt(getValue("IPStreamMode")),
    IPStreamRateMode: parseInt(getValue("IPStreamRateMode")),
    IPStreamPort: parseInt(getNumber("IPStreamPort")),
    IPStreamDelay: parseInt(getNumber("IPStreamDelay")),
    IPStreamAddress: getNumber("IPStreamAddress"),
    BufferDelay: parseInt(getNumber("BufferDelay")),
  };
  json_send = JSON.stringify(str_val);
  set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      alert("Config Changes Applied Successfully" + "\n" + reply.trim());
    }
  );
  set_json.open("POST", url, true);
  set_json.setRequestHeader("Content-type", "application/json");
  set_json.send(json_send);
}

function restoreDef()
{
  if (confirm("Are You Sure You Want To Restore Default Settings?"))
  {
    var set_json = new XMLHttpRequest();
    var data = new FormData();
    var url = "/cgi-bin/wui_action.cgi";
    data.append("action", 1);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      alert(reply.trim());
    });
    set_json.open("POST", url, true);
    set_json.send(data);
  }
}

function resetCodec()
{
    var set_json = new XMLHttpRequest();
    var data = new FormData();
    var url = "/cgi-bin/wui_action.cgi";
    data.append("action", 2);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      alert(reply.trim());
    });
    set_json.open("POST", url, true);
    set_json.send(data);
}

function rebootUnit()
{
  if (confirm("Are You Sure You Want To Reboot?"))
  {
    var set_json = new XMLHttpRequest();
    var data = new FormData();
    var url = "/cgi-bin/wui_action.cgi";
    data.append("action", 3);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      alert(reply.trim());
    });
    set_json.open("POST", url, true);
    set_json.send(data);	  
  }
}

function setCustomID()
{
  var cid = parseInt(getNumber("CustIDVal"));
  if (isNaN(cid) || cid == 0 || cid > 65535)
  {
	  alert("Invalid Custom ID");
  }
  else if (confirm("Are You Sure You Want To Change Custom ID?"))
  {
    var set_json = new XMLHttpRequest();
    var data = new FormData();
    var url = "/cgi-bin/wui_action.cgi";
    data.append("action", 4);
    data.append("cid", cid);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      alert(reply.trim());
    });
    set_json.open("POST", url, true);
    set_json.send(data);	  
  }
}

function uploadPreset()
{
  if(document.getElementById('uploadFile').value == "")
  {
    alert("No File To Upload");
  }
  else
  {
    var json_send;
    var set_json = new XMLHttpRequest();
    var url = "/cgi-bin/upload_preset.cgi";
    var product_name;
    fileInput  = document.getElementById('uploadFile')
    var data = fileInput.files[0];
    var file = new FormData();
    file.append('file', data);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      getPreset()
      alert(reply.trim());
    });
    set_json.open("POST", url, true);
    set_json.send(file);
  }
}

function getPreset()
{
  var get_json = new XMLHttpRequest();
  get_json.addEventListener ("load", (e) =>
  {
    var json_load = JSON.parse(e.target.responseText);
    removeOptions("PresetSelect");
    selectOptionsValue("PresetSelect",json_load,json_load);
    downloadPreset()
  }
  );
  get_json.open("GET", "/cgi-bin/fileList.cgi");
  get_json.setRequestHeader('Cache-Control', 'no-cache')
  get_json.setRequestHeader('Pragma', 'no-cache')
  get_json.setRequestHeader('Expires', '0')
  get_json.send();
}

function newPreset()
{
  if(document.getElementById('fileName').value == "")
  {
    alert("Preset Name is not Set");
  }
  else
  {
    var type = document.getElementById('filePre').value;
    var id = document.getElementById('fileName').value;
    var str_val
    var set_json = new XMLHttpRequest();
    var data = new FormData();
    var url = "/cgi-bin/add_preset.cgi";
    var fileName = type + "-" + id;
    data.append("filename", fileName);
    data.append("type", type);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      getPreset()
      alert("Preset Saved Successfully");
    });
    set_json.open("POST", url, true);
    set_json.send(data);
  }
}

function delPreset()
{
  var file = document.getElementById('PresetSelect').value;
  if (confirm("Are You Sure You Want To Delete" + "\n\n" + file))
  {
    var set_json = new XMLHttpRequest();
    var data = new FormData();
    var url = "/cgi-bin/del_preset.cgi";
    data.append("file", file);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      getPreset()
      alert(reply.trim());
    });
    set_json.open("POST", url, true);
    set_json.send(data);
  }
}

function setPreset()
{
  var file = document.getElementById('PresetSelect').value;
  if (confirm("Are You Sure You Want To Load" + "\n\n" + file))
  {
    var set_json = new XMLHttpRequest();
    var data = new FormData();
    var url = "/cgi-bin/set_preset.cgi";
    data.append("file", file);
    data.append("TxPresent", TxPresent);
    set_json.addEventListener ("load", (e) =>
    {
      var reply = e.target.responseText;
      alert(reply.trim());
    });
    set_json.open("POST", url, true);
    set_json.send(data);
  }
}

function downloadPreset()
{
  var url;
  if(document.getElementById('PresetSelect').value != "")
  {
    url = window.location.origin + "/preset/" + document.getElementById('PresetSelect').value
  }
  else
  {
    url = "JavaScript:void(0);"
  }
  document.getElementById('preDown').href = url
}
