var req;
var h=window.location.host;
var h='www.apymem.com';
function Initialize(){
	try{
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc){
			req=null;
		}
	}
	if(!req&&typeof XMLHttpRequest!="undefined"){
		req=new XMLHttpRequest();
	}
}
function SendQuery(key){
	key = key.replace(/^\s*|\s*$/g,"");
	if(key){
	Initialize();
	var u="http://"+h+"/autocomplete.php?q="+key;
	if(req!=null){
		req.onreadystatechange = Process;
		req.open("GET", u, true);
		req.send(null);
	}
	}
}
function autocomplete(n, ac_array){
try{
    if (event.keyCode == 8 && n.backspace){
        n.value = n.value.substr(0,n.value.length-1);
        n.backspace = false;
    }
    var r = n.createTextRange();    
    tmp= n.value;
    if (tmp == "")return 0;
    for (z=0;z<ac_array.length;z++){
        tmp2 = ac_array[z];
        count = 0;
        for (i = 0;i<tmp.length;i++){
            if (tmp2.charAt(i) == tmp.charAt(i)){
                count++
            }
        }
        if (count == tmp.length){
            diff = tmp2.length - tmp.length;
            if (diff <= 0) break;
            kap = "";
            for (i=0;i<tmp2.length;i++){
                if (i >= tmp.length) kap += tmp2.charAt(i);
            }
            n.backspace = true;
            r.text += kap;
            r.findText(kap,diff*-2);
            r.select();
            return 0;
        }
    }
    n.backspace = false;
    return 0;
	}catch(e){}
}
function Process(){

	if (req.readyState == 4){
		if (req.status == 200){
			if(req.responseText=="")HideDiv("autocomplete");
			else{
				var responseText_array=req.responseText.split("\n");
				var results = '<table width="100%"  border="0" cellspacing="0" cellpadding="0">';
				var line_array= new Array;
				var ac_array = new Array;
				for(var i=0;i<responseText_array.length-1;i++){
					line_array=responseText_array[i].split(":|:");
					if(line_array[1].length < 40){
						results += '<tr style="color:#000000;" onClick="document.location.href=\'/search?query='+encodeURIComponent(line_array[0]).replace(/(\'|\")/g,"\\'")+'\'" ';
						results += ' onMouseOver="this.style.backgroundColor=\'#FFDF32\';this.style.color=\'#000000\';this.style.cursor=\'hand\';" ';
						results += ' onMouseOut="this.style.backgroundColor=\'#FFFFFF\';this.style.color=\'#000000\';">';
						results += '<td class="key">&nbsp;';
						results += line_array[0].toLowerCase().replace(/(\'|\")/g,"\'");
						results += '</td>';
						results += '</tr>';
						ac_array.push(line_array[0].replace(/(\'|\")/g,"\\'"));
					}
	
				}
				results += '</table>';
				ShowDiv("autocomplete");
				document.getElementById("autocomplete").innerHTML =results;
				autocomplete(document.getElementById('q'), ac_array);
				//window.setTimeout("HideDiv('autocomplete')", 1000);
			}
		}
		else{
			document.getElementById("autocomplete").innerHTML="There was a problem retrieving data:<br>"+req.statusText;
		}
	}
}
function autocom(arr){
autocomplete(document.getElementById('q'), arr);
}
function ShowDiv(divid){
	if (document.layers)
		document.layers[divid].visibility="show";
	else
		document.getElementById(divid).style.visibility="visible";
}

function HideDiv(divid){
	if (document.layers) 
		document.layers[divid].visibility="hide";
	else 
		document.getElementById(divid).style.visibility="hidden";
}

function BodyLoad(){
	setLyr();
	HideDiv("autocomplete");
	document.f.q.focus();
}
// get element's height
function domGetHeight(obj) {
	var h = 20;
	if (document.layers) 
		h = obj.height;
	else 
		h = obj.style.height;
	return h;
}
function setLyr()
{
	var obj = document.getElementById('q');
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	var newH = domGetHeight(obj);
	var div = document.getElementById('autocomplete');
	//newY += domGetHeight();
	newY += 20;
	div.style.top = newY + 'px';
	div.style.left = newX + 'px';
}
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function rLyr(){
	HideDiv("autocomplete");
}