var g_sError = "insufficient number of tests were done.";
var g_pBatteryTests = BuildBatteryTestsHash();
var g_pStdChain = null;
var g_pChain = null;

function BuildBatteryTestsHash()
{
  var pObj = new Object();

  for (var i = 0; i < g_pBatteryTestsList.length; i++)
  {
    var pInfo = g_pBatteryTestsList[i];

    pObj[pInfo.m_nodeName] = pInfo;
  }

  return pObj;
}

function GenerateReportByList(pSec, pTestsList, bUseStd)
{
  var res = "";
  var nNumCols = bUseStd ? 3 : 2;

  res += "<table class='ctable' border=1>";

  res += "<tr><td class='ct'><b>Test</b></td><td class='ct'><b>Time</b></td><td class='ct'><b>Speed</b></td><td class='ct'><b>% of iPAQ 3650<sup>*</sup> speed</b></td></tr>";

  for (var i = 0; i < pTestsList.length; i++)
  {
    var pInfo = pTestsList[i];
    var testEl = pSec.selectSingleNode(pInfo.m_nodeName);

    if (testEl == null)
      continue;

    res += "<tr>"
      + "<td class='ct' nowrap><a href='' onclick=\"OpenHelpWindow('" + pInfo.m_nodeName + "'); return false\">" + pInfo.m_name + "</a>&nbsp;</td>";

    var nValue = parseFloat(testEl.text);

    if (!isNaN(nValue) && nValue > 0)
    {
      res += "<td class='ct'>" + SmartFloatToString(nValue, 0) + " ms" + "&nbsp;</td>"
        + "<td class='ct'>" + SmartFloatToString(pInfo.m_coef * 1.0 / nValue, 0) + " " + pInfo.m_unit + "&nbsp;</td>";

      if (bUseStd)
      {
        res += "<td class='ct'>" + FloatToString(pInfo.m_std * 100.0 / nValue, 0) + "%&nbsp;</td>";
      }
    }
    else
    {
      res += "<td class='ct' align='center' colspan=" + nNumCols + "><font color='#FF0000'>" + testEl.text + "&nbsp;</font></td>";
    }

    res += "</tr>";
  }

  res += "</table>";
  res += "<p><sup>*</sup>iPAQ 3650 running Pocket PC 2000</p>";

  return res;
}

function GenerateReportForMain(pSec)
{
  do {

   if (pSec == null)
     break;

  var res = "";
  var bUseStd = g_pStdChain && g_pStdChain.m_pHead && g_pStdChain.m_pHead.m_bOk;

  res += "<h4>Main test results</h4>";
  res += GenerateReportByList(pSec, g_pMainTests, bUseStd);

  document.all.main_tests_results.innerHTML = res;
  document.all.main_tests_noresult.style.display = "none";
  document.all.main_tests_results.style.display = "";
  return;

  } while (false);

  document.all.main_tests_results.style.display = "none";
  document.all.main_tests_noresult.style.display = "";
}

function GenerateReportFor1SC(pSec)
{
  do {

   if (pSec == null)
     break;

  var res = "";
  var pCardNameSec = pSec.selectSingleNode("@card-name");

  if (!pCardNameSec || pCardNameSec.text == "")
    break;

  var pStdRootSec = g_pStdChain.m_pHead.m_pObjDoc.documentElement;
  var pStdMainSec = pStdRootSec.selectSingleNode("main-tests");
  var dStorageCardIndex = g_pStorageCardIndex.DoCalculate(pSec, pStdMainSec);

  res += "<h5>Storage card \"" + pCardNameSec.text + "\"</h5>";
  res += "<p>";

  if (dStorageCardIndex < 0)
  {
    res += g_sError;
  }
  else
  {
    res += "Speed index = " + FloatToString(dStorageCardIndex, 2);
  }

  res += "</p>";
  res += GenerateReportByList(pSec, g_pStorageCardTests, false);

  return res;

  } while (false);

  return "";
}

function GenerateReportForStorageCards(pSec)
{
  do {

  if (pSec == null)
      break;
   
  var res = "";

  res += "<h4>Storage card test results</h4>";

  var pList = pSec.selectNodes("storagecard-test");

  if (!pList)
    break;

  var bListEmpty = true;

  for (var i = 0; i < pList.length; i++)
  {
    var pCardSec = pList[i];
    var res1SC = GenerateReportFor1SC(pCardSec);

    if (res1SC == "")
      continue;

    res += res1SC;

    bListEmpty = false;
  }

  if (bListEmpty)
    break;

  document.all.storagecard_tests_results.innerHTML = res;
  document.all.storagecard_tests_noresult.style.display = "none";
  document.all.storagecard_tests_results.style.display = "";
  return;

  } while (false);

  document.all.storagecard_tests_results.style.display = "none";
  document.all.storagecard_tests_noresult.style.display = "";
}

