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 = "<table width='708' border='0' align='left' cellpadding='0' cellspacing='0'>";

	for(i = 0; i < searchdata.length; i++)
	{		


			if(eval(strcond))
			{

				divdisp += "<tr>";

				if(searchdata[i].orderable == "Yes")
				{
					divdisp += "<td align='left' valign='top' class='adv_searchbox'>";
				}
				else
				{
					divdisp += "<td align='left' valign='top' class='adv_searchbox_outofstock'>";
				}
				divdisp += "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
				divdisp +=  "<tr>";
				divdisp += "<td width='23%' align='center' valign='middle'><a href='" + searchdata[i].id + ".html' target='_blank'><img src='" + searchdata[i].image + "' alt='" + searchdata[i].name + "' border='0'></a></td>";
				divdisp += "<td width='77%' class='category_itme_name'>";
				divdisp += "<table width='80%' border='0' cellpadding='0' cellspacing='0' class='category_itme_name'>";
				divdisp += "<tr><td colspan='2' align='left' valign='top'><span class='advance_name'><a href='" + searchdata[i].id + ".html' target='_blank'>" + searchdata[i].name + "</a></span></td></tr>";
				divdisp += "<tr><td width='60%' align='center' valign='middle'>";
				divdisp += "<table width='100%' border='0' align='left' cellpadding='0' cellspacing='0'>";
				divdisp += "<tr><td height='20' align='left' valign='middle' class='adv_details'>Brand: " + searchdata[i].brand + "</td></tr>";
				divdisp += "<tr><td height='20' align='left' valign='middle' class='adv_details'>Category: " + searchdata[i].category + "</td></tr>";
				
				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 += "<tr><td height='20' align='left' valign='middle'  class='adv_details'>Price : <span class='advance_price'>$" + price.toFixed(2) + "</span></td></tr>";
					divdisp += "<tr><td height='20' align='left' valign='middle' class='adv_details'>You Save : <span class='advance_yousave'>(" + saveprice.toFixed(2) + "%) $" + saveval.toFixed(2) + "</span></td></tr>";
				}
				divdisp += "</table></td>";
				divdisp += "<td width='70%' align='left' valign='top' class='advance_saleprice'><img src='http://site.modaqueen.com/default/images/logo_price.gif' alt='ModaQueen Price' title='ModaQueen Price' width='59' height='49' align='texttop'> $";
				
				divdisp += (searchdata[i].saleprice > 0) ? searchdata[i].saleprice : searchdata[i].price;
				divdisp += "<br><a href='" + searchdata[i].id + ".html' target='_blank'><img src='http://site.modaqueen.com/default/images/view_more.jpg' alt='" + searchdata[i].name + "'  title='" + searchdata[i].name + " - More Info...' width='67' height='21'></a> </td></tr>";
				divdisp += "</table></td></tr>";
				divdisp += "</table>";
				divdisp += "</td></tr>";

				divdisp += "<tr><td>&nbsp;</td></tr>";

				totalcount++;
				itemcount++;
			}
		
	
	}
	str += divdisp + "</table>";
	if(totalcount > 0)
	{
		document.getElementById("searchresult").innerHTML = itemcount + " Products found<br>" + str;
	}
	else
	{
		document.getElementById("searchresult").innerHTML = itemcount + " Products found<br>No Products Found...";
	}
	
}

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;
	
	
	
}


