var suggest_out = true;
var suggest_currentIndex = 0;
var suggest_items= ['Chicken', 'Chicken Soup', 'Chicken Curry']
var suggest_val = '';
function ProcessChgText(e)
{
    
	if(e.value != suggest_val)
	{
		ProcessText(GetControl('txt_suggest'))
		
	}
	suggest_val = GetControl('txt_suggest').value;
}
function Suggest_Select(e)
{
	GetControl('txt_suggest').value = e;
	HideSuggestion();
}
function Suggest_SetText()
{
	var c = suggest_items[suggest_currentIndex-1];
	if(c) GetControl('txt_suggest').value = c;
	HideSuggestion();
}
function LoadString(contents)
{
   
    var box = GetControl('suggestBox');
    suggest_currentIndex = 0;
    box.innerHTML = ''; 
    for(var i = 0 ; i < contents.length ; i++)
   {
       
        box.innerHTML += '<a href="javascript:Suggest_Select(\'' + contents[i] + '\')" id="suggest_o'+ (i + 1)+'"   >'+contents[i]+'</a>'
   } 
}
/*
suggestBox
<a href="javascript:Suggest_Select('Chicken')" id="suggest_o1"   >Chicken</a>
<a href="javascript:Suggest_Select('Chicken Soup')"  id="suggest_o2" >Chicken Soup</a>
<a href="javascript:Suggest_Select('Chicken Curry')" id="suggest_o3" >Chicken Curry </a>
*/
function UpIndex()
{
	if(suggest_currentIndex > 1)
	{
		ClearIndex(suggest_currentIndex);
		
		suggest_currentIndex--;
	}
	UpdateDisplay();
}
function ClearIndex(index)
{
	var c = GetControl('suggest_o' + index);
	if(c) c.className = '';
}
function LowIndex()
{
	if(suggest_currentIndex  < suggest_items.length)
	{
		ClearIndex(suggest_currentIndex);
		
		suggest_currentIndex++;
	}
	UpdateDisplay();
}
function UpdateDisplay()
{
	var _link = GetControl('suggest_o' + suggest_currentIndex);
	_link.className = 'selected';
}
function ShowSuggestion()
{
//show the suggestion box
GetControl('suggestBox').style.display = 'block';
}
function HideSuggestion()
{
GetControl('suggestBox').style.display = 'none';
}
function ProcessText(e)
{
	//check mininum length;
	//CheckText(e.value);
	GetSuggestions();
}
function CheckText(str)
{
//call to ajax later
	//CheckTextCallback('');
}
function GetSuggestions()
{
    var phase = GetControl('txt_suggest').value;
   //GetControl('lbl_c').innerHTML = phase;
   if(phase.length < 1)return;
   SearchSuggestService.GetSuggestion(phase, GetSuggestionCB); 
   
    //SearchSuggestService GetSuggestion
}
function GetSuggestionCB(e)
{

    PopulateSuggestion(e.value);
}
function CheckTextCallback(result)
{
    //alert(result.result)
	PopulateSuggestion(result);
	ShowSuggestion();
}
function PopulateSuggestion(e)
{
        //do something to convert into array;
        
        suggest_items = e;
         LoadString(suggest_items)
    
//populate the suggestion box
}
function suggest_mouse_out()
{
suggest_out = true;
}
function suggest_mouse_in()
{
suggest_out = false;
}

function suggest_key_press(the_key, o)
{

	if ( ! the_key )
    {
        the_key = event.keyCode;
    }
	//alert(the_key)
	switch(Number(the_key))
	{
		case 13:
		
		if(suggest_currentIndex > 0)
		{
		    Suggest_SetText();
		    var keyword = document.getElementById("txt_suggest").value;
            window.location.href = "/pages/Public/AdvancedSearch.aspx?kw=" + keyword + "&sb=1";
         } 
		//window.location.href = 'foodlane.sg';
		//Suggest_SetText();
		break;
		case 38:
		//up
		UpIndex();
		break;
		case 40:
		LowIndex();
		break;
		default:
		
		ProcessChgText(the_key)
		break;
	}
}
	
function Suggest_Blur()
{
  if(suggest_out)
	{
		HideSuggestion();
	}
	
}
function Suggest_Blur_lower()
{
  if(suggest_out_lower)
	{
		HideSuggestion_lower();
	}
	
}
AddEvent(window, "mousedown", function()
{
//alert('mousedown');
    if(suggest_out)
	{
		HideSuggestion();
	}
			
  //  if(suggest_out_lower)
//	{
	//	HideSuggestion_lower();
	//}
})

/*
AddEvent(window, "click", function()
{
   if(suggest_out)
	{
		HideSuggestion();
	}
			
    if(suggest_out_lower)
	{
		HideSuggestion_lower();
	}
})*/


//------------------------------------------



var suggest_out_lower = true;
var suggest_currentIndex_lower = 0;
var suggest_items_lower= ['Chicken', 'Chicken Soup', 'Chicken Curry']
var suggest_val_lower = '';

