function SimpleAjaxSearch(_webRoot, _resultsId, _searchFrmId, _ajaxPage) {

	// ********************
	// LOCAL VARIABLES
	var t = this;
	var __webRoot = _webRoot;
	var __results = document.getElementById(_resultsId);
	var __termField = document.getElementById(_searchFrmId);
	var __initValue = __termField.value;
	var __ajaxPage = _ajaxPage;
	var iex = document.all;
	var __initValue = (__termField.value) ? __termField.value : "";

	// ***************************************
	// PUBLIC FUNCTIONS	
	t.doRealSearch = function() {
		window.location.href = __webRoot+"blog/search/"+__termField.value;
	}
	
	__termField.onfocus = function() {
		if(__termField.value == __initValue) __termField.value = "";
	}
	__termField.onblur = function() {
		if(__termField.value == "") __termField.value = __initValue;
		__results.innerHTML = "";
	}

	// ***************************************
	// PRIVATE FUNCTIONS
	
	__termField.onkeyup = function(event) {
		var evt = (iex) ? window.event : event;
		if(evt.keyCode == '13') { onEnter(); } // enter key
		else if(__termField.value.length > 1) doAjaxBlogSearch();
		else onBlogSearchResponse('');
	}
	
	function doAjaxBlogSearch() {
		var term = __termField.value;
		if(term != "" && term != __initValue) {
			// make ajax call
			var stg = "term="+term
			var blogReq = ajaxCall(__webRoot, __ajaxPage, stg);
			blogReq.callback = function(_resp) { onBlogSearchResponse(_resp); };
		}
	}
	
	function onBlogSearchResponse(_resp) {
		var output = "";
		if(_resp) {
			var output = "<div class=\"searchAjaxBlog\"><div class=\"searchBlogBg\">";
			output += _resp;
			output += "</div></div>";
		}
		__results.innerHTML = output;
	}
	
	function onEnter() {
		if(__termField.value.length > 1 && __termField.value != "" && __termField.value != __initValue) {
			t.doRealSearch();
		}
	}
}

function search(_webRoot, _displayId, _searchFrmId) {

	// ********************
	// LOCAL VARIABLES
	var t = this;
	var __container = document.getElementById(_displayId);
	var __frm = document.getElementById(_searchFrmId);
	var __overClass = "searchItemOver";
	var __outClass = "searchItem";
	var __webRoot = _webRoot;
	var __selectedIndex;
	var __selectableDivs; // array of div's that can be selectable
	var __slugAry;
	var iex = document.all;	
	var __initValue = (__frm.search.value) ? __frm.search.value : "";

	// ***************************************
	// PUBLIC FUNCTIONS	
	t.doSearch = function(_frmName) {
		var term = __frm.search.value;
		
		// redirect to search page
		if(term != "" && term != "Enter Store Name") {
			window.location.href = __webRoot+"search/" + term + "/";
		}
		return false;
	}
	
	// keyup function
	__frm.search.onkeyup = function(event) {
		var evt = (iex) ? window.event : event;
		if(evt.keyCode == '13') { onEnter(); }
		else if(evt.keyCode == '38') { onUp(); }
		else if(evt.keyCode == '40') { onDown(); }
		else if(this.value.length > 1) makeAjaxRequest(this.value);
		else __container.innerHTML = "";
	}
	
	__frm.search.onfocus = function() {
		if(__frm.search.value == __initValue) __frm.search.value = "";
	}
	__frm.search.onblur = function() {
		if(__frm.search.value == "") __frm.search.value = __initValue;
		__container.innerHTML = "";
	}

	// ***************************************
	// PRIVATE FUNCTIONS
	function setLinks() {
		var allDivs = __container.getElementsByTagName("DIV");
		__selectableDivs = new Array();
		__selectedIndex = null;
		var len = allDivs.length;
		for(var i = 0;i < len;i++) {
			var thisDiv = allDivs[i];
			if(thisDiv.className == __outClass || thisDiv.className == __overClass) {
				thisDiv.onmouseover = function() { 
					this.className = __overClass;
				};
				thisDiv.onmouseout = function() { 
					this.className = __outClass;				
				};
				__selectableDivs.push(thisDiv);
			}
		}
	}
	
	function makeAjaxRequest(_term) {
		var stg = "term="+_term
		var req = ajaxCall(__webRoot, 'ajax.quickSearch.php', stg);
		req.callback = function(_resp) { onUpdatedData(_resp); };
	}
	
	function onUpdatedData(_resp) {
		var respAry = _resp.split("||");
		if(respAry != "" && respAry) {
			// stors and categories
			var storesStg = respAry[0];
			var catsStg = respAry[1];
			
			var output = "<div class=\"searchAjax\">";
			
			// separate stores
			__slugAry = new Array();
			if(storesStg != "") {
				output += "<div class=\"searchHeader\"><div class=\"txt\">Stores</div></div>";
				var storeAry = storesStg.split("|");
				var len = storeAry.length;
				for(var i = 0; i < len; i++) {
					var storeData = storeAry[i].split(",");
					output += "<div class=\"searchItem\" onMouseDown=\"window.location.href='"+__webRoot+"category/"+storeData[0]+"/'\">";
    				output += "<img src=\"/images/logos/"+storeData[2]+"\" class=\"inline\" />";
    				output += "<div class=\"txt\">"+storeData[1]+"</div>";
    				output += "</div>";
    				__slugAry.push(storeData[0]);
				}
			}
			// separate categories
			if(catsStg != "") {
				output += "<div class=\"searchHeader\"><div class=\"txt\">Categories</div></div>";
				var catAry = catsStg.split("|");
				output += "<div class=\"searchCSV\"><div class=\"txt\">";
				var len = catAry.length;
				for(var i = 0; i < len; i++) {
					var catData = catAry[i].split(",");
					if(i) output += ", ";
					output += "<a href=\""+__webRoot+"category/"+catData[0]+"/\" onMouseDown=\"window.location.href='"+__webRoot+"category/"+catData[0]+"/'\">"+catData[1]+"</a>";
				}
				output += "</div></div>";
			}
			
			output += "</div>";
			
			// output to screen
			__container.innerHTML = output;
			
			setLinks();
		}else {
			__container.innerHTML = "";
		}
	}
	
	function onDown() {
		if(__selectedIndex == null || __selectedIndex < __selectableDivs.length-1) {
			if(__selectedIndex == null) __selectedIndex = -1;
			else __selectableDivs[__selectedIndex].onmouseout();
			__selectedIndex++;
			__selectableDivs[__selectedIndex].onmouseover();
		}
	}
	
	function onUp() {
		if(__selectedIndex >= 0 && __selectedIndex != null) {
			__selectableDivs[__selectedIndex].onmouseout();
			__selectedIndex--;
			if(__selectedIndex >= 0) __selectableDivs[__selectedIndex].onmouseover();
		}
		if(__selectedIndex < 0) __selectedIndex = null;
	}
	
	function onEnter() {
		if(__selectedIndex != null && __selectedIndex >= 0 && __selectedIndex < __selectableDivs.length) {
			window.location.href = __webRoot+"category/"+__slugAry[__selectedIndex]+"/";
		}else t.doSearch();
	}
}
