﻿// ----------------------------------------------
// Default search box text (thank you, Dunstan)
// ----------------------------------------------
 
// event handler
function addEventToObject(obj,evt,func) {
        var oldhandler = obj[evt];
        obj[evt] = (typeof obj[evt] != 'function') ? func : function(){oldhandler();func();};
}
// search box stuff
var Searchbox = {
        init : function()
               {
               var sBox = document.getElementById('autoCmpType2');
               if (sBox)
                       {
                       addEventToObject(sBox,'onclick',Searchbox.click);
                       addEventToObject(sBox,'onblur',Searchbox.blur);
                       }       
               },
        click : function()
               {
        //       var sBox = document.getElementById('autoCmpType2');
        //       if (sBox.value == 'e.g. mugs, pens, t-shirts')
        //               {
        //               sBox.value = '';
        //               }
                },
        blur : function()
                {
               var sBox = document.getElementById('autoCmpType2');
              // if (sBox.value == '' || sBox.value == ' ') {sBox.value = 'e.g. mugs, pens, t-shirts';}
               }
        };
 
// add event onload
addEventToObject(window,'onload',Searchbox.init);


// This define's auto complete fields on the page
function defineOneField(AFieldType) {
	// AFieldType = 1 for company and 2 for categories

	// Only proceed if we've got an input box for this type of field
	if (document.getElementById("autoCmpType" + AFieldType)) {
		// Create the datasource required
		var dsData = new YAHOO.widget.DS_XHR("/feeds/feed.asp", ["result", "description", "id" ]);
		dsData.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
		dsData.scriptQueryParam = "q";
		dsData.scriptQueryAppend = "type=prod";	// Hardcode "prod" for now

		// Hook the data source onto the field (if the fields don't exist, the Yahoo stuff crashes gracefully)
	 	var autoCField = new YAHOO.widget.AutoComplete("autoCmpType" + AFieldType, "autoCmpType" + AFieldType + "Results", dsData);

		// Disable the browser's built-in autocomplete caching mechanism 
		autoCField.allowBrowserAutocomplete = false; 
		autoCField.highlightClassName = "highLight";
		autoCField.autoHighlight = false;

		// Format the results
		autoCField.formatResult = function(aResultItem, ATextSearchFor) {
			// This format's a single result
			var strResult = aResultItem[0].toLowerCase();
			
			var intStart = strResult.indexOf(ATextSearchFor.toLowerCase());
			
			var strFirstPart = aResultItem[0].substr(0,intStart);
			
			var strMatchedPart = aResultItem[0].substr(intStart, ATextSearchFor.length); // The part that matches what they typed
			
			var strLastPart = aResultItem[0].substring(intStart + ATextSearchFor.length); 		  // The last part after the matched part

			// This is where you define your markup
			// var aRetval = ["<a href=\"\"><strong>" + strMatchedPart + "</strong>" + strTheRest + "</a>"];
			var aRetval = ["<span>" + strFirstPart + "<strong>" + strMatchedPart + "</strong>" + strLastPart + "</span>"];
			return (aRetval.join(""));
		}
	}
}

function setupAutoCompleteFields() {
//	defineOneField("1");
	defineOneField("2");
}

// Add to the onload event
addEventToObject(window, 'onload', setupAutoCompleteFields);


