var count = 1;
var bAdvanced = false;

function doDropDown(bAdvancedIn)
{
	bAdvanced = bAdvancedIn;
	count = count + 1;
	setTimeout("dropDownGo(" + count + ")", 50);
}


function dropDownGo(currCount)
{
   if (currCount == count)
   {
		count = 0;
//		showHint(bAdvanced);

		searchSuggest(bAdvanced);
		//doUpDownKeyPressCheck(event, -1, 0);
		//whateverYouDoToGetDropDown();
   }
}


function getKeycode(e)
{
	if(window.event) // IE
	{
		return e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		return e.which
	}
}

function doKeyCheck(e, currentIndex, total, currentFieldValue)
{
	doUpDownKeyPressCheck(e, currentIndex, total, bAdvanced); 
	doEnterPressCheck(e, currentFieldValue);
}

function doUpDownKeyPressCheck(e, currentIndex, total, bAdvancedIn)
{
	bAdvanced = bAdvancedIn;

	var oSearchSuggestContainer = document.getElementById('search_suggest');
	if(oSearchSuggestContainer.innerHTML == "")	return;
	
	var keyPressed = getKeycode(e);
//alert(keyPressed);

	var bFocus = false;
	var bPressedUp = false;
	var bPessedDown = false;
	switch (keyPressed)
	{
		case 37: 
		//	alert("LEFT");
			break;
		case 39: 
			//alert("RIGHT");
			break;
		case 38: 
		//	alert("UP");
			var bPressedUp = true;
			if(currentIndex > 0) var bFocus = true;
			break;
		case 40: 
			//alert("DOWN");
			var bPessedDown = true;
			var bFocus = true;
			if(currentIndex == total) var bFocus = false;
			break;
	}
	
	if (bFocus)
	{
		if (bPressedUp) 
		{
			var nDivToFocusIndex = currentIndex - 1;
		}
		else if(bPessedDown)
		{
			var nDivToFocusIndex = currentIndex + 1;
		}
		var oDivToFocus = document.getElementById('s' + nDivToFocusIndex);
		if(!oDivToFocus) return;
		oDivToFocus.focus();
		oDivToFocus.style.background='#cccccc';
		if(document.getElementById('s' + currentIndex)) document.getElementById('s' + currentIndex).style.background='#ffffff';
	}

}

function doEnterPressCheck(e, selectedHint)
{
	var keyPressed = getKeycode(e);
	if(keyPressed == 13) setSearch(selectedHint);
}


function checkOrUncheckAllCategory(obj)
{
	var nTotalNumOfCategories = document.getElementById("TotalNumOfCategories").value;
	var bCheckAll = obj.checked;
	for(i = 0; i <= nTotalNumOfCategories; i++)
	{
		document.getElementById("CI" + i).checked = bCheckAll;
	}

	getCategoryQuery();	// Now, cache the cats for the hint generator.
	searchSuggest(bAdvanced);	// <<<
}

//checkOrUncheckACategory
function cou(obj, nCurrentIndex)	//	currentCategoryID, parentCategoryID, parentIndex)
{
	var currentCategoryID, parentCategoryID, parentIndex;
	currentCategoryID = document.getElementById("CID" + nCurrentIndex).value;
	parentIndex = document.getElementById("PI" + nCurrentIndex).value;

	var currrentCheckboxChecked = obj.checked;
	var nTotalNumOfCategories = document.getElementById("TotalNumOfCategories").value;

	setCheckChildren(nCurrentIndex, currrentCheckboxChecked);

	//Check "AllCategory" checkbox. Note: Need to all other changes and then do this one
	var bCheckAll = true;
	var bCheckParent = true;
	var bUncheckParent = true;

//	for (i = 1; i <= nTotalNumOfCategories; i++)
//	{
//		if (!document.getElementById("CI" + i).checked)
//		{
//			bCheckAll = false;
//		}
//	}
//
//	document.getElementById("CI" + 0).checked = bCheckAll;

	getCategoryQuery();	// Now, cache the cats for the hint generator.

	searchSuggest(bAdvanced);	// <<<
}


// Mark all children on/off "recursively".
function setCheckParents(nRootIndex, bChecked)
{
	nParentIndex = document.getElementById("PI" + nRootIndex).value;

	while (nParentIndex > 0)
	{
		document.getElementById("CI" + nParentIndex).checked = bChecked;
		setCheckParents(nParentIndex, bChecked);
	}
}


// Is is nice when the parents are unaffected...
function setCheckChildren(nRootIndex, bChecked)
{
	var nTotalNumOfCategories = document.getElementById("TotalNumOfCategories").value;
	for (var i = 1; i <= nTotalNumOfCategories; i++)
	{
		// check current category's parent category checkboxes
		var nParentIndex = document.getElementById("PI" + i).value;
		if (nRootIndex == nParentIndex)
		{
			document.getElementById("CI" + i).checked = bChecked;
			setCheckChildren(i, bChecked);
		}
	}
}


function doAdvancedSearch()
{
	// Get the search word from the basic search box..
	var sSearch = document.getElementById("s").value;
	window.location='SearchAdvanced.asp?s=' + escape(sSearch);
}


var winpops1 = null;

function doArrangeFreeDemo(nItemID)
{
	if (winpops1)
	{
		winpops1.close();
	}
	winpops1 = window.open("FreeDemo.asp?ID=" + nItemID, "FreeDemo", "width=400,height=400,status,scrollbars=1");
}


function hideBasicSearchHint()
{
	var ssB = document.getElementById('search_suggest');
	ssB.innerHTML = '';
}

function hideAdvancedSearchHint()
{
	var ssA = document.getElementById('search_suggest_advanced');
	if (ssA)
	{
		ssA.innerHTML = '';
	}
}
