// Functions used in conjunction with the yui autocomplete library

// This function defines a site-wide standard set of autocomplete configuration options.
// Override these values in a particular instance by resetting after the function returns 
// (easier than passing in parameters).
function setDefaultACVals(acObj, inputId, useIFrame, noSubmitOnEnter) {
	
	noSubmitOnEnter = noSubmitOnEnter || true;

	// Work around IE6 select element z-index bug (select elements always show on top, regardless of z-indices).
	// RY 7/31/07 Caution here: with the iframe shim, the word "false" appears sporadically in the autocomplete
	// results. I have no idea what's going on, but setting useIFrame = false seems to solve the problem.
	// So changing default value to false.
	// acObj.useIFrame = useIFrame != undefined ? useIFrame : true;
	acObj.useIFrame = useIFrame || false;

	acObj.maxResultsDisplayed = 20;
	acObj.minQueryLength = 1;
	acObj.queryDelay = 0.2;
		
	// Not forcing a selection is more user-friendly. For example, in a name search field, users might want
	// to search on all the Toms, without being forced to select a specific one. 
	acObj.forceSelection = false;
	acObj.allowBrowserAutocomplete = false;  

	// Use an anonymous function as a wrapper to pass the params; following generates an error
	// because params are undefined on page load:
	// acObj.formatResult = formatACResult(aResultItem, sQuery); 
	acObj.formatResult = function (aResultItem, sQuery) { 	
		return formatACResult(aResultItem, sQuery);
	};   
	
	// Stop form submission when the Enter key is pressed in the autocomplete text field, 
	// since Enter is used to select one of the autocomplete suggestions.  
	// If this is the only field in a form (e.g., a search form), we may want to
	// submit the form on enter, so it's now parameterized. RY 7/10/07
	// Using YAHOO.util.Event.addListener doesn't work, because we actually
	// want to override the previously-assigned event handler rather than adding to it.
	// YAHOO.util.Event.addListener(inputID, "keypress", stopSubmissionOnEnter);
	if (noSubmitOnEnter){
		document.getElementById(inputId).onkeypress = stopSubmissionOnEnter;
	}
}

// Format the autocomplete results. aResultItem = autocomplete result.
// sQuery = query string.
function formatACResult(aResultItem, sQuery) { 	
	var sKey = aResultItem[0],
		// Split sQuery so we can bold non-contiguous matching words.
		// E.g., sQuery = "John Smith" and matching string = "John A. Smith".
		// Trim trailing whitespace before the split, else it gets included in the array.
		sQueryTerms = sQuery.replace(/\s+$/, "").split(/\s+/),
		aMarkup = sKey,
		match,
		sQueryRe,
		termRe;
	for (var t = 0; t < sQueryTerms.length; t++) {
		// Use \\b to ensure the match is word-initial. E.g., sQuery = "In",
		// match = "Organizing Institute", bold "In" rather than preceding "in"
		termRe = new RegExp("\\b" + sQueryTerms[t], "i"); 
		// We use match() before replace() to bold the original matching substring
		// rather than a direct replace(), which bolds the query string:
		// aMarkup.replace(termRe, "<span style='font-weight:bold'>" + termRe + "</span>")
		// E.g., if the matching string is "Thomas" and the query string is "TH",
		// we want to show the result "Thomas" with "Th" bold, rather than "THomas"
		// with "TH" bold.
		match = sKey.match(termRe);
		// console.log("Number of terms: " + sQueryTerms.length + "; termRe: " + termRe + "; Match: " + match);
		// Use dummy symbols to temporarily mark the chars to be bolded, since if we insert
		// the span tag directly, it could be bolded on next loop iteration if search term is "span".
		aMarkup = aMarkup.replace(match, "<span style='font-weight:bold'>" + match + "</span>");
	}

	return aMarkup;
}

// Format the autocomplete results. aResultItem = autocomplete result.
// sQuery = query string.
function formatACResult_contiguousStringOnly(aResultItem, sQuery) { 	
	var sKey = aResultItem[0]; // the entire result key
	// Use \\b to ensure the match is word-initial. E.g., sQuery = "In",
	// match = "Organizing Institute", bold "In" rather than preceding "in"
	var sQueryRe = new RegExp("\\b" + sQuery, "i");
	// We use match() before replace() to bold the original matching substring
	// rather than a direct replace(), which bolds the query string:
	// sKey.replace(sQueryRe, "<span style='font-weight:bold'>" + sQuery + "</span>")
	// E.g., if the matching string is "Thomas" and the query string is "TH",
	// we want to show the result "Thomas" with "Th" bold, rather than "THomas"
	// with "TH" bold.
	var match = sKey.match(sQueryRe);
	var aMarkup = [
		sKey.replace(match, "<span style='font-weight:bold'>" + match + "</span>")
		];  
	return (aMarkup.join(""));
}

// Stop form submission when the Enter key (keyCode 13) is pressed in an autocomplete text field, 
// since Enter is used to select one of the autocomplete suggestions. e is the event object.  
// e is defined because in the DOM Level 2 event model the event object is always passed as an
// argument to the event handler.
function stopSubmissionOnEnter(e) {

	// The keypress event is not defined for non-printing function keys in IE, so test for the
	// existence of the event first to prevent an error.
	if (typeof e != "undefined" && e.keyCode == 13) {
		return false;
	}
	return true;
}
