System.Gadget.onSettingsClosing = SettingsClosing;

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

var tmpvalue;

function checkforupdate(){
 var vString = "";
 var url = "http://www.myfavoritegadgets.info/monitors/DrivesMonitor/DrivesMonitorversioninfo.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][89] + System.Gadget.version + "<br>";
     vString += "<strong><a href='" + updateUrl + "' onclick='System.Gadget.close()' ";
	 vString += "title='" + setlng[lngID][90] + " (" + newVersion + ")'>" + setlng[lngID][90] + " (" + newVersion + ")</a></strong></font>"; 
     updatestring.innerHTML = vString;
     updatebutton.style.display = "none";
    }
    else{
     updatestring.innerHTML = "<font color='#008000'>" + setlng[lngID][87] + "</font>";
     updatebutton.style.display = "none";
    }
   }//end status
   else{
    updatestring.innerHTML = "<font color='#FF0000' size='2'>" + setlng[lngID][88] + xmlHttp.status + "</b></font>";
    updatebutton.style.display = "none";
   }
  }//end readyState
 }//end function
 xmlHttp.send(null);
}

function OnPaste(){
 return false; //cancels the onpaste event
}

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

function validateNumberandsymbols(e){
 var numsymbcheck = /\d|;/;
 return numsymbcheck.test(String.fromCharCode(e.keyCode));
}

function validateChar(e){
 var charcheck = /[a-zA-Z]/;
 return charcheck.test(String.fromCharCode(e.keyCode));
}

function sizechange(){
 var tmp = parseInt(size.value,10);
 if (isNaN(tmp)) size.value = Math.ceil(System.Gadget.Settings.read("SetSize") * 100);
}

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

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

function setcriticaldata(){
 tmpvalue = criticalused.value;
} 

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

function createwarningselect(){
 criticalused.options.length = 0;
 //criticalused.innerHTML = null;
 for (var i=parseInt(warningused.value)+1; i<100; i++){
  newOption = new Option(i, i);
  criticalused.options[criticalused.length] = newOption;
 }
 if (parseInt(warningused.value)<tmpvalue) criticalused.options[tmpvalue - parseInt(warningused.value) - 1].selected = true;
 else criticalused.options[0].selected = true;

 setcriticaldata();
}

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");
 txtExcludedDrives.style.width = tech[lngID][0];
 txtExcludedDrivesNumber.style.width = tech[lngID][1];
}

