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.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 = "";
	   }
	};
	
	this.HandleSearchInputKeyUp = function(e,obj)
	{	
	    obj.UpdateSearchUrl(e,obj);
	    
	    if(e.keyCode == 13)
        {
            PRVD.CT.EVENTS.Event.stopEvent(e);
            window.location.href=obj.getNewURL(obj, "/productsearch.aspx", obj.SearchInputBox.value);
        }
	};
	
	this.UpdateSearchUrl = function(e,obj)
	{
	    obj.SearchLink.href = obj.getNewURL(obj, obj.HiddenNavUrl.value, obj.SearchInputBox.value);
	};
	
	this.getParameter = function(obj, parameterName)
    {
        var queryString = window.location.search.substring(1).toLowerCase();
        var parameters = new Array();
        parameters = queryString.split('&');
        for(var i = 0; i < parameters.length; i++)
        {
	        if (parameters[i].indexOf(parameterName.toLowerCase())>=0)
		        {var parameterValue = new Array();parameterValue = parameters[i].split('=');return parameterValue[1];}
        }
        return 'null';
    }

    this.getNewURL = function(obj, sCurrentURL, input )
    {
        if (obj.HiddenNavUrl.value == "0")
        {
            obj.HiddenNavUrl.value = this.SearchLink.href;
        }
        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] == 'viewpos' || singleparameter[0] == 'trackingpgroup' )
                 {
                      continue;
                 }
	            if( singleparameter[0] != 'q' )
	            {
		            if( newparameters == '' )
			            newparameters = searchOldParams[i];
		            else
		            {
			            newparameters += '&' + searchOldParams[i];
		            }
	            }
            }
        }
        if( newparameters == '' )
	        newparameters = 'q=' + input;
        else 
	        newparameters += '&q=' + input;

        	
        var sCurrentSearchHistoryValue = obj.getParameter(obj, 'searchhistory');
        var sCurrentSearchValue = obj.getParameter(obj, 'q');
        if( sCurrentSearchHistoryValue != 'null' )
        {
	        newparameters += '&searchhistory=' + sCurrentSearchHistoryValue + ',' + input;
        }
        else if( sCurrentSearchValue != 'null' )
        {
	        newparameters += '&searchhistory=' + 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.UpdateSearchUrl, this, false); 
	PRVD.CT.EVENTS.Event.addListener(this.SearchLink, "keyup", this.UpdateSearchUrl, this, false); 
    this.SearchAutoComplete();	
};
