function showSearchBox()
{
	document.getElementById("searchinput").style.display = "block";
	document.getElementById("searchresult").style.display = "none";
}


function extractCommonElements()
{
	var theBodyElements, theDriveElements;
	var theMinPriceElement, theMaxPriceElement;
	var theCommonCriteria = "";;
	var theCommonSql = "";

	theBodyElements = new Array(document.getElementById("TableData.coupe"),
		document.getElementById("TableData.sedan"),
		document.getElementById("TableData.wagon"),
		document.getElementById("TableData.hatchback"),
		document.getElementById("TableData.convertible"),
		document.getElementById("TableData.minivan"),
		document.getElementById("TableData.suv"),
		document.getElementById("TableData.pickup"));

	theDriveElements = new Array(document.getElementById("TableData.rear"),
		document.getElementById("TableData.front"),
		document.getElementById("TableData.awd"),
		document.getElementById("TableData.partTime"),
		document.getElementById("TableData.permanent"));

	var theMinPriceElement = document.getElementById("TableData.minPrice");
	var theMaxPriceElement = document.getElementById("TableData.maxPrice");

	var thePriceSql = "";

	//handle the body style
	var theBodySql = createORForElementArray(theBodyElements);
	if (theBodySql != "")
	{
		theCommonSql = "( " + theBodySql + ")";
		theCommonCriteria = theCommonCriteria + createCreteriaForElementArray(theBodyElements);
	}

	//handle the price range
	if (theMinPriceElement.value > 0 && theMaxPriceElement.value > 0)
	{
		thePriceSql = " ((TableData.minPrice >= "
			+ theMinPriceElement.value
			+ " AND TableData.minPrice < "
			+ theMaxPriceElement.value
			+ ") OR (TableData.maxPrice > "
			+ theMinPriceElement.value
			+ " AND TableData.maxPrice <= "
			+ theMaxPriceElement.value
			+ "))";
		theCommonCriteria = theCommonCriteria
			+ "\"price greater than $"
			+ theMinPriceElement.value
			+ ". price less than $"
			+ theMaxPriceElement.value
			+ "\" ";
	}
	else if (theMinPriceElement.value > 0)
	{
		thePriceSql = " TableData.maxPrice >= "
			+ theMinPriceElement.value;
		theCommonCriteria = theCommonCriteria
			+ "\"price greater than $"
			+ theMinPriceElement.value
			+ "\" ";
	}
	else if (theMaxPriceElement.value > 0)
	{
		thePriceSql = " TableData.minPrice <= "
			+ theMaxPriceElement.value;
		theCommonCriteria = theCommonCriteria
			+ "\"price less than $"
			+ theMaxPriceElement.value
			+ "\" ";
	}
	if (thePriceSql != "")
	{
		theCommonSql = theCommonSql
			+ ( theBodySql != "" ? " AND " : " ")
			+ "("
			+ thePriceSql
			+ ")";
	}

	//handle the drive wheel
	var theDriveSql = createORForElementArray(theDriveElements);
	if( theDriveSql != "")
	{
		theCommonSql = theCommonSql
			+ ( theBodySql != "" || thePriceSql != "" ? " AND " : " ")
			+ "( "
			+ theDriveSql
			+ ")";
		theCommonCriteria = theCommonCriteria
			+ createCreteriaForElementArray(theDriveElements);
	}

	return ({ sql: theCommonSql, criteria: theCommonCriteria });

}


function createORForElementArray(inElementArray)
{
	var i, theElement;
	var theQuery = "";
	var isFirst = true;

	for (i = 0; i < inElementArray.length; i++)
	{
		theElement = inElementArray[i];
		if (theElement.checked == false)
			continue;

		theQuery = theQuery
			+ (!isFirst ? " OR " : " ")
			+ theElement.id
			+ " = "
			+ theElement.value;
		isFirst = false;
	}
	return theQuery;
}