function ChargeRecord(time, value)
{
  this.m_time = time;
  this.m_value = value;
}

function GenerateReportForBattery1Test(pSec)
{
  do {

   if (pSec == null)
     break;

  var res = "";
  var pTypeSec = pSec.selectSingleNode("@type");

  if (!pTypeSec || pTypeSec.text == "")
    break;

  var pInfo = g_pBatteryTests[pTypeSec.text];

  if (!pInfo)
    break;

  var pTimeSec = pSec.selectSingleNode("result-time");

  if (!pTimeSec || pTimeSec.text == "")
    break;

  var nTime = parseFloat(pTimeSec.text);

  if (isNaN(nTime) || nTime <= 0)
    break;

  nTime = Math.round(nTime / 60000); // in minutes

  res += "<h5>Battery test \"" + pInfo.m_name + "\"</h5>";
  res += "<div class='techdiv'><p>";
  res += "Battery life time: " + Math.floor(nTime / 60) + " hours " + Math.floor(nTime % 60) + " min</p>";

  var pRecords = null;
  var pList = null;
  var pGraphSec = pSec.selectSingleNode("charge-graph");

  if (pGraphSec)
    pList = pGraphSec.selectNodes("record");

  if (pList)
  {
    var pRecords = new Array();
    var j = 0;
    var nTimeMin = null;
    var nTimeMax = null;

    for (var i = 0; i < pList.length; i++)
    {
      var pValue = pList[i];
      var pTimeSec = pValue.selectSingleNode("@time");
      var pValueSec = pValue.selectSingleNode("@value");

      if (!pTimeSec || pTimeSec.text == "" || !pValueSec || pValueSec.text == "")
        continue;

      var nTime = parseInt(pTimeSec.text, 10);
      var nValue = parseInt(pValueSec.text, 10);

      if (isNaN(nTime) || nTime < 0 || isNaN(nValue) || nValue < 0 || nValue > 100)
        continue;

      pRecords[j++] = new ChargeRecord(nTime, nValue);

      if (nTimeMin == null || nTime < nTimeMin)
        nTimeMin = nTime;

      if (nTimeMax == null || nTime > nTimeMax)
        nTimeMax = nTime;
    }

    if (pRecords.length < 2)
      pRecords = null;
  }

  nTimeMin = 0;

  if (pRecords && nTimeMin < nTimeMax)
  {
    var nGraphLoX = 46;
    var nGraphLoY = 73;
    var nGraphWidth = 337;
    var nGraphHeight = 210;
    var nLineWidth = 3;
    var cGraphColor = '#D00000';
    var cLineColor = '#73D073';

    res += "<div class='techdiv' style='position:relative; margin-left:16px'>";

    for (var i = 0; i < pRecords.length; i++)
    {
      var pRecord = pRecords[i];

      pRecord.m_x = nGraphLoX + Math.round((pRecord.m_time - nTimeMin) * nGraphWidth / (nTimeMax - nTimeMin));
      pRecord.m_y = nGraphLoY + Math.round((100 - pRecord.m_value) * nGraphHeight / 100);
    }

    res += "<div class='techdiv' style='position:absolute; left:0; top:0'>";
    res += "<img border=0 width=400 height=300 src='images/battery-background.gif'><br><img border=0 width=400 height=74 src='images/"+pInfo.m_nodeName+".gif'>";

    for (var i = 1; i <= 9; i++)
    {
      var nTime = i * 3600;

      if (nTime < nTimeMin || nTime > nTimeMax)
        continue;

      var nX = nGraphLoX + Math.round((nTime - nTimeMin) * nGraphWidth / (nTimeMax - nTimeMin));

      res += "<div class='techdiv' style='position:absolute; left:" + (nX - 18) + "; top:" + (nGraphLoY + nGraphHeight + 2) + ";'>";
      res += "<img border=0 width=37 height=12 src='images/timer" + i + "_00.gif'>";
      res += "</div>";

      res += "<div class='techdiv' style='position:absolute; left:" + nX + "; top:" + nGraphLoY + ";'>";
      res += "<table width=1 height=" + nGraphHeight + " border=0 cellpadding=0 cellspacing=0 bgcolor='" + cLineColor + "'><tr><td></td></tr></table>";
      res += "</div>";
    }

    for (var i = 20; i < 100; i += 20)
    {
      res += "<div class='techdiv' style='position:absolute; left:" + nGraphLoX + "; top:" + (nGraphLoY + i * nGraphHeight / 100) + ";'>";
      res += "<table width=" + nGraphWidth + " height=1 border=0 cellpadding=0 cellspacing=0 bgcolor='" + cLineColor + "'><tr><td></td></tr></table>";
      res += "</div>";
    }

    res += "</div>";

    for (var i = 0; i < pRecords.length - 1; i++)
    {
      var nX1 = pRecords[i].m_x;
      var nX2 = pRecords[i + 1].m_x;
      var nY1 = pRecords[i].m_y;
      var nY2 = pRecords[i + 1].m_y;

      res += "<div class='techdiv' style='position:absolute; left:" + nX1 + "; top:" + nY1 + ";'>";
      res += "<table width=" + (nX2 - nX1) + " height=" + nLineWidth + " border=0 cellpadding=0 cellspacing=0 bgcolor='" + cGraphColor + "'><tr><td></td></tr></table>";
      res += "</div>";

      if (i == pRecords.length - 2)
        break;

      if (nY1 > nY2)
      {
        var nY = nY1;
        nY1 = nY2 + nLineWidth;
        nY2 = nY + nLineWidth;
      }

      res += "<div class='techdiv' style='position:absolute; left:" + nX2 + "; top:" + nY1 + ";'>";
      res += "<table width=" + nLineWidth + " height=" + (nY2 - nY1) + " border=0 cellpadding=0 cellspacing=0 bgcolor='" + cGraphColor + "'><tr><td></td></tr></table>";
      res += "</div>";
    }

    res += "</div>";
    res += "<table height=374><tr><td></td></tr></table>";
    res += "</div>";
  }

  return res;

  } while (false);

  return "";
}

