var MsgCount = "";
var user = "";
var pass = "";
var host = "";
var _MailClientPath;
var _MailClientName;
var _WshShell;
var _MailCountRegistryPath = "\\Software\\Clients\\Mail\\";
var t;

window.onload = function(){
	System.Gadget.settingsUI = "Settings.html";
    System.Gadget.onSettingsClosed = SettingsClosed;
	System.Gadget.Flyout.file = "Flyout.html";
	System.Gadget.Flyout.onHide = SettingsClosed;
    MsgCount = 0;
	_WshShell = new ActiveXObject("WScript.Shell");
	SetMailClientPath();
    checkformail();
}

function SettingsClosed() {
	clearTimeout(t);
	checkformail();
}
function checkformail(){
	var nbMessages = 0;
    var NewMsg;
    var WshShell = new ActiveXObject("WScript.Shell");
    NewMsg = false;

    System.Gadget.beginTransition();	

    background.removeObjects();
    var mailImage = background.addImageObject("images/email.png", 7, 10)
    mailImage.addGlow( "gray", 1, 30 );
	var NbComptes = System.Gadget.Settings.read("NbComptes");
	var outputData = "";
	if(NbComptes != "" && NbComptes != 0){
		for(var i=1; i<=NbComptes; i++){
			user = System.Gadget.Settings.read("pUser"+i);
			pass = System.Gadget.Settings.read("pPass"+i);
			host = System.Gadget.Settings.read("pServer"+i);
			var args = "/U:"+user+" /P:"+pass+" /H:"+host;
		    var exec = WshShell.Exec(System.Gadget.path + "\\Client\\POP3Gadget.exe " + args);
		    outputData = new String(exec.StdOut.ReadAll());
		    outputData = outputData.substring(0,outputData.length-1);
			nbMessages += parseFloat(outputData);
		}
	}
    
    var textObject = background.addTextObject("Messages", "Segoe UI", 13, "White", 60, 40); 
            
    textObject = background.addTextObject(nbMessages  +"", "Segoe UI", 13, "White", 60, 25); 

    if(MsgCount != outputData)
    {
        MsgCount = outputData;
        System.Sound.playSound("Windows XP Notify.wav");
    }

    System.Gadget.endTransition( System.Gadget.TransitionType.morph, 0.5 );
    // 5mins
    t = setTimeout(checkformail, 300000);

}

function SetMailClientPath()
{
    // The name of the mail client should be the default value for the mail count
    // registry key. Retrieve it using the WScript shell.
    try
    {
        _MailClientName = _WshShell.RegRead("HKCU" + _MailCountRegistryPath);
    }
    catch (e)
    {
        // There is no mail client setting for this user, fall back to the system
        // default in HKLM.
        _MailClientName = _WshShell.RegRead("HKLM" + _MailCountRegistryPath);
    }
    
    // Retrieve the path to the mail client using the WScript shell.
    try
    {
        _MailClientPath = _WshShell.RegRead("HKCU" + _MailCountRegistryPath +
            _MailClientName + "\\shell\\open\\command\\");
    }
    catch (e)
    {
        // There is no mail client path setting for this user, fall back to the
        // system default in HKLM.
        _MailClientPath = _WshShell.RegRead("HKLM" + _MailCountRegistryPath +
            _MailClientName + "\\shell\\open\\command\\");
    }
}

function OpenMailClient()
{
    try
    {
        // Use the WScript shell to run the mail client, in its current size and
        // position if it's already open.
        _WshShell.Run(_MailClientPath, ActivateWithCurrentSizeAndPosition = 5);
		checkformail();
    }
    catch (e)
    {
    }
}
