/*************** functions moved to HeaderLibrary.js ****************/

// This file contains the javascript functions for the auto populate search box
// author Mark B - 30/05/2008

// variable to ensure each request is only made once
var busy = false;

// this is a boiler plate method for creating the XMLHttpRequest (cross browser)
function newXMLHttpRequest() {
	var xmlreq = false;
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
			}
		}
	}
	return xmlreq;
}


// function for submitting the request and receiving the response
function doCompletion() {
	if (busy == false) {
		var searchString = document.autoCompleteSearchForm.Ntt.value;
		if (searchString.length < 3) {
			hideSearchOptionsBox();
		}
		// we only perform a search when the space bar is hit
		if ((searchString.charAt(searchString.length-1) == ' ' && searchString.length > 3) || searchString.length == 6) {
			busy = true;
			var url = "SearchBoxAutoPopulateCmd?action=complete&Ntt=" + escape(searchString);
			var req = newXMLHttpRequest();
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					if (req.status == 200) {
						parseMessages(req.responseXML);
					} else if (req.status == 204){
						hideSearchOptionsBox();
					}
				}
			};
			req.open("GET", url, true);
			req.send(null);
		}
	}
}


// function for handling the response XML and displaying to the screen
function parseMessages(responseXML) {
	var displaypictures = true;
	var searchTermDisplay = "";
	var seeAllResults = "";
	var hideSuggestions = "";
	document.getElementById('searchOptionsBox').innerHTML = "";
	var products = responseXML.getElementsByTagName("products")[0];
	if (products != null) {
		if (products.childNodes.length > 0) {
			searchTermDisplay = "<div class=\"searchTermDisplay\"><p>Quick search results for <span class=\"strong\">'" + document.forms.autoCompleteSearchForm.Ntt.value + "'</strong></p></div>";
			hideSuggestions = "<div class=\"hideSuggestions\"><a href=\"javascript:hideSearchOptionsBox()\">Hide suggestions</a></div>";
			if (products.childNodes.length > 9 ) {
				seeAllResults = "<div class=\"seeAllResults\"><a href=\"javascript:setSearchTerm()\">See all search results</a></div>";
			}	
			document.getElementById('searchOptionsBox').innerHTML = "<div class=\"searchSuggestionsHeader\">" + searchTermDisplay + "<div style=\"clear:both;\"></div><div class=\"seeAllResults\">" + seeAllResults + "</div><div class=\"hideSuggestions\">" + hideSuggestions + "</div><div style=\"clear:both;\"></div>";
		}
		for (loop = 0; loop < products.childNodes.length; loop++) {
			var product = products.childNodes[loop];
			var productName = product.getElementsByTagName("productName")[0];
			var productImage = product.getElementsByTagName("productImage")[0];
			var productId = product.getElementsByTagName("productId")[0];
			var productUrl = product.getElementsByTagName("productUrl")[0];			
			if (displaypictures) {
				var productImageURLLink = "";
				if (productImage.childNodes[0].nodeValue != null && productImage.childNodes[0].nodeValue != "null") {
					productImageURLLink = "<a href=\""+ productUrl.childNodes[0].nodeValue + "?cm_sp=search-_-search_suggestion-_-product_suggestion\"><img src=\"" + document.forms.autoCompleteSearchForm.imageDomain.value + "" + productImage.childNodes[0].nodeValue + "\" /></a>";
				}
				var productNameLink = "<a href=\""+ productUrl.childNodes[0].nodeValue + "?cm_sp=search-_-search_suggestion-_-product_suggestion" + "\" onmouseover=\"highlightSearchElement(this)\" onmouseout=\"unhighlightSearchElement(this)\">" + productName.childNodes[0].nodeValue + "</a>";
				document.getElementById('searchOptionsBox').innerHTML = document.getElementById('searchOptionsBox').innerHTML + "<div class=\"searchSuggestionImage\">" + productImageURLLink + "</div><div class=\"searchSuggestionName\">" + productNameLink + "</div><div class=\"searchSuggestionBreak\"></div>";			
			} else {
				var productUrl = product.getElementsByTagName("productUrl")[0];
				document.getElementById('searchOptionsBox').innerHTML = document.getElementById('searchOptionsBox').innerHTML + "<div class=\"searchSuggestionNameOnly\"><a href=\""+ productUrl.childNodes[0].nodeValue + "?cm_sp=search-_-search_suggestion-_-product_suggestion" + "\" onmouseover=\"highlightSearchElement(this)\" onmouseout=\"unhighlightSearchElement(this)\">" + productName.childNodes[0].nodeValue + "</a></div>";			
			}
		}

		if (products.childNodes.length > 0) {
			document.getElementById('searchOptionsBox').innerHTML = document.getElementById('searchOptionsBox').innerHTML + "<div class=\"searchSuggestionsHeader\"><div class=\"seeAllResults\">" + seeAllResults + "</div><div class=\"hideSuggestions\">" + hideSuggestions + "</div><div style=\"clear:both;\"></div></div>";	
        	document.getElementById('searchOptionsBox').style.display = "block";
		} else {
			hideSearchOptionsBox();
		}
	}
	busy = false;
}


// Set the selected search term into search box and submit
function setSearchTerm() {
	// document.autoCompleteSearchForm.Ntt.value = productName;
	hideSearchOptionsBox();
	document.autoCompleteSearchForm.submit();
}


// hide the product name options display box
function hideSearchOptionsBox() {
	document.getElementById('searchOptionsBox').style.display = "none";
}

function clearSearch() {
	if (document.forms.autoCompleteSearchForm.Ntt.value == 'Search by keyword or item #') {
		document.forms.autoCompleteSearchForm.Ntt.value='';
		hideSearchOptionsBox();
	}
}
	
function highlightSearchElement(element) {
	//if (isIe > 0) {
	//	element.style.background = "#EEEEEE";
	//}
}

function unhighlightSearchElement(element) {
	//if (isIe > 0) {
	//	element.style.background = "#FFFFFF";
	//}
}
