
/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function RemoteSuggestions(sEngine) {

    if (typeof XMLHttpRequest != "undefined") {
        this.http = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        this.http = new ActiveXObject("MSXML2.XmlHttp");
    } else {
        //alert("No XMLHttpRequest object available. This functionality will not work.");
    }
	this.engine = sEngine;	
}

/**
 * Request suggestions for the given autosuggest control. 
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
RemoteSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {

    var oHttp = this.http;
	var sEngine = this.engine;
	
	// wie viele Vorschläge?
	var numSuggestions = 7;
                                                             
    //if there is already a live request, cancel it
    if (oHttp.readyState != 0) {
        oHttp.abort();
    }                 
    
    //build the URL
    var sURL = "/suggest/suggest.php?engine=" + sEngine + "&qu=" + encodeURIComponent(oAutoSuggestControl.textbox.value);
    
    oHttp.open("get", sURL , true);
    oHttp.onreadystatechange = function () {
        if (oHttp.readyState == 4) {
			
			var aSuggestions = new Array();
			var a = new Array();
			
			//evaluate the returned text JavaScript (an array)
            var a = eval(oHttp.responseText); 
            
			// Nachbearbeitung der JSON-Response
			switch (sEngine) {
				case "google" :
					for (i=0;i<numSuggestions;i++) {
						if (a[1][i]) aSuggestions[i] = a[1][i];
					}
				break;
				
				case "amazon" :
					for (i=0;i<numSuggestions;i++) {
						if (a[1][i]) aSuggestions[i] = a[1][i];
					}
				break;
				
				case "wiki" :
					for (i=0;i<numSuggestions;i++) {
						if (a[1][i]) aSuggestions[i] = a[1][i];
					}
				break;
			}			
       
			// Auswahlzähler zurücksetzen
			oAutoSuggestControl.cur = -1;

            //provide suggestions to the control
            oAutoSuggestControl.autosuggest(aSuggestions, false);        
        }    
    };
    oHttp.send(null);
};
