var MWSTimer_secs=20;
var MWSTimer_timerID = null;
var MWSTimer_timerRunning = false;
var MWSTimer_delay = 1000;
var MWSTimer_CallbackFunction;

function MWSTimer_InitializeTimer(duration, callback_function)
{
    // Set the length of the timer, in seconds
    MWSTimer_CallbackFunction = callback_function;
    MWSTimer_secs = duration;
    MWSTimer_StopTheClock();
    MWSTimer_StartTheTimer();
}

function MWSTimer_StopTheClock()
{
	if(MWSTimer_timerRunning)
		clearTimeout(MWSTimer_timerID);
	MWSTimer_timerRunning = false;
}

function MWSTimer_StartTheTimer()
{
    if (MWSTimer_secs==0)
    {
        MWSTimer_StopTheClock();
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
        MWSTimer_CallbackFunction();
        //alert("You have just wasted 10 seconds of your life.");
    }
    else
    {
        MWSTimer_secs = MWSTimer_secs - 1;
        MWSTimer_timerRunning = true;
        MWSTimer_timerID = self.setTimeout("MWSTimer_StartTheTimer()", MWSTimer_delay);
    }
}

function MWSTimer_ServiceRunner()
{
	if(MWS.MWS_ArrService!=null)
	for( var i = 0; i < MWS.MWS_ArrService.length; i++) 
	if (MWS.MWS_ArrService[i].TimerRunning==true)
	{
		//alert(MWS.MWS_ArrService[i].ID);
		var fn = MWS.MWS_getServiceProperty(MWS.MWS_ArrService[i].ID, "time", "TimeRefreshFunction");
		if (fn != null)
			fn(MWS.MWS_ArrService[i].ID);
		else
			alert("Refresh function not defined!");
	}
    
    	if (MWS.MWS_TimerRunning)
		MWSTimer_InitializeTimer(MWS.MWS_TimerRefreshInterval, MWSTimer_ServiceRunner);
}



