var NS4      = (document.layers) ? 1 : 0; // Netscape v4.0+
var IE4      = (document.all)    ? 1 : 0; // Internet Explorer v4.0+

// Determine which browser we are working with and update the text appropriately
function updateField(str)
{     
    if (IE4)
    {
        str = "&nbsp;&nbsp;" + str;
        document.all.timerCount.innerHTML = str;
    }
    else
        document.timerCount.setting.value = str;
}
                                            
function updateClock(curHours, curMinutes, curSeconds, action)
{
    // Regularly update the page (more than once every 20 minutes) in order
    // to keep alive session variables while the client is 'connected'
    if (--nextUpdate <= 0)
    {
        refreshConfPanel(1);
        return;
    }

    if (3 == action) // If we are a post-conference timer
    {
        handlePostGracePeriod(curHours, curMinutes, curSeconds);
        return;
    }
                                                
    // assert(3 != action)
                                                
    if (!curHours && !curMinutes && curSeconds < 1)
    {
        if (1 == action) // Rejoin the meeting when the timer runs out
            reJoinMeeting(1);
        else if (2 == action && 0 == secGraceAfter) // End of the conference with no post grace period
            reJoinMeeting(5); // Wait for a little in case of timing issues, then rejoin to the post join page
        else if (2 == action && 0 < secGraceAfter) // Switch to the post-grace counter
            refreshConfPanel(5);
        return;
    }
                                                
    // 5 minute warnings
    if (!curHours && 5 == curMinutes && !curSeconds && 3 != action)
    {
        if (2 == action) // 5 minute warning before the conference ends
        {
            L_FiveMinutesRemaining_Text = "The conference is scheduled to end in five minutes. If you organized this conference, you may be able to extend the conference duration by clicking Extend Time.";
            alert(L_FiveMinutesRemaining_Text);
            // fall through and refresh
        }
        else if (1 == action) // 5 minute warning before the conference starts
        {
            L_FiveMinutesUntilStart_Text = "The conference is scheduled to begin in five minutes.";
            alert(L_FiveMinutesUntilStart_Text);
            reJoinMeeting(1);
            return;
        }
        else
            return; // error: stop updating the clock

        refreshConfPanel(1); // Update us due to time changing while the alert was sitting open
        return;
    }
                                                
    handlePreJoinOrJoinPeriod(curHours, curMinutes, curSeconds);
}
                            
function generateTimerVal(curHours, curMinutes, curSeconds)
{
    TimerVal =  "" + ((curHours < 10) ? "0" : "")+ curHours;
    TimerVal += ((curMinutes < 10) ? ":0" : ":") + curMinutes;
    TimerVal += ((curSeconds < 10) ? ":0" : ":") + curSeconds;
    return TimerVal;
}
                            
function handlePreJoinOrJoinPeriod(curHours, curMinutes, curSeconds)
{
    if (curSeconds > 0)
    {
        curSeconds -= 1;
    }
    else if (curMinutes > 0)
    {
        curMinutes -= 1;
        curSeconds = 59;
    }
    else if (curHours > 0)
    {
        curHours   -= 1;
        curMinutes = 59;
        curSeconds = 59;
    }
    updateField(generateTimerVal(curHours, curMinutes, curSeconds));
    window.setTimeout("updateClock("+curHours+","+curMinutes+","+curSeconds+","+action+");", 1000);
}
                            
function handlePostGracePeriod(curHours, curMinutes, curSeconds)
{
    var LateMilliSecs = (curSeconds*SECOND) + (curMinutes*MINUTE) + (curHours*HOUR);

    if (LateMilliSecs < (secGraceAfter*SECOND)+MINUTE)
    {
        if (curSeconds < 59)
        {
            curSeconds += 1;
        }
        else if (curMinutes < 59)
        {
            curMinutes += 1;
            curSeconds = 0;
        }
        else
        {
            curHours   += 1;
            curMinutes = 0;
            curSeconds = 0;
        }
        updateField(generateTimerVal(curHours, curMinutes, curSeconds));
        window.setTimeout("updateClock("+curHours+","+curMinutes+","+curSeconds+",3);", 1000); // 3 means post grace
        return;
    }

    reJoinMeeting(1);
    return;
}
                                            
function timerMain()
{
    updateClock(hours, minutes, seconds, action);
}

window.onload = timerMain;