// This define's auto complete fields on the page
function defineOneField() {
	// AFieldType = 1 for company and 2 for categories
	// var AFieldType = 2; Product by default.

	// Only proceed if we've got an input box for this type of field
	if (document.getElementById("autoCmp")) {
		// Create the datasource required
		var dsData = new YAHOO.widget.DS_XHR("/data/sitepro_data.asp", ["result", "thedesc", "theurl"]);
		dsData.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
		dsData.scriptQueryParam = "q";
		
		// Hook the data source onto the field (if the fields don't exist, the Yahoo stuff crashes gracefully)
	 	var autoCField = new YAHOO.widget.AutoComplete("autoCmp", "autoCmpResults", dsData);

		// Disable the browser's built-in autocomplete caching mechanism 
		autoCField.allowBrowserAutocomplete = false; 
		autoCField.highlightClassName = "highLight";
		autoCField.autoHighlight = false;


		// Set the search type to be calculated before the XML feed is requested
		autoCField.doBeforeSendQuery = function(sQuery) {
			// This will add the type of search we require to the end of the search term given (and
			//   therefore to the end of the URL)
			var iSearchType = 1;	// 1=Company search, 2=product search
			if (document.getElementById("type_product").checked==true) {
				iSearchType = 2;
			}
			
			return sQuery + "&type=" + iSearchType;
		};

		// Format the results
		autoCField.formatResult = function(aResultItem, ATextSearchFor) {
			// This format's a single result
			var strMatchedPart = aResultItem[0].substr(0, ATextSearchFor.length); // The part that matches what they typed
			var strTheRest = aResultItem[0].substr(ATextSearchFor.length); 		  // The result of the matched string without the matched part

			// This is where you define your markup
			// var aRetval = ["<a href=\"\"><strong>" + strMatchedPart + "</strong>" + strTheRest + "</a>"];
			var aRetval = ["<span><strong>" + strMatchedPart + "</strong>" + strTheRest + "</span>"];
			return (aRetval.join(""));
		}
	}
}

function setupAutoCompleteFields() {
	defineOneField();
//	defineOneField("2");
}

// Add to the onload event
addEventToObject(window, 'onload', setupAutoCompleteFields);

