
/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function RemoteStateSuggestions(mytype) {

    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.mytype = mytype;
}

/**
 * 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();
    }                 
    
    //build the URL
//    var sURL = "http://localhost/drug/index.php?option=com_drug&view=ajax&format=raw&userInput=" + encodeURIComponent(oAutoSuggestControl.textbox.value);
    //open connection to states.txt file
//    oHttp.open("get", sURL , true);

	var pageurl = "index.php?option=com_realtyna&view=ajax&format=raw";
	var params;
	var myfunction;
	if (this.mytype == 'state')
		myfunction = 'auto_suggest_state';
	else if (this.mytype == 'agent')
		myfunction = 'auto_suggest_agent';
	else if (this.mytype == 'ref_no')
		myfunction = 'auto_suggest_ref_no';
	params = "userInput=" + encodeURIComponent(oAutoSuggestControl.textbox.value) + "&function=" + myfunction ;
	//alert(params);
	//xmlHttp.open("GET", pageurl + '?' + params ,true);
	oHttp.open("POST", pageurl ,true);
	oHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oHttp.setRequestHeader("Content-length", params.length);
	oHttp.setRequestHeader("Connection", "close");
	oHttp.send(params);

	oHttp.onreadystatechange = function () {
        if (oHttp.readyState == 4) {
			//alert(oHttp.responseText);
			//evaluate the returned text JavaScript (an array)
            var aSuggestions = eval(oHttp.responseText);
        
            //provide suggestions to the control
            oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);        
        }    
    };
	oHttp.open("POST", pageurl ,true);
	oHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oHttp.setRequestHeader("Content-length", params.length);
	oHttp.setRequestHeader("Connection", "close");
	oHttp.send(params);
    

};
