﻿// JScript File

 //when now=-1, it's the first time come in this page
 var now = -1;
 var max = 0;
 var e_keyCode = 0;
 var hoverColor='#808080';
 var originalColor='white';

function ajaxFunction()
 {
 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("Not support Ajax.");
         return false;
         }
      }
    }
	return xmlHttp;
}

function SearchSuggest(SearchTerm,SsearchBox)
{
   xmlhttp = new ajaxFunction();
   xmlhttp.onreadystatechange=function()
   {
   	if(xmlhttp.readyState==4)
	{
		if(xmlhttp.status==200)
		{
           if(xmlhttp.responseText.indexOf('<div id="inDiv"></div>')==-1)
           {
            if(SsearchBox =="searchBox2")
            {
             document.getElementById('SearchSuggestBox2').innerHTML=xmlhttp.responseText; 
            }
            else
            {
             document.getElementById('SearchSuggestBox').innerHTML=xmlhttp.responseText; 
            }  
		   }
		   else
		   {
		     if(SsearchBox =="searchBox2")
             {
               document.getElementById('SearchSuggestBox2').innerHTML='';
             }
             else
             {
		       document.getElementById('SearchSuggestBox').innerHTML='';
		     }		
		   }
		}
		setMax();
		setNow(-1);
	}
   }
   url="ajaxSearch.aspx?SearchTerm="+SearchTerm;
   xmlhttp.open('GET',url,true);
   xmlhttp.send(null);
}
 function ShowDiv()
 {
    document.getElementById('ResultDiv').style.display='block';
 }
 
 function HidenDiv()
 {
    document.getElementById('ResultDiv').style.display='none';
 }
 
 function mOver(item,m_now)
 {
    if(now != -1)
    {
        document.getElementById('inDiv').getElementsByTagName('a')[now].style.backgroundColor=originalColor;
    }
    item.style.backgroundColor=hoverColor;
    
        var s=document.getElementById('searchBox2');
        if(s)
        { 
         document.getElementById('searchBox2').value=item.innerHTML.replace(/<[^>]+>/g,"");  //  knight
        } 
     document.getElementById('searchBox').value=item.innerHTML.replace(/<[^>]+>/g,"");  //  knight
 
    now=m_now;
 }
 
 function mOut(item)
 {
    if( e_keyCode != 38 && e_keyCode != 40)
    {
        item.style.backgroundColor='white';
    }
 }
 
 function setColor(m_hcolor,m_ocolor)
 {
    hoverColor=m_hcolor;
    originalColor=m_ocolor;
 }
 
 function setMax()
 {
     var i=-1;
     if(document.getElementById('inDiv') != null)
     {
     while(document.getElementById('inDiv').getElementsByTagName('a')[i+1] != null)
     {
        i=i+1;
     }
     max=i+1;
     }
 }
 
 function setNow(m_now)
 {
    now = m_now;
 }
 
 function down()
 {
    if(document.getElementById('inDiv') != null)
    {
        if(now == -1)
        {
            document.getElementById('inDiv').getElementsByTagName('a')[0].style.backgroundColor=originalColor;
        }
        else
        {
            document.getElementById('inDiv').getElementsByTagName('a')[now].style.backgroundColor=originalColor;
        }
        if(now < max-1)
        {
            setNow(now+1);
        }
        else if(now = max)
        {
            setNow(0);
        }
        document.getElementById('inDiv').getElementsByTagName('a')[now].style.backgroundColor=hoverColor;
        document.getElementById('links').value = document.getElementById('inDiv').getElementsByTagName('a')[now].href;
        
        var s=document.getElementById('links2');        
        if(s)
        {
        document.getElementById('links2').value = document.getElementById('inDiv').getElementsByTagName('a')[now].href;
        }  
    }
 }
 
 function up()
 {
   if(document.getElementById('inDiv') != null)
    {
        if(now == -1)
        {
            document.getElementById('inDiv').getElementsByTagName('a')[max-1].style.backgroundColor=originalColor;
        }
        else
        {
            document.getElementById('inDiv').getElementsByTagName('a')[now].style.backgroundColor=originalColor;
        }
        document.getElementById('inDiv').getElementsByTagName('a')[now].style.backgroundColor=originalColor;
        if(now > 0)
        {
            setNow(now-1);
        }
        else if(now < 1)
        {
            setNow(max-1);
        }
        document.getElementById('inDiv').getElementsByTagName('a')[now].style.backgroundColor=hoverColor;
        document.getElementById('links').value = document.getElementById('inDiv').getElementsByTagName('a')[now].href;
        
        var s=document.getElementById('links2');        
        if(s)
        {
        document.getElementById('links2').value = document.getElementById('inDiv').getElementsByTagName('a')[now].href;
        }  
    } 
 }
 
 function Change(e)
 {
    e = e || window.event;
    e_keyCode = e.keyCode;
    if(e_keyCode == 38)
    {
        up(); 
        
        var s=document.getElementById('searchBox2');
        
        if(s)
        { 
         document.getElementById('searchBox2').value=document.getElementById('inDiv').getElementsByTagName('a')[now].innerHTML.replace(/<[^>]+>/g,""); //knight
        }            
         document.getElementById('searchBox').value=document.getElementById('inDiv').getElementsByTagName('a')[now].innerHTML.replace(/<[^>]+>/g,""); //knight
         
       
    }
    if(e_keyCode == 40)
    {
        down();
        var s=document.getElementById('searchBox2');
        //document.getElementById('links').value = document.getElementById('inDiv').getElementsByTagName('a')[now].href.value;
        if(s)
        { 
         document.getElementById('searchBox2').value=document.getElementById('inDiv').getElementsByTagName('a')[now].innerHTML.replace(/<[^>]+>/g,""); //knight
        }  
        document.getElementById('searchBox').value=document.getElementById('inDiv').getElementsByTagName('a')[now].innerHTML.replace(/<[^>]+>/g,""); //knight        
    }
    
//    if(e_keyCode == 13)
//    {
//    
//    alert("1111");   
//        return;
//    }   
    
    
 }
 
 window.onload = function()
 {
    document.onkeydown = Change;
 }