var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var objWMIService = locator.ConnectServer(null, "root\\cimv2");
var wbemFlagReturnImmediately = 0x10;
var wbemFlagForwardOnly = 0x20;

var lngID = System.Gadget.document.parentWindow.lngID;

var Int_Nr = "";
var Int_Nr2 = "";
var Int_Nr3 = "";
var changeflag;

System.Gadget.onSettingsClosing = SettingsClosing;

function checkforupdate(){
 var vString = "";
 var url = "http://www.myfavoritegadgets.info/monitors/NetworkMonitorII/NetworkMonitorIIversioninfo.xml";

 var xmlHttp = new XMLHttpRequest();
 xmlHttp.open("GET", url, true); //true mean call is asynchronous
 xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); //for non cached
 
 xmlHttp.onreadystatechange = function(){
  // Verify that the document is ready to parse.
  if (xmlHttp.readyState == 4){
   // Makes sure it found the file.
   if (xmlHttp.status == 200){
    // Extract information
    var newVersion = xmlHttp.responseXML.getElementsByTagName("version")[0].firstChild.nodeValue;
    var updateUrl = xmlHttp.responseXML.getElementsByTagName("url")[0].firstChild.nodeValue;
    // Compare and show message if a newer version is available.
    if (parseFloat(newVersion) > parseFloat(System.Gadget.version)){
     vString = "<font color='#FF0000'>" + setlng[lngID][163] + System.Gadget.version + "<br>";
     vString += "<strong><a href='" + updateUrl + "' onclick='System.Gadget.close()' ";
	 vString += "title='" + setlng[lngID][164] + " (" + newVersion + ")'>" + setlng[lngID][164] + " (" + newVersion + ")</a></strong></font>"; 
     updatestring.innerHTML = vString;
     updatebutton.style.display = "none";
    }
    else{
     updatestring.innerHTML = "<font color='#008000'>" + setlng[lngID][161] + "</font>";
     updatebutton.style.display = "none";
    }
   }//end status
   else{
    updatestring.innerHTML = "<font color='#FF0000' size='2'>" + setlng[lngID][162] + xmlHttp.status + "</b></font>";
    updatebutton.style.display = "none";
   }
  }//end readyState
 }//end function
 xmlHttp.send(null);
}

function validateNumber(e){
 var numcheck = /\d/;
 return numcheck.test(String.fromCharCode(e.keyCode));
}

function ScileSizeCh(){
 var tmp = parseInt(ScaleSize.value);
 if (isNaN(tmp) || tmp < 0) ScaleSize.value = System.Gadget.Settings.read("scalesize");
}

function sizechange(){
 var tmp = parseInt(size.value);
 if (isNaN(tmp) || tmp < 0) size.value = System.Gadget.Settings.read("SetSize") * 100;
}

function timechange(){
 var tmp = parseInt(timetorefresh.value,10);
 if (isNaN(tmp) || tmp < 0) timetorefresh.value = Math.ceil(System.Gadget.Settings.read("refreshtime") / 1000);
}

function timetointipchange(){
 var tmp = parseInt(timetointip.value,10);
 if (isNaN(tmp) || tmp < 0) timetointip.value = Math.ceil(System.Gadget.Settings.read("intiptime") / 1000);
}

function timetoextipchange(){
 var tmp = parseInt(timetoextip.value,10);
 if (isNaN(tmp) || tmp < 0) timetoextip.value = Math.ceil(System.Gadget.Settings.read("extiptime") / 1000);
}

function timetoforciblychange(){
 var tmp = parseInt(timetoforciblycheck.value,10);
 if (isNaN(tmp) || tmp <= 0) timetoforciblycheck.value = Math.ceil(System.Gadget.Settings.read("ForciblyCheckTime") / 60000);
}

function BarUpCh(){
 var tmp = parseInt(barup.value);
 if (isNaN(tmp) || tmp < 0) barup.value = System.Gadget.Settings.read("barup");
}

function BarDownCh(){
 var tmp = parseInt(bardown.value);
 if (isNaN(tmp) || tmp < 0) bardown.value = System.Gadget.Settings.read("bardown");
}

function FixedHideUnhide(){
 if (ScaleType.value == 1){
  hidefixed.disabled = false;
  ScaleSize.disabled = false;
 }
 else{
  hidefixed.disabled = true;
  ScaleSize.disabled = true;
 }
}

function ResetHideUnhide(){
 timetoreset.disabled = true;
 dayofweek.disabled = true;
 dayofmonth.disabled = true;
 switch (parseInt(ResetCounter.value)){
  case 1:
   timetoreset.disabled = false;
   dayofweek.disabled = true;
   dayofmonth.disabled = true;
   break;
  case 2:
   timetoreset.disabled = false;
   dayofweek.disabled = false;
   dayofmonth.disabled = true;
   break;
  case 3:
   timetoreset.disabled = false;
   dayofweek.disabled = true;
   dayofmonth.disabled = false;
   break;
 }
}

function glwchange(){
 var tmp = parseInt(graphlinewidth.value,10);
 if (isNaN(tmp) || (tmp <1) || (tmp >20)) graphlinewidth.value = System.Gadget.Settings.read("GraphLineWidth");
}

function uncheckb(){
 if (hideupdownspeed.checked){
  hidebitpersec.checked = false;
  hidebytepersec.checked = false;
  hidebitbyte.disabled = true;
 }
 else hidebitbyte.disabled = false;
}

function uncheckbytes(){
 if (hidebitpersec.checked) hidebytepersec.checked = false;
}

function uncheckbites(){
 if (hidebytepersec.checked) hidebitpersec.checked = false;
}

function grhide(){
 graphhide.disabled = hideallgraphics.checked;
}

function forciblytimehide(){
 hideforcibly.disabled = !enableforcibly.checked;
}

function peakstrhide(){
 if (hidepeak.checked){
  peakstringhide.disabled = true;
  hidepeakstring.checked = false;
 } 
 else peakstringhide.disabled = false;
}

function saveIPhide(){
 if (autocheckextIP.checked) disableshowIPmenu.disabled = false;
 else{
  disableshowIPmenu.disabled = true;
  enablesaveextip.checked = false;
 }
}

function setarrows(){
 if (SkinType.value == 0){
  arrowup.src = "images/arrow_up_green.png";
  arrowdown.src = "images/arrow_down_yellow.png";
 }
 else{
  arrowup.src = "images/arrow_up_red_modern.png";
  arrowdown.src = "images/arrow_down_green_modern.png";
 } 
}

function hidesize(){
 sizehide.disabled = autoscaleDPI.checked;
}

function selectfile(){
 if (!playsound.checked) return;
 var strInitialDirectory = System.Environment.getEnvironmentVariable("USERPROFILE") + "\\Music\\"
 var strFilter = "Audio File (*.wav):*.wav::";
 var file_location = System.Shell.chooseFile(true, strFilter, strInitialDirectory, ""); //true for open
 if (file_location) document.getElementById("soundpath").value = file_location.path;
}

function selecconnectiontfile(){
 if (!playconnectionsound.checked) return;
 var strInitialDirectory = System.Environment.getEnvironmentVariable("USERPROFILE") + "\\Music\\"
 var strFilter = "Audio File (*.wav):*.wav::";
 var file_location = System.Shell.chooseFile(true, strFilter, strInitialDirectory, ""); //true for open
 if (file_location) document.getElementById("soundconnectionpath").value = file_location.path;
}

function hideselectbutton(){
 if (playsound.checked){
  document.getElementById("soundhide").disabled = false;
  document.getElementById("soundpath").disabled = false;
 }
 else{
  document.getElementById("soundhide").disabled = true;
  document.getElementById("soundpath").disabled = true;
 }
}

