/*
 $Source: /home/cvs/bw/directory/web/javascript/tools.js,v $
 $Revision: 1.4 $
 
 Copyright 2001 Causeway Technologies Ltd.
 
 */

/*
 * Incremental Select
 *
 * Created:     09/11/01
 * Author:      Terry Matthews
 * Description: This will provide incremental search functionality for html
 *              select drop-down boxes.
 *
 * To use it add an onkeypress="doIncrementalSelect(this);" to the 
 * select tag, eg.
 *
 *     <select name=test onkeypress="doIncrementalSelect(this);">
 *         <option>Lots of options go here
 *     </select>
 */

currentSearch   = ""; // Used to hold the current search string for 
                      // incremental searching
currentSelected = ""; // The last found text. Used for checking we are searching
                      // with the same select.

function doIncrementalSelect(comboBox) {
	if(document.all) {
		nbr = event.keyCode;
		searchIndex = -1;
		// Check to make sure the select hasn't changed
		if (comboBox.selectedIndex == -1 || 
			currentSelected != comboBox.options[comboBox.selectedIndex]) {
			currentSearch   = "";
		}
		// Add the new key to the search
		currentSearch += String.fromCharCode(nbr);
		switch(nbr) {
			case 13: // CR
				break;
			case  8: // Backspace
				// Remove the backspace and the last character from the search
				currentSearch = currentSearch.slice(0, currentSearch.length - 2);
			default: 
				searchIndex = searchOptions(comboBox.options, currentSearch);
				// Re-start the search on no-match with the last character enetered
				if (searchIndex == -1) {
					currentSearch = String.fromCharCode(nbr);
					searchIndex = searchOptions(comboBox.options, currentSearch);
				}     
				// If it's still not found then go to the first option
				if (searchIndex == -1) {
					currentSearch = " ";
					searchIndex   = 0;
				}
				// Set the option and store for later comparison
				comboBox.selectedIndex = searchIndex;
				currentSelected        = comboBox.options[comboBox.selectedIndex];
				// Remove the keystroke from the event queue
				event.returnValue = 0;
		}
	}
}

function searchOptions(options, searchText) {
    searchArray = -1;
    searchText  = searchText.toUpperCase();
    
    // Search for a matching option
    for (i = 0; i <= options.length - 1; i++) {
        if (options[i].text.toUpperCase().indexOf(searchText) == 0) {
            searchIndex = i;
            break;
        }
    }
    
    return searchIndex;
}


    
// End of incremental select code

    /**
     * trimWhitespace(str) - use this on form values before they are checked
     * for zero-length string on form validation to prevent users from entering
     * blank spaces.
    **/
    
    function trimWhitespace(str) {
        return str.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1');
    }

    // This is here 'cos I don't know why Jamie named it trimWhitespace?!!
    function trim(str) {
        return trimWhitespace(str);
    }
