// AutoSuggestion Control

function autoSuggestControl(objTextBox, objProvider){
	this.provider=objProvider;
	this.textbox=objTextBox;
	this.init();
}

autoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/) {
    
    //make sure there's at least one suggestion
    if (aSuggestions.length > 0) {
        this.typeAhead(aSuggestions[0]);
    }
};

autoSuggestControl.prototype.selectRange = function (iStart, iLength) {
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange(); 
        oRange.moveStart("character", iStart); 
        oRange.moveEnd("character", iLength - this.textbox.value.length); 
        oRange.select();
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iLength);
    } 

    this.textbox.focus(); 
};

autoSuggestControl.prototype.typeAhead = function (sSuggestion) {
    if (this.textbox.createTextRange || this.textbox.setSelectionRange) {
        var iLen = this.textbox.value.length; 
        this.textbox.value = sSuggestion; 
        this.selectRange(iLen, sSuggestion.length);
    }
};

autoSuggestControl.prototype.handleKeyUp = function (oEvent) {
    var iKeyCode = oEvent.keyCode;

     if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //ignore
    } else {
        this.provider.requestSuggestions(this);

    }
};

autoSuggestControl.prototype.init = function () {
    var oThis = this;
    this.textbox.onkeyup = function (oEvent) {
        if (!oEvent) {
            oEvent = window.event;
        }
        oThis.handleKeyUp(oEvent);
    };
};


function suggestions() {
    this.objData = suggest["vendor_supply_name"];
}

suggestions.prototype.requestSuggestions = function (objSuggestControl) {

    var aSuggestions = [];

    var sTextboxValue = objSuggestControl.textbox.value;

    if (sTextboxValue.length > 0){
        for (var i=0; i < this.objData.length; i++) { 
            if (this.objData[i].indexOf(sTextboxValue) == 0) {
                aSuggestions.push(this.objData[i]);
            } 
        } 
        objSuggestControl.autosuggest(aSuggestions);
    } 

};

