function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}

function ltrim(argvalue) {

  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }

  return argvalue;
}

function trim(argvalue) {
  var tmpstr = ltrim(argvalue);

  return rtrim(tmpstr);
}

function iniparse(inistring) {
  var tree = new Object;
  var lines = inistring.split('\n');
  var line;
  var l

  for (l = 0; l < lines.length; l++) {
    var line = lines[l];
    var i = line.indexOf("=");

    if(i!=0) {
      var name = line.substring(0, i);
      name = name.replace(/\./g,"_");
      name = trim(name.toLowerCase());
      var value = line.substring(i+1);
      value = value.replace(/"/g,' ');
      value = trim(value);

      if(name.length != 0) {
//        console.warn(line + '-> tree[' + name + '] = "' + value + '";');
        tree[name] = value;
      }
    }
  }

  return tree;
}

function readvmx() {
  var i, a, b, x, y, z;
  var initext = $('vmxin');
  var vmxform = $('vmxform');
  var drive = new Array("0", "1", "2", "3", "4", "5", "6", "8", "9", "a", "b", "c", "d", "e", "f");

  var tree = iniparse(initext.value);

//  if(tree['config_version']) 
//  if(tree['virtualhw_version']) 
//  if(tree['uuid_location']) 
//  if(tree['uuid_bios'])
//  if(tree['uuid_action'])
//  if(tree['checkpoint_vmState'])
    if(tree['displayname'])
      vmxform.displayname.value = tree['displayname'];
    if(tree['annotation'])
      vmxform.annotation.value = tree['annotation'];
    if(tree['guestinfo_vmware_product_long'])
      vmxform.productlong.value = tree['guestinfo_vmware_product_long'];
    if(tree['guestinfo_vmware_product_url'])
      vmxform.producturl.value = tree['guestinfo_vmware_product_long'];
    if(tree['guestos']) {
      for(i=0; i<vmxform.guestos.options.length; i++) {
        if(vmxform.guestos.options[i].value == tree['guestos'])
          vmxform.guestos.selectedIndex = i;
      }
    }
    if(tree['autostart']) {
      for(i=0; i<vmxform.autostart.options.length; i++) {
        if(vmxform.autostart.options[i].value == tree['autostart'])
          vmxform.autostart.selectedIndex = i;
      }
    }
    if(tree['autostop']) {
      for(i=0; i<vmxform.autostop.options.length; i++) {
        if(vmxform.autostop.options[i].value == tree['autostop'])
          vmxform.autostop.selectedIndex = i;
      }
    }
    if(tree['numvcpus'])
      vmxform.numvcpus.selectedIndex = parseInt(tree['numvcpus'])-1;
    if(tree['paevm'])
      vmxform.paevm.checked = (tree['paevm'].toLowerCase() == 'true');
    if(tree['sch_mem_pshare_enable'])
      vmxform.pshare.checked = (tree['sch_mem_pshare_enable'].toLowerCase() == 'true');
    if(tree['memallowautoscaledown'])
      vmxform.memallowautoscaledown.checked = (tree['memallowautoscaledown'].toLowerCase() == 'true');

    if(tree['nvram'])
      vmxform.nvram_filename.value = tree['nvram'];
    if(tree['svga_maxwidth']) {
      for(i=0; i<vmxform.svgamaxsize.options.length; i++) {
        if(vmxform.svgamaxsize.options[i].value == tree['svga_maxwidth']) {
          vmxform.svgamaxsize.selectedIndex = i;
          s_svgamaxsize.setValue(i);
        }
      }
    }
    if(tree['svga_vramsize']) {
      for(i=0; i<vmxform.vramsize.options.length; i++) {
        if(vmxform.vramsize.options[i].value == tree['svga_vramsize']) {
          vmxform.vramsize.selectedIndex = i;
          s_vramsize.setValue(i);
        }
      }
    }
    if(tree['mks_enable3d'])
      vmxform.mks_enable3d.checked = (tree['mks_enable3d'].toLowerCase() == 'true');
    if(tree['vmmouse_present'])
      vmxform.vmmouse_present.checked = (tree['vmmouse_present'].toLowerCase() == 'true');
    if(tree['tools_synctime'])
      vmxform.synctime.checked = (tree['tools_synctime'].toLowerCase() == 'true');
    if(tree['tools_remindinstall'])
      vmxform.remindinstall.checked = (tree['tools_remindinstall'].toLowerCase() == 'true');

    if(tree['isolation_tools_hgfs_disable'])
      vmxform.hgfsdisable.checked = (tree['isolation_tools_hgfs_disable'].toLowerCase() == 'true');
    if(tree['isolation_tools_dnd_disable'])
      vmxform.dnddisable.checked = (tree['isolation_tools_dnd_disable'].toLowerCase() == 'true');
    if(tree['isolation_tools_copy_enable'])
      vmxform.copydisable.checked = (tree['isolation_tools_copy_enable'].toLowerCase() == 'false');
    if(tree['isolation_tools_paste_enable'])
      vmxform.pastedisable.checked = (tree['isolation_tools_paste_enable'].toLowerCase() == 'false');

    if(tree['gui_restricted'])
      vmxform.guirestricted.checked = (tree['gui_restricted'].toLowerCase() == 'true');
    
    z = 0;
    for(x=0; x<4; x++) {
      if(tree['ethernet' + x + '_present']) {
        if(tree['ethernet' + x + '_connectiontype']) {
          a = tree['ethernet' + x + '_connectiontype'];
          b = vmxform['ethernet' + z + '_connectiontype'];
          for(i=0; i<b.options.length; i++) {
            if(b.options[i].value == a)
              b.selectedIndex = i;
          }
        }
        if(tree['ethernet' + x + '_address']) {
          if(tree['ethernet' + x + '_address'].substring(0,6) == '00:50:56') {
            vmxform['ethernet' + z + '_address_vendor'].selectedIndex = 0;
          } else {
            vmxform['ethernet' + z + '_address_vendor'].selectedIndex = 1;
          }
          vmxform['ethernet' + z + '_address_host'].value = tree['ethernet' + x + '_address'].substring(7);
        }
        z++;
      }
    }
    vmxform.ethcount.selectedIndex = z;
    s_ethcount.setValue(z);
    ethcount_onchange();

    if(tree['usb_present'])
      vmxform.usb_present.checked = (tree['usb_present'].toLowerCase() == 'true');
    if(tree['usb_generic_autoconnect'])
      vmxform.usb_generic_autoconnect.checked = (tree['usb_generic_autoconnect'].toLowerCase() == 'true');

    if(tree['sound_present'])
      vmxform.sound_present.checked = (tree['sound_present'].toLowerCase() == 'true');
    if(tree['sound_virtualdev']) {
      for(i=0; i<vmxform.sound_virtualdev.options.length; i++) {
        if(vmxform.sound_virtualdev.options[i].value == tree['svga_sound_virtualdev'])
          vmxform.sound_virtualdev.selectedIndex = i;
      }
    }

    for(y=0; y<2; y++) {
      z = 0;
      for(x=0; x<2; x++) {
        if(tree['ide' + y + ':' + x + '_present']) {
          if(tree['ide' + y + ':' + x + '_devicetype']) {
            a = tree['ide' + y + ':' + x + '_devicetype'];
            b = vmxform['ide' + y + '' + z + '_devicetype'];
            for(i=0; i<b.options.length; i++) {
              if(b.options[i].value == a)
                b.selectedIndex = i;
            }
          }
          if(tree['ide' + y + ':' + x + '_mode']) {
            a = tree['ide' + y + ':' + x + '_mode'];
            b = vmxform['ide' + y + '' + z + '_mode'];
            for(i=0; i<b.options.length; i++) {
              if(b.options[i].value == a)
                b.selectedIndex = i;
            }
          }
          if(tree['ide' + y + ':' + x + '_filename'])
            vmxform['ide' + y + '' + z + '_filename'].value = tree['ide' + y + ':' + x + '_filename'];
          if(tree['ide' + y + ':' + x + '_writethrough'])
            vmxform['ide' + y + '' + z + '_writethrough'].checked = (tree['ide' + y + ':' + x + '_writethrough'].toLowerCase() == 'true');
          if(tree['ide' + y + ':' + x + '_autodetect'])
            vmxform['ide' + y + '' + z + '_autodetect'].checked = (tree['ide' + y + ':' + x + '_autodetect'].toLowerCase() == 'true');
          if(tree['ide' + y + ':' + x + '_startconnected'])
            vmxform['ide' + y + '' + z + '_startconnected'].checked = (tree['ide' + y + ':' + x + '_startconnected'].toLowerCase() == 'true');

          z++;
        }
      }
      vmxform['ide' + y + 'count'].selectedIndex = z;
      eval('s_ide' + y + 'count.setValue(z);');
      idecount_onchange(y);
    }

    for(y=0; y<4; y++) {
      if(tree['scsi' + y + '_virtualdev']) {
        a = tree['scsi' + y + '_virtualdev'];
        b = vmxform['scsi' + y + '_virtualdev'];
        for(i=0; i<b.options.length; i++) {
          if(b.options[i].value == a)
            b.selectedIndex = i;
        }
      }
      z = 0;
      for(x=0; x<16; x++) {
        if(tree['scsi' + y + ':' + drive[x] + '_present']) {
          if(tree['scsi' + y + ':' + drive[x] + '_devicetype']) {
            a = tree['scsi' + y + ':' + drive[x] + '_devicetype'];
            b = vmxform['scsi' + y + '' + drive[z] + '_devicetype'];
            for(i=0; i<b.options.length; i++) {
              if(b.options[i].value == a)
                b.selectedIndex = i;
            }
          }
          if(tree['scsi' + y + ':' + drive[x] + '_mode']) {
            a = tree['scsi' + y + ':' + drive[x] + '_mode'];
            b = vmxform['scsi' + y + '' + drive[z] + '_mode'];
            for(i=0; i<b.options.length; i++) {
              if(b.options[i].value == a)
                b.selectedIndex = i;
            }
          }
          if(tree['scsi' + y + ':' + drive[x] + '_filename'])
            vmxform['scsi' + y + '' + drive[z] + '_filename'].value = tree['scsi' + y + ':' + drive[x] + '_filename'];
          if(tree['scsi' + y + ':' + drive[x] + '_writethrough'])
            vmxform['scsi' + y + '' + drive[z] + '_writethrough'].checked = (tree['scsi' + y + ':' + drive[x] + '_writethrough'].toLowerCase() == 'true');
          if(tree['scsi' + y + ':' + drive[x] + '_autodetect'])
            vmxform['scsi' + y + '' + drive[z] + '_autodetect'].checked = (tree['scsi' + y + ':' + drive[x] + '_autodetect'].toLowerCase() == 'true');
          if(tree['scsi' + y + ':' + drive[x] + '_startconnected'])
            vmxform['scsi' + y + '' + drive[z] + '_startconnected'].checked = (tree['scsi' + y + ':' + drive[x] + '_startconnected'].toLowerCase() == 'true');

          z++;
        }
      }
      vmxform['scsi' + y + 'count'].selectedIndex = z;
      eval('s_scsi' + y + 'count.setValue(z);');
      scsicount_onchange(y);
    }


    if(tree['floppy0_present'])
      vmxform.floppy0_present.checked = (tree['floppy0_present'].toLowerCase() == 'true');
    if(tree['floppy0_filename'])
      vmxform.floppy0_filename.value = tree['floppy0_filename'];
    if(tree['floppy0_startconnected'])
      vmxform.floppy0_startconnected.checked = (tree['floppy0_startconnected'].toLowerCase() == 'true');

    for(x=0; x<2; x++) {
      if(tree['serial' + x + '_present'])
        vmxform['serial' + x + '_present'].checked = (tree['serial' + x + '_present'].toLowerCase() == 'true');
      if(tree['serial' + x + '_filename'])
        vmxform['serial' + x + '_filename'].value = tree['serial' + x + '_filename'];
      if(tree['serial' + x + '_flowcontrol'])
        vmxform['serial' + x + '_flowcontrol'].checked = (tree['serial' + x + '_flowcontrol'].toLowerCase() == 'true');
    }

    if(tree['parallel0_present'])
      vmxform.parallel0_present.checked = (tree['parallel0_present'].toLowerCase() == 'true');
    if(tree['parallel0_filename'])
      vmxform.parallel0_filename.value = tree['parallel0_filename'];
    if(tree['parallel0_bidirectional'])
      vmxform.parallel0_bidirectional.checked = (tree['parallel0_bidirectional'].toLowerCase() == 'true');
}

