function count_products(category,subcategory)
{
	total_products=0;
	for ( var i=0; i<(FoodID.length); i++)
	{
		if((FoodCategory[i] == category)||('all' == category))
		{
		  	total_products++;
		}
	}
	return total_products;
}

function shop_layout(category,subcategory,page)
{
	total_products = count_products(category,subcategory);
	pages = Math.ceil(count_products(category,subcategory)/9);
	
	if(total_products < 10)
	{
		products_on_page = total_products;
	}
	else
	{
		if(page < pages)
		{
	    	products_on_page = 9;
		}
		else
		{
			products_on_page = total_products - (pages-1)*9;
		}
	}
	
	if(total_products == 0)
	{
	  	first_product=0;
	}
	else
	{
		first_product=(page*9-8);
	}
	last_product=(page*9-9+products_on_page);

	
	shopstring = initial = "<table border='0' ID='ContentTable' cellpadding='4' cellspacing='8'><tr><td ID=shopnav class=shop valign=center>"+category+" » "+subcategory+"</td> <td></td> <td ID=shopcontent class=shop>Displaying products "+first_product+"-"+last_product+" of "+total_products+"</td></tr>";

	j=0;
	n_layout=0;
	
	for ( var i=0; i<(FoodID.length); i++)
	{
		if((FoodCategory[i] == category)||('all' == category))
		{
			n_layout++;
			if( first_product <= n_layout )
			{
				if( n_layout <= last_product )
				{
					if(j%3 == 0)
					{
						shopstring=shopstring+"<tr><td class=shop><DIV ID='$"+FoodID[i]+"'></DIV></td>";
					}
			
					if(j%3 == 1)
					{
						shopstring=shopstring+"<td bgcolor=white><DIV ID='$"+FoodID[i]+"'></DIV></td>";
					}
					
					if(j%3 == 2)
					{
						shopstring=shopstring+"<td bgcolor=white><DIV ID='$"+FoodID[i]+"'></DIV></td></tr>";
					}
					j++;
				}
			}			
		}
	}
	
	if(shopstring==initial)
	{
		shopstring = shopstring + "<tr><td width=250 bgcolor=white><br>Sorry There are no products available in this section<br><br></td></tr>";
	}
	shopstring = shopstring + '<tr><td></td><td class=shop ID=shop_pages>Page: '
	
	for( var i=0; i<pages; i++)
	{
		if(page==i+1)
		{
		  	shopstring = shopstring + '<b> ' + (i+1) + ' </b>';
		}
		else
		{
		  	shopstring = shopstring + '<a href="shop.php?cat='+category+'&sub='+subcategory+'&page='+ (i+1) + '">' + (i+1) + '</a>' ;
		}
	}
	
	shopstring = shopstring + '</td></tr></table>';
	
	Content.innerHTML=shopstring;

// POPULATE
	
	n_populate=0;

	for ( var i=0; i<(FoodID.length); i++)
	{
		if((FoodCategory[i] == category)||('all' == category))
		{
			n_populate++;
			if( first_product <= n_populate )
			{
				if( n_populate <= last_product )
				{
					eval('$'+FoodID[i]).innerHTML="<table border=0><tr><td><br><img src=product_data/"+FoodID[i]+".jpg width=110 height=110></td><td><b>"+FoodName[i]+"</b><br>"+FoodDescription[i]+"<br><a href=product.php?code="+FoodID[i]+">Read More>></a></td></tr><tr><td colspan=2 align=center><form name='form"+FoodID[i]+"'>£"+FoodPrice[i]+" - Buy  <input type=text  name=text"+FoodID[i]+" value=1 border=0 maxlength=4 size=1> - <input type=button  onclick='increasequantity("+FoodID[i]+",form"+FoodID[i]+".text"+FoodID[i]+".value);' name=add"+FoodID[i]+" value='Add to Trolley'><br>Quick Find Code:"+FoodID[i]+"</form></td></tr></table>";
				}
			}	
		}
	}
	
	
}