PRVD.APP.UC.UCProductSearchInput = function (htmlElementID) {
    this.ElementID = htmlElementID;
    this.Element = document.getElementById(this.ElementID);
    this.SearchInputBox = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "SearchInput")[0];
    this.SuggestionsContainer = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "autocompletecontainer")[0];
    this.SearchLink = PRVD.CT.UT.GetElementsByPrvdName(this.Element, "productsearchlink")[0];
    this.HiddenNavUrl = PRVD.CT.UT.GetElementsByPrvdName(document, "hdnNavUrl")[0];
    this.AutoCompWSUrl = PRVD.CT.UT.GetElementsByPrvdName(document, "hdnAutoCWsUrl")[0];
    this.DomainUrl = PRVD.CT.UT.GetElementsByPrvdName(document, "hdnDomainUrl")[0];
    this.SearchLinkAttrName = "href";

    this.SearchAutoComplete = function () {
        oDS = new YAHOO.util.ScriptNodeDataSource(this.AutoCompWSUrl.value);

        oDS.responseSchema = {
            resultsList: "Table",
            fields: ["Keyword"]
        };
        oDS.maxCacheEntries = 1000;

        // The webservice needs to support execution of a callback function.
        oDS.scriptCallbackParam = "callback";

        oAC = new YAHOO.widget.AutoComplete(this.SearchInputBox.id, this.SuggestionsContainer.id, oDS);
        oAC.queryMatchSubset = true;
        oAC.queryDelay = .2;
        oAC.autoHighlight = true;
        oAC.queryInterval = 1000;
        oAC.applyLocalFilter = true;
        oAC.autoSnapContainer = true;

        oAC.generateRequest = function (sQuery) {
            return "?query=" + sQuery.toLowerCase();
        };
        oAC.collapseContainer();
        return {
            oDS: oDS,
            oAC: oAC
        };
    };

    this.HandleSearchInputClick = function (e, obj) {
        if ((obj.SearchInputBox.value == " Keyword / Item #") || (obj.SearchInputBox.value == "Search")) {
            obj.SearchInputBox.value = "";
        }
    };

    this.HandleSearchInputKeyUp = function (e, obj) {
        obj.UpdateSearchUrl(e, obj);

        if (e.keyCode == 13) {
            PRVD.CT.EVENTS.Event.stopEvent(e);
            obj.ExecuteSearch(e, obj);
        }
    };

    this.ExecuteSearch = function (e, obj) {
        PRVD.CT.EVENTS.Event.stopEvent(e);
        if ((obj.SearchInputBox.value == "") || (obj.SearchInputBox.value == " Keyword / Item #") || (obj.SearchInputBox.value == "Search")) {
            /* DO NOT REDIRECT TO "PRODUCT SEARCH" IF THE SEARCH ITEM ENTERED IS EMPTY OR THE DEFAULT "Keyword / Item #" */
        }
        else {
            window.location.href = obj.getNewURL(obj, "/productsearch.aspx", obj.SearchInputBox.value);
        }
    };

    this.UpdateSearchUrl = function (e, obj) {
        obj.SearchLink.attributes[obj.SearchLinkAttrName].value = obj.getNewURL(obj, obj.HiddenNavUrl.value, obj.SearchInputBox.value);
    };

    this.getNewURL = function (obj, sCurrentURL, input) {
        if (obj.HiddenNavUrl.value == "0") {
            obj.HiddenNavUrl.value = this.SearchLink.attributes[obj.SearchLinkAttrName].value;
        }
        sCurrentURL = obj.HiddenNavUrl.value;
        var searchbaseurl = sCurrentURL.split('?');
        var newparameters = '';
        if (typeof (searchbaseurl[1]) != "undefined") {
            var searchOldParams = searchbaseurl[1].split('&');
            for (var i = 0; i < searchOldParams.length; i++) {
                var singleparameter = searchOldParams[i].split('=');
                /* Remove productgroup from Querystring*/
                if (singleparameter[0] == 'productgroup' || singleparameter[0] == "searchhistory" || singleparameter[0] == 'viewpos' || singleparameter[0] == 'trackingpgroup') {
                    continue;
                }
                if (singleparameter[0] != 'q') {
                    if (newparameters == '')
                        newparameters = searchOldParams[i];
                    else {
                        newparameters += '&' + searchOldParams[i];
                    }
                }
            }
        }
        if (newparameters == '')
            newparameters = 'q=' + escape(input);
        else
            newparameters += '&q=' + escape(input);

        /* These methods will return escaped versions of the query string parameters */
        var sCurrentSearchHistoryValue = GetQueryStringParam('searchhistory');
        var sCurrentSearchValue = GetQueryStringParam('q');

        if (sCurrentSearchHistoryValue) {
            /* make sure to re-escape the values we're saving to the query string */
            newparameters += '&searchhistory=' + escape(sCurrentSearchHistoryValue + ',' + input);
        }
        else if (sCurrentSearchValue) {
            /* make sure to re-escape the values we're saving to the query string */
            newparameters += '&searchhistory=' + escape(sCurrentSearchValue + ',' + input);
        }
        else {

            newparameters += '&start=&spell=';
        }

        var url2 = searchbaseurl[0].substring(searchbaseurl[0].lastIndexOf('/') + 1);
        var url1 = obj.DomainUrl.value;
        return url1 + url2 + '?' + newparameters;
    }

    var ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf("msie") != -1) {
        PRVD.CT.EVENTS.Event.addListener(this.SearchInputBox, "keydown", this.HandleSearchInputKeyUp, this, false);
    }
    else {
        PRVD.CT.EVENTS.Event.addListener(this.SearchInputBox, "keyup", this.HandleSearchInputKeyUp, this, false);
    }

    PRVD.CT.EVENTS.Event.addListener(this.SearchInputBox, "click", this.HandleSearchInputClick, this, false);
    PRVD.CT.EVENTS.Event.addListener(this.SearchLink, "click", this.ExecuteSearch, this, false);
    PRVD.CT.EVENTS.Event.addListener(this.SearchLink, "keyup", this.ExecuteSearch, this, false);
    /* this.SearchAutoComplete();	*/
};

