// Originally: Email Alert for the Microsoft Windows Sidebar
// Modified by: Lawrence James
///////////////////////////////////////                    

var _MailClientName;
var _MailClientPath;
var _UnreadEmailCount;
var _EmailCountView;
var _WshShell;
var _bIntervalsSet          = false;
var _RefreshInterval        = 20;
var _RefreshIntervalID;
var _MailCountRegistryPath = "\\Software\\Clients\\Mail\\";
var _MailCountApplicationName = "Microsoft.EmailAlertGadget.GetUnreadEmailCountVB.exe";
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

function GadgetInit() /// Initialize the Gadget.
{
    try
    {
		_EmailCountView = document.getElementById("EmailCount");
		_WshShell = new ActiveXObject("WScript.Shell");
    	SetMailClientPath();
	}
	catch (e)
	{
	}
	if (_MailClientPath != null)
	{
	    _EmailCountView.onclick = OpenMailClient;
	    _EmailCountView.href = "";
	}
}


function SetMailClientPath() // Determine the name and path of the user's e-mail client.
{
    try
    {
        _MailClientName = _WshShell.RegRead("HKCU" + _MailCountRegistryPath);
    }
    catch (e)
    {
        _MailClientName = _WshShell.RegRead("HKLM" + _MailCountRegistryPath);
    }
    try
    {
        _MailClientPath = _WshShell.RegRead("HKCU" + _MailCountRegistryPath +
            _MailClientName + "\\shell\\open\\command\\");
    }
    catch (e)
    {
        _MailClientPath = _WshShell.RegRead("HKLM" + _MailCountRegistryPath +
            _MailClientName + "\\shell\\open\\command\\");
    }
}

function SetRefreshIntervals()  // Activate the timer to refresh unread mail count.
{
    try
    {
        if (_bIntervalsSet == false)
        {
	        // Set intervals for refreshing the email display
            _RefreshIntervalID = setInterval(RefreshEmail, 1000 * _RefreshInterval);
            _bIntervalsSet = true;         
        }
	}
    catch (e)
    {
    }
 }

function ClearRefreshIntervals() // Deactivate the timer to refresh unread mail count.
{
    try
    {
        if (_bIntervalsSet == true)
        {
            clearInterval(_RefreshIntervalID);
            _bIntervalsSet = false;
        }
    }
    catch (e)
    {
    }
 }

function DisplayErrorMessage(errorText) // Display an error message in the Gadget.
{
    ClearRefreshIntervals();
    _EmailCountView.innerText = errorText;
}

function RefreshEmail() // Retrieve the unread mail count from the GetUnreadMailCount application.
{
    try
    {
    	ClearRefreshIntervals();
        exec = _WshShell.Exec("\"" + System.Gadget.path + "\\" + _MailCountApplicationName + "\" \"" + _MailClientName + "\"");
        _UnreadEmailCount = exec.StdOut.ReadAll();
        UpdateView();
		SetRefreshIntervals();     
    }
    catch (e)
    {
        // An error occurred, display it and restart the timer.
        DisplayErrorMessage(_errEmailErrorText);
        SetRefreshIntervals();
    }
}

function UpdateView() // Get the unread mail count from the _UnreadMailCount global variable
{
if (_UnreadEmailCount<100)
{viewmailcount=_UnreadEmailCount}
else if (_UnreadEmailCount>=99)
{viewmailcount="99"}
_EmailCountView.innerHTML = "<center>" + viewmailcount + "</center>";          
}

function OpenMailClient() // Open the user's e-mail client
{
    try
    {
        _WshShell.Run(_MailClientPath, ActivateWithCurrentSizeAndPosition = 5);
    }
    catch (e)
    {
        DisplayErrorMessage(_errEmailErrorText);
        SetRefreshIntervals();
    }
    return false;
}
///////////////////////////////////////////////////////////////////
function LoadEmail()
{
    GadgetInit();
    RefreshEmail();
    CheckUpdate()
}
///////////////////////////////////////////////////////////////////
function CheckUpdate()
{
var d = new Date()
currentdate=d.getDate() + " - " + d.getDay()
if (document.getElementById("lastupdate").innerHTML!=currentdate)
{checkforupdate()}
}
//////////////////////////////////////////////////////////// 
function checkforupdate() //get update xml
{
xmlFile="http://galley78.f2s.com/update/unreadmail.xml";
xmlDoc.async="false";
xmlDoc.load(xmlFile);
xmlObj=xmlDoc.documentElement; 
    if (xmlDoc.parseError!="0")
    {   
    }
    else
    {
    interpretupdate()
    }  
}    

function interpretupdate() //check if new version
{
updateversion=xmlObj.childNodes(0).childNodes(0).firstChild.text; 
link=xmlObj.childNodes(0).childNodes(2).firstChild.text; 
currentversion=System.Gadget.version
    if (currentversion!=updateversion)
    {    
    document.getElementById("update").style.visibility="visible"
    document.getElementById("update").innerHTML = "<a href=\"" + link + "\" target=\"_blank\">" + "<b>New Update</b>";     
    document.getElementById("update").onclick=function(){ 
    System.Gadget.close() }   
    }
//set last update date
var d = new Date()
currentdate=d.getDate() + " - " + d.getDay()
document.getElementById("lastupdate").innerHTML=currentdate
}