var txt_suggest_lower;
function ProcessChgText_lower(e)
{
    
	if(e.value != suggest_val_lower)
	{
		ProcessText_lower( txt_suggest_lower)
		
	}
	suggest_val_lower =  txt_suggest_lower.value;
}
function Suggest_Select_lower(e)
{
	 txt_suggest_lower.value = e;
	HideSuggestion_lower();
}
function Suggest_SetText_lower()
{
	var c = suggest_items_lower[suggest_currentIndex_lower -1];
	if(c)  txt_suggest_lower.value = c;
	HideSuggestion_lower();
}
function LoadString_lower(contents)
{
   
    var box = GetControl('suggestBox_lower');
    suggest_currentIndex_lower = 0;
    box.innerHTML = ''; 
    for(var i = 0 ; i < contents.length ; i++)
   {
       
        box.innerHTML += '<a href="javascript:Suggest_Select_lower(\'' + contents[i] + '\')" id="suggest_o_lower'+ (i + 1)+'"   >'+contents[i]+'</a>'
   } 
}
/*
suggestBox
<a href="javascript:Suggest_Select('Chicken')" id="suggest_o1"   >Chicken</a>
<a href="javascript:Suggest_Select('Chicken Soup')"  id="suggest_o2" >Chicken Soup</a>
<a href="javascript:Suggest_Select('Chicken Curry')" id="suggest_o3" >Chicken Curry </a>
*/
function UpIndex_lower()
{
	if(suggest_currentIndex_lower > 1)
	{
		ClearIndex_lower(suggest_currentIndex_lower);
		
		suggest_currentIndex_lower--;
	}
	UpdateDisplay_lower();
}
function ClearIndex_lower(index)
{
	var c = GetControl('suggest_o_lower' + index);
	if(c) c.className = '';
}
function LowIndex_lower()
{
	if(suggest_currentIndex_lower  < suggest_items_lower.length)
	{
		ClearIndex_lower(suggest_currentIndex_lower);
		
		suggest_currentIndex_lower++;
	}
	UpdateDisplay_lower();
}
function UpdateDisplay_lower()
{
	var _link = GetControl('suggest_o_lower' + suggest_currentIndex_lower);
	_link.className = 'selected';
}
function ShowSuggestion_lower()
{
//show the suggestion box
GetControl('suggestBox_lower').style.display = 'block';
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
if(isIE)
{
GetControl('drop1').style.display = 'none';
GetControl('drop2').style.display = 'none';
}
}
function HideSuggestion_lower()
{
GetControl('suggestBox_lower').style.display = 'none';
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
if(isIE)
{
GetControl('drop1').style.display = 'block';
GetControl('drop2').style.display = 'block';
}
}
function ProcessText_lower(e)
{
	//check mininum length;
	//CheckText(e.value);
	GetSuggestions_lower();
}
function CheckText_lower(str)
{
//call to ajax later
	//CheckTextCallback('');
}
function GetSuggestions_lower()
{
    var phase =  txt_suggest_lower.value;
   //GetControl('lbl_c').innerHTML = phase;
   if(phase.length < 1)return;
   SearchSuggestService.GetSuggestion(phase, GetSuggestionCB_lower); 
   
    //SearchSuggestService GetSuggestion
}
function GetSuggestionCB_lower(e)
{

    PopulateSuggestion_lower(e.value);
}
function CheckTextCallback_lower(result)
{
    //alert(result.result)
	PopulateSuggestion_lower(result);
	ShowSuggestion_lower();
}
function PopulateSuggestion_lower(e)
{
        //do something to convert into array;
        
        suggest_items_lower = e;
         LoadString_lower(suggest_items_lower)
    
//populate the suggestion box
}
function suggest_mouse_out_lower()
{
suggest_out_lower = true;
}
function suggest_mouse_in_lower()
{
suggest_out_lower = false;
}

function suggest_key_press_lower(the_key, o)
{

	if ( ! the_key )
    {
        the_key = event.keyCode;
    }
	//alert(the_key)
	switch(Number(the_key))
	{
		case 13:
		
		if(suggest_currentIndex_lower > 0)
		{
		    Suggest_SetText_lower();
		    var keyword =  txt_suggest_lower.value;
            window.location.href = "/pages/Public/AdvancedSearch.aspx?kw=" + keyword + "&sb=1";
         } 
		//window.location.href = 'foodlane.sg';
		//Suggest_SetText();
		break;
		case 38:
		//up
		UpIndex_lower();
		break;
		case 40:
		LowIndex_lower();
		break;
		default:
		
		ProcessChgText_lower(the_key)
		break;
	}
}

AddEvent(window, "load", function()
{
        //txtKeyword
        if(GetControl('txtKeyword'))
        txt_suggest_lower = GetControl(GetControl('txtKeyword').value);
})

