System.Gadget.onSettingsClosing = SettingsClosing;

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

var tmpvalue;
var numcyclepos = 9;

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

function copyText(field){
 window.clipboardData.setData('Text',field.innerText);
}

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

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)) timetorefresh.value = Math.ceil(System.Gadget.Settings.read("refreshtime") / 1000);
}

function numslotschange(){
 var tmp = parseInt(numslots.value,10);
 if (isNaN(tmp) || (tmp <1)) numslots.value = System.Gadget.Settings.read("NumProcesses");
}

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

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

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

function init(){
 buildlngselect();

 processmode.value = System.Gadget.Settings.read("ProcessMode");
 numslots.value = System.Gadget.Settings.read("NumProcesses");
 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);

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

 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("TotalHide");
 if (tmp == "false") hidetotal.checked = false;
 else hidetotal.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!!!!

 for (var i=1; i<=numcyclepos; i++){
  tmp = System.Gadget.Settings.read("ProcCycle" + i);
  if (tmp == "false") document.getElementById("proc" + i).checked = false;
  else document.getElementById("proc" + i).checked = true;
 }

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

 action.value = System.Gadget.Settings.read("dblclick");

 //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("sServStrColorSelect");
 if (tmp !=""){
  document.getElementById("ServStrColorSelect").selectedIndex = tmp;
 }
 else{
  document.getElementById("ServStrColorSelect").selectedIndex = 0;
 }
 document.getElementById("simplecolor3").style.background = System.Gadget.Settings.read("colServStr");
 document.getElementById("multiColorPicker3").value = System.Gadget.Settings.read("colServStr").substr(1);
 needenable("ServStrColorSelect","multiColorPicker3");

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

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

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

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

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

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