function GenerateReportForBattery(pSec)
{
  do {

   if (pSec == null)
     break;

  var res = "";

  res += "<h4>Battery test results</h4>";

  var pList = pSec.selectNodes("battery-test");

  if (!pList)
    break;

  var bListEmpty = true;

  for (var i = 0; i < pList.length; i++)
  {
    var pTestSec = pList[i];
    var res1Test = GenerateReportForBattery1Test(pTestSec);

    if (res1Test == "")
      continue;

    res += res1Test;

    bListEmpty = false;
  }

  if (bListEmpty)
    break;

  document.all.battery_tests_results.innerHTML = res;
  document.all.battery_tests_noresult.style.display = "none";
  document.all.battery_tests_results.style.display = "";
  return;

  } while (false);

  document.all.battery_tests_results.style.display = "none";
  document.all.battery_tests_noresult.style.display = "";
}

function GenerateReportIndexes()
{
  var pRootSec = g_pChain.m_pHead.m_pObjDoc.documentElement;
  var pStdRootSec = g_pStdChain.m_pHead.m_pObjDoc.documentElement;

  var dSpbIndex = g_pSpbIndex.Calculate(pRootSec, pStdRootSec);
  var dCPUIndex = g_pCPUIndex.Calculate(pRootSec, pStdRootSec);
  var dFileSystemIndex = g_pFileSystemIndex.Calculate(pRootSec, pStdRootSec);
  var dGraphicsIndex = g_pGraphicsIndex.Calculate(pRootSec, pStdRootSec);
  var dActiveSyncIndex = g_pActiveSyncIndex.Calculate(pRootSec, pStdRootSec);
  var dPlatformIndex = g_pPlatformIndex.Calculate(pRootSec, pStdRootSec);

  var res = "";

  res += "<h4>Spb Benchmark Indices</h4>";
  res += "<table class='ctable' border=1>";

  res += "<tr><td class='ct'><a href='' onclick=\"OpenHelpWindow('index-spb'); return false\"><font color='#FF0000'><b>" + g_pSpbIndex.m_name + "</b></font></a></td><td class='ct'>";
  if (dSpbIndex < 0)
  {
    res += g_sError;
  }
  else
  {
    res += FloatToString(dSpbIndex, 2);
  }
  res += "</td><td class='ct'>(iPAQ 3650 scored 1000)</td></tr>";

  res += "<tr><td class='ct'><a href='' onclick=\"OpenHelpWindow('index-cpu'); return false\">" + g_pCPUIndex.m_name + "</a></td><td class='ct'>";
  if (dCPUIndex < 0)
  {
    res += g_sError;
  }
  else
  {
    res += FloatToString(dCPUIndex, 2);
  }
  res += "</td><td class='ct'>(iPAQ 3650 scored 1000)</td></tr>";

  res += "<tr><td class='ct'><a href='' onclick=\"OpenHelpWindow('index-filesystem'); return false\">" + g_pFileSystemIndex.m_name + "</a></td><td class='ct'>";
  if (dFileSystemIndex < 0)
  {
    res += g_sError;
  }
  else
  {
    res += FloatToString(dFileSystemIndex, 2);
  }
  res += "</td><td class='ct'>(iPAQ 3650 scored 1000)</td></tr>";

  res += "<tr><td class='ct'><a href='' onclick=\"OpenHelpWindow('index-graphics'); return false\">" + g_pGraphicsIndex.m_name + "</a></td><td class='ct'>";
  if (dGraphicsIndex < 0)
  {
    res += g_sError;
  }
  else
  {
    res += FloatToString(dGraphicsIndex, 2);
  }
  res += "</td><td class='ct'>(iPAQ 3650 scored 1000)</td></tr>";

  res += "<tr><td class='ct'><a href='' onclick=\"OpenHelpWindow('index-activesync'); return false\">" + g_pActiveSyncIndex.m_name + "</a></td><td class='ct'>";
  if (dActiveSyncIndex < 0)
  {
    res += g_sError;
  }
  else
  {
    res += FloatToString(dActiveSyncIndex, 2);
  }
  res += "</td><td class='ct'>(iPAQ 3650 scored 1000)</td></tr>";

  res += "<tr><td class='ct'><a href='' onclick=\"OpenHelpWindow('index-platform'); return false\">" + g_pPlatformIndex.m_name + "</a></td><td class='ct'>";
  if (dPlatformIndex < 0)
  {
    res += g_sError;
  }
  else
  {
    res += FloatToString(dPlatformIndex, 2);
  }
  res += "</td><td class='ct'>(iPAQ 3650 scored 1000)</td></tr>";

  res += "</table>";

  document.all.indexes_results.innerHTML = res;
  document.all.indexes_results.style.display = "";
}

