var jo_autocomplete = null;
var jo_bac_temp = null;
var JO_MATCH_ALL_JOBS = 'view all jobs matching';

// Attach ajax 'dropdown search' to an input field
// $1 - action to call
// $2 - id of search input
// $3 - id of field which recieves the value fo the search input
// $4 - id of search progress indicator
// $5 - class of the drop down search results
// $6 - number of characters to be typed before performing search
function joAttach(actionUrl, sourceId, targetId, progressIndicator, dropdownCSS, charsBeforeSearch) {

	var beforeAutoComplete = new function() {
		if (progressIndicator) {
			//alert('showing');
			$(progressIndicator).hide();
		}
		// save current search value incase all results are selected from dropbox
		var e = $(kwSearchId);
		if (e) {
			var v = e.value;
			if (v) {
				jo_bac_temp = v;
			}
		}
	}

    jo_autocomplete = new AjaxJspTag.Autocomplete(
        actionUrl, {
        indicator: progressIndicator,
        preFunction: beforeAutoComplete,
        postFunction: joStripSearchCount,
        minimumCharacters: charsBeforeSearch,
        parser: new ResponseXmlToHtmlListParser(),
        target: targetId,
        className: dropdownCSS,
        source: sourceId
    });
    
    if (progressIndicator) {
		$(progressIndicator).hide();
	}
    
}


// revert to search term if all jobs and removed result count from form value
function joStripSearchCount(options) {
	var lastDelim = " (";
	var e = $(kwSearchId);
	if (e) {
		var v = e.value;
		if (v) {
			//alert('back to orig: ' + jo_bac_temp + ': ' + (v.startsWith(JO_MATCH_ALL_JOBS)));
			if (v.startsWith(JO_MATCH_ALL_JOBS)) {
				// revert back to search term on click if matching all jobs
				//e.value = jo_bac_temp;
				var newval = v.substring(JO_MATCH_ALL_JOBS.length, i);
				newval = newval.substring(0, newavl.lastIndexOf("("));
				e.value = newval;
				alert('starts with ' + JO_MATCH_ALL_JOBS + " setting " + e.id + " to " + newval);
			} else {
				var i = v.lastIndexOf(lastDelim);
				if (i != -1) {
					e.value = v.substring(0, i);
					// hacky: set jobtitle param so that search can be exact rather than 'like' fragment matches
					var jobtitle = $("jobtitle");
					if (jobtitle) {
						jobtitle.value = e.value;
					}
				}
			}
		}
		e.form.submit();
	}
}

// Ajax call made after the location select box is changed
function joOnLocationSelected() {
	//mop=LocationSelectedWrapper&
    var url = '/act/ProtojLocChg?cnt=' + encodeURIComponent($F('cnt'));
    var myAjax = new Ajax.Request(url, { method: 'get' });
}

// remove the default search message as the 'value' of the search field if the user
// has not bothered to type anything else this will return a search against
// this default value
function joBlankIfDefault(fieldId, defaultValue) {
	var blankField = $(fieldId)
	if ( blankField && blankField.value == defaultValue ) {
		blankField.value= '';
	}
}

// called when onBlur even is notified on search field
function joSearchBlur(e) {
	if (e.value == '') {
		e.value = kwSearchValue;
		e.style.color = kwSearchBlurColor;
	}
}

// called when onFocus even is notified on search field
function joSearchFocus(e) {
	if (e.value == kwSearchValue) {
		e.value = '';
		e.style.color = kwSearchFocusColor;
	}
}
