// JavaScript Document
var xmlHttp = createXMLHttpRequestObject();
// Creates an XMLHttpRequest instance
function createXMLHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except for IE6 and older
	try
	{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e)
	{
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
		// Try every prog id until one works
		for (var i=0; i<XMLHttpVersions.length && !xmlHttp; i++) {
			try
			{
				//try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}	
			catch(e) {}
		}
	}
	if (!xmlHttp) {
		alert("Error creating the XmlHttpRequest object.");
	} else {
		return xmlHttp;
	}
}

function searchSuggest() {
	if (document.getElementById('search').value != "Search ..." && document.getElementById('search').value != '') {
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
			var str = escape(document.getElementById('search').value);
			xmlHttp.open("GET", "/searchsuggest.php?search=" + str, true);
			xmlHttp.onreadystatechange = handleSearchSuggest;
			xmlHttp.send(null);
		}
	} else {
		var rb = document.getElementById('search_suggest');
		rb.style.display = "none";
	// if the connection is busy, try again after one second
	setTimeout('searchSuggest()',1000);
	}

}

function userClick(value) {
	window.location='/sales_main.php?search='+value;
}

function handleSearchSuggest() {
	if (xmlHttp.readyState == 4) {
		var ss = document.getElementById('search_suggest');
		ss.innerHTML = '';
		var str = xmlHttp.responseText.split("\n");
		ss.style.border = "1px solid black";
		ss.style.display = "block";
		if (str.length == 1) {
			ss.style.height = 17;
			ss.style.border = "none";
			//alert(ss.style.height);
			//ss.innerHTML = '<span style="width: 100%; overflow: hidden">No Matches found for: ' + document.getElementById("search").value + '</span>';
		} else {
			//alert("There are " + str.length - 1 + " Items that match your search");
			var suggest = '<ul>';
			if (navigator.appName == "Microsoft Internet Explorer") {
					document.getElementById('search_suggest').style.marginLeft = "46%";
					var style = 'style="margin-bottom: 5px; margin-top: 5px; list-style: none; paddng-left: none; margin-left: -35px; border-bottom: 1px dotted #AEAEAE; cursor: pointer"';
				} else if (navigator.userAgent.match("Chrome")) {
					document.getElementById('search_suggest').style.marginLeft = "46%";
					var style = 'style="margin-bottom: 5px; margin-top: 5px; list-style: none; paddng-left: none; margin-left: -35px; border-bottom: 1px dotted #AEAEAE; cursor: pointer"';
				} else {
					var style = 'style="margin-bottom: 5px; margin-top: 5px; list-style: none; paddng-left: none; margin-left: -35px; border-bottom: 1px dotted #AEAEAE; cursor: pointer"';
				}
			for(i=0; i < str.length - 1; i++) {
				//alert("Before we add: " + ss.style.height);
				ss.style.height = ss.style.height + 17;
				//alert("After we add: " + ss.style.height);
				// Build our element string, This is cleaner using the DOM
				var temp = 'onClick="userClick(\''+str[i]+'\')"';
				//alert(temp);
				suggest += '<li ' + style + ' ' + temp + ' title="'+str[i] + '">' + str[i] + '</li> ';
				//suggest += 'onclick="userClick("'+str[i]+'")" '; 
				}
				if (str.length > 10) {
					suggest += '<li ' + style + ' ' + temp + '>Keep Typing to see more results!</li> ';
				}
				suggest += '</ul>';
				ss.innerHTML = suggest;
		}
		setTimeout('searchSuggest()', 1000);

	}
}

function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}

function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

function setSearch(value) {
	document.getElementById("search").value = value;
	document.getElementById("search_suggest").innerHTML = '';
}

function clearSearch() {
	if (document.getElementById('search').value = 'Search...') {
		document.getElementById('search').value = '';
	}
}


