function showdrop()
{
	dropbox=document.getElementById("dropbox");
	if (dropbox) {
		dropbox.style.display='block';
	}
}

function hidedrop(id)
{
	setTimeout("commit_hidedrop('"+id+"')", 400); // needs to be long enough or links don't get followed
}

function commit_hidedrop(id)
{
	dropbox=document.getElementById(id+"dropbox");
	if (dropbox) {
		dropbox.style.display='none';
	}
}

function searchkey(input,id)
{
	dropbox=document.getElementById(id+"dropbox");
	//newMessage=document.createTextNode(input.value);

	if (input.value.length)
	{
		if (arguments.length >2)
		{
			url="http://bowlsclub.org/XMLsearch.php?query="+input.value+"&id="+id;
			
			// need to read country/region etc parameters
			countryElement=document.getElementById(arguments[2]);
			if (countryElement) {
				// url+="&country="+countryElement.value
				url+="&Country="+countryElement.options[countryElement.selectedIndex].value;
			}
			
			regionElement=document.getElementById(arguments[3]);
			if (regionElement) {
				// url+="&state="+regionElement.value
				url+="&State="+regionElement.options[regionElement.selectedIndex].value;
			}
			
			typeElement=document.getElementById(arguments[4]+"1");
			if (typeElement && typeElement.checked) {
				url+="&lawn=lawn";
			}
			typeElement=document.getElementById(arguments[4]+"2");
			if (typeElement && typeElement.checked) {
				url+="&crown=crown";
			}
			typeElement=document.getElementById(arguments[4]+"3");
			if (typeElement && typeElement.checked) {
				url+="&indoor=indoor";
			}
			typeElement=document.getElementById(arguments[4]+"4");
			if (typeElement && typeElement.checked) {
				url+="&carpet=carpet";
			}
			typeElement=document.getElementById(arguments[4]+"5");
			if (typeElement && typeElement.checked) {
				url+="&shortmat=shortmat";
			}
			
			strictElement=document.getElementById(arguments[5]);
			if (strictElement && strictElement.checked) {
				url+="&strict=strict";
			}
			
			load_xml_doc(url);
		}
		else
		{
			load_xml_doc("http://bowlsclub.org/XMLsearch.php?query="+input.value+"&id="+id);
		}
	}
	else
	{
		commit_hidedrop(id);
	}

	// dropbox.replaceChild(newMessage,dropbox.firstChild);
	//clearbox(dropbox);
	//dropbox.appendChild(newMessage);
}

function clearbox(dropbox)
{
	while (dropbox.hasChildNodes())
	{
		dropbox.removeChild(dropbox.lastChild);
	}
}

function load_xml_doc(url)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    }
    else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req)
        {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange()
{
    // only if req shows "complete" and OK
    if (req.readyState == 4)
    {
        // only if "OK"
        if (req.status == 200)
        {
      		response  = req.responseXML.documentElement;

      		clubs = response.getElementsByTagName('club');
      		id    = response.getElementsByTagName('id')[0].firstChild.data;
      		query=document.getElementById(id).value;
      		document.cookie="query="+escape(query)+';path=/';

			dropbox=document.getElementById(id+"dropbox");

      		if (clubs.length >0)
      		{
      			dropbox.style.display='block';
      			clearbox(dropbox);

      			for (i=0; i<clubs.length;i++)
      			{
   					clubId=clubs[i].getAttribute('id');
   					
   					if (clubId !=null)
   					{
      					clubLink=document.createElement("a");
      					clubLink.setAttribute("href","/club/"+clubId+"/");
      					clubLink.setAttribute("title",clubs[i].firstChild.data);
      					clubLink.appendChild(document.createTextNode(clubs[i].firstChild.data));
      					dropbox.appendChild(clubLink);
      					dropbox.appendChild(document.createElement("br"));
      				}
      				else
      				{
      					assocId=clubs[i].getAttribute('associd');
      					assocType=clubs[i].getAttribute('type');
      					assocCountry=clubs[i].getAttribute('country');
      					clubLink=document.createElement("a");
						clubLink.setAttribute("href","/associations/"+assocType+"/"+assocCountry+"/"+assocId+"/");
						clubLink.setAttribute("title",clubs[i].firstChild.data);
						clubLink.appendChild(document.createTextNode(clubs[i].firstChild.data));
						dropbox.appendChild(clubLink);
      					dropbox.appendChild(document.createElement("br"));
      				}
      			}

      			if (response.getElementsByTagName('more')[0].firstChild.data =="1")
      			{
      				clubLink=document.createElement("a");
					//clubLink.setAttribute("style","font-weight: bold;");
      				clubLink.style.fontWeight="bold";
      				
      				clubLink.setAttribute("href","/search.php?query="+query+"&search=search");
					clubLink.setAttribute("title","show more search results");
					clubLink.appendChild(document.createTextNode("more..."));
      				dropbox.appendChild(clubLink);
      			}

      		}
      		else
      		{
      			dropbox.style.display='none';
      		}


      		//method    = response.getElementsByTagName('method')[0].firstChild.data;
      		//result    = response.getElementsByTagName('result')[0].firstChild.data;

      		//eval(method + '(\'\', result)');
        }
    }
}
window.onload=function(){document.getElementById("query") && document.getElementById("query").focus();};
