var strcond = "";
var searchdata = new Array();
var bln = false;

function searchByPrice()
{
	bln = true;
	searchProduct();
}
function dataadd(id, name, image, price, saleprice, brand, category, orderable)
{
	this.id = id;
	this.name = name;
	this.image = image;
	this.brand = brand;
	this.category = category;
	this.price = price;
	this.saleprice = saleprice;
	this.orderable = orderable;
	
	if(saleprice > 0 && price > 0)
	{
		this.calcprice = saleprice;
	}
	else
	{
		this.calcprice = (saleprice > 0) ? saleprice : price;
	}
}
function searchProduct()
{
	var strcond = "";
	
	var totalcount = 0;
	count = 0;
	
	var val = "";
	var totalbrandcount = 0;
	/*for(i=0; i < document.searchform.brandlist.length; i++)
	{
		if(document.searchform.brandlist[i].checked == true)
		{
			totalbrandcount++;
		}
	}
	
	brandcount = 0;*/
	count = 0;
	/*if(totalbrandcount > 0)
	{
		strcond += "(";
	}
	for(i=0; i < document.searchform.brandlist.length; i++)
	{
			if(document.searchform.brandlist[i].checked == true)
			{
				if(count < (totalbrandcount - 1))
				{
					strcond += "searchdata[i].brand.toUpperCase() == \"" + document.searchform.brandlist[i].value.toUpperCase() + "\" || ";
				}
				else
				{
					strcond += "searchdata[i].brand.toUpperCase() == \"" + document.searchform.brandlist[i].value.toUpperCase() + "\" ";
				}
				brandcount++;
				count++;
			}
	}

	if(totalbrandcount > 0)
	{
		strcond += ")";
	}*/
	

	var totalcategorycount = 0;
	
	for(i=0; i < document.searchform.categorylist.length; i++)
	{
		if(document.searchform.categorylist[i].checked == true)
		{
			totalcategorycount++;
		}
	}

	if(totalcategorycount > 0 && totalbrandcount > 0)
	{
		strcond += " && (";
	}
	else if(totalcategorycount > 0)
	{
		strcond += "(";
	}
	var categorycount = 0;
	count = 0;
	for(i=0; i < document.searchform.categorylist.length; i++)
	{
		if(document.searchform.categorylist[i].checked == true)
		{
			if(count < (totalcategorycount - 1))
			{
				strcond += "searchdata[i].category.toUpperCase() == \"" + document.searchform.categorylist[i].value.toUpperCase() + "\" || ";
			}
			else
			{
				strcond += "searchdata[i].category.toUpperCase() == \"" + document.searchform.categorylist[i].value.toUpperCase() + "\" ";
			}
			categorycount++;
			count++;
		}
	}
	count=0;
	
	if(categorycount > 0)
	{
			strcond += ")";
	}

	
	if(bln == true)
	{
		stprice = Number(document.getElementById('price1').innerHTML);
		endprice = Number(document.getElementById('price2').innerHTML);
		
		if(totalcategorycount > 0 || totalbrandcount > 0)
		{
			strcond += " && ";	
		}
		if(endprice > stprice)
		{
			strcond += "(Number(searchdata[i].calcprice) >= " + stprice + " && Number(searchdata[i].calcprice) <= " + endprice + ")";
		}
		else if(stprice > endprice)
		{
			strcond += "(Number(searchdata[i].calcprice) >= " + endprice + " && Number(searchdata[i].calcprice) <= " + stprice + ")";
		}
		else
		{
			strcond += " (Number(searchdata[i].calcprice) == " + stprice + ")";
		}
		
		if(totalcategorycount > 0)
		{
//			strcond += ")";
		}
	}


	str = "";
	var itemcount = 0;
	searchdata.sort(sortByPriceAscending);
	divdisp = "";

	for(i = 0; i < searchdata.length; i++)
	{		


			if(eval(strcond))
			{

				divdisp += "<tr>";

				if(searchdata[i].orderable == "Yes")
				{
					divdisp += "<div class='advanced_search'>";
				}
				else
				{
					divdisp += "<div class='advanced_search'>";
				}
				
				divdisp += "<div class='advanced_search_image'><a href='" + searchdata[i].id + ".html' target='_blank'><img src='" + searchdata[i].image + "' alt='" + searchdata[i].name + "' border='0'></a></div>";
				divdisp += "<div class='advanced_search_text'>";
				divdisp += "<a href='" + searchdata[i].id + ".html' target='_blank'>" + searchdata[i].name + "</a>";
				divdisp += "<p><span class='adv_text'>Brand: " + searchdata[i].brand + "</span> <span class='adv_text'>Category: " + searchdata[i].category + "</span></p>";
				
				if(searchdata[i].price > 0 && searchdata[i].saleprice > 0)
				{
					var price = Number(searchdata[i].price);
					var saleprice = new Number(searchdata[i].saleprice);
					var saveprice = new Number(saleprice.toFixed(2) * 100) / price.toFixed(2);
					saveprice = 100 - saveprice;
					var saveval = Number(price.toFixed(2) - saleprice.toFixed(2));
					
					divdisp += "<p><span class='adv_text'>Retail Price : <span class='product_line'>$" + price.toFixed(2) + "</span></span>";
					divdisp += "<span class='adv_text'>Sale Price : <span class='price'>$" + saleprice.toFixed(2) + "</span></span></p>";
					divdisp += "<p><span>You Save : </span><span class='ver_12'>" + saveprice.toFixed(2) + "%</span></p>";
				}
				else
				{
					divdisp += "<p><span class='adv_text'>Sale Price : <span class='price'>$";
					if(searchdata[i].saleprice > 0)
						divdisp += searchdata[i].saleprice.toFixed(2);
					else
						divdisp += searchdata[i].price.toFixed(2);
					divdisp += "</span></span></p>";
				}
				
				divdisp += "<p><a href='" + searchdata[i].id + ".html' target='_blank'><img src='http://site.verseo.com/ystore/images/view_more.jpg' alt='" + searchdata[i].name + "'  title='" + searchdata[i].name + " - More Info...'></a></p>";
				divdisp += "</div></div>";

				totalcount++;
				itemcount++;
			}
		
	
	}
	str += divdisp;
	if(totalcount > 0)
	{
		document.getElementById("searchresult").innerHTML = "<b>" + itemcount + " Products found<br><br></b>" + str;
	}
	else
	{
		document.getElementById("searchresult").innerHTML = "<b>" + itemcount + " Products found<br>No Products Found...</b>";
	}
	
}