function SaveStdValues()
{
  if (!g_pStdChain.m_pHead || !g_pStdChain.m_pHead.m_bOk)
    g_pStdChain = null;

  var pMainSec = null;  

  if (g_pStdChain)
  {
    var pRootSec = g_pStdChain.m_pHead.m_pObjDoc.documentElement;

    if (!(pMainSec = pRootSec.selectSingleNode("main-tests")))
      g_pStdChain = null;
  }

  if (g_pStdChain)
  {
    for (var i = 0; i < g_pMainTests.length; i++)
    {
      var pInfo = g_pMainTests[i];
      var pTestSec = pMainSec.selectSingleNode(pInfo.m_nodeName);
    
      pInfo.m_std = 0;
    
      if (pTestSec)
      {
        var nValue = parseFloat(pTestSec.text);
    
        if (!isNaN(nValue) && nValue > 0)
          pInfo.m_std = nValue;
      }
    }
  }

  GenerateReport();
}

function HTMLDeviceName()
{
  var res = "";

  var strName = g_pChain.m_pHead.m_pHeader.m_strName;

  res += "<h3>" + strName + "</h3>";

  return res;
}

function GenerateReport()
{
  if (!g_pChain || !g_pStdChain)
    return;

  var pRootSec = g_pChain.m_pHead.m_pObjDoc.documentElement;
  var pStdRootSec = g_pStdChain.m_pHead.m_pObjDoc.documentElement;

  ClearIndexes();

  document.all.d_report.style.display = "";
  document.all.d_name.innerHTML = HTMLDeviceName();
  GenerateReportIndexes(pRootSec, pStdRootSec);
  GenerateReportForMain(pRootSec.selectSingleNode("main-tests"));
  GenerateReportForStorageCards(pRootSec.selectSingleNode("storagecard-tests"));
  GenerateReportForBattery(pRootSec.selectSingleNode("battery-tests"));
}

function StartGenerateReport()
{
  if (!checkSecuritySettings())
    return;

  if (g_pStdChain && g_pStdChain.m_nState == DPS_IN_PROGRESS)
    return;

  g_pChain = new DeviceParsersChain(null, null);

  g_pChain.AddXML(document.all.devicexml.value);
  g_pChain.Run();

  if (!g_pChain.m_pHead || !g_pChain.m_pHead.m_bOk)
  {
    g_pChain = null;
    alert("Cannot parse XML text");
    return;
  }

  if (!g_pStdChain)
  {
    g_pStdChain = new DeviceParsersChain(SaveStdValues, null);
    g_pStdChain.AddURL("http://spb.com/ext/benchmark/devices/iPAQ3600.xml");
    g_pStdChain.Run();
  }
  else
    GenerateReport();
}

function CompareWithOther()
{
  document.all.form_editor.submit();
}

function DoLoad()
{
  if (document.all.devicexml.value != "")
  {
    document.all.d_editor.style.display = "none";
    document.all.d_description.style.display = "inline";
    document.all.s_editor.innerText = "Performance Report";
    StartGenerateReport();
  }
  else
  {
    document.all.d_editor.style.display = "inline";
    document.all.d_description.style.display = "none";
    document.all.s_editor.innerHTML = "Summary";
    document.all.devicexml.focus();
  }
}

function DoUnload()
{
  CloseHelpWindow();
}

