var xmlHttp;
var xmlHttp1;
var xmlHttp2;
var clientInputId;
var clientContainerId;
var clientCompareInputId;
var clientCompareContainerId;

//phong.nguyen modified this part
var enableCache = true;
var Cached = Array();

function InitAC(response)
{   
    var data_Autocomplex_MCK = new Array();
    data_Autocomplex_MCK = response.split("|");
	
    var statesArray = new Array();
    var tam= new Array();
    for(var i=0 ; i<data_Autocomplex_MCK.length ; i++)
    {
        tam = data_Autocomplex_MCK[i].split("*");
        statesArray[i] = tam;		
    }
    YAHOO.example.ACJSArray = new function() {
        // Instantiate first JS Array DataSource
        this.oACDS = new YAHOO.widget.DS_JSArray(statesArray);

        // Instantiate first AutoComplete
        this.oAutoComp = new YAHOO.widget.AutoComplete(clientInputId,clientContainerId, this.oACDS);
        //this.oAutoComp.prehighlightClassName = "yui-ac-prehighlight";
        this.oAutoComp.setHeader("<table class='headautocomlete' ><tr valign=\"top\" ><td style=\"padding-left:6px;background-color:#9CD6F7\" align=\"left\" width='60px'><b>MÃ CK</b></td> <td style=\"background-color:#9CD6F7\" align=\"left\" width='240px'>Tên Công Ty</td></tr></table>");

        this.oAutoComp.typeAhead = true;
        this.oAutoComp.useShadow = true;
        this.oAutoComp.autoHighlight = true;
        this.oAutoComp.minQueryLength = 1;
        this.oAutoComp.useIFrame = true;
        this.oAutoComp.allowBrowserAutocomplete = false;
        this.oAutoComp.queryDelay = 0.5;
        this.oAutoComp.maxResultsDisplayed =statesArray.length;
        this.oAutoComp.formatResult = function(oResultItem, sQuery) {
            var sMarkup ="<table class='headautocomlete' ><tr valign=\"top\" ><td style=\"padding:0px 15px 0px 6px; *padding:0px 0px 0px 6px;\" align=\"left\" width='60px'><b>"+oResultItem[0]+ "</b></td> <td style=\"padding:0px 10px 0px 0px; *padding:0px 10px 0px 0px;\" align=\"left \" width='240px'>" + oResultItem[1] + "</td></tr></table>";
            return (sMarkup);
        };
        this.oAutoComp.textboxFocusEvent.subscribe(function(){
            var sInputValue = YAHOO.util.Dom.get(inputId).value;
            if(sInputValue.length === 0) {
                var oSelf = this;
                setTimeout(function(){oSelf.sendQuery(sInputValue);},0);
            }
        });
    };   
}

function ResponseHandler()
{
    if (xmlHttp.readyState == 4)
    {	        
        var response = xmlHttp.responseText;
        if(enableCache)
            Cached[clientInputId] = response;
        InitAC(response);
    }
}

function GetStockForPriceHistory(inputID, containerID)
{
    clientInputId = inputID;
    clientContainerId = containerID;
    
    if(enableCache && Cached[clientInputId])
    {
        InitAC(Cached[inputID]);
        return;
    }
    
    xmlHttp=GetXmlHttpObject();
    
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }     
    
	var url="dataautocomlete.aspx?type=all";
	
	xmlHttp.onreadystatechange = ResponseHandler;	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
//------------------------------------END

function show_Data_Autocomplex_MCK()
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="dataautocomlete.aspx?type=search";
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function show_ListSymbolForChart(_inputId,_containerId)
{ 
	xmlHttp1=GetXmlHttpObject();
	clientInputId = _inputId;
	clientContainerId = _containerId;
	if (xmlHttp1==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="dataautocomlete.aspx?type=chart";
	xmlHttp1.onreadystatechange=stateChangedChart;
	xmlHttp1.open("GET",url,true);
	xmlHttp1.send(null);

}
function show_ListSymbolCompare(_inputId,_containerId)
{ 
	xmlHttp2=GetXmlHttpObject();
	clientCompareInputId = _inputId;
	clientCompareContainerId = _containerId;
	if (xmlHttp2==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="dataautocomlete.aspx?type=chart";
	xmlHttp2.onreadystatechange=stateChangedChartCompare;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);

}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 	
		getStockTrandHtml(xmlHttp.responseText);		
	}
}

function stateChangedChart() 
{ 
	if (xmlHttp1.readyState==4)
	{ 	
		getListSymbolForChartHtml(xmlHttp1.responseText,clientInputId,clientContainerId);		
	}
}