function sortByPriceAscending(a,b)
{
	var x = a.price;
	var y = b.price;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

/*
function displayProducts()
{
	strcond = "";
	val = "";
	
	searchdata.sort(sortByPriceAscending);

	str = "";
	for(i = 0; i < searchdata.length; i++)
	{		

		if(searchdata[i].orderable == "Yes")
		{
			divdisp = "<div class='advser_box'>";
			divdisp += "<img src='http://98.129.37.252/stores/topsleepingbags/images/advance_search_top.gif' class='img_left' alt='Advance Search Top'>";
		}
		else
		{
			divdisp = "<div class='advser_box1'>";
			divdisp += "<img src='http://98.129.37.252/stores/topsleepingbags/images/advance_search_top1.gif' class='img_left' alt='Advance Search Top'>";
		}

		divdisp += "<div class='pt1'>";
		divdisp += "<div class='img_center'><span></span>";
		divdisp += "<a href='" + searchdata[i].id + ".html'><img src='" + searchdata[i].image + "' alt='" + searchdata[i].name + "'></a>";
		divdisp += "</div>";
		divdisp += "</div>";
		
		divdisp += "<div class='pt2'>";
		divdisp += "<h2><a href='" + searchdata[i].id + ".html'>" + searchdata[i].name + "</a></h2>";

		if(searchdata[i].brand.length > 0)
		{
			divdisp += "<p class='left'>Brand:" + "</p>";
			divdisp += "<p class='right'>" + searchdata[i].brand + "</p>";
		}
		
		
	
		divdisp += "</div>";
		
		divdisp += "<div class='pt3'>$";
		divdisp += searchdata[i].price;
		divdisp += "</div>";
		
		if(searchdata[i].orderable == "Yes")
		{
			divdisp += "<img src='http://98.129.37.252/stores/topsleepingbags/images/advance_search_bottom.gif' class='img_left' alt='Advance Search Bottom'></div>";
		}
		else
		{
			divdisp += "<img src='http://98.129.37.252/stores/topsleepingbags/images/advance_search_bottom1.gif' class='img_left' alt='Advance Search Bottom'></div>";
		}
		
		str += divdisp;

	
	}

	document.getElementById("searchresult").innerHTML = str;
	
	
	
}


*/
