﻿	function getDictionary() {
		var list = document.forms[0].dict;
		var dictionary = list.options[list.selectedIndex].value;
		return dictionary;
	}
	function changeDict() {
			if (getDictionary() == "jaen" || getDictionary() == "zhen" || getDictionary() == "koen"){
				oAC.minQueryLength = 1;
				oAC.maxResultsDisplayed  = 1000;
			} else {
				oAC.minQueryLength = 2;
				oAC.maxResultsDisplayed  = 15;
			}
	}

	document.getElementById('fSelect').onchange = changeDict;
	document.body.className = "yui-skin-sam";
	YAHOO.example.BasicRemote = function() {

	// if not Firefox on OS X, cause of need to hit enter twice there
	if (navigator.userAgent.search(/OS X.*Firefox/gi) == -1)
	{
		// Use an XHRDataSource
		var oDS = new YAHOO.util.XHRDataSource("/autocomplete/autocompletedb.aspx");
		// Set the responseType
		oDS.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT;
		// Define the schema of the delimited results
		oDS.responseSchema = {
			recordDelim: "\n",
			fieldDelim: "\t"
		};
		// Enable caching
		oDS.maxCacheEntries = 1000;
		oDS.queryMatchSubset = true;
		// Instantiate the AutoComplete
		var oAC = new YAHOO.widget.AutoComplete("si", "myACContainer", oDS);
		oAC.autoHighlight = false; 
		oAC.queryDelay = 0; 
		oAC.minQueryLength = 2;
		oAC.maxResultsDisplayed  = 15;
//	YAHOO.example.BasicRemote = function() {
		oAC.generateRequest = function(sQuery) {return "?dict=" + getDictionary() + "&query=" + sQuery;};
		return {
			oDS: oDS,
			oAC: oAC
		};
	}
	}();

/**
 * overrides yahoo's script
 */

YAHOO.widget.AutoComplete.prototype.filterResults = function(sQuery, oFullResponse, oParsedResponse, oCallback) {
    // If AC has passed a query string value back to itself, grab it
    if(oCallback && oCallback.argument && oCallback.argument.query) {
        sQuery = oCallback.argument.query;
    }

    // Only if a query string is available to match against
    if(sQuery && sQuery !== "") {
        // First make a copy of the oParseResponse
        oParsedResponse = YAHOO.widget.AutoComplete._cloneObject(oParsedResponse);
        
        var oAC = oCallback.scope,
            oDS = this,
            allResults = oParsedResponse.results, // the array of results
            filteredResults = [], // container for filtered results
            bMatchFound = false,
            bMatchCase = (oDS.queryMatchCase || oAC.queryMatchCase), // backward compat
            bMatchContains = (oDS.queryMatchContains || oAC.queryMatchContains); // backward compat
            
        // Loop through each result object...
        for(var i = allResults.length-1; i >= 0; i--) {
            var oResult = allResults[i];

            // Grab the data to match against from the result object...
            var sResult = null;
            
            // Result object is a simple string already
            if(YAHOO.lang.isString(oResult)) {
                sResult = oResult;
            }
            // Result object is an array of strings
            else if(YAHOO.lang.isArray(oResult)) {
                sResult = oResult[0];
            }
            // Result object is an object literal of strings
            else if(this.responseSchema.fields) {
                var key = this.responseSchema.fields[0].key || this.responseSchema.fields[0];
                sResult = oResult[key];
            }
            // Backwards compatibility
            else if(this.key) {
                sResult = oResult[this.key];
            }
            
            if(YAHOO.lang.isString(sResult)) {
                
                var sKeyIndex = (bMatchCase) ?
                sResult.indexOf(decodeURIComponent(sQuery)) :
                deaccent(sResult.toLowerCase()).indexOf(deaccent(decodeURIComponent(sQuery).toLowerCase()));

                // A STARTSWITH match is when the query is found at the beginning of the key string...
                if((!bMatchContains && (sKeyIndex === 0)) ||
                // A CONTAINS match is when the query is found anywhere within the key string...
                (bMatchContains && (sKeyIndex > -1))) {
                    // Stash the match
                    filteredResults.unshift(oResult);
                }
            }
        }
        oParsedResponse.results = filteredResults;
    }
    else {
    }
    
    return oParsedResponse;
};

function deaccent(str) {
  var replace = ["á","é","í","ó","ú","ä","ï","ö","ü","ã","ñ","õ","à","è","ì","ò","ù","ç"];
  var by =        ["a","e","i","o","u","a","i","o","u","a","n","o","a","e","i","o","u","c"];
  for (var i=0; i<replace.length; i++) {
     str = str.replace(replace[i], by[i]);
  }
  return str;
} 