function loadSettings(){    
 System.Gadget.document.parentWindow.returnfromsettings = true;//Important!!!

 buildlngselect();

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

 timetorefresh.value = Math.ceil(System.Gadget.Settings.read("refreshtime") / 1000);
 dskmode.value = System.Gadget.Settings.read("WhichDrivesICanSee");

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

 var mask = parseInt(System.Gadget.Settings.read("ExcludedDrives"));
 if (!isNaN(mask)) txtExcludedDrives.value = maskToString(mask);
 else txtExcludedDrives.value = "";
 txtExcludedDrivesNumber.value = System.Gadget.Settings.read("ExcludedDrivesNumber");

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

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

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

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

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

 HideGrOFF();

 tmp = System.Gadget.Settings.read("TitleHide");
 if (tmp == "false") hidetitle.checked = false;
 else hidetitle.checked = true;
 tmp = System.Gadget.Settings.read("CopyrightHide");
 if (tmp == "false") hidecopyright.checked = false;
 else hidecopyright.checked = true;
 
 tmp = System.Gadget.Settings.read("WarningUse");
 
 for (var i=1; i<99; i++){
  newOption = new Option(i, i);
  warningused.options[warningused.length] = newOption;
 }
 warningused.options[parseInt(tmp)-1].selected = true;

 tmp = System.Gadget.Settings.read("CriticalUse");
 for (var i=parseInt(warningused.value)+1; i<100; i++){
  newOption = new Option(i, i);
  criticalused.options[criticalused.length] = newOption;
 }

 criticalused.options[parseInt(parseInt(tmp)- parseInt(warningused.value) - 1)].selected = true;
 tmpvalue = criticalused.value;//Important!!!!

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

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

 tmp = System.Gadget.Settings.read("fillActivity");
 if (tmp == "false") activityfill.checked = false;
 else activityfill.checked = true;
 tmp = System.Gadget.Settings.read("fillRead");
 if (tmp == "false") readfill.checked = false;
 else readfill.checked = true;
 tmp = System.Gadget.Settings.read("fillWrite");
 if (tmp == "false") writefill.checked = false;
 else writefill.checked = true;

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

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

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

 tmp = System.Gadget.Settings.read("DisableFlyoutSounds");
 if (tmp == "false") disableflyoutsounds.checked = false;
 else disableflyoutsounds.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("sDiskNameColorSelect");
 if (tmp !=""){
  document.getElementById("DiskNameColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("DiskNameColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor3").style.background = System.Gadget.Settings.read("colDiskName");
 document.getElementById("multiColorPicker3").value = System.Gadget.Settings.read("colDiskName").substr(1);
 needenable("DiskNameColorSelect","multiColorPicker3");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 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);

  timechange();
  System.Gadget.Settings.write("refreshtime", timetorefresh.value * 1000);
  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");

  System.Gadget.Settings.write("WhichDrivesICanSee", dskmode.value);

  if (savedisks.checked) System.Gadget.Settings.write ("SaveDisksLettersNumbers", "true");
  else System.Gadget.Settings.write ("SaveDisksLettersNumbers", "false");

  // Convert string of excluded drive letters to a bit mask and save.
  System.Gadget.Settings.write("ExcludedDrives", stringToMask(txtExcludedDrives.value));
  var mask = stringToNumberMask(txtExcludedDrivesNumber.value);
  if (mask == ";") mask = "";
  System.Gadget.Settings.write("ExcludedDrivesNumber", mask);

  if (showtemperature.checked) System.Gadget.Settings.write ("ShowDiskTemperature", "true");
  else System.Gadget.Settings.write ("ShowDiskTemperature", "false");

  if (hidefulldiskinfo.checked) System.Gadget.Settings.write ("HideFullDiskInfo", "true");
  else System.Gadget.Settings.write ("HideFullDiskInfo", "false");
  if (hidegraphics.checked) System.Gadget.Settings.write ("GraphHide", "true");
  else System.Gadget.Settings.write ("GraphHide", "false");

  if (hidehiddendrive.checked) System.Gadget.Settings.write ("HiddenPartHide", "true");
  else System.Gadget.Settings.write ("HiddenPartHide", "false");
  if (hidemounteddrive.checked) System.Gadget.Settings.write ("MountedPartHide", "true");
  else System.Gadget.Settings.write ("MountedPartHide", "false");

  if (hidetitle.checked) System.Gadget.Settings.write ("TitleHide", "true");
  else System.Gadget.Settings.write ("TitleHide", "false");
  if (hidecopyright.checked) System.Gadget.Settings.write ("CopyrightHide", "true");
  else System.Gadget.Settings.write ("CopyrightHide", "false");

  System.Gadget.Settings.write("WarningUse", warningused.value);
  System.Gadget.Settings.write("CriticalUse", criticalused.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 (activityfill.checked) System.Gadget.Settings.write ("fillActivity", "true");
  else System.Gadget.Settings.write ("fillActivity", "false");
  if (readfill.checked) System.Gadget.Settings.write ("fillRead", "true");
  else System.Gadget.Settings.write ("fillRead", "false");
  if (writefill.checked) System.Gadget.Settings.write ("fillWrite", "true");
  else System.Gadget.Settings.write ("fillWrite", "false");

  if (chooseactivity.checked) System.Gadget.Settings.write ("ChooseActivity", "true");
  else System.Gadget.Settings.write ("ChooseActivity", "false");

  if (hidecounters.checked)System.Gadget.Settings.write ("CountersHide", "true");
  else System.Gadget.Settings.write ("CountersHide", "false");

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

  if (disableflyoutsounds.checked) System.Gadget.Settings.write ("DisableFlyoutSounds", "true");
  else System.Gadget.Settings.write ("DisableFlyoutSounds", "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("colDiskName", "#"+document.getElementById('multiColorPicker3').value);
  System.Gadget.Settings.write("colDiskInfo", "#"+document.getElementById('multiColorPicker4').value);
  System.Gadget.Settings.write("colFileSystem", "#"+document.getElementById('multiColorPicker5').value);
  System.Gadget.Settings.write("colDiskPeak", "#"+document.getElementById('multiColorPicker6').value);
  System.Gadget.Settings.write("colDiskActivity", "#"+document.getElementById('multiColorPicker7').value);
  System.Gadget.Settings.write("colDiskRead", "#"+document.getElementById('multiColorPicker8').value);
  System.Gadget.Settings.write("colDiskWrite", "#"+document.getElementById('multiColorPicker9').value);
  System.Gadget.Settings.write("colCounterRead", "#"+document.getElementById('multiColorPicker18').value);
  System.Gadget.Settings.write("colCounterWrite", "#"+document.getElementById('multiColorPicker19').value);
  System.Gadget.Settings.write("colDiskEasy", "#"+document.getElementById('multiColorPicker10').value);
  System.Gadget.Settings.write("colDiskMedium", "#"+document.getElementById('multiColorPicker11').value);
  System.Gadget.Settings.write("colDiskHard", "#"+document.getElementById('multiColorPicker12').value);
  System.Gadget.Settings.write("colErrMsg", "#"+document.getElementById('multiColorPicker13').value);
  System.Gadget.Settings.write("colFillGraph", "#"+document.getElementById('multiColorPicker14').value);
  System.Gadget.Settings.write("colFillPeak", "#"+document.getElementById('multiColorPicker15').value);
  System.Gadget.Settings.write("colVersion", "#"+document.getElementById('multiColorPicker16').value);
  System.Gadget.Settings.write("colCopyright", "#"+document.getElementById('multiColorPicker17').value);

  savesettingstofile();
  
  System.Gadget.document.parentWindow.setgadgetchange = true;
 }
 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(";Drives Monitor (c) 2010-2013 by Igor 'Igogo' Bushin");  
   inifile.WriteLine(";purr 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("DiskNameColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker3').value);
   inifile.WriteLine(document.getElementById("DiskInfoColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker4').value);
   inifile.WriteLine(document.getElementById("FilesystemColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker5').value);
   inifile.WriteLine(document.getElementById("DiskPeakColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker6').value);
   inifile.WriteLine(document.getElementById("DiskActivityColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker7').value);
   inifile.WriteLine(document.getElementById("DiskReadColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker8').value);
   inifile.WriteLine(document.getElementById("DiskWriteColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker9').value);
   inifile.WriteLine(document.getElementById("CounterReadColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker18').value);
   inifile.WriteLine(document.getElementById("CounterWriteColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker19').value);
   inifile.WriteLine(document.getElementById("DiskEasyColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker10').value);
   inifile.WriteLine(document.getElementById("DiskMediumColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker11').value);
   inifile.WriteLine(document.getElementById("DiskHardColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker12').value);
   inifile.WriteLine(document.getElementById("ErrMsgColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker13').value);
   inifile.WriteLine(document.getElementById("GrBgColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker14').value);
   inifile.WriteLine(document.getElementById("FillPeakColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker15').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(size.value / 100);
   inifile.WriteLine(timetorefresh.value * 1000);

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

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

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

   inifile.WriteLine(warningused.value);
   inifile.WriteLine(criticalused.value);

   inifile.WriteLine(graphlinewidth.value);

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

   if (activityfill.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (readfill.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   if (writefill.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");

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

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

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

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

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

   if (savedisks.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
   inifile.WriteLine(dskmode.value);
   inifile.WriteLine(System.Gadget.Settings.read("ExcludedDrives"));
   inifile.WriteLine(System.Gadget.Settings.read("ExcludedDrivesNumber"));

   if (showtemperature.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
  }//end try 2

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

 catch (e) {}
}

function maskToString(mask){
 var temp = "";
 for (var i = 0; i < 26; i++){
  var letterValue = Math.pow(2, i);
  if ((letterValue & mask) == letterValue) temp += String.fromCharCode(65 + i);
 }
 return temp;    
}

function stringToMask(value){
 value = value.toUpperCase();
 var mask = 0;
 for (var i = 0; i < value.length; i++){
  var letterValue = value.charCodeAt(i) - 65;
  if (letterValue < 0 || letterValue > 25) continue;
  mask |= Math.pow(2, letterValue);
 }
 return mask;
}

String.prototype.removesemibegin=function(){
 // remove semicolon at begin of string
 var res=/^;/g;
 return this.replace(res,'');
}

String.prototype.removedoublesemi=function(){
 // remove doubles semicolon from string
 var res=/;+/g;
 return this.replace(res,';');
}

function stringToNumberMask(value){
 value += ';';
 return value.removedoublesemi().removesemibegin();
}

function HideGrOFF(){
 if (dskmode.value == 1){
  hidegraphics.checked = false;
  hgr.disabled = true;
  excludelogical.style.display = "none";
  excludephysical.style.display = "block";
 }
 else{
  hgr.disabled = false;
  excludelogical.style.display = "block";
  excludephysical.style.display = "none";
 }
}

function showtab(id){
 names = new Array ("tabname_1","tabname_2","tabname_3","tabname_4","tabname_5","tabname_6","tabname_7","tabname_8","tabname_9");
 conts= new Array ("tabcontent_1","tabcontent_2","tabcontent_3","tabcontent_4","tabcontent_5","tabcontent_6","tabcontent_7","tabcontent_8","tabcontent_9");
 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 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("sDiskNameColorSelect",0);
 document.getElementById("DiskNameColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor3").style.background = document.getElementById("DiskNameColorSelect").value;
 document.getElementById("multiColorPicker3").value = document.getElementById("DiskNameColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskInfoColorSelect",0);
 document.getElementById("DiskInfoColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor4").style.background = document.getElementById("DiskInfoColorSelect").value;
 document.getElementById("multiColorPicker4").value = document.getElementById("DiskInfoColorSelect").value.substr(1);
 System.Gadget.Settings.write("sFilesystemColorSelect",0);
 document.getElementById("FilesystemColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor5").style.background = document.getElementById("FilesystemColorSelect").value;
 document.getElementById("multiColorPicker5").value = document.getElementById("FilesystemColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskPeakColorSelect",0);
 document.getElementById("DiskPeakColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor6").style.background = document.getElementById("DiskPeakColorSelect").value;
 document.getElementById("multiColorPicker6").value = document.getElementById("DiskPeakColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskActivityColorSelect",0);
 document.getElementById("DiskActivityColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor7").style.background = document.getElementById("DiskActivityColorSelect").value;
 document.getElementById("multiColorPicker7").value = document.getElementById("DiskActivityColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskReadColorSelect",0);
 document.getElementById("DiskReadColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor8").style.background = document.getElementById("DiskReadColorSelect").value;
 document.getElementById("multiColorPicker8").value = document.getElementById("DiskReadColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskWriteColorSelect",0);
 document.getElementById("DiskWriteColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor9").style.background = document.getElementById("DiskWriteColorSelect").value;
 document.getElementById("multiColorPicker9").value = document.getElementById("DiskWriteColorSelect").value.substr(1);
 System.Gadget.Settings.write("sCounterReadColorSelect",0);
 document.getElementById("CounterReadColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor18").style.background = document.getElementById("CounterReadColorSelect").value;
 document.getElementById("multiColorPicker18").value = document.getElementById("CounterReadColorSelect").value.substr(1);
 System.Gadget.Settings.write("sCounterWriteColorSelect",0);
 document.getElementById("CounterWriteColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor19").style.background = document.getElementById("CounterWriteColorSelect").value;
 document.getElementById("multiColorPicker19").value = document.getElementById("CounterWriteColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskEasyColorSelect",0);
 document.getElementById("DiskEasyColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor10").style.background = document.getElementById("DiskEasyColorSelect").value;
 document.getElementById("multiColorPicker10").value = document.getElementById("DiskEasyColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskMediumColorSelect",0);
 document.getElementById("DiskMediumColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor11").style.background = document.getElementById("DiskMediumColorSelect").value;
 document.getElementById("multiColorPicker11").value = document.getElementById("DiskMediumColorSelect").value.substr(1);
 System.Gadget.Settings.write("sDiskHardColorSelect",0);
 document.getElementById("DiskHardColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor12").style.background = document.getElementById("DiskHardColorSelect").value;
 document.getElementById("multiColorPicker12").value = document.getElementById("DiskHardColorSelect").value.substr(1);
 System.Gadget.Settings.write("sErrMsgColorSelect",0);
 document.getElementById("ErrMsgColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor13").style.background = document.getElementById("ErrMsgColorSelect").value;
 document.getElementById("multiColorPicker13").value = document.getElementById("ErrMsgColorSelect").value.substr(1);
 System.Gadget.Settings.write("sGrBgColorSelect",0);
 document.getElementById("GrBgColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor14").style.background = document.getElementById("GrBgColorSelect").value;
 document.getElementById("multiColorPicker14").value = document.getElementById("GrBgColorSelect").value.substr(1);
 System.Gadget.Settings.write("sFillPeakColorSelect",0);
 document.getElementById("FillPeakColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor15").style.background = document.getElementById("FillPeakColorSelect").value;
 document.getElementById("multiColorPicker15").value = document.getElementById("FillPeakColorSelect").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(){
 languagelist.value = 0;
 size.value = 100;
 savedisks.checked = false;
 autoscaleDPI.checked = true;
 hidesize();
 timetorefresh.value = 1;
 dskmode.value = 0;
 txtExcludedDrives.value = "";
 txtExcludedDrivesNumber.value = "";
 showtemperature.checked = false;
 hidefulldiskinfo.checked = false;
 hidegraphics.checked = false;
 hidehiddendrive.checked = false;
 hidemounteddrive.checked = false;
 HideGrOFF();
 hidetitle.checked = false;
 hidecopyright.checked = false;
 warningused.options.length = 0;
 for (var i=1; i<99; i++){
  newOption = new Option(i, i);
  warningused.options[warningused.length] = newOption;
 }
 warningused.options[90-1].selected = true;
 criticalused.options.length = 0;
 for (var i=90+1; i<100; i++){
  newOption = new Option(i, i);
  criticalused.options[criticalused.length] = newOption;
 }
 criticalused.options[95-90- 1].selected = true;
 tmpvalue = criticalused.value;//Important!!!!
 graphlinewidth.value = 1;
 hidegridlines.checked = false;
 activityfill.checked = false;
 readfill.checked = false;
 writefill.checked = false;
 chooseactivity.checked = false;
 hidecounters.checked = false;
 disableflyoutsounds.checked = false;
 enablecheckforupdate.checked = false;
 restorefactorycolors();
}