function createCreteriaForElementArray(inElementArray)
{
	var i, theElement;
	var theCriteria = "";

	for (i = 0; i < inElementArray.length; i++)
	{
		theElement = inElementArray[i];
		if (theElement.checked == false)
			continue;

		theCriteria = theCriteria + theElement.name + ". ";
	}
	return (theCriteria != "" ? "\"" + theCriteria + "\" " : "");
}


function extractElementsAndFilteringNewCars()
{
	var theSql, theCriteria;

	var theCommonResult = extractCommonElements();
	theSql = theCommonResult.sql;
	theCriteria = theCommonResult.criteria;

	var theElement;

	//handle passenger data
	theElement = document.getElementById("TableData.passengers");
	if (theElement.value > 0)
	{
		theSql = theSql
			+ ( theSql != "" ? " AND " : " ")
			+ theElement.id + " >= " + theElement.value;
        theCriteria = theCriteria
        	+ "\"seats at least "
        	+ theElement.value
        	+ " passengers \" ";
     }

     //handle EPA data
     theElement = document.getElementById("TableData.epaRounded");
     if (theElement.value > 0 )
     {
     	theSql = theSql
			+ ( theSql != "" ? " AND " : " ")
			+ theElement.id + " >= " + theElement.value;
		theCriteria = theCriteria
			+ "\"better than "
			+ theElement.value
			+ " miles per gallon\" ";
     }

     //handle recommend data
     theElement = document.getElementById("TableData.recommend");
     if (theElement.checked == true)
     {
     	theSql = theSql
     		+ ( theSql != "" ? " AND " : " ")
     		+ theElement.id + " = " + theElement.value;
		theCriteria = theCriteria + "\"CR recommended\"";
     }

     //update the search criteria text
	document.getElementById("passedCriteria").firstChild.nodeValue = theCriteria;

	//show search result and hide search input box
	document.getElementById("searchinput").style.display = "none";
	document.getElementById("searchresult").style.display = "block";

	if (theSql !== "")
		theSql = "WHERE " + theSql;
	theFilter.filter({ clause: theSql });

	return false;

}


function setNewCarsDisplaySearchResult(inData,inElementGroup,inDisplay)
{
	var theNoResultElement = document.getElementById("noresult");

	if (inDisplay == "none" || inData.length == 0)
		theNoResultElement.style.display = inDisplay;

	if (inData.length > 0)
	{
		document.getElementById("compare_top").style.display = inDisplay;
		//clear all the checked box
		var i, theElement;
		for (i = 0; i < document.forms["search_compare"].elements.length; i++)
		{
			theElement = document.forms["search_compare"].elements[i];
			theElement.checked = false;
		}

		Filter.utilities.setDisplayStyle(inData,inElementGroup,inDisplay);
		document.getElementById("compare_bottom").style.display = inDisplay;
	}
}


function extractElementsAndFilteringUsedCars()
{
	var theSql, theCriteria;

	var theCommonResult = extractCommonElements();
	theSql = theCommonResult.sql;
	theCriteria = theCommonResult.criteria;

	var theElement;

    //handle maker data
    theElement = document.getElementById("TableData.maker");
    if (theElement.value > 0)
    {
     	theSql = theSql
     		+ ( theSql != "" ? " AND " : " ")
     		+ theElement.id + " = " + theElement.value;
		theCriteria = theCriteria
			+ "\""
			+ theElement.options[theElement.selectedIndex].text
			+ "\"";
    }

    //update the search criteria text
	document.getElementById("passedCriteria").firstChild.nodeValue = theCriteria;

	//show search result and hide search input box
	document.getElementById("searchinput").style.display = "none";
	document.getElementById("searchresult").style.display = "block";

	if (theSql !== "")
		theSql = "WHERE " + theSql;
	theFilter.filter({ clause: theSql });

	return false;

}


function setUsedCarsDisplaySearchResult(inData,inElementGroup,inDisplay)
{
	var theNoResultElement = document.getElementById("noresult");

	if (inDisplay == "none" || inData.length == 0)
		theNoResultElement.style.display = inDisplay;

	if (inData.length > 0)
		Filter.utilities.setDisplayStyle(inData,inElementGroup,inDisplay);
}
