  
    var groupId = null;
    var chartIntervalTimeId=null;
    var retryInterval=3000; // milleseconds; when load failed, should retry again.
        
    var _lastInstrumentId, _lastChartType, _lastFromTime, _lastToTime;
    
    function promptError(message)
    {
		alert(message);	
    }

    function getLocale() {
        var lang = document.all("_LanguageHidden").value.toLowerCase();
        if (lang == "chs") {
            return "zh_CN";
        } else if (lang == "cht") {
            return "zh_TW";
        }
        else {
            return "en_US";
        }
    }
    
    function getSymbols()
    {
		window.setTimeout('loadChartData();', 5000 );
		if (groupId == null)groupId=window.document.getElementById("_GroupIdHidden").value;
		Quotation.Reader.Service.GetSymbols(groupId,endGetSymbols);
    }
    
    function endGetSymbols(result)
    {
        try
        {
            AioChartApplet.refreshInstruments(result);
        }catch(e){setTimeout('getSymbols();', 2000);}
    }
    
    function getChartData2(instrumentId,chartType,fromTime, toTime)
    {
        if(instrumentId !=_lastInstrumentId || chartType !=_lastChartType || fromTime != _lastFromTime || toTime != _lastToTime)
        {
            //log('getChartData2.0');
            
            log('getChartData2,' + instrumentId.toString() + ',' + chartType.toString() + ',' + fromTime.toString() + ',' + toTime.toString());
            _lastInstrumentId = instrumentId ;
            _lastChartType =chartType ;
            _lastFromTime = fromTime;
            _lastToTime = toTime;
            
            //log('getChartData2.1');
		    groupId=getGroupIdByInstrumentId(instrumentId);		
		    //return Quotation.Reader.Service.GetChartData2(groupId,instrumentId,chartType, fromTime, toTime, function (result){AioChartApplet.addChartData(result);})
		    
		    //log('getChartData2.2');
		    return Quotation.Reader.Service.GetChartData2(groupId,instrumentId,chartType, fromTime, toTime, endGetChartData2);
		    //log('getChartData2.3');
		    
        }
    }
    
    function endGetChartData2(result)
    {
        try{
            log('endGetChartData2');
            AioChartApplet.addChartData(result);
            AioChartApplet.updateChart(); 
        }catch(e){
            log(e.toString());
        }
    }    
    
    function getGroupIdByInstrumentId(instrumentId)
    {
        var k=0;
        for(k=0;k<objects.length;k++)
        {
            var ob=objects[k];
            if(ob.InstrumentId.toLowerCase()==instrumentId.toLowerCase())
            {
                return ob.GroupId;
            }
        }
    }
    
    function loadChartData()
    {

        if(!isLoadCompleted())
        {
            var now = new Date();
            log('loadChartData(),' + now.toString());
            try
            {
                log('loadChartData.1');
                getChartData2(AioChartApplet.getInstrumentId(), AioChartApplet.getFrequencyName(),now, now);
            
                log('loadChartData.2');
                window.setTimeout('loadChartData();', retryInterval);
                //cancelUpdateChartInterval();
                
                log('loadChartData.3');
                chartIntervalTimeId=setTimeout('updateChart();', retryInterval + 1000);
                log('loadChartData.4');
            }catch(e)
            {
                log(e.toString());
            }
        }
    }    
    
    function isLoadCompleted()
    {
        return AioChartApplet.isLoadCompleted();
    }
    
    function getChartInterval()
    {
        return AioChartApplet.getFrequencyInterval();
    }
    
    function updateChart()
    {
        if(isLoadCompleted())
        {        
            log('updateChart');
            
            var now=new Date();
            AioChartApplet.updateChart();        
            getChartData2(AioChartApplet.getInstrumentId(), AioChartApplet.getFrequencyName(),now, now);
            
            var interval =  getChartInterval();
            chartIntervalTimeId=setTimeout('updateChart();', interval);
            log(interval.toString());
        }
    }
    
    function cancelUpdateChartInterval()
    {
        try
        {
            if(chartIntervalTimeId != null)
            {
                clearTimeout(chartIntervalTimeId);
            }
        }catch(e) {}
    }
    
    function log(message)
    {
        //AioChartApplet.getLogHelper().debug(message);
    }
    
    function logError(message)
    {
        //AioChartApplet.getLogHelper().debug(message);
    }
        
    function setLogDebugEnable(logEnable)
    {
        //AioChartApplet.getLogHelper().setDebugEnable(logEnable);
    }
              
    function setLogErrorEnable(logEnable)
    {
        //AioChartApplet.getLogHelper().setErrorEnable(logEnable);
    }
                                
