try{var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")} //Msxml2.DOMDocument.6.0
catch (e){var xmlDoc = null}
var fso = new ActiveXObject("Scripting.FileSystemObject");

var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var objWMIService = locator.ConnectServer(null, "root\\cimv2");

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

var path;
var weixmllastfile;
var result;
var size;

//System.Gadget.Flyout.onShow = showFlyout;
System.Gadget.Flyout.onHide = closeFlyout;

function closeFlyout(){
 System.Gadget.Flyout.show = false;
}

function playSound(){
 if (!System.Gadget.document.parentWindow.disableflyoutsoundsflag) System.Sound.playSound("media/tone.wav");
}

function showFlyout(){
 gtitle.style.color = System.Gadget.Settings.read("colCPUmodel");
 cpuscorestr.style.color = System.Gadget.Settings.read("colMemMenu");
 memoryscorestr.style.color = System.Gadget.Settings.read("colMemMenu");
 graphicsscorestr.style.color = System.Gadget.Settings.read("colMemMenu");
 d3dscorestr.style.color = System.Gadget.Settings.read("colMemMenu");
 diskscorestr.style.color = System.Gadget.Settings.read("colMemMenu");
 winsprlevelscorestr.style.color = System.Gadget.Settings.read("colMemMenu");
 infoscorestr.style.color = System.Gadget.Settings.read("colMemMenu");
 cpuscore.style.color = System.Gadget.Settings.read("colMemRam");
 memoryscore.style.color = System.Gadget.Settings.read("colMemRam");
 graphicsscore.style.color = System.Gadget.Settings.read("colMemRam");
 d3dscore.style.color = System.Gadget.Settings.read("colMemRam");
 diskscore.style.color = System.Gadget.Settings.read("colMemRam");
 winsprlevelscore.style.color = System.Gadget.Settings.read("colMemRam");
 closeflyouta.style.color = System.Gadget.Settings.read("colCopyright");
 WinSATscore.style.color = "#E4E7F2";
 WinSATscoreSH.style.color = "#454545";

 size = System.Gadget.Settings.read("SetSize");

 //body
 background.src = "images/flyout.png";
 document.body.style.width = parseInt(239 * size);
 background.style.width = parseInt(239 * size);
 document.body.style.height = parseInt(156 * size);
 background.style.height = parseInt(156 * size);
 mainsquare.style.top = parseInt(5 * size); 
 mainsquare.style.left = parseInt(5 * size);  
 mainsquare.style.width =  parseInt(229 * size);
 mainsquare.style.height = parseInt(146 * size);
 mainsquare.style.background = System.Gadget.Settings.read("colBgColor");

 //top
 gtitle.style.top = parseInt(6 * size);
 cpuscorestr.style.top = parseInt(20 * size);
 cpuscore.style.top = parseInt(20 * size);
 memoryscorestr.style.top = parseInt(34 * size);
 memoryscore.style.top = parseInt(34 * size);
 graphicsscorestr.style.top = parseInt(48 * size);
 graphicsscore.style.top = parseInt(48 * size);
 d3dscorestr.style.top = parseInt(62 * size);
 d3dscore.style.top = parseInt(62 * size);
 diskscorestr.style.top = parseInt(76 * size);
 diskscore.style.top = parseInt(76 * size);
 winsprlevelscorestr.style.top = parseInt(90 * size);
 winsprlevelscore.style.top = parseInt(90 * size);
 infoscorestr.style.top = parseInt(108 * size);
 closeflyout.style.top = parseInt(136 * size);
 WinSATplace.style.top = parseInt(24 * size);
 WinSATscore.style.top = parseInt(36 * size);
 WinSATscoreSH.style.top = parseInt(38 * size);

 //width
 cpuscorestr.style.width = parseInt(104 * size);
 memoryscorestr.style.width = parseInt(104 * size);
 graphicsscorestr.style.width = parseInt(104 * size);
 d3dscorestr.style.width = parseInt(104 * size);
 diskscorestr.style.width = parseInt(104 * size);
 winsprlevelscorestr.style.width = parseInt(104 * size);

 //left
 cpuscore.style.left = parseInt(106 * size);
 memoryscore.style.left = parseInt(106 * size);
 graphicsscore.style.left = parseInt(106 * size);
 d3dscore.style.left = parseInt(106 * size);
 diskscore.style.left = parseInt(106 * size);
 winsprlevelscore.style.left = parseInt(106 * size);
 infoscorestr.style.left = parseInt(8 * size);
 WinSATplace.style.left = parseInt(140 * size);
 WinSATscore.style.left = parseInt(144 * size);
 WinSATscoreSH.style.left = parseInt(146 * size);

 //font-size
 gtitle.style.fontSize = parseInt(12 * size);
 cpuscorestr.style.fontSize = parseInt(12 * size);
 cpuscore.style.fontSize = parseInt(12 * size);
 memoryscorestr.style.fontSize = parseInt(12 * size);
 memoryscore.style.fontSize = parseInt(12 * size);
 graphicsscorestr.style.fontSize = parseInt(12 * size);
 graphicsscore.style.fontSize = parseInt(12 * size);
 d3dscorestr.style.fontSize = parseInt(12 * size);
 d3dscore.style.fontSize = parseInt(12 * size);
 diskscorestr.style.fontSize = parseInt(12 * size);
 diskscore.style.fontSize = parseInt(12 * size);
 winsprlevelscorestr.style.fontSize = parseInt(14 * size);
 winsprlevelscore.style.fontSize = parseInt(14 * size);
 infoscorestr.style.fontSize = parseInt(11 * size);
 closeflyout.style.fontSize = parseInt(10 * size);
 WinSATscore.style.fontSize = parseInt(46 * size);
 WinSATscoreSH.style.fontSize = parseInt(46 * size);

 //picture
 WinSATpix.style.width = parseInt(81 * size);
 WinSATpix.style.height = parseInt(81 * size);

 gtitle.innerText = flylng[lngID][0];
 cpuscorestr.innerText = flylng[lngID][1];
 memoryscorestr.innerText = flylng[lngID][2];
 graphicsscorestr.innerText = flylng[lngID][3];
 d3dscorestr.innerText = flylng[lngID][4];
 diskscorestr.innerText = flylng[lngID][5];
 winsprlevelscorestr.innerText = flylng[lngID][6];
 closeflyoutstr.innerText = flylng[lngID][7];

 closeflyouta.blur();
 System.Gadget.document.parentWindow.enablegetweifromwmiflag ? getWEIfromWMI() : getWEIfromXML();
}

function getWEIfromXML(){
 var last = "";
 var lastfn = "";
 result = 0;
 path = System.Environment.getEnvironmentVariable("WINDIR") + "\\Performance\\WinSAT\\DataStore\\"
 try{
  var fc = new Enumerator(fso.GetFolder(path).Files);
  for (; !fc.atEnd(); fc.moveNext()){
   var name = fc.item() + ""; //convert to string
   var where = name.indexOf("Formal.Assessment");
   if (where != -1){
    var filedata = name.substring(path.length,path.length+23)
    if (last < filedata){
     last = filedata;
     lastfn = name;
     result = 2;
    }
   }
  }//for
 }//try
 catch (e){result = 1}

 weixmllastfile = lastfn;
 if (result == 0){
  infoscorestr.innerText = flylng[lngID][8];
  showWMIerror();
 }
 else if (result == 1){
  infoscorestr.innerText = flylng[lngID][9];
  showWMIerror();
 }
 else{
  var readstats = false;
  xmlDoc.async = "false";
  readstats = xmlDoc.load(weixmllastfile);
  if (readstats){
   var t = xmlDoc.xml;
   var x = xmlDoc.getElementsByTagName("WinSPR")[0];
   cpuscore.innerText = x.getElementsByTagName("CpuScore")[0].childNodes[0].nodeValue;
   memoryscore.innerText = x.getElementsByTagName("MemoryScore")[0].childNodes[0].nodeValue;
   graphicsscore.innerText = x.getElementsByTagName("GraphicsScore")[0].childNodes[0].nodeValue;
   d3dscore.innerText = x.getElementsByTagName("GamingScore")[0].childNodes[0].nodeValue;
   diskscore.innerText = x.getElementsByTagName("DiskScore")[0].childNodes[0].nodeValue;
   var rating = x.getElementsByTagName("SystemScore")[0].childNodes[0].nodeValue;
   winsprlevelscore.innerText = rating;
   WinSATscore.innerText = rating.charAt(0) + "," + rating.charAt(2);
   infoscorestr.innerText = flylng[lngID][10];
  }
  else{
   infoscorestr.innerText = flylng[lngID][9];
   showWMIerror();
  }
 }
}

function showWMIerror(){
  cpuscore.innerText = flylng[lngID][11];
  memoryscore.innerText = flylng[lngID][11];
  graphicsscore.innerText = flylng[lngID][11];
  d3dscore.innerText = flylng[lngID][11];
  diskscore.innerText = flylng[lngID][11];
  winsprlevelscore.innerText = flylng[lngID][11];
  WinSATscore.style.left = parseInt(170 * size);
  WinSATscoreSH.style.left = parseInt(172 * size);
  WinSATscore.innerText = "?";
}

function getWEIfromWMI(){
 try{
  var datascore = objWMIService.ExecQuery("SELECT * FROM Win32_WinSAT");
  var scoredata = new Enumerator(datascore).item();
  var scorecpu = scoredata.CPUScore.toFixed(1);
  var scoremem = scoredata.MemoryScore.toFixed(1);
  var scoregraph = scoredata.GraphicsScore.toFixed(1);
  var scored3d = scoredata.D3DScore.toFixed(1);
  var scoredisk = scoredata.DiskScore.toFixed(1);
  var rating = scoredata.WinSPRLevel.toFixed(1);
  var res;
  switch (scoredata.WinSATAssessmentState){
   case 0:
    res = flylng[lngID][12];
    break;
   case 1:
    res = flylng[lngID][13];
    break;
   case 2:
    res = flylng[lngID][14];
    break;
   case 3:
    res = flylng[lngID][15];
    break;
   case 4:
    res = flylng[lngID][16];
    break;
  }
  cpuscore.innerText = scorecpu;
  memoryscore.innerText = scoremem;
  graphicsscore.innerText = scoregraph;
  d3dscore.innerText = scored3d;
  diskscore.innerText = scoredisk;
  winsprlevelscore.innerText = rating;
  WinSATscore.innerText = rating.charAt(0) + "," + rating.charAt(2);
  infoscorestr.innerText = res;
 }
 catch (err) {
  infoscorestr.innerText = flylng[lngID][17];
  showWMIerror();
  }
 WinSATscoreSH.innerText = WinSATscore.innerText;
}