window.onresize = fixWidth;

var w_x; var w_y;

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

var ie = (navigator.appName.indexOf('Microsoft Internet Explorer')>-1);
var ns = (navigator.appName.indexOf('Netscape')>-1);
var NS4 = (navigator.appName.indexOf("Netscape")>=0 && !document.getElementById)? true : false;
var IE4 = (document.all && !document.getElementById)? true : false;
var IE5 = (document.getElementById && document.all)? true : false;
var IE6 = (ie && (typeof document.body.style.maxHeight == "undefined") && (typeof document.body.filters == 'object')) ? true : false;
var NS6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
var W3C = (document.getElementById)? true : false;

function getWindowDims(){
w_y=(NS4||NS6||window.opera)? window.innerHeight : (IE5||IE4)? document.body.clientHeight : 0;
w_x=(NS4||NS6||window.opera)? window.innerWidth : (IE5||IE4)? document.body.clientWidth : 0;
}

function fixWidth() {
	var bodyWidth = document.body.clientWidth;
	document.getElementById("yelA").style.width = bodyWidth + "px";
	document.getElementById("yelB").style.width = bodyWidth + "px";
	if(bodyWidth < 1050) {
		if (bodyWidth > 950) {
			document.getElementById("top-banner").style.left = ((bodyWidth - 950)/2) + "px";
		} else { 
			document.getElementById("top-banner").style.left = "0px";
		}
	} else {
		document.getElementById("top-banner").style.left = "50px";
	}
}
function fixPage() {
	var contentHeight = document.getElementById("pagecontent").offsetHeight;
	document.getElementById("bikesign").style.top = (parseInt(contentHeight)-120) + "px";
	document.getElementById("bikesign").style.visibility = "visible"; 
	document.getElementById("yellowdbl").style.top = (parseInt(contentHeight)+235) + "px";
	fixWidth();
	document.getElementById("yellowdbl").style.visibility = "visible";
	//document.getElementById("content").style.height = "500px;"
	//document.getElementById("content").style.overflow = "hidden";
}

function clearSearchBox()
{	
	if (document.getElementById("search1").value == 'Search') {document.getElementById("search1").value = ''}
	if (document.getElementById("search1").value.length > 1){document.getElementById("suggestions").style.visibility = "visible";}
	return true;
}

function checkSearchBox() {
	if (document.getElementById("search1").value == '') {document.getElementById("search1").value = 'Search';}
}
function checkSuggest() {
	if(suggestCount >=0) {return false;}
}
function hideSuggestions()
{
	document.getElementById("suggestions").style.visibility = "hidden";
}

// called when textbox is edited
var scnt = 0;
var suggestCount = -1;
var sugVisible = false;
var lastSuggest = "";

function getSuggestions(str,keycode) {
	if (keycode == 40 && sugVisible) {
		suggestCount++;
		if(document.getElementById("sug"+suggestCount)) {
			document.getElementById("sug"+suggestCount).style.backgroundColor="#1f4594";
			document.getElementById("sug"+suggestCount).style.color="white";
			if(document.getElementById("sug"+(suggestCount-1))) {
				document.getElementById("sug"+(suggestCount-1)).style.backgroundColor="#ffffff";
				document.getElementById("sug"+(suggestCount-1)).style.color="#717b86";
			}
		} else {suggestCount--;}
		return;
	}
	if (keycode == 38 && sugVisible) {
		if(document.getElementById("sug"+suggestCount)) {
			document.getElementById("sug"+suggestCount).style.backgroundColor="#ffffff";
			document.getElementById("sug"+suggestCount).style.color="#717b86";
		}
		if (suggestCount >= 0) {
			suggestCount--;
			if(document.getElementById("sug"+suggestCount)) {
				document.getElementById("sug"+suggestCount).style.backgroundColor="#1f4594";
				document.getElementById("sug"+suggestCount).style.color="white";
			} 
		}
		return;
	}
	if (keycode == 13 && suggestCount >=0) {
//		alert(document.getElementById("sug"+suggestCount).href);
		window.location=document.getElementById("sug"+suggestCount).href;
		return false;
	}
	if(str.length >= 6) {
		if (str.substr(0,6) == 'Search') {str = str.substring(6);document.getElementById("search1").value = str;}
	}
	if(str.length < 2) {
		// hide the suggestions area
		document.getElementById("suggestions").style.visibility = "hidden";
		suggestCount = -1;
	} else {
		scnt = 0;
		suggestDelay(str);
	}			
}

function suggestDelay(str) {
	scnt++;
	if (scnt < 10) {setTimeout("suggestDelay('" + str + "');",50);}
	if (scnt >= 10 && str == document.getElementById("search1").value) {getSuggestion();} 
}

function getSuggestion() {
	var thisSuggest = document.getElementById("search1").value;
	if (thisSuggest != lastSuggest && thisSuggest.length >= 2){
		thisSuggest = thisSuggest.replace(" ","+");
		lastSuggest = thisSuggest;
		xmlHttp = GetXmlHttpObject();
		xmlHttp.onreadystatechange = showSuggestion;
		var suggestURL = "/ajaxsuggest?q="+thisSuggest;
		xmlHttp.open("GET", suggestURL, true);		
		xmlHttp.send(null)
	}	
}	

function goSearch() {
	if(suggestCount < 0 && document.getElementById("search1").value != 'Search') {
		window.location.assign("/search_results.asp?kw2="+document.getElementById("search1").value);
	}
	if(suggestCount >= 0) {
	console.log(document.getElementById("sug"+suggestCount).href);
		window.location=document.getElementById("sug"+suggestCount).href;
	}
}	
	
function showSuggestion() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		// query has returned
		var xmlResponseText = checkAjax(xmlHttp.responseText)
		if (xmlResponseText != "-" && xmlResponseText.length > 0)
		{
			document.getElementById("suggestions").innerHTML=xmlResponseText;
			document.getElementById("suggestions").style.visibility = "visible";
			sugVisible = true;
		}
		else
		{
			document.getElementById("suggestions").style.visibility = "hidden";
			sugVisible = false;
			suggestCount = -1;
		}	
	} 
} 
		
// wrapper to create an xmlHttp object cross-browser
function GetXmlHttpObject(handler) { 
	var objXMLHttp = null;
	
	if (window.XMLHttpRequest) {
		// we're in mozilla
		objXMLHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// we're in IE
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function checkAjax(retString) {
//console.log('ajaxCheckCode='+ajaxCheckCode);
//console.log('retString='+retString.substr(0,8));
	if(retString.substr(0,8) == ajaxCheckCode) {retString = retString.substr(8)} else {retString = ''}
	return retString
}
