/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function PartSuggestions() {
	this.strUrl = "/servlet/AjaxServer";
	this.requestId = 0;		/* To keep track of the latest request (responses with older ids can be rejected. */
	this.queryStr = function() {
		return $(document.bmAjaxForm).serialize();
	};
}

/**
 * Request suggestions for the given autosuggest control. 
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
PartSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl, bTypeAhead) {
    //autosuggestions is passing the query to the provider (partsuggestions.js) 
    var sTextboxValue = oAutoSuggestControl.textbox.value;
	var strAjaxFormBody = this.queryStr();
	strAjaxFormBody += '&query=' + escape(sTextboxValue);
    if (sTextboxValue.length > 0){
		this.sendRequest(++this.requestId, strAjaxFormBody, oAutoSuggestControl, bTypeAhead);
    }
};

/* Send request; check if the response is for the latest request; and suggest the control.
 * Each send request is given an id (increases on each request).  When the response is 
 * received the id given is checked with the latest id.  */
PartSuggestions.prototype.sendRequest = function(curReqId, postBody, oAutoSuggestControl, bTypeAhead) {
	var ps = this;	/* to make 'this' available in the onSuccess function */
	new Ajax.Request(this.strUrl, { /* standard prototype ajax call (async post request) */
		postBody: postBody,
		onSuccess: function(response) {
			var strResponseText = response.responseText;
			/* check if current request's id is the latest request's id */
			if(curReqId == ps.requestId && strResponseText && strResponseText.length > 0){
				var aSuggestions = eval(strResponseText);
				//provide suggestions to the control
				oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
			}
		},
		onFailure: function(response) {
		}
	});
};


PricebookPartSuggestions.prototype = new PartSuggestions;
function PricebookPartSuggestions() {
	this.priceBookId; // This is a function to get the pricebook id
	this.queryStr = function() {
		return 'ajaxaction=getPartNumberSuggestionsForUser&_price_book_id=' + this.priceBookId(); 
	};
};
