// JScript File

var isSearching = false;
var itemKList = [];
var currentSelection = -1;
var isFocus = false;

function urlencode( str ) {
    var hash_map = {}, unicodeStr='', hexEscStr='';
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    
    ret = encodeURIComponent(ret);
 
    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr];
        ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
    }
    
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	 	// Firefox, Opera 8.0+, Safari
	 	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	 	//Internet Explorer
		 try
		 {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 }
		 catch (e)
		 {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		 }
	}
	return xmlHttp;
}
function StateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		isSearching = false;
		var str = xmlHttp.responseText;
		if(str != "")
		{
			document.getElementById("SearchSuggestion").style.visibility = "visible";
			document.getElementById("SearchSuggestion").innerHTML=str;
			currentSelection = -1;
		}
		else
		{
	 		document.getElementById("SearchSuggestion").style.visibility = "hidden";
		}
	} 
}

function ExecuteAjax(url)
{
    xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		 alert ("Browser does not support HTTP Request");
		 return;
	}
	xmlHttp.onreadystatechange=StateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function displaySearchSuggestion(strSearch,e)
{
	var evt = (e) ? e : window.event;
    var key = (evt.keyCode) ? evt.keyCode : evt.which;
	if(key == 13){
		if(currentSelection > -1){
			document.getElementById("suggestion").value = "yes";
		}
		document.getElementById("SearchSuggestion").style.visibility = "hidden";
		document.frmSearch.submit();
	}else if(key == 38){
		kUp();
	}else if(key == 40){
		kDown();
	}else{
		currentSelection = -1;
		var url = "searchSuggestion.php?q=" + urlencode(strSearch);
		ExecuteAjax(url);
	}
}
function writeSearch(val)
{
	document.getElementById("suggestion").value = "yes";
	document.getElementById("txtSearch").value = val;
	document.getElementById("SearchSuggestion").style.visibility = "hidden";
	document.frmSearch.submit();
}
function hideList()
{
	if(isFocus==false){
		document.getElementById("SearchSuggestion").style.visibility = "hidden";
	}
}
function showMe()
{
	document.getElementById("SearchSuggestion").style.visibility = "visible";
}
function kDown()
{
	var objSContainer = document.getElementById("SearchSuggestion");
	itemKList = objSContainer.getElementsByTagName("div");
	currentSelection++;
	if(currentSelection >= itemKList.length){
		currentSelection = 0;
	}
	for(i=0; i<itemKList.length; i++){
		if(i == currentSelection){
			itemKList[i].className = "hover";
			document.getElementById("txtSearch").value = itemKList[i].innerHTML;
		}else{
			itemKList[i].className = "normal";
		}
	}
	
}
function kUp()
{
	var objSContainer = document.getElementById("SearchSuggestion");
	itemKList = objSContainer.getElementsByTagName("div");
	currentSelection--;
	if(currentSelection <= -1){
		currentSelection = itemKList.length - 1;
	}
	for(i=0; i<itemKList.length; i++){
		if(i == currentSelection){
			itemKList[i].className = "hover";
			document.getElementById("txtSearch").value = itemKList[i].innerHTML;
		}else{
			itemKList[i].className = "normal";
		}
	}
	
}
function itemHover(Obj)
{
	Obj.className = "hover";
}
function itemOut(Obj)
{
	Obj.className = "normal";
}

