var MsgCount = "";
var user = "";
var pass = "";
var host = "";
var t;

window.onload = function(){
    var gOpacitySetting = System.Gadget.Settings.read("gOpacity");
    if (gOpacitySetting != "")
        background.opacity = gOpacitySetting;

    var pUser = System.Gadget.Settings.read("pUser");
    var pPass = System.Gadget.Settings.read("pPass");
    var pServer = System.Gadget.Settings.read("pServer");

    if (pUser != "")
        user = pUser;
    if (pPass != "")
        pass = pPass;
    if (pServer != "")
        host = pServer;

    System.Gadget.settingsUI = "Settings.html";
    System.Gadget.onSettingsClosed = SettingsClosed;

    checkformail();
}

function SettingsClosed() {
    var gOpacitySetting = System.Gadget.Settings.read("gOpacity");
    var pUser = System.Gadget.Settings.read("pUser");
    var pPass = System.Gadget.Settings.read("pPass");
    var pServer = System.Gadget.Settings.read("pServer");
	
	
    if (gOpacitySetting != "")
        background.opacity = gOpacitySetting;
    
    if (pUser != "")
        user = pUser;
	if (pPass != "")
	    pass = pPass;
	if (pServer != "")
	    host = pServer;
	clearTimeout(t);
	checkformail();
}
function checkformail(){
    var NewMsg;
    var WshShell = new ActiveXObject("WScript.Shell");
    NewMsg = false;

    var args = "/U:"+user+" /P:"+pass+" /H:"+host;

    System.Gadget.beginTransition();	

    background.removeObjects();
    var mailImage = background.addImageObject("images/Email40.png", 7, 10)
    mailImage.addGlow( "gray", 1, 30 );

    var exec = WshShell.Exec(System.Gadget.path + "\\Client\\POP3Gadget.exe " + args);
    var outputData = new String(exec.StdOut.ReadAll());
    outputData = outputData.substring(0,outputData.length-1);
    
    var textObject = background.addTextObject("messages", "Segoe UI", 13, "White", 50, 30); 
    if(outputData == "")
        outputData = 0;
        
    textObject = background.addTextObject(outputData  +" new", "Segoe UI", 13, "White", 50, 15);

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

    System.Gadget.endTransition( System.Gadget.TransitionType.morph, 0.5 );
    // 15mins
    t = setTimeout(checkformail, 900000);
    // Test
  //var t = setTimeout(checkformail, 900);

}
