
/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function RemoteStateSuggestions_MultiFiltered(URL) {

    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.");
    }
    /**022506
	* The suggestions URL.
	* @scope private
	*/   
	this.URL /*:string*/ = URL;
	/**
    * A variable to control caching
    * @scope private
    */
	this.useCaching/*:bool*/=true;	//use caching by default - added 2nd overload to allow 
										//passing of var to this class to control caching

}
//an overload added to control whether to cache results:
function RemoteStateSuggestions_MultiFiltered(URL,useCaching) {

    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.");
    }
    /**022506
	* The suggestions URL.
	* @scope private
	*/   
	this.URL /*:string*/ = URL;
	/**
    * A variable to control caching
    * @scope private
    */
	this.useCaching/*:bool*/=useCaching;
}
function pausecomp(millis) 
{
date = new Date();
var curDate = null;

do { var curDate = new Date(); } 
while(curDate-date < millis);
}
RemoteStateSuggestions_MultiFiltered.prototype.requestSuggestions = function(oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {

    //debugger;
    //pausecomp(5000);
    var oHttp = this.http;


    //if there is already a live request, cancel it
    if (oHttp.readyState != 0) {
        oHttp.abort();
    }

    //build the URL
    //var sURL = "suggestions.php?userInput=" + encodeURIComponent(oAutoSuggestControl.textbox.value);
    //var sURL = 'http://localhost/lab/ajaxsample/GetAJAXData.aspx?q=' + encodeURIComponent(oAutoSuggestControl.textbox.value);
    var sURL = this.URL + '?q=' + encodeURIComponent(oAutoSuggestControl.textbox.value)
    //+ '&id=' + encodeURIComponent(oAutoSuggestControl.textbox.id) + 
    //'&Filterid=' + encodeURIComponent(oAutoSuggestControl.textboxFilter.value);

    sURL = sURL + "&control=" + oAutoSuggestControl.textbox.id;
    sURL = sURL + "&scrollPos=" + oAutoSuggestControl.scrollPos;
    var sFilter = "";
    /**/
    if (oAutoSuggestControl.chkFilter != null && oAutoSuggestControl.chkFilter.checked == false) {
        if (oAutoSuggestControl.textboxFilterArray != null) {
            for (i = 0; i < oAutoSuggestControl.textboxFilterArray.length; i++) {
                sFilter = sFilter + "&" + oAutoSuggestControl.textboxFilterArray[i].id + "=" + oAutoSuggestControl.textboxFilterArray[i].value;
            }
        }
    }
    if (oAutoSuggestControl.chkFilter == null) {
        if (oAutoSuggestControl.textboxFilterArray != null) {
            for (i = 0; i < oAutoSuggestControl.textboxFilterArray.length; i++) {
                sFilter = sFilter + "&" + oAutoSuggestControl.textboxFilterArray[i].id + "=" + oAutoSuggestControl.textboxFilterArray[i].value;
            }
        }
    }
    //VPR Inventory v1.3.1.5 - to avoid caching, added timestamp such that url is 
    //always different and results are not cached
    //sURL=sURL + sFilter
    if (this.useCaching == undefined || this.useCaching == true)	//true by default (according to overload#1 for this class
    {
        sURL = sURL + sFilter;
    }
    else {
        //VPR Inventory v1.3.1.5 - to avoid caching, added timestamp such that url is 
        //always different and results are not cached
        sURL = sURL + sFilter + "&timestamp=" + new Date().getTime();
    }
    //alert(sURL);
    /**/
    //open connection to states.txt file
    //debugger;
    oHttp.open("get", sURL, true);
    oHttp.onreadystatechange = function() {

        if (oHttp.readyState == 4) {
            //evaluate the returned text JavaScript (an array)
            //Here is where error exists!!!!
            // alert("Howdy-before eval response");
            //var aSuggestions = eval(oHttp.responseText);
            var aSuggestions = oHttp.responseText;
            //var aSuggestionsXML = oHttp.responseXML;

            var bHasIDbox = false;

            //debugger;
            if(aSuggestions.indexOf('^EOF^')>=0)
            {
                aSuggestions = aSuggestions.substring(0, aSuggestions.indexOf('^EOF^'))

            }
            Brady = new Array(); //http://www.devx.com/tips/Tip/12455
            if (aSuggestions == "") {
                X = "";
            }
            else {
                //delimited string passed as example:
                //"item1|item2|item3|..."
                X = aSuggestions.split(":");
                //multi-delimited string for value/id pairs sent as example:
                //"item1^ID1|item2^ID2|item3^ID3|..."
                //used in VPR_Inventory to pass category and mfg dropdown pairs.
                if (aSuggestions.indexOf("^") > 0)	//split into a multidimensional array
                {
                    var row;
                    var arr2;
                    for (row = 0; row < X.length; ++row) {
                        arr2 = X[row].split("^");
                        Brady[row] = new Array(2); //add second dimension for Brady[row]
                        Brady[row][0] = arr2[0];
                        Brady[row][1] = arr2[1];
                    }
                    X = Brady
                    //need to tell the calling app that we are returning a dual array
                    bHasIDbox = true;
                }
            }
            //			for (var i=0; i < 10; i++) 
            //			{
            //				alert(X[i]);
            //			}

            //provide suggestions to the control
            oAutoSuggestControl.autosuggest(X, bTypeAhead, bHasIDbox);

        }
    };
    oHttp.send(null);


};