﻿
function filterUserInput(input)
{
	var selectStateElement = document.getElementById("selectState");
	var selectFoodServiceCountryElement = document.getElementById("selectCountry-foodServiceMode");
	
	// Reseting other filters
	//selectStateElement.selectedIndex = 0;
	
	// If state filter or country filter is on (case of filter on filter)
	var countStateFilter = isCurrentMode(privateConsumerModeText) && (selectStateElement.disabled != true);
	var countFoodSrviceFilter = isCurrentMode (foodServiceModeText);
	
	// 1. Getting current country's brands
	var listItems = document.getElementById("displayScreenSideBarContent").getElementsByTagName("li");
	var logoFrame = document.getElementById("availableBrandsLogos");
	
	var logoItems;  // the logo frame might be unreachable (if hidden after brand display)
	if (logoFrame) logoItems = logoFrame.getElementsByTagName("a");
	
	var visibleBrandsCount = 0;
	
	// 2. Running on each Brand
	for (var i = 0;  i < listItems.length;  i++)
	{
		var itemMatchesHigherFilter = true;
		if (countStateFilter)
		{
			// Checking if the current list item passes the state filter
			var stateId = selectStateElement.options[selectStateElement.selectedIndex].value;
			itemMatchesHigherFilter = isItemMatchesStateFilter(listItems[i], stateId);
		}
		if (countFoodSrviceFilter)
		{
			// If default item is selected
			if (selectFoodServiceCountryElement.selectedIndex == 0)
			{
				itemMatchesHigherFilter = true;
			}
			else
			{	
				// Checking if the current list item passes the country filter
				var countryName = selectFoodServiceCountryElement.options[selectFoodServiceCountryElement.selectedIndex].innerHTML;
				itemMatchesHigherFilter = isItemMatchesCountryFilter(listItems[i], countryName);
			}
		}
		
		if (isItemMatchesAttributeFilter (listItems[i], input)  && itemMatchesHigherFilter)
		{
		
			listItems[i].style.display = "block";
			if (logoItems) logoItems[i].style.display = "inline-block";
			
			visibleBrandsCount++;
		}
		else
		{
			listItems[i].style.display = "none";
			if (logoItems) logoItems[i].style.display = "none";
		}
	}
	
	if (logoItems)
		setLogosHeight (logoItems, visibleBrandsCount);
}

function filterBrandsByState(stateId, stateText)
{
	// Reseting other filters
	document.getElementById("productsFilterSelectControl").selectedIndex = 0;
	
	 // 1. Getting current country's brands
	var listItems = document.getElementById("displayScreenSideBarContent").getElementsByTagName("li");
	var logoFrame = document.getElementById("availableBrandsLogos");
	
	var logoItems;  // the logo frame might be unreachable (if hidden after brand display)
	if (logoFrame) logoItems = logoFrame.getElementsByTagName("a");
	
	var visibleBrandsCount = 0;
	
	// 2. Running on each Brand
	for (var i = 0;  i < listItems.length;  i++)
	{
		if (isItemMatchesStateFilter (listItems[i], stateId))
		{
			listItems[i].style.display = "block";
			if (logoItems) logoItems[i].style.display = "inline-block";
			
			visibleBrandsCount++;
		}
		else
		{
			listItems[i].style.display = "none";
			if (logoItems) logoItems[i].style.display = "none"; 
		}
	}
	
	if (logoItems)
		setLogosHeight (logoItems, visibleBrandsCount);
}

//function filterDisplayedBrandsByCountry(countryId)
//{
//	// Reseting other filters
//	document.getElementById("productsFilterSelectControl").selectedIndex = 0;
//	
//	 // 1. Getting displayed brands
//	var listItems = document.getElementById("displayScreenSideBarContent").getElementsByTagName("li");
//	var logoFrame = document.getElementById("availableBrandsLogos");
//	
//	var logoItems;  // the logo frame might be unreachable (if hidden after brand display)
//	if (logoFrame) logoItems = logoFrame.getElementsByTagName("a");
//	
//	var visibleBrandsCount = 0;
//	
//	// 2. Running on each Brand
//	for (var i = 0;  i < listItems.length;  i++)
//	{
//		if (isItemMatchesCountryFilter (listItems[i], countryId))
//		{
//			listItems[i].style.display = "block";
//			if (logoItems) logoItems[i].style.display = "inline-block";
//		}
//		else
//		{
//			listItems[i].style.display = "none";
//			if (logoItems) logoItems[i].style.display = "none"; 
//		}
//	}
//	
//	if (logoItems)
//		setLogosHeight (logoItems, visibleBrandsCount);
//}

