// global variable to manage the timeout
var t;
// Start a timeout with each keypress
function StartSearch() {
   if (t) window.clearTimeout(t);
   t = window.setTimeout("LiveSearch()",200);
}
// Perform the search
function LiveSearch() {
   // assemble the PHP filename
   query = document.getElementById("searchlive").value;
   filename = "search.php?query=" + query;
   // DisplayResuls will handle the Ajax response
   ajaxCallback = DisplayResults;
   // Send the Ajax request
   ajaxRequest(filename);
}

// Display search results
function DisplayResults() 
	 {
   // remove old list
   ul = document.getElementById("list");
   div = document.getElementById("results");
   div.removeChild(ul);
   
	 // make a new list
   		ul = document.createElement("UL");
   		ul.id="list";
			
			// Get the data from the XML:
      //names = ajaxreq.responseXML.getElementsByTagName("name");
			var data = ajaxreq.responseXML;
			var url_ids = data.getElementsByTagName('url_id');
			var names = data.getElementsByTagName('org_name');
			var addresses = data.getElementsByTagName('address');
			var phones = data.getElementsByTagName('phone');
			//var emails = data.getElementsByTagName('email');
			
   		// Declare some necessary variables:
        var li, span, name, name_node, address_node, address_label, br, strong, a, email, url, phone, org_id;
      
			
		 for (i = 0; i < names.length; i++) 
		  {
			 	 	li = document.createElement('LI');
          
          //span = document.createElement('span');
          //span.setAttribute('class', 'name');
          //name = document.createTextNode(names[i].firstChild.nodeValue);
          //span.appendChild(name);
          //li.appendChild(span);
          
					a_1 = document.createElement('a');
					a_1.setAttribute('href', '../directory.php?id=' + url_ids[i].firstChild.nodeValue);
          //a_1.setAttribute('href', '#?id=' + url_ids[i].firstChild.nodeValue);
          //url = document.createTextNode(urls[i].firstChild.nodeValue);
					url_node_1 = document.createTextNode(names[i].firstChild.nodeValue);
          a_1.appendChild(url_node_1);
          li.appendChild(a_1);
          
          //br = document.createElement('br');
          //li.appendChild(br);
          
					spacer1_node = document.createTextNode(' ');
          li.appendChild(spacer1_node);
					
          strong = document.createElement('strong');
          address_label = document.createTextNode('Address');
          strong.appendChild(address_label);
          li.appendChild(strong);
          address_node = document.createTextNode(': ' + addresses[i].firstChild.nodeValue);
          li.appendChild(address_node);
					
					spacer2_node = document.createTextNode(' ');
          li.appendChild(spacer2_node);
					
					strong = document.createElement('strong');
          phone_label = document.createTextNode('Ph');
          strong.appendChild(phone_label);
          li.appendChild(strong);
          phone_node = document.createTextNode(': ' + phones[i].firstChild.nodeValue);
          li.appendChild(phone_node);
          
          //br = document.createElement('br');
          //li.appendChild(br);
          
          //a = document.createElement('a');
          //a.setAttribute('href', 'mailto:' + emails[i].firstChild.nodeValue);
          //email = document.createTextNode(emails[i].firstChild.nodeValue);
          //a.appendChild(email);
          //li.appendChild(a);
					
					
					//spacer3_node = document.createTextNode(' ');
          //li.appendChild(spacer3_node);
					
          //a = document.createElement('a');
          //a.setAttribute('href', 'viewListing.php?id=' + url_ids[i].firstChild.nodeValue);
          //url_node = document.createTextNode('view');
          //a.appendChild(url_node);
          //li.appendChild(a);
					
          ul.appendChild(li);
   		}
			
   		if (names.length==0) 
	 		{
      li = document.createElement("LI");
      li.appendChild(document.createTextNode("No results"));
      ul.appendChild(li);
   		}
   
	 		// display the new list
   		div.appendChild(ul);
		}

// set up event handler
obj=document.getElementById("searchlive");
obj.onkeydown = StartSearch;