// This define's auto complete fields on the page
function defineOneField(AFieldType) {
	// AFieldType = 1 for company and 2 for categories

	// Only proceed if we've got an input box for this type of field
	if (document.getElementById("autoCmpType" + AFieldType)) {
		// Create the datasource required
		var dsData = new YAHOO.widget.DS_XHR("/feeds/sitepro_data.asp", ["result", "thedesc", "theurl"]);
		dsData.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
		dsData.scriptQueryParam = "q";

		if (AFieldType == 1)
			dsData.scriptQueryAppend = "type=dir";
		else
			dsData.scriptQueryAppend = "type=cat";
		
		
		// Hook the data source onto the field (if the fields don't exist, the Yahoo stuff crashes gracefully)
	 	var autoCField = new YAHOO.widget.AutoComplete("autoCmpType" + AFieldType, "autoCmpType" + AFieldType + "Results", dsData);

		// Disable the browser's built-in autocomplete caching mechanism 
		autoCField.allowBrowserAutocomplete = false; 
		autoCField.highlightClassName = "highLight";
		autoCField.autoHighlight = false;

		// 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("1");
	defineOneField("2");
}

// Add to the onload event
addEventToObject(window, 'onload', setupAutoCompleteFields);

