var searchContainer
var searchOptions
var searchOptionsTable
var scrollDown
var scrollUp
var index, firstOptionsIndex, noOfOptions
var optionsList, optionsArr, optionsHTML
var callAjaxFunction
var myReg
callAjaxFunction = true
index=0
firstOptionsIndex = 1
noOfOptions = 5


function ajaxFunction()
{
	getSearchObjs()
	
	if(document.search.keyword.value.length >= 3)
	{
		var xmlHttp;
		
		try
		{  // Firefox, Opera 8.0+, Safari  
			xmlHttp=new XMLHttpRequest();  
		}
		catch (e)
		{  // Internet Explorer  
			try
			{    
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
			}
			catch (e)
			{    
				try
				{      
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
				}
				catch (e)
				{      
					alert("Your browser does not support AJAX!");      
					return false;      
				}    
			}  
		}  


		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				optionsList = xmlHttp.responseText;
					//alert(optionsList)			
				index=0				
				firstOptionsIndex = 1
				if(optionsList != "")
				{
					optionsArr = optionsList.split("|")
					if(optionsArr.length > 1)
					{
						searchContainer.style.display = "block"
						searchContainer.style.visibility = "visible"
						
						optionsHTML = ""
						for(i=1;i<optionsArr.length;i++)
						{
							if( i<= noOfOptions)
							{								
								optionsHTML = optionsHTML + "<DIV id=\"optionsList" +i+ "\" onmouseover=\"this.className='overme';\" onmouseout=\"this.className='';\" >" 
							}
							else
							{
								optionsHTML = optionsHTML + "<DIV id=\"optionsList" +i+ "\" onmouseover=\"this.className='overme';\" onmouseout=\"this.className='';\" style=\"display:none;\">" 
							}
							myReg = new RegExp(encodeURIComponent(document.search.keyword.value), "i")
							myArray = myReg.exec(encodeURIComponent(optionsArr[i]))
							
							optionsHTML = optionsHTML + "<a href=\"#\" onclick=\"setSearchValue(" + i + ");document.search.action = '/cn/search.aspx';document.search.submit()\">"
							optionsHTML = optionsHTML + optionsArr[i].replace(decodeURIComponent(myArray[0]),"<span>"+decodeURIComponent(myArray[0])+"</span>") + "</a></DIV>"
						}
						searchOptions.innerHTML = optionsHTML
						
						if(optionsArr.length > noOfOptions+1)
						{
							scrollDown.style.display = "block"
							scrollUp.style.display = "block"
						}
						else
						{
							scrollDown.style.display = "none"
							scrollUp.style.display = "none"
						}
		
					}
					else
					{
						searchContainer.style.visibility = "hidden"
					}
				}
				else
				{
					searchContainer.style.visibility = "hidden"
				}
			}
		}

		xmlHttp.open("GET","/cn/include/getSearchOptions.aspx?s="+encodeURIComponent(document.search.keyword.value),true);
		xmlHttp.send(null);
	}
	else
	{
		searchContainer.style.visibility = "hidden"
	}
}

function doSomething(e) {
	var code;

	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);

	if (code == "40"){selectOption("D",true)}
	else if (code == "38"){selectOption("U",true)}
	else {callAjaxFunction = true}
}
		
function selectOption(action,keyboard)
{
	callAjaxFunction = false
	if (searchContainer.style.visibility == "visible" && document.getElementById)
	{
		if (action == "D")
		{
			if (index < optionsArr.length-1)
			{
				index = index + 1			
				if (index >= firstOptionsIndex + noOfOptions)
				{
					document.getElementById("optionsList"+firstOptionsIndex).style.display="none"					
					firstOptionsIndex = firstOptionsIndex + 1
				}
			
				if (keyboard)
				{
					document.getElementById("optionsList" + index).className = "overme"
					document.getElementById("optionsList" + index).style.display = "block"
					setSearchValue(index)
					if (index>1)
					{
						document.getElementById("optionsList" + (index-1)).className = ""
					}
				}
				else
				{
					document.getElementById("optionsList"+index).style.display="block"
				}
			}
		}
		else
		{
			if (index > 1)
			{
				index = index - 1
				if (index < firstOptionsIndex)
				{
					document.getElementById("optionsList"+(firstOptionsIndex-1+noOfOptions)).style.display="none"
					firstOptionsIndex = firstOptionsIndex - 1
				}
				
				if (keyboard)
				{
					document.getElementById("optionsList" + index).className = "overme"
					document.getElementById("optionsList" + index).style.display = "block"
					setSearchValue(index)
					if (index < optionsArr.length-1)
					{
						document.getElementById("optionsList" + (index+1)).className = ""
					}
				}
				else
				{
					document.getElementById("optionsList"+index).style.display="block"					
				}	
			}
		}
		
	}
}

function getSearchObjs()
{
	if (document.getElementById) 
	{ 
		searchContainer = document.getElementById("search_container"); 
		searchOptions = document.getElementById("search_options");
		scrollDown = document.getElementById("scroll_down");
		scrollUp = document.getElementById("scroll_up");
	} 	
}

function setSearchValue(x)
{
	if(document.all){
     document.search.keyword.value = document.getElementById("optionsList" + x).innerText
	} else{
		document.search.keyword.value = document.getElementById("optionsList" + x).textContent
	}
}

if(navigator.appName == "Microsoft Internet Explorer")
{
	document.getElementById("search_container").style.left = 662/1280*screen.width;
}

function isAlphaNum(input){
//var inputStr=input.value
if(window.RegExp && input){
	var regExp1 = /[^a-zA-Z0-9]/
	if(regExp1.test(input)) {return false}; return true
}}
