

// This is the AJAX search script that I created that searches all of 
//TheBathOutlet and presents the results back to the user immediately in a DIV.
	
//Please do not use without permission. Thanks.
		
// Ajax Search Version 1.0
	
// Date Created: Sept 09,2008														 

	/** do nothing if enter character **/
	function kH(e) {
		//var pK = e ? e.which : window.event.keyCode;
        var key; 
        var code;
        if (!e) var e = window.event; // set var e for ie 	
        if (e.keyCode) code = e.keyCode; // ie and mozilla/gecko 
        else if (e.which) code = e.which; // ns4 and opera 		


		if (code == 13) {
		    doCompleteSearch();
		}
	}
	
	/** clear input of search field **/	
	function clearInput() { 
		if (document.forms.ajaxSiteSearch.query.value =="By Keyword or Item #") {
			document.forms.ajaxSiteSearch.query.value="";
		}
	}
	
	/** truncate description in quick search **/
	function truncate(d) {
		 	
		 	var description =  d.replace("\n"," ");

			var words = description.split(" ");
			
			var newD = "";
			
			for (var i=0; i<20; i++) {
		    		newD = newD + words[i] + " ";				
			}
			
			newD = newD + " ...";
			return newD;			
			
		}
		
		

		

function doCompleteSearch() {	

        if (document.forms.ajaxSiteSearch.query.value == "") {
           document.getElementById("ajaxSiteSearch").style.borderColor = "#990000";
           document.getElementById("ajaxSiteSearch").style.borderWidth = "1";
           document.forms.ajaxSiteSearch.query.value = "By Keyword or Item #";
        } else {

		var f = document.forms.ajaxSiteSearch;
		f.action = "http://www.thebathoutlet.com/search.htm?query=" + document.forms.ajaxSiteSearch.query.value;
		f.submit();
        
        }
}		
	
	
var queryBoxUp = false;		
function doQuery() {			
	setTimeout("getQueryText()", 500);	
}		
		
function getQueryText() {
		   if (document.forms[0].query.value.length >= 3) {	    	
		    	var queryText = document.forms[0].query.value;
		    	searchQueryText(queryText);
		    } else if (document.forms[0].query.value.length == 0) {		    
		    	closeQuery();
		    }
}
		
		
function searchQueryText(text){
	
				var url = 'http://www.thebathoutlet.com/searchProducts.htm?query=' + text;
								
				if (window.XMLHttpRequest) {
					 req = new XMLHttpRequest();
				} else if (window.ActiveXObject) {
					 req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				req.onreadystatechange = processRequest;
				req.open("GET", url, true);
				req.send(null); 
}

function processRequest() {
			if (req.readyState == 4) {	
        		if (req.status == 200) {	
        			if (queryBoxUp==false) {
						document.getElementById("queryBox").innerHTML="<div style='margin-top:0px;'>" +
																	  "<div class='queryResultDiv' style='text-align:center;'>" +
																	  "<br/><img src='../images/base/ajax-loader.gif'> loading...<br/><br/>" +
																	  "</div>"+
																	  "<div id='queryProductFooter'><a href='javascript:closeQuery();'>Close</a></div>";
						
						document.getElementById('theSearchContainer').style.display = 'block';
						var q = document.getElementById("queryBox");
		    			q.style.display='block';
		    			q.status=true;	
					}
					setTimeout("parseMessages()", 750);        			
        		} else if (req.status == 400) {
          			alert("bad request");
        		} else if (req.status == 403) {
           			alert("forbidden");
        		} else if (req.status == 500) {
           			alert ("internal error")
        		} else {
					alert("Sorry. Cannot connect to the server.");
                 }				
   			 }
}

function parseMessages() {
			queryBoxUp=true;
			
    		response  = req.responseXML.documentElement;

    		/** only show 8 products **/
    		var html = "";
    		
    		var products = response.getElementsByTagName('product');
    		  	 		
    		for (var i=0; i<products.length; i++) {
    		
    			var collName = response.getElementsByTagName('collName')[i].firstChild.data;
    			

    			if (collName=="0") break; //this is an empty result passed from middle tier, break it!
    			
    			var seriesName = response.getElementsByTagName('seriesName')[i].firstChild.data;
    			var prodName = response.getElementsByTagName('prodName')[i].firstChild.data;
    			var prodSku = response.getElementsByTagName('prodSku')[i].firstChild.data;
    			var prodDescription = response.getElementsByTagName('prodDescription')[i].firstChild.data;
    			prodDescription = truncate(prodDescription);
    			    			
    			var prodImage = response.getElementsByTagName('prodImage')[i].firstChild.data;	
    			var prodListPrice = response.getElementsByTagName('prodListPrice')[i].firstChild.data;
    			var prodSalePrice = response.getElementsByTagName('prodSalePrice')[i].firstChild.data;
    			
    			html = html + "<div class='queryResultDiv' onclick=\"window.location = 'http://www.thebathoutlet.com/product.htm?sku=" + prodSku + "'\" onmouseover=\"this.style.background='url(http://static.thebathoutlet.com/images/base/query_bg_on.png)'; this.style.cursor='hand'\" onmouseout=\"this.style.background='url(http://static.thebathoutlet.com/images/base/query_bg.png)'; this.style.color='#000000';\">" +     			  						  
    						   "<div style='float:left;width:70px;height:70px;padding-left:5px;'>" +
    						  			"<img src='../search" + prodImage + "' style='margin-top:5px;margin-left:5px;' width='60px' border='0'>" +
    						  	"</div>" +
    							"<div class='queryTableResult'>" +
    							   		 "<strong>" + prodName + " by " + collName + "</strong></br>" +
    							   		 "<span class='ajaxProdDescription'>" + prodDescription + "</span></br>" +
    							   		 "<span class='priceWord'>Price:</span>&nbsp;<span class='ajaxListPrice'>$" + prodListPrice + "</span>"+
										 "&nbsp;<span class='ajaxSalePrice'>$" + prodSalePrice + "</span></br>" +
    						  	"</div>"+
    						  "</div><div style='clear:both'></div>";
		
    		}
    		
    		if (html =="") {
    			html = "<div class='queryResultDiv' style='text-align:center;'>" +
					   "<div style='padding-top:10px;'> Sorry. There were no products found for this search. </div><br/>" +
					   "</div>";	
    		} 
    		
    		var divHTML = "<div>" + html + "</div><div id='queryProductFooter'><div style='padding:4px'><a href='javascript:closeQuery();'>Close</a></div></div>"
  
    		
    		var ajaxDisplay = document.getElementById('queryBox');
    		ajaxDisplay.innerHTML = divHTML;
}

function closeQuery() {
			if (queryBoxUp==true) {
				queryBoxUp=false;
				document.getElementById('queryBox').style.display = 'none';
				document.getElementById('theSearchContainer').style.display = 'none';
				document.getElementById('queryBox').status = false;
				document.getElementById('theSearchContainer').status = false;
				clearInput();
			}	
}


		
function goToProduct(sku) {
 			window.location = 'http://www.thebathoutlet.com/product.htm?sku=' + sku;
 		 }