// Test if the browser is DOM capable
if (document.getElementById && document.getElementsByTagName)
{
//	addLoadEvent(filterOnSubmit);
	addLoadEvent(filterOnClick);
	addLoadEvent(filterItems);
}

// Loads functions on body.onload
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}


// FUNCTIONS MOBILT
// ---------------------------------------------------------------------------

function filterOnSubmit() {
	if (document.getElementById('product-selector')) {
		var selectForm = document.getElementById('product-selector');
		selectForm.onsubmit = function() {
			filterItems();
			return false;
		}
	}
}

function filterOnClick() {
	if (document.getElementById('product-selector')) {
		var selectForm = document.getElementById('product-selector');
		var inputList = selectForm.getElementsByTagName('input');
		for (var i=0; i < inputList.length; i++) {
			inputList[i].onclick = function () {
				filterItems();
				// return false;
			}
		}
	}
}

function filterItems() {
	if(document.getElementById('product-selector')) {
		var foundProducts = false;
		var noResults = document.getElementById("product-filter-noresults");

		if(document.getElementById('productList01')) {
			var showAllMakes = true;
			var showAllProperties = true;
			var makeList = document.getElementById("makes").getElementsByTagName("input");
			var propertiesList = document.getElementById("properties").getElementsByTagName("input");
			var productList = getElementsByClass("product", document.getElementById('productList01'), "li");

			if(productList.length > 0) {
				// Check if any make is selected
				for(var i=0; i < makeList.length; i++) {
					if(makeList[i].checked == true) {
						showAllMakes = false;
					}
				}
				
				// Check if any property is selected
				for(var i = 0; i < propertiesList.length; i++) {
					if(propertiesList[i].checked == true) {
						showAllProperties = false;
					}
				}
				
				// Step through the productlist
				for(var i = 0; i < productList.length; i++) {
					var product = productList[i];
					var productName = product.getElementsByTagName("h2")[0].firstChild.nodeValue;
					var productPropertyLabels = product.getElementsByTagName("dt");
					var productPropertyValues = product.getElementsByTagName("dd");
					var makeListed = false;
					var showProduct = true;

					if(showAllMakes == false || showAllProperties == false) {
						if(showAllMakes == false) {
							// Check if product name contains one of listed makes and if product make is selected
							for(var j = 0; j < makeList.length; j++) {
								if(productName.indexOf(makeList[j].value) != -1) {
									makeListed = true;
									if(makeList[j].checked == false) {
										showProduct = false;
									}
								}
							}
							
							// If product make is not listed, check if "Show other makes" is selected
							if(makeListed == false && document.getElementById("otherMakes").checked == false) {
								showProduct = false;
							}
						}
						
						if(showAllProperties == false) {
							// Check if product has selected properties
							for(var j = 0; j < propertiesList.length; j++) {
								if(propertiesList[j].checked == true) {
									for(var k = 0; k < productPropertyLabels.length; k++) {
										var propertyLabel = productPropertyLabels[k].firstChild.nodeValue;
										var propertyValue = productPropertyValues[k].firstChild.nodeValue;
										if((propertyLabel.indexOf(propertiesList[j].value)) != -1) {
											if(propertyValue.indexOf("Ja") == -1) {
												showProduct = false;
											}
										}
									}
								}
							}
						}
					}

					if(showProduct == true) {
						product.style.display = "list-item";
						foundProducts = true;
					}
					else {
						product.style.display = "none";
					}
				}
			}
			else {
				noResults.style.display = "block";
			}

			if(foundProducts == false) {
				noResults.style.display = "block";
			}
			else {
				noResults.style.display = "none";
			}
		}
		else {
			noResults.style.display = "block";
		}
	}
	return false;
}



// DOM EXTENSIONS
// ---------------------------------------------------------------------------

function getElementsByClass(searchClass,node,tag) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        var els = node.getElementsByTagName(tag);
        var elsLen = els.length;
        var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
        for (var i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                        classElements[j] = els[i];
                        j++;
                }
        }
        return classElements;
}

