﻿/**
* Provides suggestions for state names (USA).
* @class
* @scope public
*/
function RemoteStateSuggestions() {

    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.");
    }

}

/**
* Request suggestions for the given autosuggest control. 
* @scope protected
* @param oAutoSuggestControl The autosuggest control to provide suggestions for.
*/
RemoteStateSuggestions.prototype.requestSuggestions = function(oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {

    var oHttp = this.http;

    //if there is already a live request, cancel it
    if (oHttp.readyState != 0) {
        oHttp.abort();
    }

    var thelexicon = oAutoSuggestControl.lexicon;
    var thehost = oAutoSuggestControl.host;
    var theport = oAutoSuggestControl.port;

    //build the URL
    var sURL = RootPath + "/Catalog/Suggestions.aspx?text=" + encodeURIComponent(oAutoSuggestControl.textbox.value) + "&language=" + thelexicon + "&host=" + thehost + "&port=" + theport;
    //open connection to states.txt file
    oHttp.open("get", sURL, true);
    oHttp.onreadystatechange = function() {


        if (oHttp.readyState == 4) {
            //evaluate the returned text JavaScript (an array)
            var data = oHttp.responseText;
            var deb = data.indexOf("<res>");
            var fin = data.indexOf("</res>");
            data = data.substring(deb + 5, fin);

            var aSuggestions = eval(data);
            //provide suggestions to the control
            oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
        }
    };
    oHttp.send(null);


};