function stateChangedChartCompare() 
{ 
	if (xmlHttp2.readyState==4)
	{ 	
		getListSymbolForChartHtml(xmlHttp2.responseText,clientCompareInputId,clientCompareContainerId);		
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function getStockTrandHtml(str)
{
	data_Autocomplex_MCK = new Array();
	data_Autocomplex_MCK = str.split("|");
	
	var statesArray = new Array();
	var tam= new Array();
	for(var i=0 ; i<data_Autocomplex_MCK.length ; i++)
	{
		tam = data_Autocomplex_MCK[i].split("*");
		statesArray[i] = tam;		
	}
YAHOO.example.ACJSArray = new function() {
    // Instantiate first JS Array DataSource
    this.oACDS = new YAHOO.widget.DS_JSArray(statesArray);

    // Instantiate first AutoComplete
    this.oAutoComp = new YAHOO.widget.AutoComplete('statesinput','statescontainer', this.oACDS);
    //this.oAutoComp.prehighlightClassName = "yui-ac-prehighlight";
	this.oAutoComp.setHeader("<table class='headautocomlete' ><tr valign=\"top\" ><td style=\"padding-left:6px;background-color:#9CD6F7\" align=\"left\" width='60px'><b>MÃ CK</b></td> <td style=\"background-color:#9CD6F7\" align=\"left\" width='240px'>Tên Công Ty</td></tr></table>");

    this.oAutoComp.typeAhead = true;
    this.oAutoComp.useShadow = true;
	this.oAutoComp.autoHighlight = true;
    this.oAutoComp.minQueryLength = 1;
    this.oAutoComp.useIFrame = true;
    this.oAutoComp.allowBrowserAutocomplete = false;
    this.oAutoComp.queryDelay = 0.5;
    this.oAutoComp.maxResultsDisplayed =statesArray.length;
	this.oAutoComp.formatResult = function(oResultItem, sQuery) {
        var sMarkup ="<table class='headautocomlete' ><tr valign=\"top\" ><td style=\"padding:0px 15px 0px 6px; *padding:0px 0px 0px 6px;\" align=\"left\" width='60px'><b>"+oResultItem[0]+ "</b></td> <td style=\"padding:0px 10px 0px 0px; *padding:0px 10px 0px 0px;\" align=\"left \" width='240px'>" + oResultItem[1] + "</td></tr></table>";
        return (sMarkup);
    };
    this.oAutoComp.textboxFocusEvent.subscribe(function(){
        var sInputValue = YAHOO.util.Dom.get('statesinput').value;
        if(sInputValue.length === 0) {
            var oSelf = this;
            setTimeout(function(){oSelf.sendQuery(sInputValue);},0);
        }
    });
};
}

function getListSymbolForChartHtml(str,inputId,containerId)
{
	var data_Autocomplex_MCK = new Array();
	data_Autocomplex_MCK = str.split("|");
	
	var statesArray = new Array();
	var tam= new Array();
	for(var i=0 ; i<data_Autocomplex_MCK.length ; i++)
	{
		tam = data_Autocomplex_MCK[i].split("*");
		statesArray[i] = tam;		
	}
YAHOO.example.ACJSArray = new function() {
    // Instantiate first JS Array DataSource
    this.oACDS = new YAHOO.widget.DS_JSArray(statesArray);

    // Instantiate first AutoComplete
    this.oAutoComp = new YAHOO.widget.AutoComplete(inputId,containerId, this.oACDS);
    //this.oAutoComp.prehighlightClassName = "yui-ac-prehighlight";
	this.oAutoComp.setHeader("<table class='headautocomlete' ><tr valign=\"top\" ><td style=\"padding-left:6px;background-color:#9CD6F7\" align=\"left\" width='60px'><b>MÃ CK</b></td> <td style=\"background-color:#9CD6F7\" align=\"left\" width='240px'>Tên Công Ty</td></tr></table>");

    this.oAutoComp.typeAhead = true;
    this.oAutoComp.useShadow = true;
	this.oAutoComp.autoHighlight = true;
    this.oAutoComp.minQueryLength = 1;
    this.oAutoComp.useIFrame = true;
    this.oAutoComp.allowBrowserAutocomplete = false;
    this.oAutoComp.queryDelay = 0.5;
    this.oAutoComp.maxResultsDisplayed =statesArray.length;
	this.oAutoComp.formatResult = function(oResultItem, sQuery) {
        var sMarkup ="<table class='headautocomlete' ><tr valign=\"top\" ><td style=\"padding:0px 15px 0px 6px; *padding:0px 0px 0px 6px;\" align=\"left\" width='60px'><b>"+oResultItem[0]+ "</b></td> <td style=\"padding:0px 10px 0px 0px; *padding:0px 10px 0px 0px;\" align=\"left \" width='240px'>" + oResultItem[1] + "</td></tr></table>";
        return (sMarkup);
    };
    this.oAutoComp.textboxFocusEvent.subscribe(function(){
        var sInputValue = YAHOO.util.Dom.get(inputId).value;
        if(sInputValue.length === 0) {
            var oSelf = this;
            setTimeout(function(){oSelf.sendQuery(sInputValue);},0);
        }
    });
};
}
