var server = GetObject( "winmgmts:" ); 
var refresher = new ActiveXObject("WbemScripting.SWbemRefresher");
var items = refresher.AddEnum( server, "Win32_PerfFormattedData_Tcpip_NetworkInterface").ObjectSet;

System.Gadget.onSettingsClosing = SettingsClosing;

function init() 
{
    // Refresh WMI collection
    refresher.Refresh();

    // Get enumerator of items
    var e = new Enumerator(items);
    
    // Populate options with network cards
    for(var i=0; !e.atEnd(); e.moveNext() & i++ )
    {
         NetworkCardSelection.options[i] = new Option( e.item().Name, e.item().Name );
    }
    
    if( System.Gadget.Settings.read("selectedIndex") != "" )
    {
        NetworkCardSelection.options.selectedIndex = System.Gadget.Settings.read("selectedIndex");
    } else {
        NetworkCardSelection.options.selectedIndex = 0;
    }

    if( System.Gadget.Settings.read("intervalIndex") != "" )
    {
        Interval.options.selectedIndex = System.Gadget.Settings.read("intervalIndex");
    } else {
	    Interval.options.selectedIndex = 0;
    }
}

function SettingsClosing(event)
{
	if (event.closeAction == event.Action.commit) {
		System.Gadget.Settings.write("selectedItem", NetworkCardSelection.options[ NetworkCardSelection.options.selectedIndex ].value );
		System.Gadget.Settings.write("selectedIndex", NetworkCardSelection.options.selectedIndex);

		System.Gadget.Settings.write("intervalItem", Interval.options[ Interval.options.selectedIndex ].value );
		System.Gadget.Settings.write("intervalIndex", Interval.options.selectedIndex );
	}
	
	event.cancel = false;
}