function SettingsClosing(event){
 if (event.closeAction == event.Action.commit){
  System.Gadget.Settings.write("LanguageId", languagelist.value);
  System.Gadget.Settings.write("ProcessMode", processmode.value);
  numslotschange();
  System.Gadget.Settings.write("NumProcesses", numslots.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");

  if (memorypercent.checked) System.Gadget.Settings.write ("PersentInMemory", "true");
  else System.Gadget.Settings.write ("PersentInMemory", "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");

  if (hidetotal.checked) System.Gadget.Settings.write ("TotalHide", "true");
  else System.Gadget.Settings.write ("TotalHide", "false");

  System.Gadget.Settings.write("WarningUse", warningused.value);
  System.Gadget.Settings.write("CriticalUse", criticalused.value);

  for (var i=1; i<=numcyclepos; i++){
   if (document.getElementById("proc" + i).checked) {System.Gadget.Settings.write ("ProcCycle" + i, "true")}
   else {System.Gadget.Settings.write ("ProcCycle" + i, "false")}
  }

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

  System.Gadget.Settings.write("dblclick", action.value);

  //color settings
  System.Gadget.Settings.write("colBgColor", "#"+document.getElementById('multiColorPicker1').value);
  System.Gadget.Settings.write("colTitle", "#"+document.getElementById('multiColorPicker2').value);
  System.Gadget.Settings.write("colServStr", "#"+document.getElementById('multiColorPicker3').value);
  System.Gadget.Settings.write("colUsedEasy", "#"+document.getElementById('multiColorPicker4').value);
  System.Gadget.Settings.write("colUsedMedium", "#"+document.getElementById('multiColorPicker5').value);
  System.Gadget.Settings.write("colUsedHard", "#"+document.getElementById('multiColorPicker6').value);
  System.Gadget.Settings.write("colErrMsg", "#"+document.getElementById('multiColorPicker7').value);
  System.Gadget.Settings.write("colVersion", "#"+document.getElementById('multiColorPicker8').value);
  System.Gadget.Settings.write("colCopyright", "#"+document.getElementById('multiColorPicker9').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(";Top Process Monitor (c) 2010-2013 by Igor 'Igogo' Bushin");  
   inifile.WriteLine(";purr purr 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("ServStrColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker3').value);
   inifile.WriteLine(document.getElementById("ProcessEasyColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker4').value);
   inifile.WriteLine(document.getElementById("ProcessMediumColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker5').value);
   inifile.WriteLine(document.getElementById("ProcessHardColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker6').value);
   inifile.WriteLine(document.getElementById("ErrMsgColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker7').value);
   inifile.WriteLine(document.getElementById("VersnColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker8').value);
   inifile.WriteLine(document.getElementById("CopyColorSelect").selectedIndex);
   inifile.WriteLine('#'+document.getElementById('multiColorPicker9').value);
   //color settings end

   inifile.WriteLine(size.value / 100);
   inifile.WriteLine(numslots.value);
   inifile.WriteLine(warningused.value);
   inifile.WriteLine(criticalused.value);
   if (memorypercent.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");

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

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

   inifile.WriteLine(action.value);

   if (autoscaleDPI.checked) inifile.WriteLine("true");
   else inifile.WriteLine("false");
  }//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");
 conts= new Array ("tabcontent_1","tabcontent_2","tabcontent_3","tabcontent_4","tabcontent_5","tabcontent_6","tabcontent_7");
 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("sServStrColorSelect",0);
 document.getElementById("ServStrColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor3").style.background = document.getElementById("ServStrColorSelect").value;
 document.getElementById("multiColorPicker3").value = document.getElementById("ServStrColorSelect").value.substr(1);
 System.Gadget.Settings.write("sProcessEasyColorSelect",0);
 document.getElementById("ProcessEasyColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor4").style.background = document.getElementById("ProcessEasyColorSelect").value;
 document.getElementById("multiColorPicker4").value = document.getElementById("ProcessEasyColorSelect").value.substr(1);
 System.Gadget.Settings.write("sProcessMediumColorSelect",0);
 document.getElementById("ProcessMediumColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor5").style.background = document.getElementById("ProcessMediumColorSelect").value;
 document.getElementById("multiColorPicker5").value = document.getElementById("ProcessMediumColorSelect").value.substr(1);
 System.Gadget.Settings.write("sProcessHardColorSelect",0);
 document.getElementById("ProcessHardColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor6").style.background = document.getElementById("ProcessHardColorSelect").value;
 document.getElementById("multiColorPicker6").value = document.getElementById("ProcessHardColorSelect").value.substr(1);
 System.Gadget.Settings.write("sErrMsgColorSelect",0);
 document.getElementById("ErrMsgColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor7").style.background = document.getElementById("ErrMsgColorSelect").value;
 document.getElementById("multiColorPicker7").value = document.getElementById("ErrMsgColorSelect").value.substr(1);
 System.Gadget.Settings.write("sVersnColorSelect",0);
 document.getElementById("VersnColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor8").style.background = document.getElementById("VersnColorSelect").value;
 document.getElementById("multiColorPicker8").value = document.getElementById("VersnColorSelect").value.substr(1);
 System.Gadget.Settings.write("sCopyColorSelect",0);
 document.getElementById("CopyColorSelect").selectedIndex = 0;
 document.getElementById("simplecolor9").style.background = document.getElementById("CopyColorSelect").value;
 document.getElementById("multiColorPicker9").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;
 processmode.value = "CPU";
 numslots.value =  5;
 size.value = 100;
 autoscaleDPI.checked = true;
 hidesize();
 timetorefresh.value = 5;
 memorypercent.checked = false;
 hidetotal.checked = false;
 var tmp = 65;
 warningused.options[parseInt(tmp)-1].selected = true;
 tmp = 90;
 criticalused.options.length = 0;
 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!!!!
 for (var i=1; i<=numcyclepos; i++){
  document.getElementById("proc" + i).checked = false;
 }
 enablecheckforupdate.checked = false;
 action.value = 0;
 hidetitle.checked = false;
 hidecopyright.checked = false;
 restorefactorycolors();
}