function hideconnectselectbutton(){
 if (playconnectionsound.checked){
  document.getElementById("soundconnectionhide").disabled = false;
  document.getElementById("soundconnectionpath").disabled = false;
  document.getElementById("hidefirstrunstring").disabled = false;
 }
 else{
  document.getElementById("soundconnectionhide").disabled = true;
  document.getElementById("soundconnectionpath").disabled = true;
  document.getElementById("hidefirstrunstring").disabled = true;
 }
}

function setcolor(){
 if (SkinType.value == 0){
  System.Gadget.Settings.write("sUpColorSelect",0);
  document.getElementById("UpColorSelect").selectedIndex = 0;
  document.getElementById("simplecolor11").style.background = document.getElementById("UpColorSelect").value;
  document.getElementById("multiColorPicker11").value = document.getElementById("UpColorSelect").value.substr(1);
  System.Gadget.Settings.write("sDownColorSelect",0);
  document.getElementById("DownColorSelect").selectedIndex = 0;
  document.getElementById("simplecolor12").style.background = document.getElementById("DownColorSelect").value;
  document.getElementById("multiColorPicker12").value = document.getElementById("DownColorSelect").value.substr(1);
 }
 else{
  System.Gadget.Settings.write("sUpColorSelect",4);
  document.getElementById("UpColorSelect").selectedIndex = 4;
  document.getElementById("simplecolor11").style.background = document.getElementById("UpColorSelect").value;
  document.getElementById("multiColorPicker11").value = document.getElementById("UpColorSelect").value.substr(1);
  System.Gadget.Settings.write("sDownColorSelect",1);
  document.getElementById("DownColorSelect").selectedIndex = 1;
  document.getElementById("simplecolor12").style.background = document.getElementById("DownColorSelect").value;
  document.getElementById("multiColorPicker12").value = document.getElementById("DownColorSelect").value.substr(1);
 }
}

function wirelesssettings(){
 if (ConnectType.value != "1"){
  wir1.disabled = false;
  wir2.disabled = false;
  wir3.disabled = false;
  SignalIDArea.disabled = false;
  ConnectArea.disabled = false;
  SignalArea.disabled = false;
  NoSignalArea.disabled = false;
 }
 else{
  wir1.disabled = true;
  wir2.disabled = true;
  wir3.disabled = true;
  SignalIDArea.disabled = true;
  ConnectArea.disabled = true;
  SignalArea.disabled = true;
  NoSignalArea.disabled = true;
 } 
}

function saveconnections(){
 if (ConnectType.value == "1"){
  System.Gadget.Settings.write("WiredNet", interface1.value);
  System.Gadget.Settings.write("WiredNet2", interface2.value);
 }
 else{
  System.Gadget.Settings.write("WirelessNet", interface1.value);
  System.Gadget.Settings.write("WirelessNet2", interface2.value);
 }
}

function buildlngselect(){
 for (var i=0; i<lnglist.length; i++){
  newOption = new Option(lnglist[i], i);
  languagelist.options[languagelist.length] = newOption;
 }
 languagelist.value = System.Gadget.Settings.read("LanguageId");
}

