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

var ini_file = cFos_object.ini_file;
var items = new Object();
ml = new ml_object();
optk = new Array(5);
var brim_regexp = new RegExp("^\\s+|\\s+$", "g");

function btrim(s) {
   return s.replace(brim_regexp, "");
}

function get_used(opt, optk, is_protocols) {
   var sn = is_protocols ? "used_protocols" : "used_programs";
   var f = is_protocols ? "pt" : "pg";
   ini_file.open("global.ini", sn);
   var cnt = ini_file.length;
   for (var i=0; i < cnt; ++i) {
      var key = ini_file.key(i);
      if (items[f][key]) opt.push(items[f][key]);
      else opt.push(key);
      optk.push(key);
   }
}

function get_tot(num, is_rx) {
   var val = String(cFos_object.param((is_rx ? "graph_tot_rx" : "graph_tot_tx") + num));
   function insert(i) { return val.substr(0, i) + "," + val.substr(i); }
   for (var i=val.length-3; i > 0; i -= 3) val = insert(i);
   return val;
}


function upd_graph(full) {
//   $('#loading').show();
   function do_upd() {
      for (var i=1; i <= 4; ++i) {
         cFos_object.param("graph_tot_rx"+i) = "";
         cFos_object.param("graph_tot_tx"+i) = "";
      }
      cFos_object.action(full ? "bgraph_full" : "bgraph");
      var d = new Date();
      $('#uimg').attr('src', "images/usage.bmp?" + d.getTime());
      $('#total1r').html(get_tot(1, true));
      $('#total2r').html(get_tot(2, true));
      $('#total3r').html(get_tot(3, true));
      $('#total4r').html(get_tot(4, true));
      $('#total1t').html(get_tot(1, false));
      $('#total2t').html(get_tot(2, false));
      $('#total3t').html(get_tot(3, false));
      $('#total4t').html(get_tot(4, false));
//      $('#loading').hide();
   }
   window.setTimeout(do_upd, 100);
}

function write_selection(num) {
   var idx = document.getElementById("s"+num).selectedIndex;
   cFos_object.param("graph_fsel"+num) = idx;
   cFos_object.param("graph_sel"+num) = optk[num][document.getElementById("i"+num).selectedIndex];
   upd_graph(false);
}

function do_set_item_sel(num) {
   var s = document.getElementById("s"+num);
   var it = document.getElementById("i"+num);
   optk[num] = new Array();
   var opt = new Array("---");
   optk[num].push("---");
   switch (s.selectedIndex) {
   case 1:
      opt.push(ml.bgraph_totals);
      optk[num].push("total");
      break;
   case 2:
      for (var i=1; i < 11; ++i) {
         opt.push(ml.budget + " " + i);
         optk[num].push(i);
      }
      break;
   case 3:
      get_used(opt, optk[num], true);
      break;
   case 4:
      get_used(opt, optk[num], false);
      break;
   }
   it.options.length = 0; // clear old options
   for (i=0; i < opt.length; ++i) it.options[i] = new Option(opt[i], "");
   it.selectedIndex = 0;
}

function set_item_sel(num) {
   $('#debug_item').text(num);
   do_set_item_sel(num);
   write_selection(num);
}

function write_range() {
   cFos_object.param("graph_range") = document.getElementById("rg_sel").selectedIndex;
   upd_graph(false);
}

function read_selection(num) {
   var idx = cFos_object.param("graph_fsel"+num);
   if (idx == "") return;
   document.getElementById("s"+num).selectedIndex = idx;
   do_set_item_sel(num);
   var k = cFos_object.param("graph_sel"+num);
   for (var i=0; i < optk[num].length; ++i) {
      if (k == optk[num][i]) {
         document.getElementById("i"+num).selectedIndex = i;
         break;
      }
   }
}

function read_items(iname, sec_name) {
   var prefix = sec_name.indexOf("prots") != -1 ? "pt" : "pg";
   ini_file.open(iname, sec_name);
   var cnt = ini_file.length;
   for (var i=0; i < cnt; ++i) {
      var key = ini_file.key(i);
      var val = ini_file.value(key);
      var m = val.match(/\s*([\w\s]*)\s*,\s*(.*)\s*/);
      var desc = "";
      if (m != null) desc = m[2];
      else desc = key;
      if (!items[prefix]) items[prefix] = new Object;
      items[prefix][key] = desc;
   }
}

function set_mode(m) {
   cFos_object.param("graph_rxtx") = m;
   upd_graph(false);
}

function set_sum(v) {
   cFos_object.param("graph_sum") = v;
   upd_graph(false);
}

function resize_divs() {
    $('#dlg').width($(window).width() - 270);
    $('#dlg').height($(window).height());
    $('#menu').height($(window).height());
}

function dialog() {
   read_items(def_ini_name, "higherprots");
   read_items(def_ini_name, "highprots");
   read_items(def_ini_name, "normalprots");
   read_items(def_ini_name, "lowprots");
   read_items(def_ini_name, "lowestprots");
   read_items(ini_name, "higherprots");
   read_items(ini_name, "highprots");
   read_items(ini_name, "normalprots");
   read_items(ini_name, "lowprots");
   read_items(ini_name, "lowestprots");

   read_items(def_ini_name, "higherprogs");
   read_items(def_ini_name, "highprogs");
   read_items(def_ini_name, "normalprogs");
   read_items(def_ini_name, "lowprogs");
   read_items(def_ini_name, "lowestprogs");
   read_items(ini_name, "higherprogs");
   read_items(ini_name, "highprogs");
   read_items(ini_name, "normalprogs");
   read_items(ini_name, "lowprogs");
   read_items(ini_name, "lowestprogs");

   read_items(def_ini_name, "descprogs");
   read_items(ini_name, "descprogs");

   if (cFos_object.param("graph_fsel1") == "" && cFos_object.param("graph_fsel2") == ""
      && cFos_object.param("graph_fsel3") == "" && cFos_object.param("graph_fsel4") == "") {
      cFos_object.param("graph_fsel1") = "1";
      cFos_object.param("graph_sel1") = "total";
   }
   for (var i=0; i < 4; ++i) read_selection(i+1);
   var sel = cFos_object.param("graph_range");
   if (btrim(sel) == "") sel = 3;
   var e = $("#rg_sel");
   e[0].selectedIndex = sel;
   var m = btrim(cFos_object.param("graph_rxtx"));
   if (m == "") m = 3;
   document.getElementsByName("rxtx")[m-1].checked = true;
   $("#sum")[0].checked = cFos_object.param("graph_sum") != 0;
   upd_graph(true);
   e.focus();
   $('h2').wrapInner('<span class="h2_text"></span>');
   var current_mode= $("input[name='rxtx']:checked")[0];
   $('input[name="rxtx"]').change(function(event) {
      var new_mode= $('input[name="rxtx"]:checked')[0];
      if (new_mode == current_mode)
         return;
      else {
         set_mode(new_mode.value);
         current_mode = new_mode;
      }
   });
   $('#sum').change(function(event) {
      set_sum($('#sum').attr('checked') ? 1 : 0);
   });
}

window.onresize = resize_divs;
