
var g_bNoChange = false;


var cFos_object;
try {
   cFos_object = new ActiveXObject(ole_object);
}
catch(x) {
   var m = new ml_object;
   alert(ole_object + "\n\n" + x.message + "\n\n" + m.ole_error);
   window.close();
}

var ini = cFos_object.ini_file;
var show_prio_id = true;
var filter_key = "program_filter";
var prio_table_id = "programs";
var prio1_section = "lowestprogs";
var prio2_section = "lowprogs";
var prio3_section = "normalprogs";
var prio4_section = "highprogs";
var prio5_section = "higherprogs";

var np_prio = 3;
var ports = 0;


function set_np_prio(p, pname) {
   np_prio = p;
   get_element("npname").innerHTML = pname;
}

function add_np() 
{
   window.focus();
   var keyText = exefile.innerText;
   if( keyText=="" ) return;

   g_bNoChange = true;
   var key = get_element("exefile").value;
   key = btrim(key);

   if (prio_items[key])
   {
      var s = ml.overwrite.replace(/#p/, key);
      if (!confirm(s)) return;
      delete prio_items[key];
   }

   prio_items[key] = new prio_obj(current_filter, btrim(get_element("desc").value), np_prio, ini_name);
   load_table();
}

function browse_for_file() 
{
    var f = cFos_object.open_file_dialog("*.exe\0*.exe\0*.*\0*.*\0", 0);
    var bCheckExe = f.search(".exe") 
    if( f!="" && bCheckExe!=-1 )
    { 
       get_element("exefile").value = f.replace(/^.*\\/, "");
       exefile.innerText = get_element("exefile").value ;

       var NoExe = get_element("exefile").value;
       NoExe = NoExe.replace(/.exe/i, "");

       get_element("desc").value = NoExe;
       desc.innerText = get_element("desc").value ;
    }   
}

function save_settings() 
{
   save_prio_settings();
}

function initBmp() 
{
  var p = cFos_object.ports;   
  if(p!=1) 
    return;

  if (p != ports) 
  {  
         ports = p;
         var s = "";
         for (var i=0; i < p; ++i) 
	 {
            cFos_object.port_open(i, false);
            cFos_object.port_start_disp_timer(i, true);
            s += "<img class=\"im\" id=\"i" + i + "\" /><div class=\"desc\" id=\"d" + i + "\"></div>";
         }
         document.getElementById("d").innerHTML = s;
  }
      
  document.body.style.height = document.getElementById("d").clientHeight;

  for (var i=0; i < p; ++i) 
  {
         var el = document.getElementById("i"+i);
         el.src = "";	 
         el.src = cFos_object.port_skin_refresh(i, "gadget"+i+".png");
         //document.getElementById("d"+i).innerText = cFos_object.port_name(i);
         // you can also use cFos_object.port_skin_variables(i) to get a string with all skin variables
  }

  window.setTimeout("initBmp()", 500);           
}

function uninitBmp() 
{
      var p = cFos_object.ports;
      for (var i=0; i < p; ++i) 
          cFos_object.port_start_disp_timer(i, false);
}   

function get_ip_address(wmi, idx) 
{
   var inst = wmi.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE InterfaceIndex = '" + idx + "'");
   e = new Enumerator(inst);
   var n = e.item();   // should be only one
   return n.IPAddress(0);
}

function get_adapter_addresses(name) 
{
   var w = new ActiveXObject("WbemScripting.SWbemLocator");
   var wmi = w.ConnectServer(".", "root\\cfos"); 
   e = new Enumerator(wmi.InstancesOf("cfosspeed_perf_data"));
   if (!e.atEnd()) 
   {
      var it = e.item();
      return { mac: it.interface_mac, ip: it.interface_ip };
   }
   return { mac: "", ip: "" };

/*
   var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}");
   e = new Enumerator(wmi.InstancesOf("Win32_NetworkAdapter"));
   for(; !e.atEnd(); e.moveNext()) 
   {
      var s = e.item();
      if (s.Name == name) {
         var ip = get_ip_address(wmi, s.InterfaceIndex);
         return { mac: s.MACAddress, ip: ip };
      }
   }
   return { mac: "", ip: "" };
*/
}

function GetRx_Tx() 
{
   var cFos_object_X = new ActiveXObject("cFosSpeed.Config");
   var v = cFos_object_X.port_skin_variables(0);

   var a = v.split(',');
   var obj = { };
   for (var i=0; i < a.length; ++i) 
   {
      var f = a[i].split(':');
      obj[f[0]] = f[1];
   }
   var s = "";
   s += "rx = " + obj.rx_scale + " bytes/s" + "<br/>" ;// + "<br/>";
   s += "tx = " + obj.tx_scale + " bytes/s" +"<br/>";
   document.getElementById("stats").innerHTML = s;

   var param = GameFirstII_APP.commandLine.match(/[\w!#$%&'*+-/=?^_`{}~\\:]+|\"[^\"]*\"/g);
   for (var i=0; i < param.length; ++i) 
   {
       if (param[i].charAt(0) == '"') param[i] = param[i].substr(1, param[i].length-2);
   }
   homedir = param[0].substr(0, param[0].lastIndexOf('\\')+1);
   var el = document.getElementById("imageClock");
   var Value = obj.rx_scale;
   if( Value<=5000 )	
       el.src = homedir + "clock\\clock01.png"
   else if( Value<=10000 )	
       el.src = homedir + "clock\\clock02.png"
   else if( Value<=15000 )	
       el.src = homedir + "clock\\clock03.png"
   else if(Value<=20000 )	
       el.src = homedir + "clock\\clock04.png"
   else
       el.src = homedir + "clock\\clock05.png"

   var s2 = "";
   var s3 = "";
   s2 += "rx = " + obj.rx_cps + " bytes/s" ;
   s3 += "tx = " + obj.tx_cps + " bytes/s" ;
   document.getElementById("stats2").innerHTML = s2;
   document.getElementById("stats3").innerHTML = s3;
   document.getElementById("PingTime").innerHTML = obj.ping_time;

   // Current RX
   var g_nCurRateBasic = 200;

   // Current RX
   //var CurRX = obj.rx_cps;
   var eRX = document.getElementById("CurDownload");
   eRX.src = "";
   if( obj.rx_cps<=g_nCurRateBasic )	
       eRX.src = homedir + "usage\\bar_00.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*2) )	
       eRX.src = homedir + "usage\\bar_01.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*3) )		
       eRX.src = homedir + "usage\\bar_02.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*4) )		
       eRX.src = homedir + "usage\\bar_03.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*5) )	
       eRX.src = homedir + "usage\\bar_04.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*6) )	
       eRX.src = homedir + "usage\\bar_05.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*7) )	
       eRX.src = homedir + "usage\\bar_06.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*8) )		
       eRX.src = homedir + "usage\\bar_07.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*9) )		
       eRX.src = homedir + "usage\\bar_08.png"
   else if( obj.rx_cps<=(g_nCurRateBasic*10) )		
       eRX.src = homedir + "usage\\bar_09.png"
   else
       eRX.src = homedir + "usage\\bar_10.png"

   // Current TX
   var eTX = document.getElementById("CurUpload");
   eTX.src = "";
   if( obj.tx_cps<=g_nCurRateBasic )	
       eTX.src = homedir + "usage\\bar_00.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*2) )	
       eTX.src = homedir + "usage\\bar_01.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*3) )		
       eTX.src = homedir + "usage\\bar_02.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*4) )		
       eTX.src = homedir + "usage\\bar_03.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*5) )	
       eTX.src = homedir + "usage\\bar_04.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*6) )	
       eTX.src = homedir + "usage\\bar_05.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*7) )	
       eTX.src = homedir + "usage\\bar_06.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*8) )		
       eTX.src = homedir + "usage\\bar_07.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*9) )		
       eTX.src = homedir + "usage\\bar_08.png"
   else if( obj.tx_cps<=(g_nCurRateBasic*10) )		
       eTX.src = homedir + "usage\\bar_09.png"
   else
       eTX.src = homedir + "usage\\bar_10.png"

   window.setTimeout("GetRx_Tx()", 1000); 
}

function getInternetExplorerVersion()
{
  var rv = 1; // Return value assumes OK
  if( navigator.appName.indexOf("Internet Explorer") != -1 &&
      navigator.appVersion.indexOf("MSIE 6") != -1 )
  {
      rv = 0;
  }

  return rv;
}

function dialog() 
{
   window.focus();
   window.moveTo(-1000,-1000); 	

   var checkResult = getInternetExplorerVersion();
   if( checkResult==0 )
   {  
      window.alert("Please install Internet Explorer 7 or above version to use this function.");
      window.focus();
      window.setTimeout("self.close();",100);   
      return;
   }

   init_filter_translation();
   read_section(def_ini_name, prio5_section, 5);
   read_section(def_ini_name, prio4_section, 4);
   read_section(def_ini_name, prio3_section, 3);
   read_section(def_ini_name, prio2_section, 2);
   read_section(def_ini_name, prio1_section, 1);
   read_section(ini_name, prio5_section, 5);
   read_section(ini_name, prio4_section, 4);
   read_section(ini_name, prio3_section, 3);
   read_section(ini_name, prio2_section, 2);
   read_section(ini_name, prio1_section, 1);
  // if (!filters.other_name) filters[other_name] = true;

   var f = new Array();
   for (var x in filters) 
   {
      var o = new Object();
  
      o.trans_filter = translate_filter(x);
      o.filter = x;
      f[f.length] = o;
   }

   //fill_table_row("filters", 0, 0, f);

   current_filter = cFos_object.param(filter_key);
   if (!filters[current_filter]) {
      for (var x in filters) {
         current_filter = x;
         break;
      }
   }

   set_filter(current_filter);
   load_table();


   var cFos_object_X = new ActiveXObject("cFosSpeed.Config");
   var portNum = cFos_object.ports; 
   if( portNum<=0 )
   {
       //alert("faill open net card");
       return;
   }

   var name = cFos_object_X.port_name(0);
   document.getElementById("Card").innerHTML = name;
   var adr = get_adapter_addresses(name);
   document.getElementById("Mac").innerHTML = adr.mac;
   document.getElementById("IP").innerHTML = adr.ip;

   initBmp();
   GetRx_Tx();

}

function Push_Cancel() 
{
   window.focus();
   if( g_bNoChange )
       window.location.reload();
   else
   {
       get_element("exefile").value = "";
       exefile.innerText = get_element("exefile").value ;
       get_element("desc").value = "";
       desc.innerText = get_element("desc").value ;
   }
   g_bNoChange = false;
}

function Exec_ConnectionHTA_Console() 
{
   var param = Traffic_Shaping_Settings.commandLine.match(/[\w!#$%&'*+-/=?^_`{}~\\:]+|\"[^\"]*\"/g);
   for (var i=0; i < param.length; ++i) 
   {
       if (param[i].charAt(0) == '"') param[i] = param[i].substr(1, param[i].length-2);
   }
   homedir = param[0].substr(0, param[0].lastIndexOf('\\')+1);

   var sh = new ActiveXObject("Shell.Application");
   sh.ShellExecute("\"" + homedir + "console.hta"+ "\"");
}