function setLogosHeight(logoItems, visibleBrandsCount)
{
	var logoHeight = getBrandLogoHeight (visibleBrandsCount);

	// 3. Running on each Brand logo and setting its height according to number of visible logos
	for (var i = 0;  i < logoItems.length;  i++)
	{
		logoItems[i].getElementsByTagName("img")[0].style.height = logoHeight + "px";
	}
}

function isItemMatchesAttributeFilter (listItem, input)
{
		// Case of no filter
		if (input == ".*")
		{
			return true;
		}
		
		var brandId = listItem.id.substr(3); // 3 = "li_".length
		
		// FireFox adjustment (IE & Chrome ok)
		brandId = adjustToFireFox(brandId);
		
		// 2.1 Getting this brand details Box
		var brandDetailsBox = document.getElementById(brandId);
		
		// 2.2 Getting this brand's list Items
		var brandAttributes = brandDetailsBox.getElementsByTagName("li");
		
		var filterCategoryExists = false;
		
		// 2.3 Running on each list item
		for (var j = 0;  j < brandAttributes.length;  j++)
		{		
			// 2.3.1 if the user choose to filter by Organic brands
			if (input == "Organic")
			{
				if (brandAttributes[j].innerHTML.substr(0,7) == "Organic")
				{
					// may be in use later use
					filterCategoryExists = true;
					
					if (brandAttributes[j].innerHTML.indexOf ("v.gif") != -1)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			// 2.3.2 if the user choose to filter by Organic brands
			if (input == "Part or More Organic")
			{
			
				if (brandAttributes[j].innerHTML.substr(0,7) == "Organic")
				{
					// may be in use later use
					filterCategoryExists = true;
					
					if (	(brandAttributes[j].innerHTML.indexOf ("v.gif") != -1) ||
							(brandAttributes[j].innerHTML.indexOf ("vx.gif") != -1) )
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			// 2.3.3 if the user choose to filter by Non-GMO brands
			else if (input == "Non-GMO")
			{	 
				if (brandAttributes[j].innerHTML.substr(0,7) == "Non-GMO")
				{
					// may be in use later use
					filterCategoryExists = true;
					
					if (brandAttributes[j].innerHTML.indexOf ("v.gif") != -1)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			// 2.3.4 if the user choose to filter by Kosher brands
			else if (input == "Kosher"  ||  input == "Halal" )
			{
				if (brandAttributes[j].innerHTML.substr(0,21) == "Additional attributes")
				{
					// may be in use later use
					filterCategoryExists = true;
					
					if (brandAttributes[j].innerHTML.indexOf (input) != -1)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			// 2.3.5 if the user choose to filter by Home Delivery service
			else if ((input == "Home Delivery") || (input == "Food-Service")  || (input == "Catering"))
			{
				if (brandAttributes[j].innerHTML.substr(0,8) == "Category")
				{
					// may be in use later use
					filterCategoryExists = true;
					
					if (brandAttributes[j].innerHTML.indexOf (input) != -1)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
			// 2.3.6 If this is the product range item
			else if (brandAttributes[j].innerHTML.indexOf("productRange") != -1)
			{
				filterCategoryExists = true;
				
				// 2.3.6.1 Searching for the input in this item's text
				var regexp = new RegExp(input,"i");  // i: [i]gnore case
				var match = regexp.exec (brandAttributes[j].innerHTML);
				
				// 2.3.6.2 If the product range's contains the input
				if (match != null)
				{
					// 2.3.6.2.1 setting the brand's list item as visible
					return true;
				}
				else // 2.3.6.3 Else: setting the brand's list item as hidden
				{
					return false;
				}
				
				// 2.3.4.4 Continuing with the next brand list Item
				//break;		  Some brands may have two list items of "Product range"
			}
		}

		// Hiding if brand does not have product range
		if (filterCategoryExists == false)
		{
			return false;
		}
		return false;   // can't reach here
}

function isItemMatchesStateFilter (listItem, stateId)
{
	// Case of no filter
		if (stateId == ".*")
		{
			return true;
		}
		
		var brandId = listItem.id.substr(3); // 3 = "li_".length
		
		// FireFox adjustment (IE & Chrome ok)
		brandId = adjustToFireFox(brandId);
		
		// 2.1 Getting this brand details Box
		var brandDetailsBox = document.getElementById(brandId);
		
		// 2.2 Getting this brand's list Items
		var brandAttributes = brandDetailsBox.getElementsByTagName("li");
		
		var filterCategoryExists = false;
		
		//Setting location category to search in via selected country
		var selectCountryElement, filterCategory;
		switch (currentMode)
		{
			case privateConsumerModeText:
			{
				selectCountryElement = document.getElementById("selectCountry");
				filterCategory = " regions in";
				break;
			}
			case foodServiceModeText:
			{
				selectCountryElement = document.getElementById("selectCountry-foodServiceMode");
				filterCategory = "Food-Service regions";
				break;
			}
			default: break;
		}
	
		var selectedCountry = selectCountryElement.options[selectCountryElement.selectedIndex].text;
		
			 
		// 2.3 Running on each list item
		for (var j = 0;  j < brandAttributes.length;  j++)
		{
			if (brandAttributes[j].innerHTML.indexOf(filterCategory) != -1)
			{		   
				filterCategoryExists = true;
				
				// 2.3.1
				if (isCurrentMode(privateConsumerModeText))
				{
					var countriesListItems = brandAttributes[j].getElementsByTagName("li");
				   
					for (var k = 0;  k < countriesListItems.length;  k++)
					{
						// If the current country is not the country the user selected
						if (countriesListItems[k].innerHTML.indexOf(selectedCountry) == -1)
							continue;

						// 2.3.1.1 Searching for the input in this item's text
						var regexp = new RegExp("(" + stateId +")|(Nation\-wide)","i");  // i: [i]gnore case
						var match = regexp.exec (countriesListItems[k].innerHTML);
						
						// 2.3.1.2 If the item contains the state/province
						if (match != null)
						{
							// 2.3.1.2.1 setting the brand's list item as visible
							return true;
						}
						else
						{
							// 2.3.2.1.2: setting the brand's list item as hidden
							return false;
						}
					}
				}
				else if (isCurrentMode(foodServiceModeText))	// 2.3.2
				{
					var regionsElement = brandAttributes[j].getElementsByTagName("label")[0];
				
					// 2.3.2.1 Searching for the input in this item's text
					var regexp = new RegExp("(" + stateId +")|(Nation\-wide)","i");  // i: [i]gnore case
					var match = regexp.exec (regionsElement.innerHTML);
					
					// 2.3.2.2 If the item contains the state/province
					if (match != null)
					{
						// 2.3.2.2.1 setting the brand's list item as visible
						return true;
					}
					else
					{
						// 2.3.2.2.2: setting the brand's list item as hidden
						return false;
					}
				}
				
				// 2.3.3 No matches found: setting the brand's list item as hidden
				return false;
			}
		}
		
		// Hiding if filter category does not exist
		if (filterCategoryExists == false)
		{
			return false;
		}
		return false;	   // can't reach here
}

function isItemMatchesCountryFilter (listItem, countryName)
{
	// Case of no filter
		if (countryName == ".*")
		{
			return true;
		}
		
		var brandId = listItem.id.substr(3); // 3 = "li_".length
		
		// FireFox adjustment (IE & Chrome ok)
		brandId = adjustToFireFox(brandId);
		
		// 2.1 Getting this brand details Box
		var brandDetailsBox = document.getElementById(brandId);
		
		// 2.2 Getting this brand's list Items
		var brandAttributes = brandDetailsBox.getElementsByTagName("li");
		
		var filterCategoryExists = false;
	 
		var filterCategory = "Company location";
		
		// 2.3 Running on each list item
		for (var j = 0;  j < brandAttributes.length;  j++)
		{
			if (brandAttributes[j].innerHTML.indexOf(filterCategory) != -1)
			{		   
				filterCategoryExists = true;
				
				var countryElement = brandAttributes[j].getElementsByTagName("label")[0];
				
				return (countryElement.innerHTML == countryName);
			}
		}
		
		// Hiding if filter category does not exist
		if (filterCategoryExists == false)
		{
			return false;
		}
		return false;	   // can't reach here
}