function onLoad(){
 var tmp = System.Gadget.Settings.read("UseNetLib");
 if (tmp == "false") netlibbox.checked = false;
 else{
  netlibbox.checked = true;
  try{
   for (var i=0; i<System.Gadget.document.parentWindow.Netcount; i++){
    interface2.options[interface2.length] = new Option(i + ": " + System.Gadget.document.parentWindow.NetLib.Description(i), i);
   }
  }
  catch(e){}
 }

 buildlngselect();

 changeflag = netlibbox.checked;

 Int_Nr2 = System.Gadget.Settings.read("Int_Nr2");
 Int_Nr3 = System.Gadget.Settings.read("Int_Nr3");

 size.value = Math.ceil(System.Gadget.Settings.read("SetSize") * 100);
 timetorefresh.value = Math.ceil(System.Gadget.Settings.read("refreshtime") / 1000);
 tmp = System.Gadget.Settings.read("DPIautoscale");
 if (tmp == "false") autoscaleDPI.checked = false;
 else autoscaleDPI.checked = true;
 hidesize();

 tmp = System.Gadget.Settings.read("PlaySound");
 if (tmp == "false") playsound.checked = false;
 else playsound.checked = true;
 hideselectbutton();
 soundpath.value = System.Gadget.Settings.read("AlarmSound");

 tmp = System.Gadget.Settings.read("PlayConnectionSound");
 if (tmp == "false") playconnectionsound.checked = false;
 else playconnectionsound.checked = true;
 hideconnectselectbutton();
 soundconnectionpath.value = System.Gadget.Settings.read("ConnectionSound");
 tmp = System.Gadget.Settings.read("PlayWhenStart");
 if (tmp == "false") firstrunplay.checked = false;
 else firstrunplay.checked = true;

 SkinType.value = System.Gadget.Settings.read("gadgetskin");
 setarrows();
 ConnectType.value = System.Gadget.Settings.read("NetworkType");
 wirelesssettings();
 ScaleType.value = System.Gadget.Settings.read("scaletype");
 FixedHideUnhide();
 ScaleSize.value = System.Gadget.Settings.read("scalesize");
 tmp = System.Gadget.Settings.read("HistorySwitch");
 if (tmp == "false") TrafficHistoryOn.checked = false;
 else TrafficHistoryOn.checked = true;
 ResetTraffic.checked = false;
 barup.value = System.Gadget.Settings.read("barup"); 
 bardown.value = System.Gadget.Settings.read("bardown");

 displayunits.value = System.Gadget.Settings.read("UnitFormat");
 peakunits.value = System.Gadget.Settings.read("PeakUnitFormat");

 var colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPenabled=True", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
 var enumItems = new Enumerator(colItems);
 if(enumItems.atEnd()){
  newOption = new Option("No connected device", "");
  interface1.options[interface1.length] = newOption;
 }
 for (var i=0; !enumItems.atEnd(); enumItems.moveNext()){
  var objItem = enumItems.item();
  var name = objItem.Description;
  if(name.length > 42) name = name.substr(0,42);
  newOption = new Option(name, i);
  interface1.options[interface1.length] = newOption;
  i++;
 }
 interface1.disabled = (interface1.length < 2);

 Int_Nr = System.Gadget.Settings.read("Int_Nr");
 if (Int_Nr == "") interface1.selectedIndex = 0;
 else interface1.selectedIndex = Int_Nr;

 if (!netlibbox.checked){
  colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
  enumItems = new Enumerator(colItems);
  if(enumItems.atEnd()){
   newOption = new Option("No connected device", "");
   interface2.options[interface2.length] = newOption;
  }

  for (var i=0; !enumItems.atEnd(); enumItems.moveNext()){
   var objItem = enumItems.item();
   var name = objItem.Name;
   if(name.length > 40) name = name.substr(0,40);
   newOption = new Option((i+1) + ": " + name, i);
   interface2.options[interface2.length] = newOption;
   i++;
  }
  if (Int_Nr2 == "") interface2.selectedIndex = 0;
  else interface2.selectedIndex = Int_Nr2;
 }//end if
 else{
  if (Int_Nr3 == "") interface2.selectedIndex = 0;
  else interface2.selectedIndex = Int_Nr3;
 }
 interface2.disabled = (interface2.length < 2);

 tmp = System.Gadget.Settings.read("GridlinesHide");
 if (tmp == "false") hidegridlines.checked = false;
 else hidegridlines.checked = true;

 graphlinewidth.value = System.Gadget.Settings.read("GraphLineWidth");

 tmp = System.Gadget.Settings.read("TitleHide");
 if (tmp == "false") hidetitle.checked = false;
 else hidetitle.checked = true;
 tmp = System.Gadget.Settings.read("HideID");
 if (tmp == "false") hideid.checked = false;
 else hideid.checked = true;
 tmp = System.Gadget.Settings.read("HideIntIP");
 if (tmp == "false") hideintip.checked = false;
 else hideintip.checked = true;
 tmp = System.Gadget.Settings.read("HideExtIP");
 if (tmp == "false") hideextip.checked = false;
 else hideextip.checked = true;
 tmp = System.Gadget.Settings.read("HideServ");
 if (tmp == "false") hideserv.checked = false;
 else hideserv.checked = true;
 tmp = System.Gadget.Settings.read("HideConnect");
 if (tmp == "false") hideconnect.checked = false;
 else hideconnect.checked = true;
 tmp = System.Gadget.Settings.read("HideSignal");
 if (tmp == "false") hidesignal.checked = false;
 else hidesignal.checked = true;
 tmp = System.Gadget.Settings.read("HideNetUsage");
 if (tmp == "false") hidenetusage.checked = false;
 else hidenetusage.checked = true;
 tmp = System.Gadget.Settings.read("HideUpDownSpeed");
 if (tmp == "false") hideupdownspeed.checked = false;
 else hideupdownspeed.checked = true;
 uncheckb();
 tmp = System.Gadget.Settings.read("HideBitPerSec");
 if (tmp == "false") hidebitpersec.checked = false;
 else hidebitpersec.checked = true;
 tmp = System.Gadget.Settings.read("HideBytePerSec");
 if (tmp == "false") hidebytepersec.checked = false;
 else hidebytepersec.checked = true;
 tmp = System.Gadget.Settings.read("HideBars");
 if (tmp == "false") hidebars.checked = false;
 else hidebars.checked = true;
 tmp = System.Gadget.Settings.read("HideAllGraphics");
 if (tmp == "false") hideallgraphics.checked = false;
 else hideallgraphics.checked = true;
 grhide();
 tmp = System.Gadget.Settings.read("HidePeakNum");
 if (tmp == "false") hidegraphscale.checked = false;
 else hidegraphscale.checked = true;
 tmp = System.Gadget.Settings.read("HideUploadGraph");
 if (tmp == "false") hideuploadgraph.checked = false;
 else hideuploadgraph.checked = true;
 tmp = System.Gadget.Settings.read("HideDownloadGraph");
 if (tmp == "false") hidedownloadgraph.checked = false;
 else hidedownloadgraph.checked = true;
 tmp = System.Gadget.Settings.read("HidePeak");
 if (tmp == "false") hidepeak.checked = false;
 else hidepeak.checked = true;
 tmp = System.Gadget.Settings.read("HidePeakStr");
 if (tmp == "false") hidepeakstring.checked = false;
 else hidepeakstring.checked = true;
 peakstrhide(); //after hidepeakstring check
 tmp = System.Gadget.Settings.read("HideCurrent");
 if (tmp == "false") hidecurrent.checked = false;
 else hidecurrent.checked = true;
 tmp = System.Gadget.Settings.read("HideTotal");
 if (tmp == "false") hidetotal.checked = false;
 else hidetotal.checked = true;
 tmp = System.Gadget.Settings.read("HideProfileFirewall");
 if (tmp == "false") hideprofilefirewall.checked = false;
 else hideprofilefirewall.checked = true;
 tmp = System.Gadget.Settings.read("CopyrightHide");
 if (tmp == "false") hidecopyright.checked = false;
 else hidecopyright.checked = true;

 tmp = System.Gadget.Settings.read("intIPautocheck");
 if (tmp == "false") autocheckintIP.checked = false;
 else autocheckintIP.checked = true;
 tmp = System.Gadget.Settings.read("extIPautocheck");
 if (tmp == "false") autocheckextIP.checked = false;
 else autocheckextIP.checked = true;
 saveIPhide();

 tmp = System.Gadget.Settings.read("EnableForciblyCheck");
 if (tmp == "false") enableforcibly.checked = false;
 else enableforcibly.checked = true;
 forciblytimehide();

 ResetCounterType.value = System.Gadget.Settings.read("WhichCounterReset");
 ResetCounter.value = System.Gadget.Settings.read("WhenResetCounter");
 dayselect.value = System.Gadget.Settings.read("DayReset");
 ipserver.value = System.Gadget.Settings.read("ServerIP");
 cfserver.value = System.Gadget.Settings.read("ServerCF");
 dayofmonthselect.value = System.Gadget.Settings.read("DayOfMonthReset");
 
 tmp = System.Gadget.Settings.read("HourReset");
 for (var i=0; i<24; i++){
  newOption = new Option(i<10 ? "0" + i : i, i);
  hoursselect.options[hoursselect.length] = newOption;
 }
 hoursselect.options[parseInt(tmp)].selected = true;
 
 tmp = System.Gadget.Settings.read("MinutesReset");
 for (var i=0; i<60; i++){
  newOption = new Option(i<10 ? "0" + i : i, i);
  minutesselect.options[minutesselect.length] = newOption;
 }
 minutesselect.options[parseInt(tmp)].selected = true;

 ResetHideUnhide();

 tmp = System.Gadget.Settings.read("IPsRename");
 if (tmp == "false") renameips.checked = false;
 else renameips.checked = true;

 tmp = System.Gadget.Settings.read("AutoUpdate");
 if (tmp == "false") enablecheckforupdate.checked = false;
 else enablecheckforupdate.checked = true;

 tmp = System.Gadget.Settings.read("fillUpload");
 if (tmp == "false") uploadfill.checked = false;
 else uploadfill.checked = true;
 tmp = System.Gadget.Settings.read("fillDownload");
 if (tmp == "false") downloadfill.checked = false;
 else downloadfill.checked = true;

 tmp = System.Gadget.Settings.read("PutDownloadFirst");
 if (tmp == "false") downloadfirst.checked = false;
 else downloadfirst.checked = true;

 tmp = System.Gadget.Settings.read("DisableFlyoutSounds");
 if (tmp == "false") disableflyoutsounds.checked = false;
 else disableflyoutsounds.checked = true;

 tmp = System.Gadget.Settings.read("SaveCountersWhenExit");
 if (tmp == "false") savecounterswhenexit.checked = false;
 else savecounterswhenexit.checked = true;

 tmp = System.Gadget.Settings.read("UseforSelectedCard");
 if (tmp == "false") useforselectedcard.checked = false;
 else useforselectedcard.checked = true;
 timetointip.value = Math.ceil(System.Gadget.Settings.read("intiptime") / 1000);
 timetoextip.value = Math.ceil(System.Gadget.Settings.read("extiptime") / 1000);

 timetoforciblycheck.value = Math.ceil(System.Gadget.Settings.read("ForciblyCheckTime") / 60000);

 tmp = System.Gadget.Settings.read("EnableSaveExtIP");
 if (tmp == "false") enablesaveextip.checked = false;
 else enablesaveextip.checked = true;

 tmp = System.Gadget.Settings.read("AutoPickNet");
 if (tmp == "false") autopicknet.checked = false;
 else autopicknet.checked = true;

 //color settings 
 tmp = System.Gadget.Settings.read("sBgColorSelect");
 if (tmp !=""){
  document.getElementById("BgColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("BgColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor1").style.background = System.Gadget.Settings.read("colBgColor");
 document.getElementById("multiColorPicker1").value = System.Gadget.Settings.read("colBgColor").substr(1);
 needenable("BgColorSelect","multiColorPicker1");

 tmp = System.Gadget.Settings.read("sTitColorSelect");
 if (tmp !=""){
  document.getElementById("TitColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("TitColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor2").style.background = System.Gadget.Settings.read("colTitle");
 document.getElementById("multiColorPicker2").value = System.Gadget.Settings.read("colTitle").substr(1);
 needenable("TitColorSelect","multiColorPicker2");

 tmp = System.Gadget.Settings.read("sSignalIDSelect");
 if (tmp !=""){
  document.getElementById("SignalIDSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("SignalIDSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor3").style.background = System.Gadget.Settings.read("colSignalID");
 document.getElementById("multiColorPicker3").value = System.Gadget.Settings.read("colSignalID").substr(1);
 needenable("SignalIDSelect","multiColorPicker3");

 tmp = System.Gadget.Settings.read("sIntIPSelect");
 if (tmp !=""){
  document.getElementById("IntIPSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("IntIPSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor4").style.background = System.Gadget.Settings.read("colintIP");
 document.getElementById("multiColorPicker4").value = System.Gadget.Settings.read("colintIP").substr(1);
 needenable("IntIPSelect","multiColorPicker4");

 tmp = System.Gadget.Settings.read("sExtIPSelect");
 if (tmp !=""){
  document.getElementById("ExtIPSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("ExtIPSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor5").style.background = System.Gadget.Settings.read("colextIP");
 document.getElementById("multiColorPicker5").value = System.Gadget.Settings.read("colextIP").substr(1);
 needenable("ExtIPSelect","multiColorPicker5");

 tmp = System.Gadget.Settings.read("sMenuColorSelect");
 if (tmp !=""){
  document.getElementById("MenuColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("MenuColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor6").style.background = System.Gadget.Settings.read("colMenu");
 document.getElementById("multiColorPicker6").value = System.Gadget.Settings.read("colMenu").substr(1);
 needenable("MenuColorSelect","multiColorPicker6");

 tmp = System.Gadget.Settings.read("sConnectColorSelect");
 if (tmp !=""){
  document.getElementById("ConnectColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("ConnectColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor7").style.background = System.Gadget.Settings.read("colConnect");
 document.getElementById("multiColorPicker7").value = System.Gadget.Settings.read("colConnect").substr(1);
 needenable("ConnectColorSelect","multiColorPicker7");

 tmp = System.Gadget.Settings.read("sSignalColorSelect");
 if (tmp !=""){
  document.getElementById("SignalColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("SignalColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor8").style.background = System.Gadget.Settings.read("colSignal");
 document.getElementById("multiColorPicker8").value = System.Gadget.Settings.read("colSignal").substr(1);
 needenable("SignalColorSelect","multiColorPicker8");

 tmp = System.Gadget.Settings.read("sNoSignalColorSelect");
 if (tmp !=""){
  document.getElementById("NoSignalColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("NoSignalColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor9").style.background = System.Gadget.Settings.read("colNoSignal");
 document.getElementById("multiColorPicker9").value = System.Gadget.Settings.read("colNoSignal").substr(1);
 needenable("NoSignalColorSelect","multiColorPicker9");

 tmp = System.Gadget.Settings.read("sNetColorSelect");
 if (tmp !=""){
  document.getElementById("NetColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("NetColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor10").style.background = System.Gadget.Settings.read("colNetUsage");
 document.getElementById("multiColorPicker10").value = System.Gadget.Settings.read("colNetUsage").substr(1);
 needenable("NetColorSelect","multiColorPicker10");

 tmp = System.Gadget.Settings.read("sUpColorSelect");
 if (tmp !=""){
  document.getElementById("UpColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("UpColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor11").style.background = System.Gadget.Settings.read("colUpload");
 document.getElementById("multiColorPicker11").value = System.Gadget.Settings.read("colUpload").substr(1);
 needenable("UpColorSelect","multiColorPicker11");

 tmp = System.Gadget.Settings.read("sDownColorSelect");
 if (tmp !=""){
  document.getElementById("DownColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("DownColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor12").style.background = System.Gadget.Settings.read("colDownload");
 document.getElementById("multiColorPicker12").value = System.Gadget.Settings.read("colDownload").substr(1);
 needenable("DownColorSelect","multiColorPicker12");

 tmp = System.Gadget.Settings.read("sScaleColorSelect");
 if (tmp !=""){
  document.getElementById("ScaleColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("ScaleColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor13").style.background = System.Gadget.Settings.read("colFixedScale");
 document.getElementById("multiColorPicker13").value = System.Gadget.Settings.read("colFixedScale").substr(1);
 needenable("ScaleColorSelect","multiColorPicker13");

 tmp = System.Gadget.Settings.read("sServStrColorSelect");
 if (tmp !=""){
  document.getElementById("ServStrColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("ServStrColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor14").style.background = System.Gadget.Settings.read("colServStr");
 document.getElementById("multiColorPicker14").value = System.Gadget.Settings.read("colServStr").substr(1);
 needenable("ServStrColorSelect","multiColorPicker14");

 tmp = System.Gadget.Settings.read("sGraphScaleColorSelect");
 if (tmp !=""){
  document.getElementById("GraphScaleColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("GraphScaleColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor18").style.background = System.Gadget.Settings.read("colNetPeak");
 document.getElementById("multiColorPicker18").value = System.Gadget.Settings.read("colNetPeak").substr(1);
 needenable("GraphScaleColorSelect","multiColorPicker18");

 tmp = System.Gadget.Settings.read("sGrBgColorSelect");
 if (tmp !=""){
  document.getElementById("GrBgColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("GrBgColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor15").style.background = System.Gadget.Settings.read("colFillGraph");
 document.getElementById("multiColorPicker15").value = System.Gadget.Settings.read("colFillGraph").substr(1);
 needenable("GrBgColorSelect","multiColorPicker15");

 tmp = System.Gadget.Settings.read("sGrScaleBgColorSelect");
 if (tmp !=""){
  document.getElementById("GrScaleBgColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("GrScaleBgColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor19").style.background = System.Gadget.Settings.read("colFillPeak");
 document.getElementById("multiColorPicker19").value = System.Gadget.Settings.read("colFillPeak").substr(1);
 needenable("GrScaleBgColorSelect","multiColorPicker19");

 tmp = System.Gadget.Settings.read("sPFColorSelect");
 if (tmp !=""){
  document.getElementById("PFColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("PFColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor20").style.background = System.Gadget.Settings.read("colProfile");
 document.getElementById("multiColorPicker20").value = System.Gadget.Settings.read("colProfile").substr(1);
 needenable("PFColorSelect","multiColorPicker20");

 tmp = System.Gadget.Settings.read("sVersnColorSelect");
 if (tmp !=""){
  document.getElementById("VersnColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("VersnColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor16").style.background = System.Gadget.Settings.read("colVersion");
 document.getElementById("multiColorPicker16").value = System.Gadget.Settings.read("colVersion").substr(1);
 needenable("VersnColorSelect","multiColorPicker16");

 tmp = System.Gadget.Settings.read("sCopyColorSelect");
 if (tmp !=""){
  document.getElementById("CopyColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("CopyColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor17").style.background = System.Gadget.Settings.read("colCopyright");
 document.getElementById("multiColorPicker17").value = System.Gadget.Settings.read("colCopyright").substr(1);
 needenable("CopyColorSelect","multiColorPicker17");
}

function SettingsClosing(event){
 if (event.closeAction == event.Action.commit){
  System.Gadget.Settings.write("LanguageId", languagelist.value);

  var tmp;
  System.Gadget.Settings.write("NetworkType", ConnectType.value);
  timechange();
  System.Gadget.Settings.write("refreshtime", timetorefresh.value * 1000);
  System.Gadget.Settings.write("scaletype", ScaleType.value);
  ScileSizeCh();
  System.Gadget.Settings.write("scalesize", ScaleSize.value);
   
  sizechange();
  System.Gadget.Settings.write("SetSize", size.value / 100);   

  if (autoscaleDPI.checked) System.Gadget.Settings.write ("DPIautoscale", "true");
  else System.Gadget.Settings.write ("DPIautoscale", "false");

  if (TrafficHistoryOn.checked){
   System.Gadget.Settings.write ("HistorySwitch", "true");
   System.Gadget.document.parentWindow.historyenable = true;
  }
  else{
   System.Gadget.Settings.write ("HistorySwitch", "false");
   System.Gadget.document.parentWindow.historyenable = false;
  }

  if (ResetTraffic.checked){
   System.Gadget.document.parentWindow.bytesSentCurrent = 0;
   System.Gadget.document.parentWindow.bytesReceivedCurrent = 0;

   System.Gadget.document.parentWindow.bytesSentTotal = 0;
   System.Gadget.document.parentWindow.bytesReceivedTotal = 0;

   System.Gadget.Settings.write ("HistorySend", "");
   System.Gadget.Settings.write ("HistoryRece", "");
  }

  BarUpCh();
  System.Gadget.Settings.write("barup", barup.value);
  BarDownCh();
  System.Gadget.Settings.write("bardown", bardown.value);

  System.Gadget.Settings.write("UnitFormat", displayunits.value);
  System.Gadget.Settings.write("PeakUnitFormat", peakunits.value);

  System.Gadget.Settings.write("gadgetskin",SkinType.value);
   
  Int_Nr = interface1.value;
  System.Gadget.Settings.write ("Int_Nr", Int_Nr);
   
  if (changeflag == netlibbox.checked){
   if (!netlibbox.checked){
    Int_Nr2 = interface2.value;
    System.Gadget.Settings.write ("Int_Nr2", Int_Nr2);
   }
   else{
    Int_Nr3 = interface2.value;
    System.Gadget.Settings.write ("Int_Nr3", Int_Nr3);
   }
  }// end if (changeflag
  if (netlibbox.checked) System.Gadget.Settings.write ("UseNetLib", "true");
  else System.Gadget.Settings.write ("UseNetLib", "false");

  if (autocheckintIP.checked) System.Gadget.Settings.write ("intIPautocheck", "true");
  else System.Gadget.Settings.write ("intIPautocheck", "false");
  if (autocheckextIP.checked) System.Gadget.Settings.write ("extIPautocheck", "true");
  else System.Gadget.Settings.write ("extIPautocheck", "false");

  if (enableforcibly.checked) System.Gadget.Settings.write ("EnableForciblyCheck", "true");
  else System.Gadget.Settings.write ("EnableForciblyCheck", "false");

  if (hidetitle.checked) System.Gadget.Settings.write ("TitleHide", "true");
  else System.Gadget.Settings.write ("TitleHide", "false");
  if (hideid.checked) System.Gadget.Settings.write ("HideID", "true");
  else System.Gadget.Settings.write ("HideID", "false");
  if (hideintip.checked) System.Gadget.Settings.write ("HideIntIP", "true");
  else System.Gadget.Settings.write ("HideIntIP", "false");
  if (hideextip.checked) System.Gadget.Settings.write ("HideExtIP", "true");
  else System.Gadget.Settings.write ("HideExtIP", "false");
  if (hideserv.checked) System.Gadget.Settings.write ("HideServ", "true");
  else System.Gadget.Settings.write ("HideServ", "false");
  if (hideconnect.checked) System.Gadget.Settings.write ("HideConnect", "true");
  else System.Gadget.Settings.write ("HideConnect", "false");
  if (hidesignal.checked) System.Gadget.Settings.write ("HideSignal", "true");
  else System.Gadget.Settings.write ("HideSignal", "false");
  if (hidenetusage.checked) System.Gadget.Settings.write ("HideNetUsage", "true");
  else System.Gadget.Settings.write ("HideNetUsage", "false");
  if (hideupdownspeed.checked) System.Gadget.Settings.write ("HideUpDownSpeed", "true");
  else System.Gadget.Settings.write ("HideUpDownSpeed", "false");
  if (hidebitpersec.checked) System.Gadget.Settings.write ("HideBitPerSec", "true");
  else System.Gadget.Settings.write ("HideBitPerSec", "false");
  if (hidebytepersec.checked) System.Gadget.Settings.write ("HideBytePerSec", "true");
  else System.Gadget.Settings.write ("HideBytePerSec", "false");
  if (hidebars.checked) System.Gadget.Settings.write ("HideBars", "true");
  else System.Gadget.Settings.write ("HideBars", "false");
  if (hideallgraphics.checked) System.Gadget.Settings.write ("HideAllGraphics", "true");
  else System.Gadget.Settings.write ("HideAllGraphics", "false");
  if (hidegraphscale.checked) System.Gadget.Settings.write ("HidePeakNum", "true");
  else System.Gadget.Settings.write ("HidePeakNum", "false");
  if (hideuploadgraph.checked) System.Gadget.Settings.write ("HideUploadGraph", "true");
  else System.Gadget.Settings.write ("HideUploadGraph", "false");
  if (hidedownloadgraph.checked) System.Gadget.Settings.write ("HideDownloadGraph", "true");
  else System.Gadget.Settings.write ("HideDownloadGraph", "false");
  if (hidepeak.checked) System.Gadget.Settings.write ("HidePeak", "true");
  else System.Gadget.Settings.write ("HidePeak", "false");
  if (hidepeakstring.checked) System.Gadget.Settings.write ("HidePeakStr", "true");
  else System.Gadget.Settings.write ("HidePeakStr", "false");
  if (hidecurrent.checked) System.Gadget.Settings.write ("HideCurrent", "true");
  else System.Gadget.Settings.write ("HideCurrent", "false");
  if (hidetotal.checked) System.Gadget.Settings.write ("HideTotal", "true");
  else System.Gadget.Settings.write ("HideTotal", "false");
  if (hideprofilefirewall.checked) System.Gadget.Settings.write ("HideProfileFirewall", "true");
  else System.Gadget.Settings.write ("HideProfileFirewall", "false");
  if (hidecopyright.checked) System.Gadget.Settings.write ("CopyrightHide", "true");
  else System.Gadget.Settings.write ("CopyrightHide", "false");

  System.Gadget.Settings.write("WhichCounterReset", ResetCounterType.value);
  System.Gadget.Settings.write("WhenResetCounter", ResetCounter.value);
  System.Gadget.Settings.write("DayReset", dayselect.value);
  System.Gadget.Settings.write("HourReset", hoursselect.value);
  System.Gadget.Settings.write("MinutesReset", minutesselect.value);
  System.Gadget.Settings.write("DayOfMonthReset", dayofmonthselect.value);

  System.Gadget.Settings.write("ServerIP", ipserver.value);
  System.Gadget.Settings.write("ServerCF", cfserver.value);

  glwchange();
  System.Gadget.Settings.write("GraphLineWidth", graphlinewidth.value);

  if (hidegridlines.checked) System.Gadget.Settings.write ("GridlinesHide", "true");
  else System.Gadget.Settings.write ("GridlinesHide", "false");

  if (renameips.checked) System.Gadget.Settings.write ("IPsRename", "true");
  else System.Gadget.Settings.write ("IPsRename", "false");

  if (enablecheckforupdate.checked) System.Gadget.Settings.write ("AutoUpdate", "true");
  else System.Gadget.Settings.write ("AutoUpdate", "false");

  if (uploadfill.checked) System.Gadget.Settings.write ("fillUpload", "true");
  else System.Gadget.Settings.write ("fillUpload", "false");
  if (downloadfill.checked) System.Gadget.Settings.write ("fillDownload", "true");
  else System.Gadget.Settings.write ("fillDownload", "false");

  if (downloadfirst.checked) System.Gadget.Settings.write ("PutDownloadFirst", "true");
  else System.Gadget.Settings.write ("PutDownloadFirst", "false");

  if (disableflyoutsounds.checked) System.Gadget.Settings.write ("DisableFlyoutSounds", "true");
  else System.Gadget.Settings.write ("DisableFlyoutSounds", "false");

  if (savecounterswhenexit.checked) System.Gadget.Settings.write ("SaveCountersWhenExit", "true");
  else System.Gadget.Settings.write ("SaveCountersWhenExit", "false");

  if (useforselectedcard.checked) System.Gadget.Settings.write ("UseforSelectedCard", "true");
  else System.Gadget.Settings.write ("UseforSelectedCard", "false");
  timetointipchange();
  System.Gadget.Settings.write("intiptime", timetointip.value * 1000);

  timetoextipchange();
  System.Gadget.Settings.write("extiptime", timetoextip.value * 1000);

  timetoforciblychange();
  System.Gadget.Settings.write("ForciblyCheckTime", timetoforciblycheck.value * 60000);

  if (enablesaveextip.checked) System.Gadget.Settings.write ("EnableSaveExtIP", "true");
  else System.Gadget.Settings.write ("EnableSaveExtIP", "false");

  if (autopicknet.checked) System.Gadget.Settings.write ("AutoPickNet", "true");
  else System.Gadget.Settings.write ("AutoPickNet", "false");

  if (playsound.checked) System.Gadget.Settings.write ("PlaySound", "true");
  else System.Gadget.Settings.write ("PlaySound", "false");
  System.Gadget.Settings.write("AlarmSound", soundpath.value);

  if (playconnectionsound.checked) System.Gadget.Settings.write ("PlayConnectionSound", "true");
  else System.Gadget.Settings.write ("PlayConnectionSound", "false");
  System.Gadget.Settings.write("ConnectionSound", soundconnectionpath.value);
  if (firstrunplay.checked) System.Gadget.Settings.write ("PlayWhenStart", "true");
  else System.Gadget.Settings.write ("PlayWhenStart", "false");

  //color settings
  System.Gadget.Settings.write("colBgColor", "#"+document.getElementById('multiColorPicker1').value);
  System.Gadget.Settings.write("colTitle", "#"+document.getElementById('multiColorPicker2').value);
  System.Gadget.Settings.write("colSignalID", "#"+document.getElementById('multiColorPicker3').value);
  System.Gadget.Settings.write("colintIP", "#"+document.getElementById('multiColorPicker4').value);
  System.Gadget.Settings.write("colextIP", "#"+document.getElementById('multiColorPicker5').value);
  System.Gadget.Settings.write("colMenu", "#"+document.getElementById('multiColorPicker6').value);
  System.Gadget.Settings.write("colConnect", "#"+document.getElementById('multiColorPicker7').value);
  System.Gadget.Settings.write("colSignal", "#"+document.getElementById('multiColorPicker8').value);
  System.Gadget.Settings.write("colNoSignal", "#"+document.getElementById('multiColorPicker9').value);
  System.Gadget.Settings.write("colNetUsage", "#"+document.getElementById('multiColorPicker10').value);
  System.Gadget.Settings.write("colUpload", "#"+document.getElementById('multiColorPicker11').value);
  System.Gadget.Settings.write("colDownload", "#"+document.getElementById('multiColorPicker12').value);
  System.Gadget.Settings.write("colFixedScale", "#"+document.getElementById('multiColorPicker13').value);
  System.Gadget.Settings.write("colServStr", "#"+document.getElementById('multiColorPicker14').value);
  System.Gadget.Settings.write("colFillGraph", "#"+document.getElementById('multiColorPicker15').value);
  System.Gadget.Settings.write("colVersion", "#"+document.getElementById('multiColorPicker16').value);
  System.Gadget.Settings.write("colCopyright", "#"+document.getElementById('multiColorPicker17').value);
  System.Gadget.Settings.write("colNetPeak", "#"+document.getElementById('multiColorPicker18').value);
  System.Gadget.Settings.write("colFillPeak", "#"+document.getElementById('multiColorPicker19').value);
  System.Gadget.Settings.write("colProfile", "#"+document.getElementById('multiColorPicker20').value);

  savesettingstofile();
 }
 event.cancel = false;
}

function savesettingstofile(){
 var fs = new ActiveXObject("Scripting.FileSystemObject");
 var inifilename = System.Environment.getEnvironmentVariable("APPDATA") + "\\" + System.Gadget.name + "_Settings.ini";
 try {
  var inifile = fs.OpenTextFile(inifilename, 2, true); //2 write file true - create file

  try {
   inifile.WriteLine(";Network Monitor II (c) 2010-2013 by Igor 'Igogo' Bushin");
   inifile.WriteLine(";purr purr meow meow");

   inifile.WriteLine(languagelist.value);
   //color settings
   inifile.WriteLine(document.getElementById("BgColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker1').value);
   inifile.WriteLine(document.getElementById("TitColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker2').value);
   inifile.WriteLine(document.getElementById("SignalIDSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker3').value);
   inifile.WriteLine(document.getElementById("IntIPSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker4').value);
   inifile.WriteLine(document.getElementById("ExtIPSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker5').value);
   inifile.WriteLine(document.getElementById("MenuColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker6').value);
   inifile.WriteLine(document.getElementById("ConnectColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker7').value);
   inifile.WriteLine(document.getElementById("SignalColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker8').value);
   inifile.WriteLine(document.getElementById("NoSignalColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker9').value);
   inifile.WriteLine(document.getElementById("NetColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker10').value);
   inifile.WriteLine(document.getElementById("UpColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker11').value);
   inifile.WriteLine(document.getElementById("DownColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker12').value);
   inifile.WriteLine(document.getElementById("ScaleColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker13').value);
   inifile.WriteLine(document.getElementById("ServStrColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker14').value);
   inifile.WriteLine(document.getElementById("GraphScaleColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker18').value);
   inifile.WriteLine(document.getElementById("GrBgColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker15').value);
   inifile.WriteLine(document.getElementById("GrScaleBgColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker19').value);
   inifile.WriteLine(document.getElementById("PFColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker20').value);
   inifile.WriteLine(document.getElementById("VersnColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker16').value);
   inifile.WriteLine(document.getElementById("CopyColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker17').value);
   //color settings end

   inifile.WriteLine(System.Gadget.Settings.read("WiredNet")=="" ? "0" : System.Gadget.Settings.read("WiredNet"));
   inifile.WriteLine(System.Gadget.Settings.read("WiredNet2")=="" ? "0" : System.Gadget.Settings.read("WiredNet2"));
   inifile.WriteLine(System.Gadget.Settings.read("WirelessNet")=="" ? "0" : System.Gadget.Settings.read("WirelessNet"));
   inifile.WriteLine(System.Gadget.Settings.read("WirelessNet2")=="" ? "0" : System.Gadget.Settings.read("WirelessNet2"));

   inifile.WriteLine(ConnectType.value);
   inifile.WriteLine(ScaleType.value);
   inifile.WriteLine(ScaleSize.value);
   inifile.WriteLine(size.value / 100);
   inifile.WriteLine(barup.value);
   inifile.WriteLine(bardown.value);
   inifile.WriteLine(SkinType.value);
   if (TrafficHistoryOn.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (netlibbox.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (hidetitle.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideid.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideintip.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideextip.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideserv.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideconnect.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidesignal.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidenetusage.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideupdownspeed.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidebars.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (hideallgraphics.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidegraphscale.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideuploadgraph.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidedownloadgraph.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (hidepeak.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidecurrent.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidetotal.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hideprofilefirewall.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (hidecopyright.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   inifile.WriteLine(ResetCounter.value);
   inifile.WriteLine(dayselect.value);
   inifile.WriteLine(hoursselect.value);
   inifile.WriteLine(minutesselect.value);

   inifile.WriteLine(displayunits.value);

   inifile.WriteLine(timetorefresh.value * 1000);

   if (autocheckintIP.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (autocheckextIP.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   inifile.WriteLine(cfserver.value);

   inifile.WriteLine(graphlinewidth.value);

   if (hidegridlines.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (hidebitpersec.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (hidebytepersec.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (hidepeakstring.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   inifile.WriteLine(dayofmonthselect.value); //for reset counter

   if (renameips.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (enablecheckforupdate.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (uploadfill.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (downloadfill.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (downloadfirst.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (autopicknet.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (disableflyoutsounds.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (savecounterswhenexit.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (useforselectedcard.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   inifile.WriteLine(timetointip.value * 1000);
   inifile.WriteLine(timetoextip.value * 1000);

   inifile.WriteLine(ipserver.value);

   inifile.WriteLine(ResetCounterType.value);

   if (enablesaveextip.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   inifile.WriteLine(peakunits.value);

   if (autoscaleDPI.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (playsound.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   inifile.WriteLine(soundpath.value);

   if (playconnectionsound.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   inifile.WriteLine(soundconnectionpath.value);
   if (firstrunplay.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

   if (enableforcibly.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   inifile.WriteLine(timetoforciblycheck.value * 60000);
  }//end try 2

  finally {inifile.Close()}
 }//end try 1

 catch (e) {}
}

function showtab(id){
 names = new Array ("tabname_1","tabname_2","tabname_3","tabname_4","tabname_5","tabname_6","tabname_7","tabname_8","tabname_9","tabname_10","tabname_11","tabname_12");
 conts= new Array ("tabcontent_1","tabcontent_2","tabcontent_3","tabcontent_4","tabcontent_5","tabcontent_6","tabcontent_7","tabcontent_8","tabcontent_9","tabcontent_10","tabcontent_11","tabcontent_12");
 for(i=0;i<names.length;i++) { document.getElementById(names[i]).className = 'nonactive' }
 for(i=0;i<conts.length;i++) { document.getElementById(conts[i]).className = 'hide' }
 document.getElementById('tabname_' + id).className = 'active';
 document.getElementById('tabcontent_' + id).className = 'show';
}

function savecountertofile(){
 var fs = new ActiveXObject("Scripting.FileSystemObject");
 var inifilename = System.Environment.getEnvironmentVariable("APPDATA") + "\\" + System.Gadget.name + "_Traffic.ini";
 try {
  var inifile = fs.OpenTextFile(inifilename, 2, true); //2 write file true - create file

  try {
   inifile.WriteLine(";Traffic counter for Network Monitor II (c) 2010-2013 by Igor 'Igogo' Bushin http://www.myfavoritegadgets.info/");
   inifile.WriteLine(System.Gadget.document.parentWindow.bytesSentTotal);
   inifile.WriteLine(System.Gadget.document.parentWindow.bytesReceivedTotal);
  }//end try 2
  
  finally {inifile.Close()}
 }//end try 1

 catch (e) {}
}

function loadcounterfromfile(){
 var fs = new ActiveXObject("Scripting.FileSystemObject");
 var inifilename = System.Environment.getEnvironmentVariable("APPDATA") + "\\" + System.Gadget.name + "_Traffic.ini";
 try {
  var inifile = fs.OpenTextFile(inifilename, 1); //1 read
   try {
    var tmp = inifile.ReadLine();
    System.Gadget.document.parentWindow.bytesSentTotal = parseInt(inifile.ReadLine());
    System.Gadget.document.parentWindow.bytesReceivedTotal = parseInt(inifile.ReadLine());
   }//end try 2
  
   finally {inifile.Close()}
 }//end try 1   
 
 catch (e) {}

 finally {fs = null}
}

function showipfile(){
 var txtfilename = System.Environment.getEnvironmentVariable("APPDATA") + "\\" + System.Gadget.name + "_ExternalIPs.txt";
 System.Shell.execute("notepad", txtfilename);
}

function deleteipfile(){
 var txtfilename = System.Environment.getEnvironmentVariable("APPDATA") + "\\" + System.Gadget.name + "_ExternalIPs.txt";
 var myObject = new ActiveXObject("Scripting.FileSystemObject");
 var f = myObject.GetFile(txtfilename);
 f.Delete();
}

function resettodefault(){
 ScaleType.value = 0;
 ScaleSize.value = 0;
 barup.value = 0; 
 bardown.value = 0;
 displayunits.value = 0;
 peakunits.value = 0;
}

function restorefactorycolors(){
 System.Gadget.Settings.write("sBgColorSelect",0);
 document.getElementById("BgColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor1").style.background = document.getElementById("BgColorSelect").value;
 document.getElementById("multiColorPicker1").value = document.getElementById("BgColorSelect").value.substr(1);
 System.Gadget.Settings.write("sTitColorSelect",0);
 document.getElementById("TitColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor2").style.background = document.getElementById("TitColorSelect").value;
 document.getElementById("multiColorPicker2").value = document.getElementById("TitColorSelect").value.substr(1);
 System.Gadget.Settings.write("sSignalIDSelect",0);
 document.getElementById("SignalIDSelect").selectedIndex = 0;
 document.getElementById("simplecolor3").style.background = document.getElementById("SignalIDSelect").value;
 document.getElementById("multiColorPicker3").value = document.getElementById("SignalIDSelect").value.substr(1);
 System.Gadget.Settings.write("sIntIPSelect",0);
 document.getElementById("IntIPSelect").selectedIndex = 0;
 document.getElementById("simplecolor4").style.background = document.getElementById("IntIPSelect").value;
 document.getElementById("multiColorPicker4").value = document.getElementById("IntIPSelect").value.substr(1);
 System.Gadget.Settings.write("sExtIPSelect",0);
 document.getElementById("ExtIPSelect").selectedIndex = 0;
 document.getElementById("simplecolor5").style.background = document.getElementById("ExtIPSelect").value;
 document.getElementById("multiColorPicker5").value = document.getElementById("ExtIPSelect").value.substr(1);
 System.Gadget.Settings.write("sMenuColorSelect",0);
 document.getElementById("MenuColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor6").style.background = document.getElementById("MenuColorSelect").value;
 document.getElementById("multiColorPicker6").value = document.getElementById("MenuColorSelect").value.substr(1);
 System.Gadget.Settings.write("sConnectColorSelect",0);
 document.getElementById("ConnectColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor7").style.background = document.getElementById("ConnectColorSelect").value;
 document.getElementById("multiColorPicker7").value = document.getElementById("ConnectColorSelect").value.substr(1);
 System.Gadget.Settings.write("sSignalColorSelect",0);
 document.getElementById("SignalColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor8").style.background = document.getElementById("SignalColorSelect").value;
 document.getElementById("multiColorPicker8").value = document.getElementById("SignalColorSelect").value.substr(1);
 System.Gadget.Settings.write("sNoSignalColorSelect",0);
 document.getElementById("NoSignalColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor9").style.background = document.getElementById("NoSignalColorSelect").value;
 document.getElementById("multiColorPicker9").value = document.getElementById("NoSignalColorSelect").value.substr(1);
 System.Gadget.Settings.write("sNetColorSelect",0);
 document.getElementById("NetColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor10").style.background = document.getElementById("NetColorSelect").value;
 document.getElementById("multiColorPicker10").value = document.getElementById("NetColorSelect").value.substr(1);
 System.Gadget.Settings.write("sUpColorSelect",0);
 document.getElementById("UpColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor11").style.background = document.getElementById("UpColorSelect").value;
 document.getElementById("multiColorPicker11").value = document.getElementById("UpColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDownColorSelect",0);
 document.getElementById("DownColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor12").style.background = document.getElementById("DownColorSelect").value;
 document.getElementById("multiColorPicker12").value = document.getElementById("DownColorSelect").value.substr(1);
 System.Gadget.Settings.write("sScaleColorSelect",0);
 document.getElementById("ScaleColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor13").style.background = document.getElementById("ScaleColorSelect").value;
 document.getElementById("multiColorPicker13").value = document.getElementById("ScaleColorSelect").value.substr(1);
 System.Gadget.Settings.write("sServStrColorSelect",0);
 document.getElementById("ServStrColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor14").style.background = document.getElementById("ServStrColorSelect").value;
 document.getElementById("multiColorPicker14").value = document.getElementById("ServStrColorSelect").value.substr(1);
 System.Gadget.Settings.write("sGraphScaleColorSelect",0);
 document.getElementById("GraphScaleColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor18").style.background = document.getElementById("GraphScaleColorSelect").value;
 document.getElementById("multiColorPicker18").value = document.getElementById("GraphScaleColorSelect").value.substr(1);
 System.Gadget.Settings.write("sGrBgColorSelect",0);
 document.getElementById("GrBgColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor15").style.background = document.getElementById("GrBgColorSelect").value;
 document.getElementById("multiColorPicker15").value = document.getElementById("GrBgColorSelect").value.substr(1);
 System.Gadget.Settings.write("sGrScaleBgColorSelect",0);
 document.getElementById("GrScaleBgColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor19").style.background = document.getElementById("GrScaleBgColorSelect").value;
 document.getElementById("multiColorPicker19").value = document.getElementById("GrScaleBgColorSelect").value.substr(1);
 System.Gadget.Settings.write("sPFColorSelect",0);
 document.getElementById("PFColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor20").style.background = document.getElementById("PFColorSelect").value;
 document.getElementById("multiColorPicker20").value = document.getElementById("PFColorSelect").value.substr(1);
 System.Gadget.Settings.write("sVersnColorSelect",0);
 document.getElementById("VersnColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor16").style.background = document.getElementById("VersnColorSelect").value;
 document.getElementById("multiColorPicker16").value = document.getElementById("VersnColorSelect").value.substr(1);
 System.Gadget.Settings.write("sCopyColorSelect",0);
 document.getElementById("CopyColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor17").style.background = document.getElementById("CopyColorSelect").value;
 document.getElementById("multiColorPicker17").value = document.getElementById("CopyColorSelect").value.substr(1);
}

function colorSelected(obj1,obj2,obj3){
 document.getElementById(obj1).blur();
 if (document.getElementById(obj1).selectedIndex == document.getElementById(obj1).length - 1){
  document.getElementById(obj2).disabled = false;
 }
 else{
  document.getElementById(obj2).disabled = true;
  document.getElementById(obj3).style.background = document.getElementById(obj1).value;
  document.getElementById(obj2).value = document.getElementById(obj1).value.substr(1);
 }
 System.Gadget.Settings.write("s"+obj1,document.getElementById(obj1).selectedIndex);
}

function needenable(obj1,obj2){
 if (document.getElementById(obj1).selectedIndex == document.getElementById(obj1).length - 1) document.getElementById(obj2).disabled = false;
 else document.getElementById(obj2).disabled = true;
}

function restoresettingstodefault(){
 netlibbox.checked = false;
 changeflag = netlibbox.checked;
 Int_Nr2 = 0;
 Int_Nr3 = 0;
 size.value = 100;
 timetorefresh.value = 1;
 autoscaleDPI.checked = true;
 hidesize();
 playsound.checked = true;
 hideselectbutton();
 soundpath.value = "media/redalert.wav";
 playconnectionsound.checked = true;
 hideconnectselectbutton();
 soundconnectionpath.value = "media/connectionon.wav";
 firstrunplay.checked = true;
 SkinType.value = 0;
 setarrows();
 ConnectType.value = 0;
 wirelesssettings();
 ScaleType.value = 0;
 FixedHideUnhide();
 ScaleSize.value = 0;
 TrafficHistoryOn.checked = true;
 ResetTraffic.checked = false;
 barup.value = 0; 
 bardown.value = 0;
 displayunits.value = 0;
 peakunits.value = 0;

 interface1.options.length = 0;
 var colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPenabled=True", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
 var enumItems = new Enumerator(colItems);
 if(enumItems.atEnd()){
  newOption = new Option("No connected device", "");
  interface1.options[interface1.length] = newOption;
 }
 for (var i=0; !enumItems.atEnd(); enumItems.moveNext()){
  var objItem = enumItems.item();
  var name = objItem.Description;
  if(name.length > 42) name = name.substr(0,42);
  newOption = new Option(name, i);
  interface1.options[interface1.length] = newOption;
  i++;
 }
 interface1.selectedIndex = 0;
 interface1.disabled = false;
 interface1.disabled = (interface1.length < 2);

 interface2.options.length = 0;
 colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface", "WQL", wbemFlagReturnImmediately | wbemFlagForwardOnly);
 enumItems = new Enumerator(colItems);
 if(enumItems.atEnd()){
  newOption = new Option("No connected device", "");
  interface2.options[interface2.length] = newOption;
 }
 for (var i=0; !enumItems.atEnd(); enumItems.moveNext()){
  var objItem = enumItems.item();
  var name = objItem.Name;
  if(name.length > 40) name = name.substr(0,40);
  newOption = new Option((i+1) + ": " + name, i);
  interface2.options[interface2.length] = newOption;
  i++;
 }
 interface2.selectedIndex = 0;
 interface2.disabled = false;
 interface2.disabled = (interface2.length < 2);

 languagelist.value = 0;
 hidegridlines.checked = false;
 graphlinewidth.value = 1;
 hidetitle.checked = false;
 hideid.checked = false;
 hideintip.checked = false;
 hideextip.checked = false;
 hideserv.checked = false;
 hideconnect.checked = false;
 hidesignal.checked = false;
 hidenetusage.checked = false;
 hideupdownspeed.checked = false;
 uncheckb();
 hidebitpersec.checked = false;
 hidebytepersec.checked = false;
 hidebars.checked = false;
 hideallgraphics.checked = false;
 grhide();
 hidegraphscale.checked = false;
 hideuploadgraph.checked = false;
 hidedownloadgraph.checked = false;
 hidepeak.checked = false;
 hidepeakstring.checked = false;
 peakstrhide(); //after hidepeakstring check
 hidecurrent.checked = false;
 hidetotal.checked = false;
 hideprofilefirewall.checked = false;
 hidecopyright.checked = false;
 autocheckintIP.checked = true;
 autocheckextIP.checked = true;
 saveIPhide();
 enableforcibly.checked = false;
 forciblytimehide();
 ResetCounterType.value = 0;
 ResetCounter.value = 0;
 dayselect.value = 0;
 cfserver.value = 1;
 dayofmonthselect.value = 1;
 hoursselect.options.length = 0;
 for (var i=0; i<24; i++){
  newOption = new Option(i<10 ? "0" + i : i, i);
  hoursselect.options[hoursselect.length] = newOption;
 }
 hoursselect.options[0].selected = true;
 minutesselect.options.length = 0
 for (var i=0; i<60; i++){
  newOption = new Option(i<10 ? "0" + i : i, i);
  minutesselect.options[minutesselect.length] = newOption;
 }
 minutesselect.options[0].selected = true;
 ResetHideUnhide();
 renameips.checked = false;
 enablecheckforupdate.checked = false;
 uploadfill.checked = false;
 downloadfill.checked = false;
 downloadfirst.checked = false;
 disableflyoutsounds.checked = false;
 autopicknet.checked = false;
 savecounterswhenexit.checked = true;
 useforselectedcard.checked = true;
 timetointip.value = 2;
 timetoextip.value = 2;
 timetoforciblycheck.value = 5;
 enablesaveextip.checked = false;
 resettodefault()
 restorefactorycolors();
}