﻿var secs;
var timerID = null;
var timerRunning = false;
var callIHI;

function ServerResponseHandler(arg, context)
{       
    window.focus();
                       
    if(arg == "False")
    {                
        GetWaitForLineText();
        StopTimer();
        WaitForAvailableLine(); 
    }
    else
    {   
        GetLineAvailableText();        
        StopTimer();
        callIHI = arg;        
        self.setTimeout('Call()',1000);
    }            
}

function Call()
{
    //alert(callIHI);
    window.location = callIHI;
}

function ServerErrorHandler(arg, context)
{
    window.focus();
    GetErrorText();
    StopTimer();
    
}

function WaitForAvailableLine()
{            
    secs = 10;    
    StartTimer();
}
        
function StopTimer()
{
    if(timerRunning)
    {
        clearTimeout(timerID);
        timerRunning = false;
    }
}

function StartTimer()
{
    if (secs < 0)
    {   
        GetAvailableLineFromServer();                   
    }
    else
    {                        
        if(secs == 0)        
            document.getElementById('TimerDisplay').innerHTML = GetStatusText();                    
        else        
            document.getElementById('TimerDisplay').innerHTML = secs;            
                
        secs -= 1;                
        timerRunning = true;
        timerID = self.setTimeout('StartTimer()', 1000);        
    }
}
